// File: /workspace/github_cpp_makefile_star2_O3/bchu7796[P]p2p-file-sharing/peers.cpp
// Function: client::execute()
int client::execute(void){
    client myclient;
    int process_fd;
    int server_fd;
    struct sockaddr_in client_address, peer_address;
    /* initialize random seed: */
    srand (time(NULL));

    //initialize server
    bzero(&server_address, sizeof(server_address));
    server_address.sin_family = PF_INET; 
    server_address.sin_addr.s_addr = inet_addr(SERVER_IP); 
    server_address.sin_port = htons(SERVER_PORT); 

    //create thread for user interface
    std::thread user_thread(&client::user_interface, myclient);
    user_thread.detach();

    //create socket
    if((server_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0){
        perror("Failed to create socket"); 
        exit(0);
    }

    //initailize address structure
    bzero(&peer_address, sizeof(peer_address));
    peer_address.sin_family = PF_INET; 
    peer_address.sin_addr.s_addr = INADDR_ANY; 
    peer_address.sin_port = htons(PORT); 

    //bind socket
    if(bind(server_fd, (struct sockaddr*) &peer_address, sizeof(peer_address)) < 0){
        perror("Failed to bind"); 
        exit(0);
    }

    //listen socket
    if(listen(server_fd, 5) < 0){
        perror("Failed to listen"); 
        exit(0);
    }


    while(1){
        int client_address_length = sizeof(client_address);
        if((process_fd = accept(server_fd,(struct sockaddr*) &client_address, (socklen_t *) &client_address_length)) < 0){
            perror("Failed to accept"); 
            exit(0);
        }
        else{
            std::string file_name;
            int chunk_index;
            uint32_t request_type;
            recv(process_fd, &file_name, sizeof(file_name), 0);
            recv(process_fd, &chunk_index, sizeof(chunk_index), 0);
            std::cout<<"upload file:" << file_name <<"," <<chunk_index<<std::endl;
            //use multi-thread to handle requests
            std::thread tid(&client::handle_request, myclient, process_fd, file_name, chunk_index);
            tid.detach();
        }
    }
    return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
xorl %edi, %edi
callq 0x32b0
movl %eax, %edi
callq 0x32c0
xorps %xmm0, %xmm0
movups %xmm0, 0x58a0(%rip)     # 0xb2d8
movw $0x2, 0x5897(%rip)      # 0xb2d8
leaq 0x26b9(%rip), %rdi      # 0x8101
callq 0x32f0
movl %eax, 0x5889(%rip)      # 0xb2dc
movw $0x901f, 0x587e(%rip)   # imm = 0x901F
leaq -0x231(%rip), %rax      # 0x5832
leaq 0x18(%rsp), %rsi
movq %rax, (%rsi)
movq $0x0, 0x8(%rsi)
leaq 0x40(%rsp), %rbx
leaq 0xb(%rsp), %rdx
movq %rbx, %rdi
callq 0x60de
movq %rbx, %rdi
callq 0x3470
movl $0x2, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x3070
testl %eax, %eax
jns 0x5aab
leaq 0x2587(%rip), %rdi      # 0x8030
jmp 0x5af2
movl %eax, %ebx
leaq 0x58(%rsp), %rsi
movq $0x0, 0x8(%rsi)
movl $0x921f0002, %eax       # imm = 0x921F0002
movq %rax, (%rsi)
movl %ebx, %edi
movl $0x10, %edx
callq 0x3050
testl %eax, %eax
jns 0x5adb
leaq 0x2632(%rip), %rdi      # 0x810b
jmp 0x5af2
movl %ebx, %edi
movl $0x5, %esi
callq 0x3310
testl %eax, %eax
jns 0x5afe
leaq 0x2628(%rip), %rdi      # 0x811a
callq 0x3250
xorl %edi, %edi
callq 0x3340
leaq 0x18(%rsp), %r12
leaq 0x10(%rsp), %r13
movq 0x54c1(%rip), %rbp      # 0xafd0
leaq 0x38(%rsp), %r14
movl $0x10, 0x14(%rsp)
movl %ebx, %edi
leaq 0x68(%rsp), %rsi
leaq 0x14(%rsp), %rdx
callq 0x30d0
movl %eax, 0xc(%rsp)
testl %eax, %eax
js 0x5c4f
leaq 0x28(%rsp), %rcx
movq %rcx, 0x18(%rsp)
movq $0x0, 0x20(%rsp)
movb $0x0, 0x28(%rsp)
movl $0x20, %edx
movl %eax, %edi
movq %r12, %rsi
xorl %ecx, %ecx
callq 0x30e0
movl 0xc(%rsp), %edi
movl $0x4, %edx
movq %r13, %rsi
xorl %ecx, %ecx
callq 0x30e0
movl $0xc, %edx
movq %rbp, %rdi
leaq 0x25b8(%rip), %rsi      # 0x813c
callq 0x3330
movq 0x18(%rsp), %rsi
movq 0x20(%rsp), %rdx
movq %rbp, %rdi
callq 0x3330
movq %rax, %r15
movl $0x1, %edx
movq %rax, %rdi
leaq 0x259c(%rip), %rsi      # 0x8149
callq 0x3330
movl 0x10(%rsp), %esi
movq %r15, %rdi
callq 0x3460
movq %rax, %r15
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x32d0
movsbl %al, %esi
movq %r15, %rdi
callq 0x3030
movq %rax, %rdi
callq 0x31d0
leaq -0x28b(%rip), %rax      # 0x5964
movq %rax, 0x48(%rsp)
movq $0x0, 0x50(%rsp)
movq %r14, %rdi
leaq 0x48(%rsp), %rsi
leaq 0xb(%rsp), %rdx
leaq 0xc(%rsp), %rcx
movq %r12, %r8
movq %r13, %r9
callq 0x614c
movq %r14, %rdi
callq 0x3470
cmpq $0x0, 0x38(%rsp)
jne 0x5c9d
movq 0x18(%rsp), %rdi
leaq 0x28(%rsp), %rax
cmpq %rax, %rdi
je 0x5b14
movq 0x28(%rsp), %rsi
incq %rsi
callq 0x3300
jmp 0x5b14
leaq 0x24d5(%rip), %rdi      # 0x812b
jmp 0x5af2
cmpq $0x0, 0x38(%rsp)
je 0x5c69
jmp 0x5c9d
jmp 0x5c69
jmp 0x5c8a
movq %rax, %r14
movq 0x18(%rsp), %rdi
leaq 0x28(%rsp), %rax
cmpq %rax, %rdi
je 0x5c8d
movq 0x28(%rsp), %rsi
incq %rsi
callq 0x3300
jmp 0x5c8d
movq %rax, %r14
cmpq $0x0, 0x40(%rsp)
jne 0x5c9d
movq %r14, %rdi
callq 0x3480
callq 0x30a0