// File: /workspace/github_c_makefile_star2_O0/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
subq $0x10e0, %rsp           # imm = 0x10E0
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl $0x1f, -0x14(%rbp)
leaq 0x5016(%rip), %rax      # 0x6707
movq %rax, -0x70(%rbp)
movq -0x70(%rbp), %rax
movq %rax, -0x80(%rbp)
movq $0x0, -0x78(%rbp)
leaq -0xc0(%rbp), %rdi
xorl %esi, %esi
movl $0x40, %edx
callq 0x1100
leaq 0x4d62(%rip), %rdi      # 0x6481
leaq 0x5b65(%rip), %rsi      # 0x728b
callq 0x11a0
movq %rax, -0x68(%rbp)
movslq -0x14(%rbp), %rdx
movq -0x68(%rbp), %rcx
leaq -0x40(%rbp), %rdi
movl $0x1, %esi
callq 0x1080
movq -0x68(%rbp), %rdi
callq 0x10b0
movq $0x0, -0x68(%rbp)
leaq -0x60(%rbp), %rdi
leaq -0x40(%rbp), %rsi
movl -0x14(%rbp), %edx
callq 0x1bf0
leaq -0x60(%rbp), %rdi
movl $0x1ff, %esi            # imm = 0x1FF
callq 0x1060
callq 0x1210
movl %eax, -0x10d4(%rbp)
cmpl $0x0, -0x10d4(%rbp)
jne 0x18a4
movl $0x50000000, %edi       # imm = 0x50000000
callq 0x1090
cmpl $0x0, %eax
jge 0x17b1
leaq 0x4cec(%rip), %rdi      # 0x648e
callq 0x11b0
movl $0x1, %edi
callq 0x11d0
leaq -0x60(%rbp), %rdi
callq 0x1050
movl %eax, -0xc4(%rbp)
cmpl $0x0, -0xc4(%rbp)
jge 0x17df
leaq 0x4cc8(%rip), %rdi      # 0x6498
callq 0x11b0
movl $0x1, %edi
callq 0x11d0
leaq -0xc0(%rbp), %rdi
leaq 0x4f1a(%rip), %rsi      # 0x6707
callq 0x11c0
leaq -0xc0(%rbp), %rdi
leaq 0x4ca0(%rip), %rsi      # 0x64a0
callq 0x11a0
movq %rax, -0x68(%rbp)
cmpq $0x0, -0x68(%rbp)
jae 0x1823
leaq 0x4c8c(%rip), %rdi      # 0x64a3
callq 0x11b0
xorl %edi, %edi
callq 0x11d0
movq -0x10(%rbp), %rdi
movl 0x89d7(%rip), %eax      # 0xa204
movl %eax, %edx
movq -0x68(%rbp), %rcx
movl $0x1, %esi
callq 0x11e0
movq -0x68(%rbp), %rdi
callq 0x10b0
leaq 0x4eba(%rip), %rdi      # 0x6707
movl $0x1ff, %esi            # imm = 0x1FF
callq 0x1190
leaq 0x4c4b(%rip), %rdi      # 0x64a9
callq 0x10d0
movq -0x70(%rbp), %rdi
leaq -0x80(%rbp), %rsi
xorl %eax, %eax
movl %eax, %edx
callq 0x1130
movl %eax, -0xc4(%rbp)
cmpl $0x0, -0xc4(%rbp)
jge 0x189d
leaq 0x4c22(%rip), %rdi      # 0x64ac
callq 0x11b0
leaq 0x4c1e(%rip), %rdi      # 0x64b4
movb $0x0, %al
callq 0x10e0
xorl %edi, %edi
callq 0x11d0
leaq -0x10dc(%rbp), %rdi
callq 0x1200
leaq -0xc0(%rbp), %rdi
leaq 0x4c03(%rip), %rsi      # 0x64c1
callq 0x1040
leaq -0xc0(%rbp), %rdi
leaq -0x60(%rbp), %rsi
callq 0x11c0
leaq -0xc0(%rbp), %rdi
callq 0x10d0
xorl %eax, %eax
addq $0x10e0, %rsp           # imm = 0x10E0
popq %rbp
retq
nopw (%rax,%rax)