// File: luisremis[P]SDFS/connections.cpp
// Function: splitWrite(int, char*, unsigned long)
size_t splitWrite( 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 writeSuccess = 0;
    for(int i=0; i < msg_num; i++){
        //write each message, max size is BUFFER_MAX
        robustWrite( connFd, data + msgs[i].begin, msgs[i].length );
        
        //cout<<"Server: sent " << msgs[i].length << endl;
        //usleep(200*1000);
        
        //make sure client do got the data
        robustRead( connFd, &writeSuccess, 1 );
        
        //cout<<"Server: recv "<<(int)writeSuccess<<endl;
        //usleep(200*1000);

        //if package failed, send again
        if(writeSuccess==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, -0x50(%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 -0x50(%rbp), %rax
movq %rcx, -0x48(%rbp)
cmpq $0x0, %rax
je 0x1ae37
movq -0x48(%rbp), %rax
movq -0x50(%rbp), %rdx
shlq $0x3, %rdx
movq %rax, %rcx
addq %rdx, %rcx
movq %rcx, -0x60(%rbp)
movq %rax, -0x58(%rbp)
movq -0x58(%rbp), %rdi
movq %rdi, -0x68(%rbp)
callq 0x1b110
jmp 0x1ae22
movq -0x60(%rbp), %rcx
movq -0x68(%rbp), %rax
addq $0x8, %rax
cmpq %rcx, %rax
movq %rax, -0x58(%rbp)
jne 0x1ae13
movq -0x48(%rbp), %rax
movq %rax, -0x28(%rbp)
movl $0x1, -0x38(%rbp)
movl -0x38(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jge 0x1ae94
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 0x1ae46
movq -0x48(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
callq 0x44b0
jmp 0x1af1c
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, -0x40(%rbp)
movl -0x40(%rbp), %eax
cmpl -0x1c(%rbp), %eax
jge 0x1af12
movl -0x4(%rbp), %edi
movq -0x10(%rbp), %rsi
movq -0x28(%rbp), %rax
movslq -0x40(%rbp), %rcx
movslq (%rax,%rcx,8), %rax
addq %rax, %rsi
movq -0x28(%rbp), %rax
movslq -0x40(%rbp), %rcx
movl 0x4(%rax,%rcx,8), %edx
callq 0x1ad10
movl -0x4(%rbp), %edi
leaq -0x39(%rbp), %rsi
movl $0x1, %edx
callq 0x1ac80
movsbl -0x39(%rbp), %eax
cmpl $0x0, %eax
jne 0x1af05
movl -0x40(%rbp), %eax
addl $-0x1, %eax
movl %eax, -0x40(%rbp)
jmp 0x1af07
movl -0x40(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x40(%rbp)
jmp 0x1aeb3
movq -0x18(%rbp), %rax
addq $0x70, %rsp
popq %rbp
retq
movq -0x30(%rbp), %rdi
callq 0x46a0
nopw %cs:(%rax,%rax)