// File: /workspace/github_c_makefile_star2_O2/VeeeectorPan[P]MyNetdisk/clientSrc/upload.c
// Function: upload
int upload(int sock_fd,char* path,int parent_id)
{
    int ctl_num = MORE_FILE;
    send(sock_fd,&ctl_num,sizeof(int),0);
    struct stat st;
    stat(path,&st);
    send(sock_fd,&st.st_mode,sizeof(mode_t),0);
    send(sock_fd,&parent_id,sizeof(int),0);
    char dir_name[128] = {0};
    get_file_name(dir_name,path);
    int len = strlen(dir_name);
    send(sock_fd,&len,sizeof(int),0);
    send(sock_fd,dir_name,strlen(dir_name),0);
    if((st.st_mode & S_IFMT) == S_IFDIR)
    {
        int file_id;
        recv(sock_fd,&file_id,sizeof(int),0);
        DIR* pdir = opendir(path);
        struct dirent* file;
        char buf[4096] = {0};
        while((file = readdir(pdir)) != NULL)
        {
            if(strcmp(file->d_name,".") == 0 || strcmp(file->d_name,"..") == 0)
            {
                continue;
            }
            memset(buf,0,4096);
            sprintf(buf,"%s/%s",path,file->d_name);
            upload(sock_fd,buf,file_id);
        }
        return 0;
    }
    send(sock_fd,&st.st_size,sizeof(off_t),0);
    char md5[33] = {0};
    compute_file_md5(path,md5,st.st_size);
    send(sock_fd,md5,32,0);
    recv(sock_fd,&ctl_num,sizeof(int),0);
    if(ctl_num == PUT_NOT_EXIST)
    {
        int file_fd = open(path,O_RDWR);
        if(file_fd == -1)
        {
            perror(path);
            ctl_num = PUT_FAIL;
            send(sock_fd,&ctl_num,sizeof(int),0);
            return 0;
        }
        else
        {
            ctl_num = PUT_SUCCESS;
            send(sock_fd,&ctl_num,sizeof(int),0);
        }
        int ret = sendfile(sock_fd,file_fd,NULL,st.st_size);
        ERROR_CHECK(ret,-1,"sendfile");
        recv(sock_fd,&ctl_num,sizeof(int),0);
        close(file_fd);
    }
    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1128, %rsp           # imm = 0x1128
movq %rsi, %r15
movl %edi, %ebx
leaq -0x38(%rbp), %r14
movl %edx, (%r14)
leaq -0x2c(%rbp), %rsi
movl $0x2, (%rsi)
pushq $0x4
popq %r13
movq %r13, %rdx
xorl %ecx, %ecx
callq 0x20c0
leaq -0xc8(%rbp), %rsi
movq %r15, %rdi
callq 0x2150
leaq -0xb0(%rbp), %r12
movl %ebx, %edi
movq %r12, %rsi
movq %r13, %rdx
xorl %ecx, %ecx
callq 0x20c0
movl %ebx, %edi
movq %r14, %rsi
movq %r13, %rdx
xorl %ecx, %ecx
callq 0x20c0
xorps %xmm0, %xmm0
leaq -0x150(%rbp), %r14
movaps %xmm0, 0x70(%r14)
movaps %xmm0, 0x60(%r14)
movaps %xmm0, 0x50(%r14)
movaps %xmm0, 0x40(%r14)
movaps %xmm0, 0x30(%r14)
movaps %xmm0, 0x20(%r14)
movaps %xmm0, 0x10(%r14)
movaps %xmm0, (%r14)
movq %r14, %rdi
movq %r15, %rsi
callq 0x2be0
movq %r14, %rdi
callq 0x2080
leaq -0x34(%rbp), %rsi
movl %eax, (%rsi)
movl %ebx, %edi
movq %r13, %rdx
xorl %ecx, %ecx
callq 0x20c0
movq %r14, %rdi
callq 0x2080
movl %ebx, %edi
movq %r14, %rsi
movq %rax, %rdx
xorl %ecx, %ecx
callq 0x20c0
movl $0xf000, %eax           # imm = 0xF000
andl (%r12), %eax
cmpl $0x4000, %eax           # imm = 0x4000
jne 0x4509
leaq -0x30(%rbp), %rsi
pushq $0x4
popq %rdx
movl %ebx, %r13d
movl %ebx, %edi
xorl %ecx, %ecx
callq 0x2070
movq %r15, %rbx
movq %r15, %rdi
callq 0x2230
movq %rax, %r15
leaq -0x1150(%rbp), %r12
movl $0x1000, %edx           # imm = 0x1000
movq %r12, %rdi
xorl %esi, %esi
callq 0x20e0
movq %r15, %rdi
callq 0x2250
testq %rax, %rax
je 0x45d9
movq %rax, %r14
cmpb $0x2e, 0x13(%rax)
jne 0x44cf
cmpb $0x0, 0x14(%r14)
je 0x44a0
cmpb $0x2e, 0x14(%r14)
jne 0x44cf
cmpb $0x0, 0x15(%r14)
je 0x44a0
addq $0x13, %r14
movl $0x1000, %edx           # imm = 0x1000
movq %r12, %rdi
xorl %esi, %esi
callq 0x20e0
movq %r12, %rdi
leaq 0xbc1(%rip), %rsi       # 0x50ad
movq %rbx, %rdx
movq %r14, %rcx
xorl %eax, %eax
callq 0x2050
movl -0x30(%rbp), %edx
movl %r13d, %edi
movq %r12, %rsi
callq 0x437c
jmp 0x44a0
leaq -0x98(%rbp), %r14
pushq $0x8
popq %rdx
movl %ebx, %edi
movq %r14, %rsi
xorl %ecx, %ecx
callq 0x20c0
leaq -0x1150(%rbp), %r12
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%r12)
movaps %xmm0, (%r12)
movb $0x0, 0x20(%r12)
movl (%r14), %edx
movq %r15, %rdi
movq %r12, %rsi
callq 0x2a60
pushq $0x20
popq %rdx
movl %ebx, %edi
movq %r12, %rsi
xorl %ecx, %ecx
callq 0x20c0
leaq -0x2c(%rbp), %r14
pushq $0x4
popq %rdx
movl %ebx, %edi
movq %r14, %rsi
xorl %ecx, %ecx
callq 0x2070
cmpl $0x4, (%r14)
movl $0x0, %r14d
jne 0x45dc
xorl %r14d, %r14d
pushq $0x2
popq %rsi
movq %r15, %rdi
xorl %eax, %eax
callq 0x2100
cmpl $-0x1, %eax
je 0x45f1
movl %eax, %r12d
leaq -0x2c(%rbp), %rsi
movl $0x1, (%rsi)
pushq $0x4
popq %rdx
xorl %r14d, %r14d
movl %ebx, %edi
xorl %ecx, %ecx
callq 0x20c0
movq -0x98(%rbp), %rcx
movl %ebx, %edi
movl %r12d, %esi
xorl %edx, %edx
callq 0x2240
cmpl $-0x1, %eax
je 0x460e
leaq -0x2c(%rbp), %rsi
pushq $0x4
popq %rdx
movl %ebx, %edi
xorl %ecx, %ecx
callq 0x2070
movl %r12d, %edi
callq 0x22e0
jmp 0x45dc
xorl %r14d, %r14d
movl %r14d, %eax
addq $0x1128, %rsp           # imm = 0x1128
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r15, %rdi
callq 0x2130
leaq -0x2c(%rbp), %rsi
andl $0x0, (%rsi)
pushq $0x4
popq %rdx
movl %ebx, %edi
xorl %ecx, %ecx
callq 0x20c0
jmp 0x45dc
leaq 0xc37(%rip), %rdi       # 0x524c
callq 0x2130
pushq $-0x1
popq %r14
jmp 0x45dc