// File: /workspace/github_c_makefile_star2_O2/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 0x3025(%rip), %rax      # 0x4581
movq %rax, -0x30(%rbp)
andq $0x0, -0x28(%rbp)
xorps %xmm0, %xmm0
movaps %xmm0, -0x40(%rbp)
movaps %xmm0, -0x50(%rbp)
movaps %xmm0, -0x60(%rbp)
movaps %xmm0, -0x70(%rbp)
leaq 0x2ed2(%rip), %rdi      # 0x4451
leaq 0x3981(%rip), %rsi      # 0x4f07
callq 0x11a0
movq %rax, %r14
leaq -0xb0(%rbp), %r15
pushq $0x1
popq %rsi
pushq $0x1f
popq %rdx
movq %r15, %rdi
movq %rax, %rcx
callq 0x1070
movq %r14, %rdi
callq 0x10a0
leaq -0x90(%rbp), %r14
pushq $0x1f
popq %rdx
movq %r14, %rdi
movq %r15, %rsi
callq 0x1910
movq %r14, %rdi
movl $0x1ff, %esi            # imm = 0x1FF
callq 0x1050
callq 0x1210
testl %eax, %eax
je 0x161a
leaq -0x1c(%rbp), %rdi
callq 0x1200
leaq -0x70(%rbp), %rbx
movl $0x2d206d72, (%rbx)     # imm = 0x2D206D72
movl $0x20722d, 0x3(%rbx)    # imm = 0x20722D
leaq -0x90(%rbp), %rsi
movq %rbx, %rdi
callq 0x11c0
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 0x1631
leaq 0x2e2f(%rip), %rdi      # 0x445e
jmp 0x1648
leaq -0x90(%rbp), %rdi
callq 0x1040
testl %eax, %eax
jns 0x1655
leaq 0x2e20(%rip), %rdi      # 0x4468
callq 0x11b0
pushq $0x1
popq %rdi
callq 0x11d0
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 0x2f02(%rip), %r14      # 0x4581
leaq 0x2dea(%rip), %rsi      # 0x4470
movq %r15, %rdi
callq 0x11a0
movq %rax, %r15
movl 0x6aad(%rip), %edx      # 0x8144
pushq $0x1
popq %rsi
movq %rbx, %rdi
movq %rax, %rcx
callq 0x11e0
movq %r15, %rdi
callq 0x10a0
movq %r14, %rdi
movl $0x1ff, %esi            # imm = 0x1FF
callq 0x1190
leaq 0x2db2(%rip), %rdi      # 0x4473
callq 0x10c0
leaq -0x30(%rbp), %rsi
movq %r14, %rdi
xorl %edx, %edx
callq 0x1120
testl %eax, %eax
jns 0x16f0
leaq 0x2d97(%rip), %rdi      # 0x4476
callq 0x11b0
leaq 0x3112(%rip), %rdi      # 0x47fd
callq 0x1060
xorl %edi, %edi
callq 0x11d0