// File: /workspace/github_c_makefile_star2_O0/sos22[P]unexec/unexec.c
// Function: write_elf_binary
static int write_elf_binary(const char * path, struct trampoline * tramp){
    int fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0711);
    if (fd < 0) return -1;
    if (write_ehdr(fd, tramp) < 0) goto fail;
    for (unsigned x = 0; x < tramp->nrmappings; x++) {
        if (tramp->mappings[x].path == tramp->procselfexe &&
            write_phdr(fd, &tramp->mappings[x]) < 0) goto fail; }
    if (write_trampoline_phdr(fd, tramp) < 0) goto fail;
    if (write_stack_phdr(fd) < 0) goto fail;
    if (write_brk_phdr(fd, tramp) < 0) goto fail;
    /* Now dump out the actual contents of the phdrs */
    int64_t offset = 0;
    for (unsigned x = 0; x < tramp->nrmappings; x++) {
        if (tramp->mappings[x].path != tramp->procselfexe) continue;
        if (offset == 0) {
            if (lseek(fd, tramp->mappings[x].offset, SEEK_SET) < 0) goto fail;
            offset = tramp->mappings[x].offset; }
        else {
            ssize_t r = lseek(fd, 0, SEEK_CUR);
            if (r < 0) goto fail;
            assert(r == offset);
            assert(r == tramp->mappings[x].offset); }
        ssize_t r = write(fd,
                          (const void *)tramp->mappings[x].start,
                          tramp->mappings[x].size);
        if (r != (ssize_t)tramp->mappings[x].size) goto fail;
        offset += r; }
    /* Last thing is the trampoline structure itself. */
    ssize_t sz = tramp->allocated + sizeof(*tramp);
    if (save_registers(&tramp->stash)) {
        /* We just got rehydrated. Let's go. */
        return 0; }
    else {
        /* Final stage of writing down the ELF binary. */
        if (write(fd, tramp, sz) != sz) goto fail;
        close(fd);
        return 1; }
  fail:
    close(fd);
    return -1; }

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x50, %rsp
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq -0x10(%rbp), %rdi
movl $0x241, %esi            # imm = 0x241
movl $0x1c9, %edx            # imm = 0x1C9
movb $0x0, %al
callq 0x1160
movl %eax, -0x1c(%rbp)
cmpl $0x0, -0x1c(%rbp)
jge 0x1fca
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x2274
movl -0x1c(%rbp), %edi
movq -0x18(%rbp), %rsi
callq 0x2ab0
cmpl $0x0, %eax
jge 0x1fe0
jmp 0x2265
movl $0x0, -0x20(%rbp)
movl -0x20(%rbp), %eax
movq -0x18(%rbp), %rcx
cmpl 0x20(%rcx), %eax
jae 0x2044
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rax
movl -0x20(%rbp), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq 0x20(%rax), %rax
movq -0x18(%rbp), %rcx
cmpq 0x28(%rcx), %rax
jne 0x2037
movl -0x1c(%rbp), %edi
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rsi
movl -0x20(%rbp), %eax
imulq $0x28, %rax, %rax
addq %rax, %rsi
callq 0x2b80
cmpl $0x0, %eax
jge 0x2037
jmp 0x2265
jmp 0x2039
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0x1fe7
movl -0x1c(%rbp), %edi
movq -0x18(%rbp), %rsi
callq 0x2c10
cmpl $0x0, %eax
jge 0x205a
jmp 0x2265
movl -0x1c(%rbp), %edi
callq 0x2cb0
cmpl $0x0, %eax
jge 0x206c
jmp 0x2265
movl -0x1c(%rbp), %edi
movq -0x18(%rbp), %rsi
callq 0x2d10
cmpl $0x0, %eax
jge 0x2082
jmp 0x2265
movq $0x0, -0x28(%rbp)
movl $0x0, -0x2c(%rbp)
movl -0x2c(%rbp), %eax
movq -0x18(%rbp), %rcx
cmpl 0x20(%rcx), %eax
jae 0x2211
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rax
movl -0x2c(%rbp), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq 0x20(%rax), %rax
movq -0x18(%rbp), %rcx
cmpq 0x28(%rcx), %rax
je 0x20c6
jmp 0x2203
cmpq $0x0, -0x28(%rbp)
jne 0x2117
movl -0x1c(%rbp), %edi
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rax
movl -0x2c(%rbp), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq 0x10(%rax), %rsi
xorl %edx, %edx
callq 0x1090
cmpq $0x0, %rax
jge 0x20f8
jmp 0x2265
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rax
movl -0x2c(%rbp), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq 0x10(%rax), %rax
movq %rax, -0x28(%rbp)
jmp 0x21a2
movl -0x1c(%rbp), %edi
xorl %eax, %eax
movl %eax, %esi
movl $0x1, %edx
callq 0x1090
movq %rax, -0x38(%rbp)
cmpq $0x0, -0x38(%rbp)
jge 0x2138
jmp 0x2265
movq -0x38(%rbp), %rax
cmpq -0x28(%rbp), %rax
jne 0x2144
jmp 0x2163
leaq 0x1053(%rip), %rdi      # 0x319e
leaq 0xebc(%rip), %rsi       # 0x300e
movl $0x28a, %edx            # imm = 0x28A
leaq 0x104c(%rip), %rcx      # 0x31aa
callq 0x10a0
movq -0x38(%rbp), %rax
movq -0x18(%rbp), %rcx
movq 0x18(%rcx), %rcx
movl -0x2c(%rbp), %edx
imulq $0x28, %rdx, %rdx
addq %rdx, %rcx
cmpq 0x10(%rcx), %rax
jne 0x2181
jmp 0x21a0
leaq 0x105a(%rip), %rdi      # 0x31e2
leaq 0xe7f(%rip), %rsi       # 0x300e
movl $0x28b, %edx            # imm = 0x28B
leaq 0x100f(%rip), %rcx      # 0x31aa
callq 0x10a0
jmp 0x21a2
movl -0x1c(%rbp), %edi
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rax
movl -0x2c(%rbp), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq (%rax), %rsi
movq -0x18(%rbp), %rax
movq 0x18(%rax), %rax
movl -0x2c(%rbp), %ecx
imulq $0x28, %rcx, %rcx
addq %rcx, %rax
movq 0x8(%rax), %rdx
callq 0x1060
movq %rax, -0x40(%rbp)
movq -0x40(%rbp), %rax
movq -0x18(%rbp), %rcx
movq 0x18(%rcx), %rcx
movl -0x2c(%rbp), %edx
imulq $0x28, %rdx, %rdx
addq %rdx, %rcx
cmpq 0x8(%rcx), %rax
je 0x21f7
jmp 0x2265
movq -0x40(%rbp), %rax
addq -0x28(%rbp), %rax
movq %rax, -0x28(%rbp)
movl -0x2c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x2c(%rbp)
jmp 0x2091
movq -0x18(%rbp), %rax
movl (%rax), %eax
addq $0x80, %rax
movq %rax, -0x48(%rbp)
movq -0x18(%rbp), %rdi
addq $0x40, %rdi
callq 0x1190
cmpl $0x0, %eax
je 0x223c
movl $0x0, -0x4(%rbp)
jmp 0x2274
movl -0x1c(%rbp), %edi
movq -0x18(%rbp), %rsi
movq -0x48(%rbp), %rdx
callq 0x1060
cmpq -0x48(%rbp), %rax
je 0x2254
jmp 0x2265
movl -0x1c(%rbp), %edi
callq 0x10c0
movl $0x1, -0x4(%rbp)
jmp 0x2274
movl -0x1c(%rbp), %edi
callq 0x10c0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
movl -0x4(%rbp), %eax
addq $0x50, %rsp
popq %rbp
retq
nopl (%rax)