// File: luisremis[P]SDFS/connections.cpp
// Function: splitRead(int, char*, unsigned long)
size_t splitRead( int connFd, char * data, size_t size ){
    
    int msg_num = size / BUFFER_MAX + 1;
    int rest = size % ((int)BUFFER_MAX);
    
    //store the should-be message sizes
    message *msgs = new message[msg_num];
    for(int i=1; i < msg_num; i++){
        msgs[i].begin = msgs[i-1].begin + BUFFER_MAX;
    }
    msgs[msg_num-1].length = rest;

    char readSuccess = 0;
    int readSize;

    for(int i=0; i < msg_num; i++){

        //read message from socket. max size BUFFER_MAX
        readSize = robustRead( connFd, data + msgs[i].begin, msgs[i].length );
        
        //cout<<"client recv size :" << readSize << endl;
        //usleep(200*1000);

        //check if reading success or not
        if(readSize == msgs[i].length)  readSuccess = 1;
        else    readSuccess = 0;

        //tell server the data transmission status (good or fail)
        robustWrite( connFd, &readSuccess, 1);
        
        //cout<<"Client: sent "<<endl;
        //usleep(200*1000);
        
        //if reading faile, read again
        if(readSuccess==0) i--;
    }
    return size;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x70, %rsp
movl %edi, -0x4(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq -0x18(%rbp), %rax
shrq $0x17, %rax
addq $0x1, %rax
movl %eax, -0x1c(%rbp)
movq -0x18(%rbp), %rax
andq $0x7fffff, %rax         # imm = 0x7FFFFF
movl %eax, -0x20(%rbp)
movslq -0x1c(%rbp), %rax
movq %rax, -0x58(%rbp)
movl $0x8, %ecx
mulq %rcx
movq %rax, %rdi
seto %cl
movq $-0x1, %rax
testb $0x1, %cl
cmovneq %rax, %rdi
callq 0x4030
movq %rax, %rcx
movq -0x58(%rbp), %rax
movq %rcx, -0x50(%rbp)
cmpq $0x0, %rax
je 0x1afd7
movq -0x50(%rbp), %rax
movq -0x58(%rbp), %rdx
shlq $0x3, %rdx
movq %rax, %rcx
addq %rdx, %rcx
movq %rcx, -0x68(%rbp)
movq %rax, -0x60(%rbp)
movq -0x60(%rbp), %rdi
movq %rdi, -0x70(%rbp)
callq 0x1b110
jmp 0x1afc2
movq -0x68(%rbp), %rcx
movq -0x70(%rbp), %rax
addq $0x8, %rax
cmpq %rcx, %rax
movq %rax, -0x60(%rbp)
jne 0x1afb3
movq -0x50(%rbp), %rax
movq %rax, -0x28(%rbp)
movl $0x1, -0x38(%rbp)
movl -0x38(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jge 0x1b034
movq -0x28(%rbp), %rax
movl -0x38(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movl (%rax,%rcx,8), %edx
addl $0x800000, %edx         # imm = 0x800000
movq -0x28(%rbp), %rax
movslq -0x38(%rbp), %rcx
movl %edx, (%rax,%rcx,8)
movl -0x38(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x38(%rbp)
jmp 0x1afe6
movq -0x50(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
callq 0x44b0
jmp 0x1b0da
movl -0x20(%rbp), %edx
movq -0x28(%rbp), %rax
movl -0x1c(%rbp), %ecx
subl $0x1, %ecx
movslq %ecx, %rcx
movl %edx, 0x4(%rax,%rcx,8)
movb $0x0, -0x39(%rbp)
movl $0x0, -0x44(%rbp)
movl -0x44(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jge 0x1b0d0
movl -0x4(%rbp), %edi
movq -0x10(%rbp), %rsi
movq -0x28(%rbp), %rax
movslq -0x44(%rbp), %rcx
movslq (%rax,%rcx,8), %rax
addq %rax, %rsi
movq -0x28(%rbp), %rax
movslq -0x44(%rbp), %rcx
movl 0x4(%rax,%rcx,8), %edx
callq 0x1ac80
movl %eax, -0x40(%rbp)
movl -0x40(%rbp), %eax
movq -0x28(%rbp), %rcx
movslq -0x44(%rbp), %rdx
cmpl 0x4(%rcx,%rdx,8), %eax
jne 0x1b09c
movb $0x1, -0x39(%rbp)
jmp 0x1b0a0
movb $0x0, -0x39(%rbp)
movl -0x4(%rbp), %edi
leaq -0x39(%rbp), %rsi
movl $0x1, %edx
callq 0x1ad10
movsbl -0x39(%rbp), %eax
cmpl $0x0, %eax
jne 0x1b0c3
movl -0x44(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0x1b0c5
movl -0x44(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x44(%rbp)
jmp 0x1b053
movq -0x18(%rbp), %rax
addq $0x70, %rsp
popq %rbp
retq
movq -0x30(%rbp), %rdi
callq 0x46a0
nopw %cs:(%rax,%rax)
nopl (%rax)