// File: /workspace/github_c_makefile_star2_O0/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
subq $0x1080, %rsp           # imm = 0x1080
movl %edi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
leaq -0x1030(%rbp), %rdi
xorl %esi, %esi
movl $0x1000, %edx           # imm = 0x1000
callq 0x2140
leaq -0x1030(%rbp), %rdi
movl -0xc(%rbp), %edx
movq -0x18(%rbp), %rcx
leaq 0x1443(%rip), %rsi      # 0x7598
movb $0x0, %al
callq 0x2070
leaq -0x1030(%rbp), %rdi
movq -0x28(%rbp), %rsi
callq 0x59e0
movq %rax, -0x1038(%rbp)
movq -0x1038(%rbp), %rdi
callq 0x2200
movq %rax, -0x1040(%rbp)
cmpq $0x0, -0x1040(%rbp)
jne 0x619c
movl $0x0, -0x4(%rbp)
jmp 0x6532
leaq -0x1030(%rbp), %rdi
xorl %esi, %esi
movl $0x1000, %edx           # imm = 0x1000
callq 0x2140
leaq -0x1030(%rbp), %rdi
addq $0x4, %rdi
movq -0x20(%rbp), %rdx
movq -0x18(%rbp), %rcx
leaq 0x1171(%rip), %rsi      # 0x733a
movb $0x0, %al
callq 0x2070
leaq -0x1030(%rbp), %rdi
addq $0x4, %rdi
callq 0x20d0
movl %eax, -0x1044(%rbp)
movl -0x1044(%rbp), %eax
movl %eax, -0x1030(%rbp)
movq -0x1040(%rbp), %rax
movq 0x10(%rax), %rax
movb (%rax), %cl
movslq -0x1044(%rbp), %rax
addq $0x4, %rax
movb %cl, -0x1030(%rbp,%rax)
movl -0x8(%rbp), %edi
leaq -0x1030(%rbp), %rsi
movslq -0x1044(%rbp), %rdx
addq $0x4, %rdx
addq $0x1, %rdx
movl $0x4000, %ecx           # imm = 0x4000
callq 0x2110
movl %eax, -0x1048(%rbp)
cmpl $-0x1, -0x1048(%rbp)
jne 0x624f
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x6532
movq -0x1040(%rbp), %rax
movq 0x10(%rax), %rax
movsbl (%rax), %eax
cmpl $0x64, %eax
jne 0x6480
movq -0x1040(%rbp), %rax
movq (%rax), %rdi
callq 0x2310
movl %eax, -0xc(%rbp)
movq -0x1038(%rbp), %rdi
callq 0x21f0
leaq -0x1030(%rbp), %rdi
movl -0xc(%rbp), %edx
leaq 0x1351(%rip), %rsi      # 0x75e6
movb $0x0, %al
callq 0x2070
leaq -0x1030(%rbp), %rdi
movq -0x28(%rbp), %rsi
callq 0x5a50
movq %rax, -0x1038(%rbp)
movq -0x1038(%rbp), %rdi
callq 0x21a0
movq %rax, -0x1050(%rbp)
cmpq $0x0, -0x1050(%rbp)
jne 0x62dc
movl $0x0, -0x4(%rbp)
jmp 0x6532
movq -0x1050(%rbp), %rdi
shlq $0x3, %rdi
callq 0x2250
movq %rax, -0x1058(%rbp)
movq -0x1050(%rbp), %rdi
shlq $0x7, %rdi
callq 0x2250
movq %rax, -0x1060(%rbp)
movq -0x1060(%rbp), %rdi
movq -0x1050(%rbp), %rdx
shlq $0x7, %rdx
xorl %esi, %esi
callq 0x2140
movq $0x0, -0x1068(%rbp)
movq -0x1068(%rbp), %rax
cmpq -0x1050(%rbp), %rax
jae 0x63ad
movq -0x1038(%rbp), %rdi
callq 0x2200
movq %rax, -0x1040(%rbp)
movq -0x1060(%rbp), %rdx
movq -0x1068(%rbp), %rax
shlq $0x7, %rax
addq %rax, %rdx
movq -0x1058(%rbp), %rax
movq -0x1068(%rbp), %rcx
movq %rdx, (%rax,%rcx,8)
movq -0x1058(%rbp), %rax
movq -0x1068(%rbp), %rcx
movq (%rax,%rcx,8), %rdi
movq -0x1040(%rbp), %rax
movq (%rax), %rsi
callq 0x21c0
movq -0x1068(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x1068(%rbp)
jmp 0x632e
movq -0x1038(%rbp), %rdi
callq 0x21f0
movq $0x0, -0x1070(%rbp)
movq -0x1070(%rbp), %rax
cmpq -0x1050(%rbp), %rax
jae 0x645a
leaq -0x1030(%rbp), %rdi
xorl %esi, %esi
movl $0x1000, %edx           # imm = 0x1000
callq 0x2140
leaq -0x1030(%rbp), %rdi
movq -0x20(%rbp), %rdx
movq -0x18(%rbp), %rcx
leaq 0xf39(%rip), %rsi       # 0x733a
movb $0x0, %al
callq 0x2070
movl -0x8(%rbp), %edi
movl -0xc(%rbp), %esi
movq -0x1058(%rbp), %rax
movq -0x1070(%rbp), %rcx
movq (%rax,%rcx,8), %rdx
leaq -0x1030(%rbp), %rcx
movq -0x28(%rbp), %r8
callq 0x6110
movl %eax, -0x1048(%rbp)
cmpl $-0x1, -0x1048(%rbp)
jne 0x6441
jmp 0x645a
jmp 0x6443
movq -0x1070(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x1070(%rbp)
jmp 0x63c4
movq -0x1058(%rbp), %rdi
callq 0x2220
movq -0x1060(%rbp), %rdi
callq 0x2220
movl -0x1048(%rbp), %eax
movl %eax, -0x4(%rbp)
jmp 0x6532
leaq -0x1030(%rbp), %rdi
movq -0x1040(%rbp), %rax
movq 0x8(%rax), %rcx
leaq 0xea1(%rip), %rsi       # 0x733a
leaq 0xea0(%rip), %rdx       # 0x7340
movb $0x0, %al
callq 0x2070
leaq -0x1030(%rbp), %rdi
movl $0x2, %esi
movb $0x0, %al
callq 0x2160
movl %eax, -0x1074(%rbp)
movl -0x1074(%rbp), %esi
leaq 0x1148(%rip), %rdi      # 0x7615
movb $0x0, %al
callq 0x2040
cmpl $-0x1, -0x1074(%rbp)
jne 0x64fe
leaq -0x1030(%rbp), %rdi
callq 0x2300
movq -0x1038(%rbp), %rdi
callq 0x21f0
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x6532
movl -0x8(%rbp), %edi
movl -0x1074(%rbp), %esi
callq 0x5e90
movl %eax, -0x1048(%rbp)
movq -0x1038(%rbp), %rdi
callq 0x21f0
movl -0x1074(%rbp), %edi
callq 0x2380
movl -0x1048(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x1080, %rsp           # imm = 0x1080
popq %rbp
retq
nop