// File: /workspace/github_c_makefile_star2_O3/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 0x34e8
leaq -0x38(%rbp), %rsi
movl $0x4, %edx
movl %ebx, %edi
xorl %ecx, %ecx
callq 0x20a0
testl %eax, %eax
je 0x3156
leaq -0x48(%rbp), %rsi
movl $0x4, %edx
movl %ebx, %edi
xorl %ecx, %ecx
callq 0x20a0
leaq -0x34(%rbp), %rsi
movl $0x4, %edx
movl %ebx, %edi
xorl %ecx, %ecx
callq 0x20a0
testl %eax, %eax
je 0x3156
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 0x47cc
cmpl $-0x1, %eax
je 0x3156
leaq -0x200(%rbp), %r14
movl $0x100, %edx            # imm = 0x100
movq %r14, %rdi
xorl %esi, %esi
callq 0x2130
movl -0x48(%rbp), %edx
movl -0x38(%rbp), %r8d
leaq 0x23eb(%rip), %rsi      # 0x517c
leaq -0x100(%rbp), %rcx
movq %r14, %rdi
xorl %eax, %eax
callq 0x2060
movq -0x40(%rbp), %rsi
movq %r14, %rdi
callq 0x4664
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
movl $0x4, %edx
movl %ebx, %edi
movq %r15, %rsi
movl $0x4000, %ecx           # imm = 0x4000
callq 0x2100
cmpl $-0x1, %eax
je 0x3156
leaq -0x2c(%rbp), %rsi
movl $0x4, %edx
movl %ebx, %edi
xorl %ecx, %ecx
callq 0x20a0
testl %eax, %eax
je 0x3156
leaq -0x100(%rbp), %r13
leaq -0x200(%rbp), %r12
leaq -0x80(%rbp), %r14
cmpl $0x3, -0x2c(%rbp)
je 0x3174
movl $0x4, %edx
movl %ebx, %edi
leaq -0x44(%rbp), %rsi
xorl %ecx, %ecx
callq 0x20a0
testl %eax, %eax
je 0x3156
movl $0x4, %edx
movl %ebx, %edi
leaq -0x30(%rbp), %rsi
xorl %ecx, %ecx
callq 0x20a0
testl %eax, %eax
je 0x3156
movl $0x4, %edx
movl %ebx, %edi
leaq -0x34(%rbp), %rsi
xorl %ecx, %ecx
callq 0x20a0
testl %eax, %eax
je 0x3156
movl -0x34(%rbp), %edx
movl %ebx, %edi
movq %r13, %rsi
callq 0x47cc
cmpl $-0x1, %eax
je 0x3156
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 0x2f49
movl -0x38(%rbp), %edx
movl -0x30(%rbp), %ecx
movq %r12, %rdi
leaq 0x2310(%rip), %rsi      # 0x51c8
movq %r13, %r8
xorl %eax, %eax
callq 0x2060
movq -0x40(%rbp), %rsi
movq %r12, %rdi
callq 0x3934
movl -0x38(%rbp), %ecx
movl -0x30(%rbp), %r8d
movq %r12, %rdi
leaq 0x2339(%rip), %rsi      # 0x5218
movq %r13, %rdx
xorl %eax, %eax
callq 0x2060
movq -0x40(%rbp), %rsi
movq %r12, %rdi
callq 0x4664
movq %rax, %r14
movq %rax, %rdi
callq 0x2200
movq (%rax), %rdi
callq 0x2300
movl %eax, -0x1200(%rbp)
movq %r14, %rdi
callq 0x21f0
movl $0x4, %edx
movl %ebx, %edi
leaq -0x1200(%rbp), %rsi
xorl %ecx, %ecx
callq 0x2100
movl $0x2, %ecx
testl %eax, %eax
jne 0x2f40
movl %ebx, %edi
callq 0x2380
movl $0x1, %ecx
leaq -0x80(%rbp), %r14
jmp 0x3027
movl $0x8, %edx
movl %ebx, %edi
leaq -0x50(%rbp), %rsi
xorl %ecx, %ecx
callq 0x20a0
testl %eax, %eax
je 0x301b
xorps %xmm0, %xmm0
movaps %xmm0, -0x70(%rbp)
movaps %xmm0, -0x80(%rbp)
movb $0x0, -0x60(%rbp)
movl %ebx, %edi
movq %r14, %rsi
movl $0x20, %edx
callq 0x47cc
cmpl $-0x1, %eax
je 0x301b
movq %r12, %rdi
leaq 0x22d3(%rip), %rsi      # 0x5267
movq %r14, %rdx
xorl %eax, %eax
callq 0x2060
movq -0x40(%rbp), %rsi
movq %r12, %rdi
callq 0x4664
movq %rax, %r14
movq %rax, %rdi
callq 0x2200
cmpq $0x1, %rax
movl $0x5, %eax
sbbl $0x0, %eax
movl %eax, -0x2c(%rbp)
movq %r14, %rdi
leaq -0x80(%rbp), %r14
callq 0x21f0
movl -0x30(%rbp), %edx
movl -0x38(%rbp), %r8d
movq -0x50(%rbp), %rax
movq %rax, (%rsp)
movq %r12, %rdi
leaq 0x22a1(%rip), %rsi      # 0x528a
movq %r13, %rcx
movq %r14, %r9
xorl %eax, %eax
callq 0x2060
movq -0x40(%rbp), %rsi
movq %r12, %rdi
callq 0x3934
movl $0x4, %edx
movl %ebx, %edi
movq %r15, %rsi
movl $0x4000, %ecx           # imm = 0x4000
callq 0x2100
cmpl $-0x1, %eax
jne 0x304e
movl %ebx, %edi
callq 0x2380
movl $0x1, %ecx
testb $0x1, %cl
jne 0x315d
movl $0x4, %edx
movl %ebx, %edi
movq %r15, %rsi
xorl %ecx, %ecx
callq 0x20a0
testl %eax, %eax
jne 0x2e19
jmp 0x3156
movl -0x2c(%rbp), %esi
leaq 0x2299(%rip), %rdi      # 0x52f1
xorl %eax, %eax
callq 0x2040
cmpl $0x5, -0x2c(%rbp)
jne 0x3078
leaq 0x2293(%rip), %rdi      # 0x52ff
xorl %eax, %eax
callq 0x2040
jmp 0x314c
movl $0x4, %edx
movl %ebx, %edi
movq %r15, %rsi
xorl %ecx, %ecx
callq 0x20a0
testl %eax, %eax
je 0x301b
cmpl $0x1, -0x2c(%rbp)
jne 0x3129
movl $0x1000, %edx           # imm = 0x1000
leaq -0x1200(%rbp), %r14
movq %r14, %rdi
xorl %esi, %esi
callq 0x2130
movq %r14, %rdi
leaq 0x2283(%rip), %rsi      # 0x533a
leaq 0x2282(%rip), %rdx      # 0x5340
leaq -0x80(%rbp), %rcx
xorl %eax, %eax
callq 0x2060
movl -0x44(%rbp), %edx
movq %r14, %rdi
movl $0xc2, %esi
xorl %eax, %eax
callq 0x2160
cmpl $-0x1, %eax
je 0x2f34
movl %eax, %r14d
movq -0x50(%rbp), %rdx
movl %ebx, %edi
movl %eax, %esi
callq 0x4888
cmpl $-0x1, %eax
je 0x2f34
movl %r14d, %edi
callq 0x2380
movl $0x4, %edx
movl %ebx, %edi
movq %r15, %rsi
movl $0x4000, %ecx           # imm = 0x4000
callq 0x2100
xorl %ecx, %ecx
cmpl $-0x1, %eax
je 0x2f34
jmp 0x2f40
movl -0x30(%rbp), %edx
movq %r12, %rdi
leaq 0x21cc(%rip), %rsi      # 0x5302
movq %r13, %rcx
xorl %eax, %eax
callq 0x2060
movq -0x40(%rbp), %rsi
movq %r12, %rdi
callq 0x3934
movl $0x2, %ecx
jmp 0x3027
movl %ebx, %edi
callq 0x2380
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 0x2380
movq -0x40(%rbp), %rdi
callq 0x2260
xorl %eax, %eax
jmp 0x3162