// File: /workspace/github_c_makefile_star2_O1/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 $0x1028, %rsp           # imm = 0x1028
movq %r8, %r14
movq %rcx, -0x48(%rbp)
movq %rdx, %r12
movl %esi, %ebx
movl %edi, -0x2c(%rbp)
leaq -0x1050(%rbp), %r15
xorl %r13d, %r13d
movl $0x1000, %edx           # imm = 0x1000
movq %r15, %rdi
xorl %esi, %esi
callq 0x2130
leaq 0xa02(%rip), %rsi       # 0x55a1
movq %r15, %rdi
movl %ebx, %edx
movq %r12, %rcx
xorl %eax, %eax
callq 0x2060
movq %r15, %rdi
movq %r14, -0x40(%rbp)
movq %r14, %rsi
callq 0x46a8
movq %rax, %rbx
movq %rax, %rdi
callq 0x21f0
testq %rax, %rax
je 0x4e03
movq %rax, %r14
movl $0x1000, %edx           # imm = 0x1000
movq %r15, %rdi
xorl %esi, %esi
callq 0x2130
leaq -0x104c(%rbp), %r13
leaq 0x749(%rip), %rsi       # 0x533a
movq %r13, %rdi
movq -0x48(%rbp), %rdx
movq %r12, %rcx
xorl %eax, %eax
callq 0x2060
movq %r13, %rdi
callq 0x20c0
movl %eax, -0x4(%r13)
movq 0x10(%r14), %rcx
movb (%rcx), %cl
movslq %eax, %rdx
movb %cl, 0x4(%rdx,%r15)
addq $0x5, %rdx
movl -0x2c(%rbp), %edi
movq %r15, %rsi
movl $0x4000, %ecx           # imm = 0x4000
callq 0x2100
movl $0xffffffff, %r13d      # imm = 0xFFFFFFFF
cmpl $-0x1, %eax
je 0x4e03
movq 0x10(%r14), %rax
cmpb $0x64, (%rax)
jne 0x4d6b
movq (%r14), %rdi
callq 0x22f0
movl %eax, %r14d
movq %rbx, %rdi
callq 0x21e0
leaq 0x989(%rip), %rsi       # 0x55ef
xorl %r13d, %r13d
leaq -0x1050(%rbp), %rbx
movq %rbx, %rdi
movl %r14d, -0x30(%rbp)
movl %r14d, %edx
xorl %eax, %eax
callq 0x2060
movq %rbx, %rdi
movq -0x40(%rbp), %rsi
callq 0x46e4
movq %rax, %r14
movq %rax, %rdi
callq 0x2190
testq %rax, %rax
je 0x4e03
movq %rax, %rbx
movq %r12, -0x50(%rbp)
leaq (,%rax,8), %rdi
callq 0x2240
movq %rax, %r15
movq %rbx, %r13
shlq $0x7, %r13
movq %r13, %rdi
callq 0x2240
movq %rax, -0x38(%rbp)
xorl %r12d, %r12d
movq %rax, %rdi
xorl %esi, %esi
movq %r13, %rdx
callq 0x2130
movq -0x38(%rbp), %r13
movq %r14, %rdi
callq 0x21f0
movq %r13, (%r15,%r12,8)
movq (%rax), %rsi
movq %r13, %rdi
callq 0x21b0
incq %r12
subq $-0x80, %r13
cmpq %r12, %rbx
jne 0x4cdf
movq %r14, %rdi
callq 0x21e0
leaq -0x1050(%rbp), %r14
xorl %r12d, %r12d
movl $0x1000, %edx           # imm = 0x1000
movq %r14, %rdi
xorl %esi, %esi
callq 0x2130
movq %r14, %rdi
leaq 0x60d(%rip), %rsi       # 0x533a
movq -0x48(%rbp), %rdx
movq -0x50(%rbp), %rcx
xorl %eax, %eax
callq 0x2060
movq (%r15,%r12,8), %rdx
movl -0x2c(%rbp), %edi
movl -0x30(%rbp), %esi
movq %r14, %rcx
movq -0x40(%rbp), %r8
callq 0x4b5c
cmpl $-0x1, %eax
je 0x4dec
movl %eax, %r13d
incq %r12
cmpq %r12, %rbx
jne 0x4d14
jmp 0x4df2
movq 0x8(%r14), %rcx
leaq 0x5c4(%rip), %rsi       # 0x533a
leaq 0x5c3(%rip), %rdx       # 0x5340
leaq -0x1050(%rbp), %r14
movq %r14, %rdi
xorl %eax, %eax
callq 0x2060
movq %r14, %rdi
movl $0x2, %esi
xorl %eax, %eax
callq 0x2150
movl %eax, %r14d
leaq 0x877(%rip), %rdi       # 0x561e
movl %eax, %esi
xorl %eax, %eax
callq 0x2040
cmpl $-0x1, %r14d
je 0x4dd6
movl -0x2c(%rbp), %edi
movl %r14d, %esi
callq 0x497c
movl %eax, %r13d
movq %rbx, %rdi
callq 0x21e0
movl %r14d, %edi
callq 0x2370
jmp 0x4e03
leaq -0x1050(%rbp), %rdi
callq 0x22e0
movq %rbx, %rdi
callq 0x21e0
jmp 0x4e03
movl $0xffffffff, %r13d      # imm = 0xFFFFFFFF
movq %r15, %rdi
callq 0x2210
movq -0x38(%rbp), %rdi
callq 0x2210
movl %r13d, %eax
addq $0x1028, %rsp           # imm = 0x1028
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq