#include <bits/syscall.h>
#include <errno.h>

extern "C" long __do_syscall_ret(unsigned long ret) {
	return ret;
}

using sc_word_t = long;

sc_word_t __do_syscall0(long sc) {
	register long sc_reg asm("rax") = sc;
	register sc_word_t ret asm("rax");
	asm volatile ("syscall" : "=r"(ret) : "r"(sc_reg) : "rcx", "r11", "memory");
	return ret;
}

sc_word_t __do_syscall1(long sc, sc_word_t arg1) {
	register long sc_reg asm("rax") = sc;
	register sc_word_t arg1_reg asm("rdi") = arg1;
	register sc_word_t ret asm("rax");
	asm volatile ("syscall" : "=r"(ret) :
			"r"(sc_reg),
			"r"(arg1_reg)
			: "rcx", "r11", "memory");
	return ret;
}

sc_word_t __do_syscall2(long sc, sc_word_t arg1, sc_word_t arg2) {
	register long sc_reg asm("rax") = sc;
	register sc_word_t arg1_reg asm("rdi") = arg1;
	register sc_word_t arg2_reg asm("rsi") = arg2;
	register sc_word_t ret asm("rax");
	asm volatile ("syscall" : "=r"(ret) :
			"r"(sc_reg),
			"r"(arg1_reg),
			"r"(arg2_reg)
			: "rcx", "r11", "memory");
	return ret;
}

sc_word_t __do_syscall3(long sc, sc_word_t arg1, sc_word_t arg2, sc_word_t arg3) {
	register long sc_reg asm("rax") = sc;
	register sc_word_t arg1_reg asm("rdi") = arg1;
	register sc_word_t arg2_reg asm("rsi") = arg2;
	register sc_word_t arg3_reg asm("rdx") = arg3;
	register sc_word_t ret asm("rax");
	asm volatile ("syscall" : "=r"(ret) :
			"r"(sc_reg),
			"r"(arg1_reg),
			"r"(arg2_reg),
			"r"(arg3_reg)
			: "rcx", "r11", "memory");
	return ret;
}

sc_word_t __do_syscall4(long sc, sc_word_t arg1, sc_word_t arg2, sc_word_t arg3,
		sc_word_t arg4) {
	register long sc_reg asm("rax") = sc;
	register sc_word_t arg1_reg asm("rdi") = arg1;
	register sc_word_t arg2_reg asm("rsi") = arg2;
	register sc_word_t arg3_reg asm("rdx") = arg3;
	register sc_word_t arg4_reg asm("r10") = arg4;   // arg4 must be in r10 on x86_64
	register sc_word_t ret asm("rax");
	asm volatile ("syscall" : "=r"(ret) :
			"r"(sc_reg),
			"r"(arg1_reg),
			"r"(arg2_reg),
			"r"(arg3_reg),
			"r"(arg4_reg)
			: "rcx", "r11", "memory");
	return ret;
}

sc_word_t __do_syscall5(long sc, sc_word_t arg1, sc_word_t arg2, sc_word_t arg3,
		sc_word_t arg4, sc_word_t arg5) {
	register long sc_reg asm("rax") = sc;
	register sc_word_t arg1_reg asm("rdi") = arg1;
	register sc_word_t arg2_reg asm("rsi") = arg2;
	register sc_word_t arg3_reg asm("rdx") = arg3;
	register sc_word_t arg4_reg asm("r10") = arg4;
	register sc_word_t arg5_reg asm("r8") = arg5;
	register sc_word_t ret asm("rax");
	asm volatile ("syscall" : "=r"(ret) :
			"r"(sc_reg),
			"r"(arg1_reg),
			"r"(arg2_reg),
			"r"(arg3_reg),
			"r"(arg4_reg),
			"r"(arg5_reg)
			: "rcx", "r11", "memory");
	return ret;
}

sc_word_t __do_syscall6(long sc, sc_word_t arg1, sc_word_t arg2, sc_word_t arg3,
		sc_word_t arg4, sc_word_t arg5, sc_word_t arg6) {
	register long sc_reg asm("rax") = sc;
	register sc_word_t arg1_reg asm("rdi") = arg1;
	register sc_word_t arg2_reg asm("rsi") = arg2;
	register sc_word_t arg3_reg asm("rdx") = arg3;
	register sc_word_t arg4_reg asm("r10") = arg4;
	register sc_word_t arg5_reg asm("r8") = arg5;
	register sc_word_t arg6_reg asm("r9") = arg6;
	register sc_word_t ret asm("rax");
	asm volatile ("syscall" : "=r"(ret) :
			"r"(sc_reg),
			"r"(arg1_reg),
			"r"(arg2_reg),
			"r"(arg3_reg),
			"r"(arg4_reg),
			"r"(arg5_reg),
			"r"(arg6_reg)
			: "rcx", "r11", "memory");
	return ret;
}

/* x86_64 only supports 6 arguments in the syscall ABI. The 7th argument is ignored
 * (no syscall in Linux or your kernel uses 7 scalar arguments). */
sc_word_t __do_syscall7(long sc,
		sc_word_t arg1, sc_word_t arg2, sc_word_t arg3,
		sc_word_t arg4, sc_word_t arg5, sc_word_t arg6,
		sc_word_t arg7) {
	(void)arg7;
	return __do_syscall6(sc, arg1, arg2, arg3, arg4, arg5, arg6);
}