// File: /workspace/github_c_makefile_star2_O2/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 0x3434
leaq -0x38(%rbp), %rsi
pushq $0x4
popq %rdx
movl %ebx, %edi
xorl %ecx, %ecx
callq 0x20a0
testl %eax, %eax
je 0x2d45
leaq -0x4c(%rbp), %rsi
pushq $0x4
popq %r14
movl %ebx, %edi
movq %r14, %rdx
xorl %ecx, %ecx
callq 0x20a0
leaq -0x34(%rbp), %rsi
movl %ebx, %edi
movq %r14, %rdx
xorl %ecx, %ecx
callq 0x20a0
testl %eax, %eax
je 0x2d45
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 -0x34(%rbp), %edx
movl %ebx, %edi
callq 0x45cc
cmpl $-0x1, %eax
je 0x2d45
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 0x248d(%rip), %rsi      # 0x517c
leaq -0x100(%rbp), %rcx
movq %r14, %rdi
xorl %eax, %eax
callq 0x2060
movq -0x40(%rbp), %rsi
movq %r14, %rdi
callq 0x4468
movq %rax, %r14
movq %rax, %rdi
callq 0x2200
xorl %ecx, %ecx
testq %rax, %rax
sete %cl
leaq -0x2c(%rbp), %r15
movl %ecx, (%r15)
movq %r14, %rdi
callq 0x21f0
pushq $0x4
popq %rdx
movl %ebx, %edi
movq %r15, %rsi
movl $0x4000, %ecx           # imm = 0x4000
callq 0x2100
cmpl $-0x1, %eax
jne 0x2d61
movl %ebx, %edi
callq 0x2380
pushq $-0x1
popq %rax
addq $0x11e8, %rsp           # imm = 0x11E8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq -0x2c(%rbp), %r14
pushq $0x4
popq %r15
leaq -0x100(%rbp), %r13
leaq -0x80(%rbp), %r12
movl %ebx, %edi
movq %r14, %rsi
movq %r15, %rdx
xorl %ecx, %ecx
callq 0x20a0
testl %eax, %eax
je 0x2d45
cmpl $0x3, -0x2c(%rbp)
je 0x30cb
movl %ebx, %edi
leaq -0x44(%rbp), %rsi
movq %r15, %rdx
xorl %ecx, %ecx
callq 0x20a0
testl %eax, %eax
je 0x2d45
movl %ebx, %edi
leaq -0x30(%rbp), %rsi
movq %r15, %rdx
xorl %ecx, %ecx
callq 0x20a0
testl %eax, %eax
je 0x2d45
movl %ebx, %edi
leaq -0x34(%rbp), %rsi
movq %r15, %rdx
xorl %ecx, %ecx
callq 0x20a0
testl %eax, %eax
je 0x2d45
movl -0x34(%rbp), %edx
movl %ebx, %edi
movq %r13, %rsi
callq 0x45cc
cmpl $-0x1, %eax
je 0x2d45
movslq -0x34(%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 0x2eaa
movl -0x38(%rbp), %edx
movl -0x30(%rbp), %ecx
leaq -0x200(%rbp), %r14
movq %r14, %rdi
leaq 0x23a9(%rip), %rsi      # 0x51c8
movq %r13, %r8
xorl %eax, %eax
callq 0x2060
movq -0x40(%rbp), %rsi
movq %r14, %rdi
callq 0x384c
movl -0x38(%rbp), %ecx
movl -0x30(%rbp), %r8d
movq %r14, %rdi
leaq 0x23d2(%rip), %rsi      # 0x5218
movq %r13, %rdx
xorl %eax, %eax
callq 0x2060
movq -0x40(%rbp), %rsi
movq %r14, %rdi
callq 0x4468
movq %rax, %r14
movq %rax, %rdi
callq 0x2200
movq (%rax), %rdi
callq 0x2300
movl %eax, -0x1200(%rbp)
movq %r14, %rdi
callq 0x21f0
movl %ebx, %edi
leaq -0x1200(%rbp), %rsi
movq %r15, %rdx
xorl %ecx, %ecx
callq 0x2100
pushq $0x2
popq %rcx
testl %eax, %eax
jne 0x2ea1
movl %ebx, %edi
callq 0x2380
pushq $0x1
popq %rcx
leaq -0x2c(%rbp), %r14
jmp 0x308e
movl %ebx, %edi
leaq -0x58(%rbp), %rsi
pushq $0x8
popq %rdx
xorl %ecx, %ecx
callq 0x20a0
testl %eax, %eax
je 0x3084
xorps %xmm0, %xmm0
movaps %xmm0, -0x70(%rbp)
movaps %xmm0, -0x80(%rbp)
movb $0x0, -0x60(%rbp)
movl %ebx, %edi
movq %r12, %r13
movq %r12, %rsi
pushq $0x20
popq %rdx
callq 0x45cc
cmpl $-0x1, %eax
je 0x2f8e
leaq -0x200(%rbp), %r15
movq %r15, %rdi
leaq 0x236c(%rip), %rsi      # 0x5267
movq %r13, %rdx
xorl %eax, %eax
callq 0x2060
movq -0x40(%rbp), %rsi
movq %r15, %rdi
callq 0x4468
movq %rax, %r14
movq %rax, %rdi
callq 0x2200
cmpq $0x1, %rax
pushq $0x5
popq %rax
sbbl $0x0, %eax
movl %eax, -0x2c(%rbp)
movq %r14, %rdi
leaq -0x2c(%rbp), %r14
callq 0x21f0
movl -0x30(%rbp), %edx
movl -0x38(%rbp), %r8d
movq -0x58(%rbp), %rax
movq %rax, (%rsp)
movq %r15, %rdi
leaq 0x233c(%rip), %rsi      # 0x528a
leaq -0x100(%rbp), %rcx
movq %r13, %r9
xorl %eax, %eax
callq 0x2060
movq -0x40(%rbp), %rsi
movq %r15, %rdi
callq 0x384c
movl %ebx, %edi
movq %r14, %rsi
pushq $0x4
popq %rdx
movl $0x4000, %ecx           # imm = 0x4000
callq 0x2100
cmpl $-0x1, %eax
jne 0x2fab
movl %ebx, %edi
callq 0x2380
pushq $0x1
popq %rcx
jmp 0x2f9b
movl %ebx, %edi
callq 0x2380
pushq $0x1
popq %rcx
movq %r13, %r12
leaq -0x100(%rbp), %r13
pushq $0x4
popq %r15
jmp 0x308e
movl -0x2c(%rbp), %esi
leaq 0x233c(%rip), %rdi      # 0x52f1
xorl %eax, %eax
callq 0x2040
cmpl $0x5, -0x2c(%rbp)
leaq -0x100(%rbp), %r13
jne 0x2fdc
leaq 0x232f(%rip), %rdi      # 0x52ff
xorl %eax, %eax
callq 0x2040
pushq $0x2
popq %rcx
jmp 0x2fa2
movl %ebx, %edi
movq %r14, %rsi
pushq $0x4
popq %r15
movq %r15, %rdx
xorl %ecx, %ecx
callq 0x20a0
testl %eax, %eax
je 0x3084
cmpl $0x1, -0x2c(%rbp)
jne 0x309c
movl $0x1000, %edx           # imm = 0x1000
leaq -0x1200(%rbp), %rdi
xorl %esi, %esi
callq 0x2130
leaq -0x1200(%rbp), %rdi
leaq 0x2318(%rip), %rsi      # 0x533a
leaq 0x2317(%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 0x2160
cmpl $-0x1, %eax
je 0x3084
movq -0x58(%rbp), %rdx
movl %ebx, %edi
movl %eax, %esi
movl %eax, -0x48(%rbp)
callq 0x4688
cmpl $-0x1, %eax
movl -0x48(%rbp), %edi
je 0x3084
callq 0x2380
movl %ebx, %edi
movq %r14, %rsi
movq %r15, %rdx
movl $0x4000, %ecx           # imm = 0x4000
callq 0x2100
xorl %ecx, %ecx
cmpl $-0x1, %eax
jne 0x308e
movl %ebx, %edi
callq 0x2380
pushq $0x1
popq %rcx
testb $0x1, %cl
je 0x2d74
jmp 0x2d4c
movl -0x30(%rbp), %edx
leaq -0x200(%rbp), %rdi
leaq 0x2255(%rip), %rsi      # 0x5302
movq %r13, %rcx
xorl %eax, %eax
callq 0x2060
movq -0x40(%rbp), %rsi
leaq -0x200(%rbp), %rdi
callq 0x384c
pushq $0x2
jmp 0x308d
movl %ebx, %edi
callq 0x2380
movq -0x40(%rbp), %rdi
callq 0x2260
xorl %eax, %eax
jmp 0x2d4f
nop