// File: /workspace/github_c_makefile_star2_O2/sos22[P]unexec/tests/signals.c
// Function: main
int main() {
    char template[] = "signalsXXXXXX";
    close(mkstemp(template));
    signal(SIGINT, siginthandler);
    int r = unexec(template, NULL);
    if (r < 0) err(1, "unexec %s", template);
    if (r == 1) {
        raise(SIGINT);
        assert(siginted);
        execl(template, template, NULL);
        err(1, "exec"); }
    if (unlink(template) < 0) err(1, "unlink %s", template);
    assert(!siginted);
    raise(SIGINT);
    assert(siginted);
    siginted = false;
    raise(SIGCHLD);
    assert(!siginted);
    stack_t ss = (stack_t){
        .ss_sp = malloc(16384),
        .ss_flags = 0,
        .ss_size = 16384,
    };
    printf("sigstack %p\n", ss.ss_sp);
    if (sigaltstack(&ss, NULL) < 0) err(1, "sigaltstack");
    printf("set sigaltstack\n");
    r = unexec(template, NULL);
    if (r < 0) err(1, "unexec2");
    if (r == 1) {
        execl(template, template, NULL);
        err(1, "exec2"); }
    assert(rsp < (unsigned long)ss.ss_sp ||
           rsp >= (unsigned long)ss.ss_sp + ss.ss_size);
    struct sigaction sa = {
        .sa_handler = siginthandler,
        .sa_flags = SA_ONSTACK, };
    if (sigaction(SIGINT, &sa, NULL) < 0) err(1, "sigaction");
    raise(SIGINT);
    printf("rsp %lx, wanted %p,%p\n",
           rsp, ss.ss_sp, ss.ss_sp + ss.ss_size);
    assert(rsp >= (unsigned long)ss.ss_sp);
    assert(rsp < (unsigned long)ss.ss_sp + ss.ss_size);
    if (unlink(template) < 0) err(1, "unlink2 %s", template);
    return 0; }

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %rbx
subq $0xc8, %rsp
movabsq $0x58585858585873, %rax # imm = 0x58585858585873
leaq -0x18(%rbp), %rbx
movq %rax, 0x6(%rbx)
movabsq $0x58736c616e676973, %rax # imm = 0x58736C616E676973
movq %rax, (%rbx)
movq %rbx, %rdi
callq 0x10e0
movl %eax, %edi
callq 0x1090
leaq 0x2e3(%rip), %rsi       # 0x1540
pushq $0x2
popq %rdi
callq 0x10a0
movq %rbx, %rdi
xorl %esi, %esi
callq 0x10f0
testl %eax, %eax
js 0x13ea
cmpl $0x1, %eax
je 0x13f6
leaq -0x18(%rbp), %rdi
callq 0x1040
testl %eax, %eax
js 0x1427
cmpb $0x1, 0x2e10(%rip)      # 0x40a8
je 0x1433
pushq $0x2
popq %rdi
callq 0x1030
cmpb $0x0, 0x2dfb(%rip)      # 0x40a8
je 0x144f
movb $0x0, 0x2dee(%rip)      # 0x40a8
pushq $0x11
popq %rdi
callq 0x1030
cmpb $0x1, 0x2ddf(%rip)      # 0x40a8
je 0x146b
movl $0x4000, %edi           # imm = 0x4000
callq 0x10d0
leaq -0x30(%rbp), %rbx
movq %rax, (%rbx)
andl $0x0, 0x8(%rbx)
movq $0x4000, 0x10(%rbx)     # imm = 0x4000
leaq 0xd5d(%rip), %rdi       # 0x2050
movq %rax, %rsi
xorl %eax, %eax
callq 0x1070
movq %rbx, %rdi
xorl %esi, %esi
callq 0x1100
testl %eax, %eax
js 0x1487
leaq 0xe38(%rip), %rdi       # 0x214e
callq 0x1050
leaq -0x18(%rbp), %rdi
xorl %esi, %esi
callq 0x10f0
testl %eax, %eax
js 0x1490
cmpl $0x1, %eax
je 0x1499
movq 0x2d72(%rip), %rax      # 0x40b0
movq -0x30(%rbp), %rcx
cmpq %rcx, %rax
jb 0x1354
addq -0x20(%rbp), %rcx
cmpq %rcx, %rax
jb 0x1523
leaq 0x29e5(%rip), %rsi      # 0x3d40
leaq -0xc8(%rbp), %rbx
movl $0x98, %edx
movq %rbx, %rdi
callq 0x10b0
pushq $0x2
popq %rdi
movq %rbx, %rsi
xorl %edx, %edx
callq 0x1060
testl %eax, %eax
js 0x14b2
pushq $0x2
popq %rdi
callq 0x1030
movq 0x2d1d(%rip), %rsi      # 0x40b0
movq -0x30(%rbp), %rdx
movq -0x20(%rbp), %rcx
addq %rdx, %rcx
leaq 0xd17(%rip), %rdi       # 0x20bc
xorl %eax, %eax
callq 0x1070
movq 0x2cfd(%rip), %rax      # 0x40b0
movq -0x30(%rbp), %rcx
cmpq %rcx, %rax
jb 0x14c3
addq -0x20(%rbp), %rcx
cmpq %rcx, %rax
jae 0x14dc
leaq -0x18(%rbp), %rdi
callq 0x1040
testl %eax, %eax
js 0x14f5
xorl %eax, %eax
addq $0xc8, %rsp
popq %rbx
popq %rbp
retq
leaq 0xc21(%rip), %rsi       # 0x2012
jmp 0x14fc
pushq $0x2
popq %rdi
callq 0x1030
cmpb $0x0, 0x2ca3(%rip)      # 0x40a8
je 0x150a
leaq -0x18(%rbp), %rdi
movq %rdi, %rsi
xorl %edx, %edx
xorl %eax, %eax
callq 0x1110
leaq 0xc15(%rip), %rsi       # 0x2037
jmp 0x14b9
leaq 0xc0e(%rip), %rsi       # 0x203c
jmp 0x14fc
leaq 0xc0c(%rip), %rdi       # 0x2046
leaq 0xbdb(%rip), %rsi       # 0x201c
leaq 0xbe4(%rip), %rcx       # 0x202c
pushq $0x1e
jmp 0x153a
leaq 0xbf1(%rip), %rdi       # 0x2047
leaq 0xbbf(%rip), %rsi       # 0x201c
leaq 0xbc8(%rip), %rcx       # 0x202c
pushq $0x20
jmp 0x153a
leaq 0xbd4(%rip), %rdi       # 0x2046
leaq 0xba3(%rip), %rsi       # 0x201c
leaq 0xbac(%rip), %rcx       # 0x202c
pushq $0x23
jmp 0x153a
leaq 0xcc4(%rip), %rsi       # 0x2152
jmp 0x14b9
leaq 0xbc6(%rip), %rsi       # 0x205d
jmp 0x14b9
leaq -0x18(%rbp), %rdi
movq %rdi, %rsi
xorl %edx, %edx
xorl %eax, %eax
callq 0x1110
leaq 0xbaf(%rip), %rsi       # 0x205f
jmp 0x14b9
leaq 0xbf9(%rip), %rsi       # 0x20b2
pushq $0x1
popq %rdi
xorl %eax, %eax
callq 0x10c0
leaq 0xc09(%rip), %rdi       # 0x20d3
leaq 0xb4b(%rip), %rsi       # 0x201c
leaq 0xb54(%rip), %rcx       # 0x202c
pushq $0x3a
jmp 0x153a
leaq 0xc0f(%rip), %rdi       # 0x20f2
leaq 0xb32(%rip), %rsi       # 0x201c
leaq 0xb3b(%rip), %rcx       # 0x202c
pushq $0x3b
jmp 0x153a
leaq 0xc21(%rip), %rsi       # 0x211d
pushq $0x1
popq %rdi
leaq -0x18(%rbp), %rdx
xorl %eax, %eax
callq 0x10c0
leaq 0xb36(%rip), %rdi       # 0x2047
leaq 0xb04(%rip), %rsi       # 0x201c
leaq 0xb0d(%rip), %rcx       # 0x202c
pushq $0x1a
jmp 0x153a
leaq 0xb3b(%rip), %rdi       # 0x2065
leaq 0xaeb(%rip), %rsi       # 0x201c
leaq 0xaf4(%rip), %rcx       # 0x202c
pushq $0x32
popq %rdx
callq 0x1080