// File: /workspace/github_cpp_makefile_star2_O2/gty980218[P]ee450/hospitalC.cpp
// Function: serve(int, Hospital&)
void serve(int sock,Hospital& hospital){
    char buff[64]={0};
    struct sockaddr_in cliaddr;                                                 //client address
    socklen_t clilen;
    int n;
    while(1){
        clilen=sizeof(cliaddr);
        memset(buff,0,sizeof(buff));
        n=recvfrom(sock,buff,sizeof(buff),0,(struct sockaddr*)&cliaddr,&clilen);
        if(n==-1){
            continue;
        }else if(n>0){
            string rev_str=buff;
            if(rev_str.compare("assigned")==0){                                 //assigned response
                hospital.occupancy+=1;
                double avil=1.0*(hospital.capacity-hospital.occupancy)/hospital.capacity;
                cout<<"Hospital C has been assigned to a client, occupancy updated to "<<hospital.occupancy<<", availability is updated to "<<avil<<endl;
            }else{                                                              //availability response
                int loc=atoi(buff);
                cout<<"Hospital C has received input from client at location "<<loc<<endl;
                double dist=-1;
                double avil=0;
                double score=0;
                if(loc!=hospital.location){
                    map<int, int>::iterator iter;
                    iter=indice_map.find(loc);
                    if(iter!=indice_map.end()){                                     //loc exits
                        dist=hospital.dist_list[iter->second].val;
                        avil=1.0*(hospital.capacity-hospital.occupancy)/hospital.capacity;
                        if(avil<0){
                            avil=-1;
                        }
                        score=1/(dist*(1.1-avil)); 
                        cout<<"Hospital C has capacity = "<<hospital.capacity<<" ,occupancy= "<<hospital.occupancy<<" , availability = "<<avil<<endl;
                        cout<<"Hospital C has found the shortest path to client,distance = "<<dist<<endl;
                        cout<<"Hospital C has the score = "<<score<<endl;
                    }else{
                        cout<<"Hospital C does not have the location "<<loc<<" in map"<<endl;
                        cout<<"Hospital C has sent \" location not found\" to the Scheduler"<<endl;
                    }                   
                }
                memset(buff,0,sizeof(buff));
                if(dist==-1 || avil==-1){
                    strcpy(buff,"-1 -1");
                }else{
                    stringstream ss;
                    ss<<score<<" "<<dist;
                    string str=ss.str();
                    strcpy(buff,str.c_str());
                    cout<<"Hospital C has sent score = "<<score<<" and distance= "<<dist<<" to the Scheduler"<<endl;
                }
                sendto(sock, buff, sizeof(buff), 0, (struct sockaddr*)&cliaddr, clilen);
            }    
        }
        cout<<"------------------------------------------------------------------------------------------------"<<endl;
    }
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x258, %rsp            # imm = 0x258
movq %rsi, %rbx
movl %edi, %r14d
leaq -0xa0(%rbp), %r13
movl $0x10, -0x3c(%rbp)
xorpd %xmm0, %xmm0
movapd %xmm0, -0xa0(%rbp)
movapd %xmm0, -0x90(%rbp)
movapd %xmm0, -0x80(%rbp)
movapd %xmm0, -0x70(%rbp)
movl %r14d, %edi
movq %r13, %rsi
pushq $0x40
popq %rdx
xorl %ecx, %ecx
leaq -0xd0(%rbp), %r8
leaq -0x3c(%rbp), %r9
callq 0x2190
cmpl $-0x1, %eax
je 0x2903
testl %eax, %eax
jle 0x2d40
leaq -0xf0(%rbp), %r15
movq %r15, %rdi
movq %r13, %rsi
leaq -0x278(%rbp), %rdx
callq 0x3136
movq %r15, %rdi
leaq 0x1807(%rip), %rsi      # 0x417a
callq 0x20d0
testl %eax, %eax
je 0x29d1
movq %r13, %rdi
callq 0x2290
movl %eax, -0x2c(%rbp)
movq 0x3642(%rip), %rdi      # 0x5fd0
leaq 0x184c(%rip), %rsi      # 0x41e1
callq 0x21e0
movl -0x2c(%rbp), %esi
movq %rax, %rdi
callq 0x22e0
movq %rax, %rdi
callq 0x2120
movl -0x2c(%rbp), %eax
cmpl 0x8(%rbx), %eax
jne 0x2a32
xorpd %xmm0, %xmm0
movapd %xmm0, -0x50(%rbp)
xorl %eax, %eax
movsd 0x1640(%rip), %xmm1     # 0x4008
movapd %xmm1, %xmm2
jmp 0x2c07
movl (%rbx), %r15d
movl 0x4(%rbx), %r12d
incl %r12d
movl %r12d, 0x4(%rbx)
movq 0x35ea(%rip), %rdi      # 0x5fd0
leaq 0x1796(%rip), %rsi      # 0x4183
callq 0x21e0
movl 0x4(%rbx), %esi
movq %rax, %rdi
callq 0x22e0
movq %rax, %rdi
leaq 0x17bc(%rip), %rsi      # 0x41c3
callq 0x21e0
cvtsi2sd %r15d, %xmm1
subl %r12d, %r15d
cvtsi2sd %r15d, %xmm0
divsd %xmm1, %xmm0
movq %rax, %rdi
callq 0x22c0
movq %rax, %rdi
callq 0x2120
jmp 0x2d34
leaq 0x377f(%rip), %rdi      # 0x61b8
leaq -0x2c(%rbp), %rsi
callq 0x32cc
leaq 0x3777(%rip), %rcx      # 0x61c0
cmpq %rcx, %rax
je 0x2b79
movq 0x10(%rbx), %rcx
movslq 0x24(%rax), %rax
shlq $0x4, %rax
movsd 0x8(%rcx,%rax), %xmm0
movsd %xmm0, -0x38(%rbp)
movl (%rbx), %r15d
movl %r15d, %r12d
subl 0x4(%rbx), %r12d
movq 0x3556(%rip), %rdi      # 0x5fd0
leaq 0x1797(%rip), %rsi      # 0x4218
callq 0x21e0
movl (%rbx), %esi
movq %rax, %rdi
callq 0x22e0
movq %rax, %rdi
leaq 0x1799(%rip), %rsi      # 0x4233
callq 0x21e0
movl 0x4(%rbx), %esi
movq %rax, %rdi
callq 0x22e0
movq %rax, %rdi
leaq 0x178d(%rip), %rsi      # 0x4241
callq 0x21e0
cvtsi2sd %r12d, %xmm0
cvtsi2sd %r15d, %xmm1
divsd %xmm1, %xmm0
movapd %xmm0, %xmm2
cmpltsd 0x154c(%rip), %xmm2     # 0x4020
movapd %xmm2, %xmm1
andnpd %xmm0, %xmm1
movsd 0x1524(%rip), %xmm0     # 0x4008
andpd %xmm0, %xmm2
orpd %xmm1, %xmm2
movq %rax, %rdi
movapd %xmm2, -0x60(%rbp)
movapd %xmm2, %xmm0
callq 0x22c0
movq %rax, %rdi
callq 0x2120
movq 0x34c4(%rip), %rdi      # 0x5fd0
leaq 0x1741(%rip), %rsi      # 0x4254
callq 0x21e0
movq %rax, %rdi
movsd -0x38(%rbp), %xmm0
callq 0x22c0
movq %rax, %rdi
callq 0x2120
movq 0x349c(%rip), %rdi      # 0x5fd0
leaq 0x1756(%rip), %rsi      # 0x4291
callq 0x21e0
movsd 0x14c8(%rip), %xmm0     # 0x4010
subsd -0x60(%rbp), %xmm0
mulsd -0x38(%rbp), %xmm0
movsd 0x14be(%rip), %xmm1     # 0x4018
divsd %xmm0, %xmm1
movq %rax, %rdi
movapd %xmm1, -0x50(%rbp)
movapd %xmm1, %xmm0
callq 0x22c0
movq %rax, %rdi
callq 0x2120
jmp 0x2be8
movq 0x3450(%rip), %rdi      # 0x5fd0
leaq 0x1726(%rip), %rsi      # 0x42ad
callq 0x21e0
movl -0x2c(%rbp), %esi
movq %rax, %rdi
callq 0x22e0
movq %rax, %rdi
leaq 0x1733(%rip), %rsi      # 0x42d4
callq 0x21e0
movq %rax, %rdi
callq 0x2120
movq 0x341b(%rip), %rdi      # 0x5fd0
leaq 0x1720(%rip), %rsi      # 0x42dc
callq 0x21e0
xorpd %xmm0, %xmm0
movapd %xmm0, -0x60(%rbp)
movq %rax, %rdi
callq 0x2120
movsd 0x142e(%rip), %xmm0     # 0x4008
movsd %xmm0, -0x38(%rbp)
xorpd %xmm0, %xmm0
movapd %xmm0, -0x50(%rbp)
movsd 0x1418(%rip), %xmm1     # 0x4008
movapd -0x60(%rbp), %xmm0
cmpeqsd %xmm1, %xmm0
movq %xmm0, %rax
andl $0x1, %eax
movsd -0x38(%rbp), %xmm2
xorpd %xmm0, %xmm0
movapd %xmm0, -0x70(%rbp)
movapd %xmm0, -0x80(%rbp)
movapd %xmm0, -0x90(%rbp)
movapd %xmm0, -0xa0(%rbp)
movsd %xmm2, -0x38(%rbp)
ucomisd %xmm1, %xmm2
jne 0x2c36
jnp 0x2d06
testb %al, %al
jne 0x2d06
leaq -0x278(%rbp), %rdi
callq 0x2160
leaq -0x268(%rbp), %rdi
movapd -0x50(%rbp), %xmm0
callq 0x22c0
movq %rax, %rdi
leaq 0x162a(%rip), %rsi      # 0x428f
callq 0x21e0
movq %rax, %rdi
movsd -0x38(%rbp), %xmm0
callq 0x22c0
leaq -0xc0(%rbp), %rdi
leaq -0x260(%rbp), %rsi
callq 0x22b0
movq -0xc0(%rbp), %rsi
movq %r13, %rdi
callq 0x21d0
movq 0x3330(%rip), %rdi      # 0x5fd0
leaq 0x1676(%rip), %rsi      # 0x431d
callq 0x21e0
movq %rax, %rdi
movapd -0x50(%rbp), %xmm0
callq 0x22c0
movq %rax, %rdi
leaq 0x1677(%rip), %rsi      # 0x433a
callq 0x21e0
movq %rax, %rdi
movsd -0x38(%rbp), %xmm0
callq 0x22c0
movq %rax, %rdi
leaq 0x1626(%rip), %rsi      # 0x4305
callq 0x21e0
movq %rax, %rdi
callq 0x2120
leaq -0xc0(%rbp), %rdi
callq 0x2110
leaq -0x278(%rbp), %rdi
callq 0x2180
jmp 0x2d19
movw $0x31, -0x9c(%rbp)
movl $0x2d20312d, -0xa0(%rbp) # imm = 0x2D20312D
movl -0x3c(%rbp), %r9d
movl %r14d, %edi
movq %r13, %rsi
pushq $0x40
popq %rdx
xorl %ecx, %ecx
leaq -0xd0(%rbp), %r8
callq 0x2040
leaq -0xf0(%rbp), %rdi
callq 0x2110
movq 0x3289(%rip), %rdi      # 0x5fd0
leaq 0x13cb(%rip), %rsi      # 0x4119
callq 0x21e0
movq %rax, %rdi
callq 0x2120
jmp 0x2903
jmp 0x2d68
jmp 0x2d8e
jmp 0x2d8e
jmp 0x2d8e
movq %rax, %rbx
jmp 0x2d7c
movq %rax, %rbx
leaq -0xc0(%rbp), %rdi
callq 0x2110
leaq -0x278(%rbp), %rdi
callq 0x2180
jmp 0x2d91
jmp 0x2d8e
jmp 0x2d8e
movq %rax, %rbx
leaq -0xf0(%rbp), %rdi
callq 0x2110
movq %rbx, %rdi
callq 0x22f0
nop