#include <stdio.h>

char stack[0x10000] __attribute__((aligned(16))) = {0};

void test(void *ctx) {
    printf("%s\n", (const char*)ctx);
}

void call_with_new_stack(void* sp, void (*fn)(void*), const void* ctx)
{
    asm volatile(
        "xchg %%rsp, %%rdi\n\t"  // 
        "sub $8, %%rsp\n\t"
        "push %%rdi\n\t"
        "mov %%rdx, %%rdi\n\t"
        "call *%%rsi\n\t"
        "pop %%rsp\n\t"
        : : "D"(sp), "S"(fn), "d"(ctx) : "memory"
    );
}

int main() {
    call_with_new_stack(stack + sizeof(stack), &test, "Hello, world!");
    return 0;
}
