// File: /workspace/github_c_makefile_star2_O1/VeeeectorPan[P]MyNetdisk/SourceFile/client_put.c
// Function: client_put
int client_put(int client_fd)
{
    MYSQL* connect;
    connect_mysql(&connect);
    int user_id;
    int ret = recv(client_fd,&user_id,sizeof(int),0);
    DISCON_CHECK(ret,0,client_fd);
    int file_id;
    recv(client_fd,&file_id,sizeof(int),0);
    DISCON_CHECK(ret,0,client_fd);
    int len;
    ret = recv(client_fd,&len,sizeof(int),0);
    DISCON_CHECK(ret,0,client_fd);
    char file_name[128] = {0};
    ret = recv_client_all(client_fd,file_name,len);
    DISCON_CHECK(ret,-1,client_fd);
    char mysql_oper[256] = {0};
    sprintf(mysql_oper,"select * from File where parent_id='%d' and file_name='%s' and user_id='%d'"
            ,file_id,file_name,user_id);
    MYSQL_RES* res = query(mysql_oper,connect);
    MYSQL_ROW row = mysql_fetch_row(res);
    int ctl_num;
    if(row == NULL)
    {
        ctl_num = PUT_SUCCESS;
    }
    else
    {
        ctl_num = PUT_FAIL;
    }
    mysql_free_result(res);
    ret = send(client_fd,&ctl_num,sizeof(int),MSG_NOSIGNAL);
    DISCON_CHECK(ret,-1,client_fd);
    // upload 
    while(1)
    {
        ret = recv(client_fd,&ctl_num,sizeof(int),0);
        DISCON_CHECK(ret,0,client_fd);
        if(ctl_num == NO_MORE_FILE)
            break;
        int parent_id;
        mode_t mode;
        ret = recv(client_fd,&mode,sizeof(mode_t),0);
        DISCON_CHECK(ret,0,client_fd);
        ret = recv(client_fd,&parent_id,sizeof(int),0);
        DISCON_CHECK(ret,0,client_fd);
        ret = recv(client_fd,&len,sizeof(int),0);
        DISCON_CHECK(ret,0,client_fd);
        ret= recv_client_all(client_fd,file_name,len);
        DISCON_CHECK(ret,-1,client_fd);
        file_name[len] = '\0';
        if((mode & S_IFMT) == S_IFDIR)
        {
            sprintf(mysql_oper,"insert into File(user_id,parent_id,file_name,file_type) values(%d,%d,'%s','d');"
                    ,user_id,parent_id,file_name);
            insert_to_mysql(mysql_oper,connect);
            sprintf(mysql_oper,"select file_id from File where file_name='%s' and user_id=%d and parent_id=%d;"
                    ,file_name,user_id,parent_id);
            res = query(mysql_oper,connect);
            row = mysql_fetch_row(res);
            int new_dir_id = atoi(row[0]);
            mysql_free_result(res);
            ret = send(client_fd,&new_dir_id,sizeof(int),0);
            DISCON_CHECK(ret,0,client_fd);
            continue;
        }
        off_t file_size;
        ret = recv(client_fd,&file_size,sizeof(off_t),0);
        DISCON_CHECK(ret,0,client_fd);
        char md5[33] = {0};
        ret = recv_client_all(client_fd,md5,32);
        DISCON_CHECK(ret,-1,client_fd);
        sprintf(mysql_oper,"select * from File where md5='%s';",md5);
        res = query(mysql_oper,connect);
        row = mysql_fetch_row(res);
        if(row == NULL)
        {
            ctl_num = PUT_NOT_EXIST;
        }
        else
        {
            ctl_num = PUT_EXIST;
        }
        mysql_free_result(res);
        sprintf(mysql_oper,"insert into File(parent_id,file_name,user_id,md5,file_size,file_type) values(%d,'%s',%d,'%s',%ld,'f');"
                ,parent_id,file_name,user_id,md5,file_size);
        insert_to_mysql(mysql_oper,connect);
        ret = send(client_fd,&ctl_num,sizeof(int),MSG_NOSIGNAL);
        DISCON_CHECK(ret,-1,client_fd);
        printf("ctl_num = %d\n",ctl_num);
        if(ctl_num == PUT_EXIST)
        {
            printf("hi");
            continue;
        }
        ret = recv(client_fd,&ctl_num,sizeof(int),0);
        DISCON_CHECK(ret,0,client_fd);
        if(ctl_num != PUT_SUCCESS)
        {
            sprintf(mysql_oper,"delete from File where parent_id=%d and file_name='%s';"
                    ,parent_id,file_name);
            insert_to_mysql(mysql_oper,connect);
            continue;
        }
        char file_path[4096] = {0};
        sprintf(file_path,"%s/%s",FILE_STORE_PATH,md5);
        int file_fd = open(file_path,O_RDWR|O_CREAT|O_EXCL,mode);
        DISCON_CHECK(file_fd,-1,client_fd);
        ret = recv_user_file(client_fd,file_fd,file_size);
        DISCON_CHECK(ret,-1,client_fd);
        close(file_fd);
        ret = send(client_fd,&ctl_num,sizeof(int),MSG_NOSIGNAL);
        DISCON_CHECK(ret,-1,client_fd);
    }
    close(client_fd);
    mysql_close(connect);
    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x11e8, %rsp           # imm = 0x11E8
movl %edi, %ebx
leaq -0x40(%rbp), %rdi
callq 0x3530
leaq -0x38(%rbp), %rsi
movl $0x4, %edx
movl %ebx, %edi
xorl %ecx, %ecx
callq 0x20a0
testl %eax, %eax
je 0x319b
leaq -0x4c(%rbp), %rsi
movl $0x4, %edx
movl %ebx, %edi
xorl %ecx, %ecx
callq 0x20a0
leaq -0x30(%rbp), %rsi
movl $0x4, %edx
movl %ebx, %edi
xorl %ecx, %ecx
callq 0x20a0
testl %eax, %eax
je 0x319b
xorps %xmm0, %xmm0
leaq -0x100(%rbp), %rsi
movaps %xmm0, 0x70(%rsi)
movaps %xmm0, 0x60(%rsi)
movaps %xmm0, 0x50(%rsi)
movaps %xmm0, 0x40(%rsi)
movaps %xmm0, 0x30(%rsi)
movaps %xmm0, 0x20(%rsi)
movaps %xmm0, 0x10(%rsi)
movaps %xmm0, (%rsi)
movl -0x30(%rbp), %edx
movl %ebx, %edi
callq 0x481c
cmpl $-0x1, %eax
je 0x319b
leaq -0x200(%rbp), %r14
movl $0x100, %edx            # imm = 0x100
movq %r14, %rdi
xorl %esi, %esi
callq 0x2130
movl -0x4c(%rbp), %edx
movl -0x38(%rbp), %r8d
leaq 0x23df(%rip), %rsi      # 0x517c
leaq -0x100(%rbp), %rcx
movq %r14, %rdi
xorl %eax, %eax
callq 0x2060
movq -0x40(%rbp), %rsi
movq %r14, %rdi
callq 0x46a8
movq %rax, %r14
movq %rax, %rdi
callq 0x21f0
xorl %ecx, %ecx
testq %rax, %rax
sete %cl
leaq -0x2c(%rbp), %r15
movl %ecx, (%r15)
movq %r14, %rdi
callq 0x21e0
movl $0x4, %edx
movl %ebx, %edi
movq %r15, %rsi
movl $0x4000, %ecx           # imm = 0x4000
callq 0x2100
cmpl $-0x1, %eax
je 0x319b
leaq -0x2c(%rbp), %rsi
movl $0x4, %edx
movl %ebx, %edi
xorl %ecx, %ecx
callq 0x20a0
testl %eax, %eax
je 0x319b
leaq -0x44(%rbp), %r14
leaq -0x30(%rbp), %r13
leaq -0x80(%rbp), %r12
cmpl $0x3, -0x2c(%rbp)
je 0x31b9
movl $0x4, %edx
movl %ebx, %edi
movq %r14, %rsi
xorl %ecx, %ecx
callq 0x20a0
testl %eax, %eax
je 0x3139
movl $0x4, %edx
movl %ebx, %edi
leaq -0x34(%rbp), %rsi
xorl %ecx, %ecx
callq 0x20a0
testl %eax, %eax
je 0x3139
movl $0x4, %edx
movl %ebx, %edi
movq %r13, %rsi
xorl %ecx, %ecx
callq 0x20a0
testl %eax, %eax
je 0x3139
movl -0x30(%rbp), %edx
movl %ebx, %edi
leaq -0x100(%rbp), %rsi
callq 0x481c
cmpl $-0x1, %eax
je 0x3139
movslq -0x30(%rbp), %rax
movb $0x0, -0x100(%rbp,%rax)
movl -0x44(%rbp), %eax
movl $0xf000, %ecx           # imm = 0xF000
andl %ecx, %eax
cmpl $0x4000, %eax           # imm = 0x4000
jne 0x2f69
movq %r13, %r14
movl -0x38(%rbp), %edx
movl -0x34(%rbp), %ecx
leaq -0x200(%rbp), %r15
movq %r15, %rdi
leaq 0x22fe(%rip), %rsi      # 0x51c8
leaq -0x100(%rbp), %r13
movq %r13, %r8
xorl %eax, %eax
callq 0x2060
movq -0x40(%rbp), %rsi
movq %r15, %rdi
callq 0x3970
movl -0x38(%rbp), %ecx
movl -0x34(%rbp), %r8d
movq %r15, %rdi
leaq 0x2320(%rip), %rsi      # 0x5218
movq %r13, %rdx
xorl %eax, %eax
callq 0x2060
movq -0x40(%rbp), %rsi
movq %r15, %rdi
callq 0x46a8
movq %rax, %r13
movq %rax, %rdi
callq 0x21f0
movq (%rax), %rdi
callq 0x22f0
movl %eax, -0x1200(%rbp)
movq %r13, %rdi
callq 0x21e0
movl $0x4, %edx
movl %ebx, %edi
leaq -0x1200(%rbp), %rsi
xorl %ecx, %ecx
callq 0x2100
movl $0x2, %ecx
testl %eax, %eax
jne 0x2f59
movl %ebx, %edi
callq 0x2370
movl $0x1, %ecx
movq %r14, %r13
leaq -0x44(%rbp), %r14
leaq -0x2c(%rbp), %r15
jmp 0x3145
movl $0x8, %edx
movl %ebx, %edi
leaq -0x58(%rbp), %rsi
xorl %ecx, %ecx
callq 0x20a0
testl %eax, %eax
je 0x3139
xorps %xmm0, %xmm0
movaps %xmm0, -0x70(%rbp)
movaps %xmm0, -0x80(%rbp)
movb $0x0, -0x60(%rbp)
movl %ebx, %edi
movq %r12, %rsi
movq %r12, %r13
movl $0x20, %edx
callq 0x481c
cmpl $-0x1, %eax
je 0x304e
leaq -0x200(%rbp), %rdi
leaq 0x22ac(%rip), %rsi      # 0x5267
movq %r13, %rdx
xorl %eax, %eax
callq 0x2060
movq -0x40(%rbp), %rsi
leaq -0x200(%rbp), %rdi
callq 0x46a8
movq %rax, %r13
movq %rax, %rdi
callq 0x21f0
cmpq $0x1, %rax
movl $0x5, %eax
sbbl $0x0, %eax
movl %eax, -0x2c(%rbp)
movq %r13, %rdi
callq 0x21e0
movl -0x34(%rbp), %edx
movl -0x38(%rbp), %r8d
movq -0x58(%rbp), %rax
movq %rax, (%rsp)
leaq -0x200(%rbp), %r13
movq %r13, %rdi
leaq 0x2273(%rip), %rsi      # 0x528a
leaq -0x100(%rbp), %rcx
leaq -0x80(%rbp), %r9
xorl %eax, %eax
callq 0x2060
movq -0x40(%rbp), %rsi
movq %r13, %rdi
callq 0x3970
movl $0x4, %edx
movl %ebx, %edi
movq %r15, %rsi
movl $0x4000, %ecx           # imm = 0x4000
callq 0x2100
cmpl $-0x1, %eax
jne 0x3063
movl %ebx, %edi
callq 0x2370
movl $0x1, %ecx
leaq -0x30(%rbp), %r13
jmp 0x3145
movl -0x2c(%rbp), %esi
leaq 0x2284(%rip), %rdi      # 0x52f1
xorl %eax, %eax
callq 0x2040
cmpl $0x5, -0x2c(%rbp)
leaq -0x30(%rbp), %r13
jne 0x3091
leaq 0x227a(%rip), %rdi      # 0x52ff
xorl %eax, %eax
callq 0x2040
jmp 0x3194
movl $0x4, %edx
movl %ebx, %edi
movq %r15, %rsi
xorl %ecx, %ecx
callq 0x20a0
testl %eax, %eax
je 0x3139
cmpl $0x1, -0x2c(%rbp)
jne 0x3165
movl $0x1000, %edx           # imm = 0x1000
leaq -0x1200(%rbp), %rdi
xorl %esi, %esi
callq 0x2130
leaq -0x1200(%rbp), %rdi
leaq 0x2265(%rip), %rsi      # 0x533a
leaq 0x2264(%rip), %rdx      # 0x5340
movq %r12, %rcx
xorl %eax, %eax
callq 0x2060
movl -0x44(%rbp), %edx
leaq -0x1200(%rbp), %rdi
movl $0xc2, %esi
xorl %eax, %eax
callq 0x2150
cmpl $-0x1, %eax
je 0x3139
movq -0x58(%rbp), %rdx
movl %ebx, %edi
movl %eax, %esi
movl %eax, -0x48(%rbp)
callq 0x48d8
cmpl $-0x1, %eax
movl -0x48(%rbp), %edi
je 0x3139
callq 0x2370
movl $0x4, %edx
movl %ebx, %edi
movq %r15, %rsi
movl $0x4000, %ecx           # imm = 0x4000
callq 0x2100
xorl %ecx, %ecx
cmpl $-0x1, %eax
jne 0x3145
movl %ebx, %edi
callq 0x2370
movl $0x1, %ecx
testb $0x1, %cl
jne 0x31a2
movl $0x4, %edx
movl %ebx, %edi
movq %r15, %rsi
xorl %ecx, %ecx
callq 0x20a0
testl %eax, %eax
jne 0x2e1f
jmp 0x319b
movl -0x34(%rbp), %edx
leaq -0x200(%rbp), %rdi
leaq 0x218c(%rip), %rsi      # 0x5302
leaq -0x100(%rbp), %rcx
xorl %eax, %eax
callq 0x2060
movq -0x40(%rbp), %rsi
leaq -0x200(%rbp), %rdi
callq 0x3970
movl $0x2, %ecx
jmp 0x3145
movl %ebx, %edi
callq 0x2370
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
addq $0x11e8, %rsp           # imm = 0x11E8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl %ebx, %edi
callq 0x2370
movq -0x40(%rbp), %rdi
callq 0x2250
xorl %eax, %eax
jmp 0x31a7
nopl (%rax)