// File: /workspace/github_cpp_makefile_star2_O1/gty980218[P]ee450/scheduler.cpp
// Function: serve(int, int)
void serve(int conn,int sock_udp){
    char buf_tcp[64]={0};
    char buf_udp[64]={0};
    memset( buf_tcp,0,sizeof( buf_tcp));
    int ret=read(conn, buf_tcp,sizeof( buf_tcp));
    if(ret==-1){
        fprintf(stderr,"Runtime read error");
        exit(1);
    }
    cout<<"The Scheduler has received client at location "<<buf_tcp<<" from the client using TCP over port 34188"<<endl;
    for(int i=0;i<3;i++){
        if(scheduler.hspt_info[i].cpblt>scheduler.hspt_info[i].ocpt){    //cability>occupancy do while
            memset( buf_udp,0,sizeof( buf_udp));
            sendto(sock_udp,  buf_tcp, strlen( buf_tcp), 0, (struct sockaddr*)&scheduler.hspt_info[i].addr, sizeof(scheduler.hspt_info[i].addr));//send location to hospital
            recvfrom(sock_udp, buf_udp, sizeof(buf_udp), 0, NULL, NULL);//get score and dist from hospital
            char* pch;
            pch=strtok(buf_udp," ");
            double score=atof(pch);
            pch=strtok(NULL," ");
            double dist=atof(pch);
            scheduler.add_info(i,score,dist); 
            if(i==0){
                cout<<"The Scheduler has sent client location to Hospital A using UDP over port 33188."<<endl;
                if(dist!=-1){
                    cout<<"The Scheduler has received map information from Hospital A, the score = "<<score<<" and the distance = "<<dist<<endl;
                }else{
                    cout<<"The Scheduler has received map information from Hospital A, the score = none and the distance = none"<<endl;
                }
            }else if(i==1){
                cout<<"The Scheduler has sent client location to Hospital B using UDP over port 33188."<<endl;
                if(dist!=-1){
                    cout<<"The Scheduler has received map information from Hospital B, the score = "<<score<<" and the distance = "<<dist<<endl;
                }else{
                    cout<<"The Scheduler has received map information from Hospital B, the score = none and the distance = none"<<endl;
                }
            }else{
                cout<<"The Scheduler has sent client location to Hospital C using UDP over port 33188."<<endl;
                if(dist!=-1){
                    cout<<"The Scheduler has received map information from Hospital C, the score = "<<score<<" and the distance = "<<dist<<endl;
                }else{
                    cout<<"The Scheduler has received map information from Hospital C, the score = none and the distance = none"<<endl;
                }
            }

        }
        
    }
    int assigned=-1;//-1:not find;-2:is hospital;
    if(scheduler.hspt_info[0].dist!=-1 && scheduler.hspt_info[1].dist!=-1 && scheduler.hspt_info[2].dist!=-1 ){     //whether client is in map and is not hospital
        if(scheduler.hspt_info[0].score!=-1 || scheduler.hspt_info[1].score!=-1 || scheduler.hspt_info[2].score!=-1){
            double max_sc=0;//find the max score and min distance
            double min_dist;
            assigned=0;
            for(int i=0;i<3;i++){
                if(scheduler.hspt_info[i].score!=-1){
                    if(max_sc<scheduler.hspt_info[i].score){
                        max_sc=scheduler.hspt_info[i].score;
                        min_dist=scheduler.hspt_info[i].dist;
                        assigned=i;// decide which hospital will be assigned
                    }else if(max_sc==scheduler.hspt_info[i].score){
                        if(min_dist<scheduler.hspt_info[i].dist){
                            min_dist=scheduler.hspt_info[i].dist;
                            assigned=i;
                        }
                    }
                }
            }
        }
    }else if(scheduler.hspt_info[0].dist!=-1 || scheduler.hspt_info[1].dist!=-1 || scheduler.hspt_info[2].dist!=-1){//is hospital
        assigned=-2;
    }
    if(assigned>=0){
        scheduler.hspt_info[assigned].ocpt+=1;
        ofstream out_file("info.txt");              //write the changed occupancy to info.txt
        for(int i=0;i<3;i++){
            out_file<<scheduler.hspt_info[i].ocpt<<endl;
        }
        out_file.close();

        memset( buf_udp,0,sizeof( buf_udp));
        memset( buf_tcp,0,sizeof( buf_tcp));
        strcpy(buf_udp,"assigned");
        sendto(sock_udp,  buf_udp,sizeof(buf_udp), 0, (struct sockaddr*)&scheduler.hspt_info[assigned].addr, sizeof(scheduler.hspt_info[assigned].addr));
        string str_asn;
        if(assigned==0){
            str_asn="A";
            cout<<"The Scheduler has assigned Hospital A to the client"<<endl;
            cout<<"The Scheduler has sent the result to Hospital A using UDP over port 33188"<<endl;

        }else if(assigned==1){
            str_asn="B";
            cout<<"The Scheduler has assigned Hospital B to the client"<<endl;
            cout<<"The Scheduler has sent the result to Hospital B using UDP over port 33188"<<endl;

        }else{
            str_asn="C";
            cout<<"The Scheduler has assigned Hospital C to the client"<<endl;
            cout<<"The Scheduler has sent the result to Hospital C using UDP over port 33188"<<endl;

        }
        strcpy(buf_tcp,str_asn.c_str());
        write(conn,buf_tcp,sizeof(buf_tcp));
    }else if(assigned==-1){// not find
        string str_err="NF";
        if(scheduler.hspt_info[0].cpblt<=scheduler.hspt_info[0].ocpt && scheduler.hspt_info[1].cpblt<=scheduler.hspt_info[1].ocpt && scheduler.hspt_info[2].cpblt<=scheduler.hspt_info[2].ocpt){
            str_err="OL";  //overload
        }
        strcpy(buf_tcp,str_err.c_str());
        write(conn,buf_tcp,sizeof(buf_tcp));
    }else{
        string str_err="N";// client is in hospital illegal input
        strcpy(buf_tcp,str_err.c_str());
        write(conn,buf_tcp,sizeof(buf_tcp));
    }
    cout<<"The Scheduler has sent the result to client using TCP over port 34188 "<<endl;
    cout<<"------------------------------------------------------------------------------------------------"<<endl;
    
    
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x2b8, %rsp            # imm = 0x2B8
movl %esi, %r14d
xorpd %xmm0, %xmm0
leaq -0xe0(%rbp), %rsi
movapd %xmm0, 0x30(%rsi)
movapd %xmm0, 0x20(%rsi)
movapd %xmm0, 0x10(%rsi)
movapd %xmm0, (%rsi)
movapd %xmm0, -0x70(%rbp)
movapd %xmm0, -0x80(%rbp)
movapd %xmm0, -0x90(%rbp)
movapd %xmm0, -0xa0(%rbp)
movl $0x40, %edx
movl %edi, -0x34(%rbp)
callq 0x2210
cmpl $-0x1, %eax
je 0x3472
movq 0x3421(%rip), %rbx      # 0x5fd0
leaq 0x1596(%rip), %rsi      # 0x414c
movl $0x2e, %edx
movq %rbx, %rdi
callq 0x21c0
leaq -0xe0(%rbp), %r15
movq %r15, %rdi
callq 0x20d0
movq %rbx, %rdi
movq %r15, %rsi
movq %rax, %rdx
callq 0x21c0
leaq 0x1594(%rip), %rsi      # 0x417b
movl $0x2a, %edx
movq %rbx, %rdi
callq 0x21c0
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x2170
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2100
leaq 0x3576(%rip), %r13      # 0x6198
xorl %r12d, %r12d
movl 0x10(%r13), %eax
cmpl 0x14(%r13), %eax
jle 0x2ea5
xorpd %xmm0, %xmm0
movapd %xmm0, -0x70(%rbp)
movapd %xmm0, -0x80(%rbp)
movapd %xmm0, -0x90(%rbp)
movapd %xmm0, -0xa0(%rbp)
leaq -0xe0(%rbp), %r15
movq %r15, %rdi
callq 0x20d0
movl %r14d, %edi
movq %r15, %rsi
movq %rax, %rdx
xorl %ecx, %ecx
movq %r13, %r8
movl $0x10, %r9d
callq 0x2040
movl $0x40, %edx
movl %r14d, %edi
leaq -0xa0(%rbp), %r15
movq %r15, %rsi
xorl %ecx, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x2120
movq %r15, %rdi
leaq 0x19e9(%rip), %r15      # 0x468b
movq %r15, %rsi
callq 0x2260
movq %rax, %rdi
callq 0x21e0
movsd %xmm0, -0x40(%rbp)
xorl %edi, %edi
movq %r15, %rsi
callq 0x2260
movq %rax, %rdi
callq 0x21e0
movsd -0x40(%rbp), %xmm1
movsd %xmm1, 0x18(%r13)
movsd %xmm0, 0x20(%r13)
cmpl $0x1, %r12d
movsd %xmm0, -0x30(%rbp)
movl $0x4f, %edx
movq %rbx, %rdi
je 0x2d56
testl %r12d, %r12d
jne 0x2db3
leaq 0x14a9(%rip), %rsi      # 0x41a6
callq 0x21c0
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x2170
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2100
movsd 0x12d7(%rip), %xmm0     # 0x4008
movsd -0x30(%rbp), %xmm1
ucomisd %xmm0, %xmm1
jne 0x2d42
jnp 0x2e45
movl $0x48, %edx
movq %rbx, %rdi
leaq 0x14a5(%rip), %rsi      # 0x41f6
jmp 0x2e0a
leaq 0x155c(%rip), %rsi      # 0x42b9
callq 0x21c0
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x2170
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2100
movsd 0x1277(%rip), %xmm0     # 0x4008
movsd -0x30(%rbp), %xmm1
ucomisd %xmm0, %xmm1
jne 0x2da2
jnp 0x2e56
movl $0x48, %edx
movq %rbx, %rdi
leaq 0x1558(%rip), %rsi      # 0x4309
jmp 0x2e0a
leaq 0x15fd(%rip), %rsi      # 0x43b7
callq 0x21c0
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x2170
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2100
movsd 0x121a(%rip), %xmm0     # 0x4008
movsd -0x30(%rbp), %xmm1
ucomisd %xmm0, %xmm1
jne 0x2dfb
jnp 0x2e67
movl $0x48, %edx
movq %rbx, %rdi
leaq 0x15fd(%rip), %rsi      # 0x4407
callq 0x21c0
movq %rbx, %rdi
movsd -0x40(%rbp), %xmm0
callq 0x2270
movq %rax, %r15
movl $0x14, %edx
movq %rax, %rdi
leaq 0x1411(%rip), %rsi      # 0x423f
callq 0x21c0
movq %r15, %rdi
movsd -0x30(%rbp), %xmm0
callq 0x2270
movq %rax, %r15
jmp 0x2e7e
movl $0x64, %edx
movq %rbx, %rdi
leaq 0x1400(%rip), %rsi      # 0x4254
jmp 0x2e76
movl $0x64, %edx
movq %rbx, %rdi
leaq 0x14ed(%rip), %rsi      # 0x4352
jmp 0x2e76
movl $0x64, %edx
movq %rbx, %rdi
leaq 0x15da(%rip), %rsi      # 0x4450
callq 0x21c0
movq %rbx, %r15
movq (%r15), %rax
movq -0x18(%rax), %rdi
addq %r15, %rdi
movl $0xa, %esi
callq 0x2170
movsbl %al, %esi
movq %r15, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2100
incq %r12
addq $0x28, %r13
cmpq $0x3, %r12
jne 0x2c25
movsd 0x32fa(%rip), %xmm0     # 0x61b8
ucomisd 0x1142(%rip), %xmm0     # 0x4008
movsd 0x3312(%rip), %xmm1     # 0x61e0
movsd 0x3332(%rip), %xmm2     # 0x6208
jne 0x2ede
jnp 0x2fae
ucomisd 0x1122(%rip), %xmm1     # 0x4008
jne 0x2eee
jnp 0x2fae
ucomisd 0x1112(%rip), %xmm2     # 0x4008
jne 0x2efe
jnp 0x2fae
movsd 0x32aa(%rip), %xmm0     # 0x61b0
ucomisd 0x10fa(%rip), %xmm0     # 0x4008
jne 0x2f43
jp 0x2f43
movsd 0x32be(%rip), %xmm0     # 0x61d8
ucomisd 0x10e6(%rip), %xmm0     # 0x4008
jne 0x2f43
jp 0x2f43
movsd 0x32d2(%rip), %xmm0     # 0x6200
movl $0xffffffff, %edx       # imm = 0xFFFFFFFF
ucomisd 0x10cd(%rip), %xmm0     # 0x4008
jne 0x2f43
jnp 0x2fde
xorl %eax, %eax
xorpd %xmm0, %xmm0
leaq 0x3268(%rip), %rcx      # 0x61b8
xorl %edx, %edx
movsd -0x8(%rcx), %xmm2
movsd 0x10a9(%rip), %xmm3     # 0x4008
ucomisd %xmm3, %xmm2
jne 0x2f67
jnp 0x2f8f
ucomisd %xmm0, %xmm2
jbe 0x2f75
movsd (%rcx), %xmm3
movl %eax, %edx
jmp 0x2f97
ucomisd %xmm2, %xmm0
jne 0x2f8f
jp 0x2f8f
movsd (%rcx), %xmm3
ucomisd %xmm1, %xmm3
jbe 0x2f8f
movl %eax, %edx
movapd %xmm0, %xmm2
jmp 0x2f97
movapd %xmm0, %xmm2
movapd %xmm1, %xmm3
incq %rax
addq $0x28, %rcx
movapd %xmm3, %xmm1
movapd %xmm2, %xmm0
cmpq $0x3, %rax
jne 0x2f52
jmp 0x2fde
movsd 0x1052(%rip), %xmm3     # 0x4008
ucomisd %xmm3, %xmm2
setp %al
setne %cl
orb %al, %cl
cmpneqpd %xmm3, %xmm1
cmpneqpd %xmm3, %xmm0
orpd %xmm1, %xmm0
movd %xmm0, %eax
orb %cl, %al
movzbl %al, %edx
notl %edx
orl $-0x2, %edx
testl %edx, %edx
js 0x316c
movl %edx, -0x40(%rbp)
movl %edx, %eax
leaq (%rax,%rax,4), %rax
leaq 0x31a2(%rip), %rbx      # 0x6198
leaq (%rbx,%rax,8), %rax
movq %rax, -0x30(%rbp)
incl 0x14(%rax)
leaq 0x10fb(%rip), %rsi      # 0x4103
leaq -0x2e0(%rbp), %r15
movq %r15, %rdi
movl $0x10, %edx
callq 0x2190
xorl %r13d, %r13d
movl 0x14(%r13,%rbx), %esi
movq %r15, %rdi
callq 0x2280
movq %rax, %r12
movq (%rax), %rax
movq -0x18(%rax), %rdi
addq %r12, %rdi
movl $0xa, %esi
callq 0x2170
movsbl %al, %esi
movq %r12, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2100
addq $0x28, %r13
cmpq $0x78, %r13
jne 0x301f
leaq -0x2e0(%rbp), %rdi
callq 0x2200
xorpd %xmm0, %xmm0
movapd %xmm0, -0xb0(%rbp)
movapd %xmm0, -0xc0(%rbp)
movapd %xmm0, -0xd0(%rbp)
movapd %xmm0, -0xe0(%rbp)
leaq -0xa0(%rbp), %rsi
movupd %xmm0, 0x9(%rsi)
movupd %xmm0, 0x19(%rsi)
movupd %xmm0, 0x29(%rsi)
movq $0x0, 0x38(%rsi)
movabsq $0x64656e6769737361, %rax # imm = 0x64656E6769737361
movq %rax, (%rsi)
movb $0x0, 0x8(%rsi)
movl $0x40, %edx
movl %r14d, %edi
xorl %ecx, %ecx
movq -0x30(%rbp), %r8
movl $0x10, %r9d
callq 0x2040
leaq -0x50(%rbp), %r14
movq %r14, -0x10(%r14)
movq $0x0, -0x8(%r14)
movb $0x0, (%r14)
movl -0x40(%rbp), %eax
cmpl $0x1, %eax
je 0x31c5
testl %eax, %eax
jne 0x322e
leaq 0x13b7(%rip), %rcx      # 0x44be
leaq -0x60(%rbp), %rdi
movl $0x1, %r8d
xorl %esi, %esi
xorl %edx, %edx
callq 0x2240
movq 0x2eaf(%rip), %rbx      # 0x5fd0
leaq 0x1398(%rip), %rsi      # 0x44c0
movl $0x33, %edx
movq %rbx, %rdi
callq 0x21c0
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x2170
movsbl %al, %esi
movq 0x2e7d(%rip), %rdi      # 0x5fd0
callq 0x2030
movq %rax, %rdi
callq 0x2100
leaq 0x138d(%rip), %rsi      # 0x44f4
jmp 0x3295
leaq -0x2d0(%rbp), %r14
cmpl $-0x1, %edx
je 0x3347
movq %r14, -0x10(%r14)
leaq 0x14bd(%rip), %rsi      # 0x4644
leaq 0x14b7(%rip), %rdx      # 0x4645
leaq -0x2e0(%rbp), %rdi
callq 0x34d4
movq -0x2e0(%rbp), %rsi
leaq -0xe0(%rbp), %rbx
movq %rbx, %rdi
callq 0x2160
movl $0x40, %edx
movl -0x34(%rbp), %edi
movq %rbx, %rsi
callq 0x20c0
jmp 0x33c8
leaq 0x1372(%rip), %rcx      # 0x453e
leaq -0x60(%rbp), %rdi
movl $0x1, %r8d
xorl %esi, %esi
xorl %edx, %edx
callq 0x2240
movq 0x2dea(%rip), %rbx      # 0x5fd0
leaq 0x1353(%rip), %rsi      # 0x4540
movl $0x33, %edx
movq %rbx, %rdi
callq 0x21c0
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x2170
movsbl %al, %esi
movq 0x2db8(%rip), %rdi      # 0x5fd0
callq 0x2030
movq %rax, %rdi
callq 0x2100
leaq 0x1348(%rip), %rsi      # 0x4574
jmp 0x3295
leaq 0x1389(%rip), %rcx      # 0x45be
leaq -0x60(%rbp), %rdi
movl $0x1, %r8d
xorl %esi, %esi
xorl %edx, %edx
callq 0x2240
movq 0x2d81(%rip), %rbx      # 0x5fd0
leaq 0x136a(%rip), %rsi      # 0x45c0
movl $0x33, %edx
movq %rbx, %rdi
callq 0x21c0
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x2170
movsbl %al, %esi
movq 0x2d4f(%rip), %rdi      # 0x5fd0
callq 0x2030
movq %rax, %rdi
callq 0x2100
leaq 0x135f(%rip), %rsi      # 0x45f4
movq 0x2d34(%rip), %rbx      # 0x5fd0
movl $0x49, %edx
movq %rbx, %rdi
callq 0x21c0
movq (%rbx), %rax
addq -0x18(%rax), %rbx
movq %rbx, %rdi
movl $0xa, %esi
callq 0x2170
movsbl %al, %esi
movq 0x2d09(%rip), %rdi      # 0x5fd0
callq 0x2030
movq %rax, %rdi
callq 0x2100
movq -0x60(%rbp), %rsi
leaq -0xe0(%rbp), %rbx
movq %rbx, %rdi
callq 0x2160
movl $0x40, %edx
movl -0x34(%rbp), %edi
movq %rbx, %rsi
callq 0x20c0
movq -0x60(%rbp), %rdi
cmpq %r14, %rdi
je 0x330c
movq -0x50(%rbp), %rsi
incq %rsi
callq 0x2180
movq 0x2cb5(%rip), %rax      # 0x5fc8
movq (%rax), %rcx
movq 0x18(%rax), %rax
leaq -0x2d8(%rbp), %rdi
movq %rcx, -0x8(%rdi)
movq -0x18(%rcx), %rcx
movq %rax, -0x2e0(%rbp,%rcx)
callq 0x2140
leaq -0x1e8(%rbp), %rdi
callq 0x20b0
jmp 0x33e3
movq %r14, -0x10(%r14)
leaq 0x12ec(%rip), %rsi      # 0x463e
leaq 0x12e7(%rip), %rdx      # 0x4640
leaq -0x2e0(%rbp), %rdi
callq 0x34d4
movl 0x2e3d(%rip), %eax      # 0x61a8
cmpl 0x2e3b(%rip), %eax      # 0x61ac
jg 0x33a2
movl 0x2e57(%rip), %eax      # 0x61d0
cmpl 0x2e55(%rip), %eax      # 0x61d4
jg 0x33a2
movl 0x2e71(%rip), %eax      # 0x61f8
cmpl 0x2e6f(%rip), %eax      # 0x61fc
jg 0x33a2
leaq 0x12ab(%rip), %rsi      # 0x4641
leaq -0x2e0(%rbp), %rdi
callq 0x21f0
movq -0x2e0(%rbp), %rsi
leaq -0xe0(%rbp), %rbx
movq %rbx, %rdi
callq 0x2160
movl $0x40, %edx
movl -0x34(%rbp), %edi
movq %rbx, %rsi
callq 0x20c0
movq -0x2e0(%rbp), %rdi
cmpq %r14, %rdi
je 0x33e3
movq -0x2d0(%rbp), %rsi
incq %rsi
callq 0x2180
movq 0x2be6(%rip), %rbx      # 0x5fd0
leaq 0x1255(%rip), %rsi      # 0x4646
movl $0x46, %edx
movq %rbx, %rdi
callq 0x21c0
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x2170
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2100
leaq 0x1261(%rip), %rsi      # 0x468d
movl $0x60, %edx
movq %rbx, %rdi
callq 0x21c0
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x2170
movsbl %al, %esi
movq %rbx, %rdi
callq 0x2030
movq %rax, %rdi
callq 0x2100
addq $0x2b8, %rsp            # imm = 0x2B8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x22f0
jmp 0x349b
jmp 0x347b
movq %rax, %rbx
movq -0x2e0(%rbp), %rdi
cmpq %r14, %rdi
je 0x34cb
movq -0x2d0(%rbp), %rsi
incq %rsi
callq 0x2180
jmp 0x34cb
movq %rax, %rbx
jmp 0x34cb
jmp 0x34bc
movq %rax, %rbx
movq -0x60(%rbp), %rdi
cmpq %r14, %rdi
je 0x34bf
movq -0x50(%rbp), %rsi
incq %rsi
callq 0x2180
jmp 0x34bf
movq %rax, %rbx
leaq -0x2e0(%rbp), %rdi
callq 0x2220
movq %rbx, %rdi
callq 0x2290
nop