// File: /workspace/github_c_makefile_star2_O1/ww9210[P]xnuca_final_ssp/main.c
// Function: elf_run_patch
int elf_run_patch(struct executor_queue* eq, void* value){
    // get random string
    int byte_cnt = 31;
    char data[32];
    char dirname[32];
    FILE* fp;
    char *target = "/helloworld";
    char *args[] = {target, NULL};
    char cmd[64];
    int res;
    char cwd[PATH_MAX];

    memset(cmd,0,64);
    fp = fopen("/dev/urandom", "r");
    fread(&data, 1, byte_cnt, fp);
    fclose(fp);
    fp=NULL;
    rand_str(dirname, data, byte_cnt);  

#ifdef DEBUG
    printf("dirname is %s\n", dirname);
#endif

    //mkdir 
    mkdir(dirname, 0777);

    // fork
    pid_t pid, wpid;
    int status;
    pid = fork();
    if(pid==0){ //child
#ifdef DEBUG
        //printf("child on\n");
#endif
        if (unshare(CLONE_NEWUSER | CLONE_NEWNET) < 0) {
#ifdef DEBUG
            printf("namespace fail\n");
#endif
            perror("unshare: ");
            exit(1);
        }
        res=chroot(dirname);
        if(res<0){
            perror("chroot:");
#ifdef DEBUG
            printf("chroot fail\n");
#endif
            exit(1);
        } 
#ifdef DEBUG
        /*
        printf("current effective uid %d \n",(int)geteuid());
        if (getcwd(cwd, sizeof(cwd)) != NULL) {
            printf("Current working dir: %s\n", cwd);
        }
        else{
            perror("getcwd() error");
            exit(0);
       }
       */
#endif
        // write patched executable to disk
        //strcpy(cmd, dirname);
        strcat(cmd, "/helloworld");
        fp = fopen(cmd,"wb");
        if(fp<0){
            perror("fopen");
            exit(0);
        }
        fwrite((char*)value, 1, elf_unpatched_size, fp);
        fclose(fp);
        chmod("/helloworld", 0777);
        // execve it
        system("id");
        res=execve(target ,args, NULL);
        if(res<0){
            perror("execve:");
            printf("execve fail\n");
        }
        exit(0);
    }
    wait(&status);
    strcpy(cmd, "rm -r ");
    strcat(cmd, dirname);
#ifdef DEBUG
    printf("%s\n",cmd);
#endif
    system(cmd);
    return 0; 
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %rbx
subq $0x98, %rsp
movq %rsi, %rbx
leaq 0x2fca(%rip), %rax      # 0x4581
movq %rax, -0x30(%rbp)
movq $0x0, -0x28(%rbp)
xorps %xmm0, %xmm0
movaps %xmm0, -0x40(%rbp)
movaps %xmm0, -0x50(%rbp)
movaps %xmm0, -0x60(%rbp)
movaps %xmm0, -0x70(%rbp)
leaq 0x2e74(%rip), %rdi      # 0x4451
leaq 0x3923(%rip), %rsi      # 0x4f07
callq 0x1190
movq %rax, %r14
leaq -0xb0(%rbp), %rdi
movl $0x1, %esi
movl $0x1f, %edx
movq %rax, %rcx
callq 0x1070
movq %r14, %rdi
callq 0x10a0
xorl %eax, %eax
leaq 0x2a2a(%rip), %rcx      # 0x4040
movb -0xb0(%rbp,%rax), %dl
movl %edx, %esi
shrb %sil
movzbl %sil, %esi
imull $0x85, %esi, %esi
shrl $0xc, %esi
leal (%rsi,%rsi), %edi
shll $0x6, %esi
subl %edi, %esi
subb %sil, %dl
movzbl %dl, %edx
movb (%rdx,%rcx), %dl
movb %dl, -0x90(%rbp,%rax)
incq %rax
cmpq $0x1f, %rax
jne 0x1616
leaq -0x90(%rbp), %rdi
movb $0x0, 0x1f(%rdi)
movl $0x1ff, %esi            # imm = 0x1FF
callq 0x1050
callq 0x1200
testl %eax, %eax
je 0x16af
leaq -0x1c(%rbp), %rdi
callq 0x11f0
leaq -0x70(%rbp), %rbx
movl $0x2d206d72, (%rbx)     # imm = 0x2D206D72
movl $0x20722d, 0x3(%rbx)    # imm = 0x20722D
leaq -0x90(%rbp), %rsi
movq %rbx, %rdi
callq 0x11b0
movq %rbx, %rdi
callq 0x10c0
xorl %eax, %eax
addq $0x98, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
movl $0x50000000, %edi       # imm = 0x50000000
callq 0x1080
testl %eax, %eax
jns 0x16c2
callq 0x1266
leaq -0x90(%rbp), %rdi
callq 0x1040
testl %eax, %eax
jns 0x16d7
callq 0x124e
leaq -0x70(%rbp), %r15
movq %r15, %rdi
callq 0x10b0
movabsq $0x6f776f6c6c65682f, %rcx # imm = 0x6F776F6C6C65682F
movq %rcx, -0x70(%rbp,%rax)
movl $0x646c72, -0x68(%rbp,%rax) # imm = 0x646C72
leaq 0x2e80(%rip), %r14      # 0x4581
leaq 0x2d68(%rip), %rsi      # 0x4470
movq %r15, %rdi
callq 0x1190
movq %rax, %r15
movl 0x6a1b(%rip), %edx      # 0x8134
movl $0x1, %esi
movq %rbx, %rdi
movq %rax, %rcx
callq 0x11d0
movq %r15, %rdi
callq 0x10a0
movq %r14, %rdi
movl $0x1ff, %esi            # imm = 0x1FF
callq 0x1180
leaq 0x2d2e(%rip), %rdi      # 0x4473
callq 0x10c0
leaq -0x30(%rbp), %rsi
movq %r14, %rdi
xorl %edx, %edx
callq 0x1120
testl %eax, %eax
jns 0x1761
callq 0x1230
xorl %edi, %edi
callq 0x11c0