// File: /workspace/github_cpp_makefile_star2_O1/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, -0x38(%rbp)
movl %edi, -0x3c(%rbp)
xorpd %xmm0, %xmm0
leaq -0xc0(%rbp), %rax
movapd %xmm0, 0x30(%rax)
movapd %xmm0, 0x20(%rax)
movapd %xmm0, 0x10(%rax)
movapd %xmm0, (%rax)
movq 0x3512(%rip), %r12      # 0x5fd0
movl $0x10, -0x40(%rbp)
xorpd %xmm0, %xmm0
movapd %xmm0, -0xc0(%rbp)
movapd %xmm0, -0xb0(%rbp)
movapd %xmm0, -0xa0(%rbp)
movapd %xmm0, -0x90(%rbp)
movl $0x40, %edx
movl -0x3c(%rbp), %edi
leaq -0xc0(%rbp), %rsi
xorl %ecx, %ecx
leaq -0xf0(%rbp), %r8
leaq -0x40(%rbp), %r9
callq 0x2190
cmpl $-0x1, %eax
je 0x2abe
testl %eax, %eax
jle 0x3101
leaq -0x60(%rbp), %rax
movq %rax, -0x70(%rbp)
leaq -0xc0(%rbp), %rbx
movq %rbx, %rdi
callq 0x2100
leaq (%rax,%rbp), %rdx
addq $-0xc0, %rdx
leaq -0x70(%rbp), %r14
movq %r14, %rdi
movq %rbx, %rsi
callq 0x3726
movq %r14, %rdi
leaq 0x1628(%rip), %rsi      # 0x417a
callq 0x20d0
testl %eax, %eax
je 0x2bd9
movq %rbx, %rdi
callq 0x2290
movl %eax, %r13d
movl $0x36, %edx
movq %r12, %rdi
leaq 0x166c(%rip), %rsi      # 0x41e1
callq 0x2230
movq %r12, %rdi
movl %r13d, %esi
callq 0x22e0
movq %rax, %r15
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x21d0
movsbl %al, %esi
movq %r15, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x2140
movq -0x38(%rbp), %rax
cmpl 0x8(%rax), %r13d
jne 0x2c6d
xorpd %xmm0, %xmm0
movapd %xmm0, -0x50(%rbp)
xorl %eax, %eax
movsd 0x1438(%rip), %xmm1     # 0x4008
movapd %xmm1, %xmm2
jmp 0x2f49
movq -0x38(%rbp), %rax
movl (%rax), %ebx
movl 0x4(%rax), %r14d
incl %r14d
movl %r14d, 0x4(%rax)
movl $0x3f, %edx
movq %r12, %rdi
leaq 0x158a(%rip), %rsi      # 0x4183
callq 0x2230
movq -0x38(%rbp), %rax
movl 0x4(%rax), %esi
movq %r12, %rdi
callq 0x22e0
movq %rax, %r13
movl $0x1d, %edx
movq %rax, %rdi
leaq 0x15a4(%rip), %rsi      # 0x41c3
callq 0x2230
cvtsi2sd %ebx, %xmm1
subl %r14d, %ebx
xorps %xmm0, %xmm0
cvtsi2sd %ebx, %xmm0
divsd %xmm1, %xmm0
movq %r13, %rdi
callq 0x22c0
movq %rax, %r13
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r13, %rdi
movl $0xa, %esi
callq 0x21d0
movsbl %al, %esi
movq %r13, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x2140
jmp 0x30e8
movq 0x355c(%rip), %rcx      # 0x61d0
leaq 0x354d(%rip), %rax      # 0x61c8
testq %rcx, %rcx
je 0x2c9e
leaq 0x3541(%rip), %rax      # 0x61c8
xorl %edx, %edx
cmpl %r13d, 0x20(%rcx)
setl %dl
cmovgeq %rcx, %rax
movq 0x10(%rcx,%rdx,8), %rcx
testq %rcx, %rcx
jne 0x2c87
leaq 0x3523(%rip), %rdx      # 0x61c8
movq %rdx, %rcx
cmpq %rdx, %rax
je 0x2cb8
cmpl 0x20(%rax), %r13d
cmovlq %rdx, %rax
movq %rax, %rcx
cmpq %rdx, %rcx
je 0x2e71
movq -0x38(%rbp), %rdx
movq 0x10(%rdx), %rax
movslq 0x24(%rcx), %rcx
shlq $0x4, %rcx
movsd 0x8(%rax,%rcx), %xmm0
movsd %xmm0, -0x30(%rbp)
movl (%rdx), %r14d
movl %r14d, %ebx
subl 0x4(%rdx), %ebx
movl $0x1a, %edx
movq %r12, %rdi
leaq 0x1524(%rip), %rsi      # 0x4218
callq 0x2230
movq -0x38(%rbp), %rax
movl (%rax), %esi
movq %r12, %rdi
callq 0x22e0
movq %rax, %r15
movl $0xd, %edx
movq %rax, %rdi
leaq 0x151a(%rip), %rsi      # 0x4233
callq 0x2230
movq -0x38(%rbp), %rax
movl 0x4(%rax), %esi
movq %r15, %rdi
callq 0x22e0
movq %rax, %r13
movl $0x12, %edx
movq %rax, %rdi
leaq 0x1502(%rip), %rsi      # 0x4241
callq 0x2230
xorps %xmm0, %xmm0
cvtsi2sd %ebx, %xmm0
cvtsi2sd %r14d, %xmm1
divsd %xmm1, %xmm0
movapd %xmm0, %xmm2
cmpltsd 0x12bf(%rip), %xmm2     # 0x4020
movapd %xmm2, %xmm1
andnpd %xmm0, %xmm1
movsd 0x1297(%rip), %xmm0     # 0x4008
andpd %xmm0, %xmm2
orpd %xmm1, %xmm2
movq %r13, %rdi
movapd %xmm2, -0x80(%rbp)
movapd %xmm2, %xmm0
callq 0x22c0
movq %rax, %r15
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x21d0
movsbl %al, %esi
movq %r15, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x2140
movl $0x3c, %edx
movq %r12, %rdi
leaq 0x1491(%rip), %rsi      # 0x4254
callq 0x2230
movq %r12, %rdi
movsd -0x30(%rbp), %xmm0
callq 0x22c0
movq %rax, %r15
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x21d0
movsbl %al, %esi
movq %r15, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x2140
movl $0x1b, %edx
movq %r12, %rdi
leaq 0x1483(%rip), %rsi      # 0x4291
callq 0x2230
movsd 0x11f5(%rip), %xmm0     # 0x4010
subsd -0x80(%rbp), %xmm0
mulsd -0x30(%rbp), %xmm0
movsd 0x11eb(%rip), %xmm1     # 0x4018
divsd %xmm0, %xmm1
movq %r12, %rdi
movapd %xmm1, -0x50(%rbp)
movapd %xmm1, %xmm0
callq 0x22c0
movq %rax, %r15
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x21d0
movsbl %al, %esi
movq %r15, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x2140
jmp 0x2f2a
movl $0x26, %edx
movq %r12, %rdi
leaq 0x142d(%rip), %rsi      # 0x42ad
callq 0x2230
movq %r12, %rdi
movl %r13d, %esi
callq 0x22e0
movq %rax, %r13
movl $0x7, %edx
movq %rax, %rdi
leaq 0x1432(%rip), %rsi      # 0x42d4
callq 0x2230
movq (%r13), %rax
movq -0x18(%rax), %rdi
addq %r13, %rdi
movl $0xa, %esi
callq 0x21d0
movsbl %al, %esi
movq %r13, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x2140
movl $0x3a, %edx
movq %r12, %rdi
leaq 0x13fe(%rip), %rsi      # 0x42dc
callq 0x2230
movq (%r12), %rax
movq -0x18(%rax), %rdi
addq %r12, %rdi
movl $0xa, %esi
callq 0x21d0
movsbl %al, %esi
movq %r12, %rdi
callq 0x2040
xorpd %xmm0, %xmm0
movapd %xmm0, -0x80(%rbp)
movq %rax, %rdi
callq 0x2140
movsd 0x10ec(%rip), %xmm0     # 0x4008
movsd %xmm0, -0x30(%rbp)
xorpd %xmm0, %xmm0
movapd %xmm0, -0x50(%rbp)
movsd 0x10d6(%rip), %xmm1     # 0x4008
movapd -0x80(%rbp), %xmm0
cmpeqsd %xmm1, %xmm0
movq %xmm0, %rax
andl $0x1, %eax
movsd -0x30(%rbp), %xmm2
xorpd %xmm0, %xmm0
movapd %xmm0, -0x90(%rbp)
movapd %xmm0, -0xa0(%rbp)
movapd %xmm0, -0xb0(%rbp)
movapd %xmm0, -0xc0(%rbp)
movsd %xmm2, -0x30(%rbp)
ucomisd %xmm1, %xmm2
jne 0x2f7e
jnp 0x30b4
testb %al, %al
jne 0x30b4
leaq -0x278(%rbp), %rdi
callq 0x2160
leaq -0x268(%rbp), %rdi
movapd -0x50(%rbp), %xmm0
callq 0x22c0
movq %rax, %r15
movl $0x1, %edx
movq %rax, %rdi
leaq 0x12da(%rip), %rsi      # 0x428f
callq 0x2230
movq %r15, %rdi
movsd -0x30(%rbp), %xmm0
callq 0x22c0
leaq -0xe0(%rbp), %rdi
leaq -0x260(%rbp), %rsi
callq 0x22b0
movq -0xe0(%rbp), %rsi
leaq -0xc0(%rbp), %rdi
callq 0x21c0
movl $0x1c, %edx
movq %r12, %rdi
leaq 0x1321(%rip), %rsi      # 0x431d
callq 0x2230
movq %r12, %rdi
movapd -0x50(%rbp), %xmm0
callq 0x22c0
movq %rax, %r15
movl $0xf, %edx
movq %rax, %rdi
leaq 0x131a(%rip), %rsi      # 0x433a
callq 0x2230
movq %r15, %rdi
movsd -0x30(%rbp), %xmm0
callq 0x22c0
movq %rax, %r13
movl $0x11, %edx
movq %rax, %rdi
leaq 0x12c1(%rip), %rsi      # 0x4305
callq 0x2230
movq (%r13), %rax
movq -0x18(%rax), %rdi
addq %r13, %rdi
movl $0xa, %esi
callq 0x21d0
movsbl %al, %esi
movq %r13, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x2140
movq -0xe0(%rbp), %rdi
leaq -0xd0(%rbp), %rax
cmpq %rax, %rdi
je 0x3093
movq -0xd0(%rbp), %rsi
incq %rsi
callq 0x2200
leaq -0x278(%rbp), %rdi
movq 0x2f17(%rip), %rsi      # 0x5fb8
callq 0x2180
leaq -0x1f8(%rbp), %rdi
callq 0x20e0
jmp 0x30c7
movw $0x31, -0xbc(%rbp)
movl $0x2d20312d, -0xc0(%rbp) # imm = 0x2D20312D
movl -0x40(%rbp), %r9d
movl $0x40, %edx
movl -0x3c(%rbp), %edi
leaq -0xc0(%rbp), %rsi
xorl %ecx, %ecx
leaq -0xf0(%rbp), %r8
callq 0x2050
movq -0x70(%rbp), %rdi
leaq -0x60(%rbp), %rax
cmpq %rax, %rdi
je 0x3101
movq -0x60(%rbp), %rsi
incq %rsi
callq 0x2200
movl $0x60, %edx
movq %r12, %rdi
leaq 0x1009(%rip), %rsi      # 0x4119
callq 0x2230
movq (%r12), %rax
movq -0x18(%rax), %rdi
addq %r12, %rdi
movl $0xa, %esi
callq 0x21d0
movsbl %al, %esi
movq %r12, %rdi
callq 0x2040
movq %rax, %rdi
callq 0x2140
jmp 0x2abe
jmp 0x3148
jmp 0x3197
jmp 0x3197
movq %rax, %rbx
jmp 0x3172
movq %rax, %rbx
movq -0xe0(%rbp), %rdi
leaq -0xd0(%rbp), %rax
cmpq %rax, %rdi
je 0x3172
movq -0xd0(%rbp), %rsi
incq %rsi
callq 0x2200
movq 0x2e3f(%rip), %rsi      # 0x5fb8
leaq -0x278(%rbp), %rdi
callq 0x2180
leaq -0x1f8(%rbp), %rdi
callq 0x20e0
jmp 0x319a
jmp 0x3197
jmp 0x3197
movq %rax, %rbx
movq -0x70(%rbp), %rdi
leaq -0x60(%rbp), %rax
cmpq %rax, %rdi
je 0x31b3
movq -0x60(%rbp), %rsi
incq %rsi
callq 0x2200
movq %rbx, %rdi
callq 0x22f0
nop