// File: /workspace/github_c_makefile_star2_O2/VeeeectorPan[P]MyNetdisk/SourceFile/send_file_to_client.c
// Function: send_file_to_client
int send_file_to_client(int client_fd,int file_id,char* file_name,char* path,MYSQL* connect)
{
    char buf[4096] = {0};
    sprintf(buf,"select file_id,md5,file_type from File where parent_id=%d and file_name='%s';"
            ,file_id,file_name);
    MYSQL_RES* res = query(buf,connect);
    MYSQL_ROW row = mysql_fetch_row(res);
    if(row == NULL)
        return 0;
    memset(buf,0,sizeof(buf));
    sprintf(buf + sizeof(int),"%s/%s",path,file_name);
    int len = strlen(buf +sizeof(int));
    memcpy(buf,&len,sizeof(int));
    buf[sizeof(int) + len] = *row[2];
    int ret = send(client_fd,buf,sizeof(int) + len + 1, MSG_NOSIGNAL);
    if(ret == -1)
        return -1;
    if(*row[2] == 'd')
    {
        file_id = atoi(row[0]);
        mysql_free_result(res);
        sprintf(buf,"select file_name from File where parent_id=%d;",file_id);
        res = query2(buf,connect);
        unsigned long num_rows = mysql_num_rows(res);
        if(num_rows == 0)
        {
            return 0;
        }
        char** file_name_arr = (char**)malloc(sizeof(char*)* num_rows);
        char* _file_name = (char*)malloc(sizeof(char) * 128 * num_rows);
        memset(_file_name,0,sizeof(char) * 128 * num_rows);
        for(unsigned long i = 0; i < num_rows;++i)
        {
            row = mysql_fetch_row(res);
            file_name_arr[i] = _file_name + i * 128;
            strcpy(file_name_arr[i],row[0]);
        }
        mysql_free_result(res);
        for(unsigned long i = 0; i < num_rows; ++i)
        {
            memset(buf,0,sizeof(buf));
            sprintf(buf,"%s/%s",path,file_name);
            ret = send_file_to_client(client_fd,file_id,file_name_arr[i],buf,connect);
            if(ret == -1)
                break;
        }
        free(file_name_arr);
        free(_file_name);
        return ret;
    }
    sprintf(buf,"%s/%s",FILE_STORE_PATH,row[1]);
    int file_fd = open(buf,O_RDWR);
    printf("file_fd = %d\n",file_fd);
    if(file_fd == -1)
    {
        puts(buf);
        mysql_free_result(res);
        return -1;
    }
    ret = send_file(client_fd,file_fd);
    mysql_free_result(res);
    close(file_fd);
    return ret;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1038, %rsp           # imm = 0x1038
movq %r8, %r14
movq %rcx, -0x50(%rbp)
movq %rdx, %r12
movl %esi, %ebx
movl %edi, -0x2c(%rbp)
leaq -0x1060(%rbp), %r13
xorl %r15d, %r15d
movl $0x1000, %edx           # imm = 0x1000
movq %r13, %rdi
xorl %esi, %esi
callq 0x2130
leaq 0xc6a(%rip), %rsi       # 0x55a1
movq %r13, %rdi
movl %ebx, %edx
movq %r12, %rcx
xorl %eax, %eax
callq 0x2060
movq %r13, %rdi
movq %r14, -0x48(%rbp)
movq %r14, %rsi
callq 0x4468
movq %rax, %rbx
movq %rax, %rdi
callq 0x2200
testq %rax, %rax
je 0x4ba2
movq %rax, %r14
movl $0x1000, %edx           # imm = 0x1000
movq %r13, %rdi
xorl %esi, %esi
callq 0x2130
leaq -0x105c(%rbp), %r15
leaq 0x9b1(%rip), %rsi       # 0x533a
movq %r15, %rdi
movq -0x50(%rbp), %rdx
movq %r12, %rcx
xorl %eax, %eax
callq 0x2060
movq %r15, %rdi
callq 0x20c0
movl %eax, -0x4(%r15)
movq 0x10(%r14), %rcx
movb (%rcx), %cl
movslq %eax, %rdx
movb %cl, 0x4(%rdx,%r13)
addq $0x5, %rdx
movl -0x2c(%rbp), %edi
movq %r13, %rsi
movl $0x4000, %ecx           # imm = 0x4000
callq 0x2100
movq %rax, %r13
pushq $-0x1
popq %r15
cmpl $-0x1, %r13d
je 0x4ba2
movq 0x10(%r14), %rax
cmpb $0x64, (%rax)
jne 0x4a99
movq (%r14), %rdi
callq 0x2300
movl %eax, %r14d
movq %rbx, %rdi
callq 0x21f0
leaq 0xbef(%rip), %rsi       # 0x55ef
xorl %r15d, %r15d
leaq -0x1060(%rbp), %rbx
movq %rbx, %rdi
movl %r14d, -0x3c(%rbp)
movl %r14d, %edx
xorl %eax, %eax
callq 0x2060
movq %rbx, %rdi
movq -0x48(%rbp), %rsi
callq 0x44a4
movq %rax, %r14
movq %rax, %rdi
callq 0x21a0
testq %rax, %rax
je 0x4ba2
movq %rax, %rbx
movq %r12, -0x60(%rbp)
leaq (,%rax,8), %rdi
callq 0x2250
movq %rax, -0x38(%rbp)
movq %rbx, %rsi
shlq $0x7, %rsi
pushq $0x1
popq %rdi
callq 0x2150
movq %rax, %r12
xorl %r15d, %r15d
movq %rax, -0x58(%rbp)
cmpq %r15, %rbx
je 0x4b09
movq %r14, %rdi
callq 0x2200
movq -0x38(%rbp), %rcx
movq %r12, (%rcx,%r15,8)
movq (%rax), %rsi
movq %r12, %rdi
callq 0x21c0
incq %r15
subq $-0x80, %r12
jmp 0x4a6c
movq 0x8(%r14), %rcx
leaq 0x896(%rip), %rsi       # 0x533a
leaq 0x895(%rip), %rdx       # 0x5340
leaq -0x1060(%rbp), %r14
movq %r14, %rdi
xorl %eax, %eax
callq 0x2060
pushq $0x2
popq %rsi
movq %r14, %rdi
xorl %eax, %eax
callq 0x2160
movl %eax, %r14d
leaq 0xb4b(%rip), %rdi       # 0x561e
movl %eax, %esi
xorl %eax, %eax
callq 0x2040
cmpl $-0x1, %r14d
je 0x4b8e
movl -0x2c(%rbp), %edi
movl %r14d, %esi
callq 0x4724
movl %eax, %r15d
movq %rbx, %rdi
callq 0x21f0
movl %r14d, %edi
callq 0x2380
jmp 0x4ba2
movq %r14, %rdi
callq 0x21f0
leaq -0x1060(%rbp), %r14
xorl %r12d, %r12d
movl -0x2c(%rbp), %r15d
cmpq %r12, %rbx
je 0x4b77
movl $0x1000, %edx           # imm = 0x1000
movq %r14, %rdi
xorl %esi, %esi
callq 0x2130
movq %r14, %rdi
leaq 0x7fd(%rip), %rsi       # 0x533a
movq -0x50(%rbp), %rdx
movq -0x60(%rbp), %rcx
xorl %eax, %eax
callq 0x2060
movq -0x38(%rbp), %rax
movq (%rax,%r12,8), %rdx
movl %r15d, %edi
movl -0x3c(%rbp), %esi
movq %r14, %rcx
movq -0x48(%rbp), %r8
callq 0x48f4
movl %eax, %r13d
incq %r12
cmpl $-0x1, %eax
jne 0x4b1f
pushq $-0x1
popq %rax
movl %eax, %r13d
movq -0x38(%rbp), %rdi
callq 0x2220
movq -0x58(%rbp), %rdi
callq 0x2220
movl %r13d, %r15d
jmp 0x4ba2
leaq -0x1060(%rbp), %rdi
callq 0x22f0
movq %rbx, %rdi
callq 0x21f0
movl %r15d, %eax
addq $0x1038, %rsp           # imm = 0x1038
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop