// File: 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
subq $0x380, %rsp            # imm = 0x380
movl %edi, -0x4(%rbp)
movq %rsi, -0x10(%rbp)
leaq -0x50(%rbp), %rdi
xorl %esi, %esi
movl $0x40, %edx
callq 0x2130
movl $0x10, -0x64(%rbp)
leaq -0x50(%rbp), %rdi
xorl %esi, %esi
movl $0x40, %edx
callq 0x2130
movl -0x4(%rbp), %edi
leaq -0x50(%rbp), %rsi
movl $0x40, %edx
xorl %ecx, %ecx
leaq -0x60(%rbp), %r8
leaq -0x64(%rbp), %r9
callq 0x21e0
movl %eax, -0x68(%rbp)
cmpl $-0x1, -0x68(%rbp)
jne 0x2e8f
jmp 0x2e52
cmpl $0x0, -0x68(%rbp)
jle 0x35b9
leaq -0x89(%rbp), %rdi
movq %rdi, -0x290(%rbp)
callq 0x2400
movq -0x290(%rbp), %rdx
leaq -0x88(%rbp), %rdi
leaq -0x50(%rbp), %rsi
callq 0x3e80
jmp 0x2ec5
leaq -0x89(%rbp), %rdi
callq 0x22c0
leaq -0x88(%rbp), %rdi
leaq 0x3293(%rip), %rsi      # 0x6172
callq 0x20e0
cmpl $0x0, %eax
jne 0x2fdf
movq -0x10(%rbp), %rax
movl 0x4(%rax), %ecx
incl %ecx
movl %ecx, 0x4(%rax)
movq -0x10(%rbp), %rcx
movl (%rcx), %eax
movl 0x4(%rcx), %edx
movl %eax, %ecx
subl %edx, %ecx
cvtsi2sd %ecx, %xmm0
cvtsi2sd %eax, %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, -0xa8(%rbp)
movq 0x60af(%rip), %rdi      # 0x8fd0
leaq 0x3253(%rip), %rsi      # 0x617b
callq 0x2250
movq %rax, -0x298(%rbp)
jmp 0x2f36
movq -0x298(%rbp), %rdi
movq -0x10(%rbp), %rax
movl 0x4(%rax), %esi
callq 0x23e0
movq %rax, -0x2a0(%rbp)
jmp 0x2f52
movq -0x2a0(%rbp), %rdi
leaq 0x325b(%rip), %rsi      # 0x61bb
callq 0x2250
movq %rax, -0x2a8(%rbp)
jmp 0x2f6e
movq -0x2a8(%rbp), %rdi
movsd -0xa8(%rbp), %xmm0
callq 0x2420
movq %rax, -0x2b0(%rbp)
jmp 0x2f8b
movq -0x2b0(%rbp), %rdi
movq 0x6027(%rip), %rsi      # 0x8fc0
callq 0x22a0
jmp 0x2fa0
jmp 0x359d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x98(%rbp)
movl %eax, -0x9c(%rbp)
leaq -0x89(%rbp), %rdi
callq 0x22c0
jmp 0x35e2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x98(%rbp)
movl %eax, -0x9c(%rbp)
jmp 0x35ab
leaq -0x50(%rbp), %rdi
callq 0x23a0
movl %eax, -0xac(%rbp)
movq 0x5fdb(%rip), %rdi      # 0x8fd0
leaq 0x31dd(%rip), %rsi      # 0x61d9
callq 0x2250
movq %rax, -0x2b8(%rbp)
jmp 0x300a
movq -0x2b8(%rbp), %rdi
movl -0xac(%rbp), %esi
callq 0x23e0
movq %rax, -0x2c0(%rbp)
jmp 0x3025
movq -0x2c0(%rbp), %rdi
movq 0x5f8d(%rip), %rsi      # 0x8fc0
callq 0x22a0
jmp 0x303a
movsd 0x2fc6(%rip), %xmm0     # 0x6008
movsd %xmm0, -0xb8(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0xc0(%rbp)
xorps %xmm0, %xmm0
movsd %xmm0, -0xc8(%rbp)
movl -0xac(%rbp), %eax
movq -0x10(%rbp), %rcx
cmpl 0x8(%rcx), %eax
je 0x33a0
leaq -0xd0(%rbp), %rdi
callq 0x3f30
leaq 0x61d2(%rip), %rdi      # 0x9258
leaq -0xac(%rbp), %rsi
callq 0x3f50
movq %rax, -0x2c8(%rbp)
jmp 0x309b
movq -0x2c8(%rbp), %rax
movq %rax, -0xd8(%rbp)
movq -0xd8(%rbp), %rax
movq %rax, -0xd0(%rbp)
leaq 0x619a(%rip), %rdi      # 0x9258
callq 0x3fb0
movq %rax, -0xe0(%rbp)
leaq -0xd0(%rbp), %rdi
leaq -0xe0(%rbp), %rsi
callq 0x3f80
testb $0x1, %al
jne 0x30e6
jmp 0x3303
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rax
movq %rax, -0x2d0(%rbp)
leaq -0xd0(%rbp), %rdi
callq 0x3fe0
movq %rax, %rcx
movq -0x2d0(%rbp), %rax
movslq 0x4(%rcx), %rcx
shlq $0x4, %rcx
addq %rcx, %rax
movsd 0x8(%rax), %xmm0
movsd %xmm0, -0xb8(%rbp)
movq -0x10(%rbp), %rax
movl (%rax), %eax
movq -0x10(%rbp), %rcx
subl 0x4(%rcx), %eax
cvtsi2sd %eax, %xmm1
movsd 0x2ed4(%rip), %xmm0     # 0x6010
mulsd %xmm1, %xmm0
movq -0x10(%rbp), %rax
cvtsi2sdl (%rax), %xmm1
divsd %xmm1, %xmm0
movsd %xmm0, -0xc0(%rbp)
xorps %xmm0, %xmm0
ucomisd -0xc0(%rbp), %xmm0
jbe 0x3171
movsd 0x2e9f(%rip), %xmm0     # 0x6008
movsd %xmm0, -0xc0(%rbp)
movsd -0xb8(%rbp), %xmm1
movsd -0xc0(%rbp), %xmm2
movsd 0x2e8f(%rip), %xmm0     # 0x6018
subsd %xmm2, %xmm0
mulsd %xmm0, %xmm1
movsd 0x2e77(%rip), %xmm0     # 0x6010
divsd %xmm1, %xmm0
movsd %xmm0, -0xc8(%rbp)
movq 0x5e24(%rip), %rdi      # 0x8fd0
leaq 0x305d(%rip), %rsi      # 0x6210
callq 0x2250
movq %rax, -0x2d8(%rbp)
jmp 0x31c1
movq -0x2d8(%rbp), %rdi
movq -0x10(%rbp), %rax
movl (%rax), %esi
callq 0x23e0
movq %rax, -0x2e0(%rbp)
jmp 0x31dc
movq -0x2e0(%rbp), %rdi
leaq 0x3041(%rip), %rsi      # 0x622b
callq 0x2250
movq %rax, -0x2e8(%rbp)
jmp 0x31f8
movq -0x2e8(%rbp), %rdi
movq -0x10(%rbp), %rax
movl 0x4(%rax), %esi
callq 0x23e0
movq %rax, -0x2f0(%rbp)
jmp 0x3214
movq -0x2f0(%rbp), %rdi
leaq 0x3017(%rip), %rsi      # 0x6239
callq 0x2250
movq %rax, -0x2f8(%rbp)
jmp 0x3230
movq -0x2f8(%rbp), %rdi
movsd -0xc0(%rbp), %xmm0
callq 0x2420
movq %rax, -0x300(%rbp)
jmp 0x324d
movq -0x300(%rbp), %rdi
movq 0x5d65(%rip), %rsi      # 0x8fc0
callq 0x22a0
jmp 0x3262
movq 0x5d67(%rip), %rdi      # 0x8fd0
leaq 0x2fdc(%rip), %rsi      # 0x624c
callq 0x2250
movq %rax, -0x308(%rbp)
jmp 0x327e
movq -0x308(%rbp), %rdi
movsd -0xb8(%rbp), %xmm0
callq 0x2420
movq %rax, -0x310(%rbp)
jmp 0x329b
movq -0x310(%rbp), %rdi
movq 0x5d17(%rip), %rsi      # 0x8fc0
callq 0x22a0
jmp 0x32b0
movq 0x5d19(%rip), %rdi      # 0x8fd0
leaq 0x2fcb(%rip), %rsi      # 0x6289
callq 0x2250
movq %rax, -0x318(%rbp)
jmp 0x32cc
movq -0x318(%rbp), %rdi
movsd -0xc8(%rbp), %xmm0
callq 0x2420
movq %rax, -0x320(%rbp)
jmp 0x32e9
movq -0x320(%rbp), %rdi
movq 0x5cc9(%rip), %rsi      # 0x8fc0
callq 0x22a0
jmp 0x32fe
jmp 0x339e
movq 0x5cc6(%rip), %rdi      # 0x8fd0
leaq 0x2f94(%rip), %rsi      # 0x62a5
callq 0x2250
movq %rax, -0x328(%rbp)
jmp 0x331f
movq -0x328(%rbp), %rdi
movl -0xac(%rbp), %esi
callq 0x23e0
movq %rax, -0x330(%rbp)
jmp 0x333a
movq -0x330(%rbp), %rdi
leaq 0x2f84(%rip), %rsi      # 0x62cc
callq 0x2250
movq %rax, -0x338(%rbp)
jmp 0x3356
movq -0x338(%rbp), %rdi
movq 0x5c5c(%rip), %rsi      # 0x8fc0
callq 0x22a0
jmp 0x336b
movq 0x5c5e(%rip), %rdi      # 0x8fd0
leaq 0x2f5b(%rip), %rsi      # 0x62d4
callq 0x2250
movq %rax, -0x340(%rbp)
jmp 0x3387
movq -0x340(%rbp), %rdi
movq 0x5c2b(%rip), %rsi      # 0x8fc0
callq 0x22a0
jmp 0x339c
jmp 0x339e
jmp 0x33a0
leaq -0x50(%rbp), %rdi
xorl %esi, %esi
movl $0x40, %edx
callq 0x2130
movsd -0xb8(%rbp), %xmm0
movsd 0x2c48(%rip), %xmm1     # 0x6008
ucomisd %xmm1, %xmm0
jne 0x33ca
jp 0x33ca
jmp 0x33e2
movsd -0xc0(%rbp), %xmm0
movsd 0x2c2e(%rip), %xmm1     # 0x6008
ucomisd %xmm1, %xmm0
jne 0x33f7
jp 0x33f7
leaq -0x50(%rbp), %rdi
leaq 0x2f22(%rip), %rsi      # 0x630f
callq 0x2230
jmp 0x357e
leaq -0x268(%rbp), %rdi
callq 0x21a0
jmp 0x3405
leaq -0x258(%rbp), %rdi
movsd -0xc8(%rbp), %xmm0
callq 0x2420
movq %rax, -0x348(%rbp)
jmp 0x3422
movq -0x348(%rbp), %rdi
leaq 0x2e57(%rip), %rsi      # 0x6287
callq 0x2250
movq %rax, -0x350(%rbp)
jmp 0x343e
movq -0x350(%rbp), %rdi
movsd -0xb8(%rbp), %xmm0
callq 0x2420
jmp 0x3454
leaq -0x288(%rbp), %rdi
leaq -0x268(%rbp), %rsi
callq 0x2240
jmp 0x3469
leaq -0x288(%rbp), %rdi
callq 0x2110
movq %rax, %rsi
leaq -0x50(%rbp), %rdi
callq 0x2230
movq 0x5b48(%rip), %rdi      # 0x8fd0
leaq 0x2e86(%rip), %rsi      # 0x6315
callq 0x2250
movq %rax, -0x358(%rbp)
jmp 0x349d
movq -0x358(%rbp), %rdi
movsd -0xc8(%rbp), %xmm0
callq 0x2420
movq %rax, -0x360(%rbp)
jmp 0x34ba
movq -0x360(%rbp), %rdi
leaq 0x2e6a(%rip), %rsi      # 0x6332
callq 0x2250
movq %rax, -0x368(%rbp)
jmp 0x34d6
movq -0x368(%rbp), %rdi
movsd -0xb8(%rbp), %xmm0
callq 0x2420
movq %rax, -0x370(%rbp)
jmp 0x34f3
movq -0x370(%rbp), %rdi
leaq 0x2dfc(%rip), %rsi      # 0x62fd
callq 0x2250
movq %rax, -0x378(%rbp)
jmp 0x350f
movq -0x378(%rbp), %rdi
movq 0x5aa3(%rip), %rsi      # 0x8fc0
callq 0x22a0
jmp 0x3524
leaq -0x288(%rbp), %rdi
callq 0x2150
leaq -0x268(%rbp), %rdi
callq 0x21c0
jmp 0x357e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x98(%rbp)
movl %eax, -0x9c(%rbp)
jmp 0x3570
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x98(%rbp)
movl %eax, -0x9c(%rbp)
leaq -0x288(%rbp), %rdi
callq 0x2150
leaq -0x268(%rbp), %rdi
callq 0x21c0
jmp 0x35ab
movl -0x4(%rbp), %edi
movl -0x64(%rbp), %r9d
leaq -0x50(%rbp), %rsi
movl $0x40, %edx
xorl %ecx, %ecx
leaq -0x60(%rbp), %r8
callq 0x2050
jmp 0x359b
jmp 0x359d
leaq -0x88(%rbp), %rdi
callq 0x2150
jmp 0x35b9
leaq -0x88(%rbp), %rdi
callq 0x2150
jmp 0x35e2
jmp 0x35bb
movq 0x5a0e(%rip), %rdi      # 0x8fd0
leaq 0x2b48(%rip), %rsi      # 0x6111
callq 0x2250
movq %rax, %rdi
movq 0x59e8(%rip), %rsi      # 0x8fc0
callq 0x22a0
jmp 0x2e52
movq -0x98(%rbp), %rdi
callq 0x23f0
nop