// File: /workspace/github_c_makefile_star2_O0/VeeeectorPan[P]MyNetdisk/clientSrc/recv_server_file.c
// Function: recv_server_file
int recv_server_file(int sock_fd,char* path)
{
    puts(path);
    struct stat st;
    recv_n_bytes(sock_fd,&st,sizeof(struct stat));
    int ret = access(path,F_OK);
    int ctl_num = -1;
    if(ret == -1)
    {
        ctl_num = GETS_NOT_EXIST;
        send(sock_fd,&ctl_num,sizeof(int),0);
        int file_fd = open(path,O_RDWR|O_CREAT|O_EXCL,st.st_mode); 
        if(file_fd == -1)
        {
            perror(path);
            ctl_num = GETS_STOP_RECV;
            send(sock_fd,&ctl_num,sizeof(int),0);
            return -1;
        }
        else
        {
            ctl_num = GETS_START_RECV;
            send(sock_fd,&ctl_num,sizeof(int),0);
            recv_file(sock_fd,file_fd,st.st_size);
            close(file_fd);
            ctl_num = GETS_OVER;
            send(sock_fd,&ctl_num,sizeof(int),0);
        }

    }
    else
    {
        ctl_num = GETS_EXIST;
        send(sock_fd,&ctl_num,sizeof(int),0);
        int file_fd = open(path,O_RDWR|O_CREAT|O_EXCL,st.st_mode); 
        if(file_fd == -1)
        {
            perror(path);
            ctl_num = GETS_STOP_RECV;
            send(sock_fd,&ctl_num,sizeof(int),0);
            return -1;
        }
        else
        {
            ctl_num = GETS_START_RECV;
            send(sock_fd,&ctl_num,sizeof(int),0);
            fstat(file_fd,&st);
            send(sock_fd,&st,sizeof(struct stat),0);
            char md5[33] = {0};
            char server_md5[33] = {0};
            compute_file_md5(path,md5,st.st_size);
            recv_client_all(sock_fd,server_md5,32);
            puts(md5);
            puts(server_md5);
            int offset = 0;
            if(strcmp(md5,server_md5) == 0)
            {
                offset = st.st_size;
            }
            send(sock_fd,&offset,sizeof(int),0);
            recv_file_from_offset(sock_fd,file_fd,st.st_size,offset);
            close(file_fd);
            ctl_num = GETS_OVER;
            send(sock_fd,&ctl_num,sizeof(int),0);
        }

    }
    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x120, %rsp            # imm = 0x120
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x10(%rbp), %rdi
callq 0x2250
movl -0x8(%rbp), %edi
leaq -0xa0(%rbp), %rsi
movl $0x90, %edx
callq 0x4da0
movq -0x10(%rbp), %rdi
xorl %esi, %esi
callq 0x2280
movl %eax, -0xa4(%rbp)
movl $0xffffffff, -0xa8(%rbp) # imm = 0xFFFFFFFF
cmpl $-0x1, -0xa4(%rbp)
jne 0x4f45
movl $0x1, -0xa8(%rbp)
movl -0x8(%rbp), %edi
leaq -0xa8(%rbp), %rsi
movl $0x4, %edx
xorl %ecx, %ecx
callq 0x20d0
movq -0x10(%rbp), %rdi
movl -0x88(%rbp), %edx
movl $0xc2, %esi
movb $0x0, %al
callq 0x2100
movl %eax, -0xac(%rbp)
cmpl $-0x1, -0xac(%rbp)
jne 0x4ee1
movq -0x10(%rbp), %rdi
callq 0x2130
movl $0x3, -0xa8(%rbp)
movl -0x8(%rbp), %edi
leaq -0xa8(%rbp), %rsi
movl $0x4, %edx
xorl %ecx, %ecx
callq 0x20d0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x50ff
movl $0x2, -0xa8(%rbp)
movl -0x8(%rbp), %edi
leaq -0xa8(%rbp), %rsi
movl $0x4, %edx
xorl %ecx, %ecx
callq 0x20d0
movl -0x8(%rbp), %edi
movl -0xac(%rbp), %esi
movq -0x70(%rbp), %rax
movl %eax, %edx
callq 0x4aa0
movl -0xac(%rbp), %edi
callq 0x22c0
movl $0x4, -0xa8(%rbp)
movl -0x8(%rbp), %edi
leaq -0xa8(%rbp), %rsi
movl $0x4, %edx
xorl %ecx, %ecx
callq 0x20d0
jmp 0x50f8
movl $0x0, -0xa8(%rbp)
movl -0x8(%rbp), %edi
leaq -0xa8(%rbp), %rsi
movl $0x4, %edx
xorl %ecx, %ecx
callq 0x20d0
movq -0x10(%rbp), %rdi
movl -0x88(%rbp), %edx
movl $0xc2, %esi
movb $0x0, %al
callq 0x2100
movl %eax, -0xb0(%rbp)
cmpl $-0x1, -0xb0(%rbp)
jne 0x4fbf
movq -0x10(%rbp), %rdi
callq 0x2130
movl $0x3, -0xa8(%rbp)
movl -0x8(%rbp), %edi
leaq -0xa8(%rbp), %rsi
movl $0x4, %edx
xorl %ecx, %ecx
callq 0x20d0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x50ff
movl $0x2, -0xa8(%rbp)
movl -0x8(%rbp), %edi
leaq -0xa8(%rbp), %rsi
movl $0x4, %edx
xorl %ecx, %ecx
callq 0x20d0
movl -0xb0(%rbp), %edi
leaq -0xa0(%rbp), %rsi
callq 0x2290
movl -0x8(%rbp), %edi
leaq -0xa0(%rbp), %rsi
movl $0x90, %edx
xorl %ecx, %ecx
callq 0x20d0
leaq -0xe0(%rbp), %rdi
xorl %esi, %esi
movl $0x21, %edx
callq 0x20f0
leaq -0x110(%rbp), %rdi
xorl %esi, %esi
movl $0x21, %edx
callq 0x20f0
movq -0x10(%rbp), %rdi
leaq -0xe0(%rbp), %rsi
movq -0x70(%rbp), %rax
movl %eax, %edx
callq 0x2c30
movl -0x8(%rbp), %edi
leaq -0x110(%rbp), %rsi
movl $0x20, %edx
callq 0x4a30
leaq -0xe0(%rbp), %rdi
callq 0x2250
leaq -0x110(%rbp), %rdi
callq 0x2250
movl $0x0, -0x114(%rbp)
leaq -0xe0(%rbp), %rdi
leaq -0x110(%rbp), %rsi
callq 0x21e0
cmpl $0x0, %eax
jne 0x509b
movq -0x70(%rbp), %rax
movl %eax, -0x114(%rbp)
movl -0x8(%rbp), %edi
leaq -0x114(%rbp), %rsi
movl $0x4, %edx
xorl %ecx, %ecx
callq 0x20d0
movl -0x8(%rbp), %edi
movl -0xb0(%rbp), %esi
movq -0x70(%rbp), %rax
movl %eax, %edx
movl -0x114(%rbp), %ecx
callq 0x4c20
movl -0xb0(%rbp), %edi
callq 0x22c0
movl $0x4, -0xa8(%rbp)
movl -0x8(%rbp), %edi
leaq -0xa8(%rbp), %rsi
movl $0x4, %edx
xorl %ecx, %ecx
callq 0x20d0
jmp 0x50f8
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x120, %rsp            # imm = 0x120
popq %rbp
retq
nopl (%rax,%rax)