// File: /workspace/github_c_makefile_star2_O3/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, %r15
movq %rcx, -0x48(%rbp)
movq %rdx, %r12
movl %esi, %r14d
movl %edi, -0x34(%rbp)
leaq -0x1060(%rbp), %rbx
xorl %r13d, %r13d
movl $0x1000, %edx           # imm = 0x1000
movq %rbx, %rdi
xorl %esi, %esi
callq 0x2130
leaq 0xa4d(%rip), %rsi       # 0x55a1
movq %rbx, %rdi
movl %r14d, %edx
movq %r12, %rcx
xorl %eax, %eax
callq 0x2060
movq %rbx, %rdi
movq %r15, -0x40(%rbp)
movq %r15, %rsi
callq 0x4664
movq %rax, %r15
movq %rax, %rdi
callq 0x2200
testq %rax, %rax
je 0x4d9e
movq %rax, %r14
movq %r15, -0x30(%rbp)
movl $0x1000, %edx           # imm = 0x1000
movq %rbx, %rdi
xorl %esi, %esi
callq 0x2130
leaq -0x105c(%rbp), %r15
leaq 0x78f(%rip), %rsi       # 0x533a
movq %r15, %rdi
movq -0x48(%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,%rbx)
addq $0x5, %rdx
movl -0x34(%rbp), %r15d
movl %r15d, %edi
movq %rbx, %rsi
movl $0x4000, %ecx           # imm = 0x4000
callq 0x2100
movl $0xffffffff, %r13d      # imm = 0xFFFFFFFF
cmpl $-0x1, %eax
je 0x4d9e
movq 0x10(%r14), %rax
cmpb $0x64, (%rax)
jne 0x4d28
movq (%r14), %rdi
callq 0x2300
movl %eax, %r14d
movq -0x30(%rbp), %rdi
callq 0x21f0
leaq 0x9cb(%rip), %rsi       # 0x55ef
xorl %r13d, %r13d
movq %rbx, %rdi
movl %r14d, -0x30(%rbp)
movl %r14d, %edx
xorl %eax, %eax
callq 0x2060
movq %rbx, %rdi
movq -0x40(%rbp), %rsi
callq 0x46a0
movq %rax, %rbx
movq %rax, %rdi
callq 0x21a0
testq %rax, %rax
je 0x4d9e
movq %rax, %r15
movq %r12, -0x58(%rbp)
leaq (,%rax,8), %rdi
callq 0x2250
movq %rax, %r14
movq %r15, %rsi
shlq $0x7, %rsi
movl $0x1, %edi
callq 0x2150
movq %rax, %r12
xorl %r13d, %r13d
movq %rax, -0x50(%rbp)
movq %rbx, %rdi
callq 0x2200
movq %r12, (%r14,%r13,8)
movq (%rax), %rsi
movq %r12, %rdi
callq 0x21c0
incq %r13
subq $-0x80, %r12
cmpq %r13, %r15
jne 0x4c8a
movq %rbx, %rdi
callq 0x21f0
decq %r15
leaq -0x1060(%rbp), %rbx
xorl %r12d, %r12d
movl $0x1000, %edx           # imm = 0x1000
movq %rbx, %rdi
xorl %esi, %esi
callq 0x2130
movq %rbx, %rdi
leaq 0x65f(%rip), %rsi       # 0x533a
movq -0x48(%rbp), %rdx
movq -0x58(%rbp), %rcx
xorl %eax, %eax
callq 0x2060
movq (%r14,%r12,8), %rdx
movl -0x34(%rbp), %edi
movl -0x30(%rbp), %esi
movq %rbx, %rcx
movq -0x40(%rbp), %r8
callq 0x4b10
movl %eax, %r13d
cmpl $-0x1, %eax
je 0x4d15
leaq 0x1(%r12), %rax
cmpq %r12, %r15
movq %rax, %r12
jne 0x4cc2
movq %r14, %rdi
callq 0x2220
movq -0x50(%rbp), %rdi
callq 0x2220
jmp 0x4d9e
movq 0x8(%r14), %rcx
leaq 0x607(%rip), %rsi       # 0x533a
leaq 0x606(%rip), %rdx       # 0x5340
movq %rbx, %rdi
xorl %eax, %eax
callq 0x2060
movq %rbx, %rdi
movl $0x2, %esi
xorl %eax, %eax
callq 0x2160
movl %eax, %ebx
leaq 0x8c2(%rip), %rdi       # 0x561e
movl %eax, %esi
xorl %eax, %eax
callq 0x2040
cmpl $-0x1, %ebx
je 0x4d89
movl %r15d, %edi
movl %ebx, %esi
callq 0x492c
movl %eax, %r13d
movq -0x30(%rbp), %rdi
callq 0x21f0
movl %ebx, %edi
callq 0x2380
jmp 0x4d9e
leaq -0x1060(%rbp), %rdi
callq 0x22f0
movq -0x30(%rbp), %rdi
callq 0x21f0
movl %r13d, %eax
addq $0x1038, %rsp           # imm = 0x1038
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop