#ifndef	MERCURY

#include "harness.cpp"

data(triple(18, 12, 6)).

benchmark(triple(X, Y, Z), Out) :-
	tak(X, Y, Z, Out).

#else

:- module tak.

:- interface.

:- import_module int, io.

:- pred benchmark(int).
:- mode benchmark(out) is det.

:- pred main(io__state, io__state).
:- mode main(di, uo) is det.

:- implementation.

benchmark(Out) :-
	tak(18, 12, 6, Out).

main -->
	{ tak(18, 12, 6, Out) },
	io__write_int(Out),
	io__write_string("\n").

:- pred tak(int, int, int, int).
:- mode tak(in, in, in, out) is det.

#endif

#ifdef	AQUARIUS_DECLS
:- mode((tak(X, Y, Z, A) :-
	ground(X),
	rderef(X),
	integer(X),
	ground(Y),
	rderef(Y),
	integer(Y),
	ground(Z),
	rderef(Z),
	integer(Z)
	)).
#endif

#ifdef	NUPROLOG_DECLS
:- tak(X, Y, Z, A) when X and Y and Z.
#endif

#ifdef	SICSTUS_DECLS
:- block tak(-, ?, ?, ?), tak(?, -, ?, ?), tak(?, ?, -, ?).
#endif

#ifdef	MERCURY

tak(X, Y, Z, A) :-
	(X =< Y ->
		Z = A
	;
		X1 is X - 1,
		tak(X1, Y, Z, A1),
		Y1 is Y - 1,
		tak(Y1, Z, X, A2),
		Z1 is Z - 1,
		tak(Z1, X, Y, A3),
		tak(A1, A2, A3, A)
	).

#else

#ifdef	SWIPROLOG

tak(X, Y, Z, A) :-
	(X =< Y ->
		Z = A
	;
		X1 is X - 1,
		tak(X1, Y, Z, A1),
		Y1 is Y - 1,
		tak(Y1, Z, X, A2),
		Z1 is Z - 1,
		tak(Z1, X, Y, A3),
		tak(A1, A2, A3, A)
	).

#else

tak(X,Y,Z,A) :-
	X =< Y,
	Z = A.
tak(X,Y,Z,A) :-
	X > Y,
	X1 is X - 1,
	tak(X1,Y,Z,A1),
	Y1 is Y - 1,
	tak(Y1,Z,X,A2),
	Z1 is Z - 1,
	tak(Z1,X,Y,A3),
	tak(A1,A2,A3,A).

#endif

#endif