// File: /workspace/github_cpp_makefile_star2_O3/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
movapd %xmm0, -0x70(%rbp)
movapd %xmm0, -0x80(%rbp)
movapd %xmm0, -0x90(%rbp)
movapd %xmm0, -0xa0(%rbp)
leaq -0xe0(%rbp), %rsi
movapd %xmm0, (%rsi)
movapd %xmm0, 0x10(%rsi)
movapd %xmm0, 0x20(%rsi)
movapd %xmm0, 0x30(%rsi)
movl $0x40, %edx
movl %edi, -0x34(%rbp)
callq 0x2210
cmpl $-0x1, %eax
je 0x345d
movq 0x3433(%rip), %rbx      # 0x5fd0
leaq 0x15a8(%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 0x15a6(%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 0x3588(%rip), %r13      # 0x6198
xorl %r12d, %r12d
movl 0x10(%r13), %eax
cmpl 0x14(%r13), %eax
jle 0x2e87
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 0x19fb(%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 0x2d40
testl %r12d, %r12d
jne 0x2d99
leaq 0x14bb(%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 -0x30(%rbp), %xmm0
ucomisd 0x12e4(%rip), %xmm0     # 0x4008
jne 0x2d2c
jnp 0x2e27
movl $0x48, %edx
movq %rbx, %rdi
leaq 0x14bb(%rip), %rsi      # 0x41f6
jmp 0x2dec
leaq 0x1572(%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 -0x30(%rbp), %xmm0
ucomisd 0x1288(%rip), %xmm0     # 0x4008
jne 0x2d88
jnp 0x2e38
movl $0x48, %edx
movq %rbx, %rdi
leaq 0x1572(%rip), %rsi      # 0x4309
jmp 0x2dec
leaq 0x1617(%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 -0x30(%rbp), %xmm0
ucomisd 0x122f(%rip), %xmm0     # 0x4008
jne 0x2ddd
jnp 0x2e49
movl $0x48, %edx
movq %rbx, %rdi
leaq 0x161b(%rip), %rsi      # 0x4407
callq 0x21c0
movq %rbx, %rdi
movsd -0x40(%rbp), %xmm0
callq 0x2270
movq %rax, %r15
movl $0x14, %edx
movq %rax, %rdi
leaq 0x142f(%rip), %rsi      # 0x423f
callq 0x21c0
movq %r15, %rdi
movsd -0x30(%rbp), %xmm0
callq 0x2270
movq %rax, %r15
jmp 0x2e60
movl $0x64, %edx
movq %rbx, %rdi
leaq 0x141e(%rip), %rsi      # 0x4254
jmp 0x2e58
movl $0x64, %edx
movq %rbx, %rdi
leaq 0x150b(%rip), %rsi      # 0x4352
jmp 0x2e58
movl $0x64, %edx
movq %rbx, %rdi
leaq 0x15f8(%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 0x2c13
movsd 0x3318(%rip), %xmm2     # 0x61b8
ucomisd 0x1160(%rip), %xmm2     # 0x4008
movsd 0x3330(%rip), %xmm1     # 0x61e0
movsd 0x3350(%rip), %xmm0     # 0x6208
jne 0x2ec0
jnp 0x3113
ucomisd 0x1140(%rip), %xmm1     # 0x4008
jne 0x2ed0
jnp 0x3113
ucomisd 0x1130(%rip), %xmm0     # 0x4008
jne 0x2ee0
jnp 0x3113
movsd 0x32c8(%rip), %xmm0     # 0x61b0
ucomisd 0x1118(%rip), %xmm0     # 0x4008
jne 0x2f20
jp 0x2f20
movsd 0x32dc(%rip), %xmm0     # 0x61d8
ucomisd 0x1104(%rip), %xmm0     # 0x4008
jne 0x2f20
jp 0x2f20
movsd 0x32f0(%rip), %xmm0     # 0x6200
ucomisd 0x10f0(%rip), %xmm0     # 0x4008
jne 0x2f20
jnp 0x314f
xorl %eax, %eax
xorpd %xmm0, %xmm0
leaq 0x328b(%rip), %rcx      # 0x61b8
xorl %edx, %edx
movsd -0x8(%rcx), %xmm2
ucomisd 0x10cc(%rip), %xmm2     # 0x4008
jne 0x2f40
jnp 0x2f68
ucomisd %xmm0, %xmm2
jbe 0x2f4e
movsd (%rcx), %xmm3
movl %eax, %edx
jmp 0x2f70
ucomisd %xmm2, %xmm0
jne 0x2f68
jp 0x2f68
movsd (%rcx), %xmm3
ucomisd %xmm1, %xmm3
jbe 0x2f68
movl %eax, %edx
movapd %xmm0, %xmm2
jmp 0x2f70
movapd %xmm0, %xmm2
movapd %xmm1, %xmm3
incq %rax
addq $0x28, %rcx
movapd %xmm3, %xmm1
movapd %xmm2, %xmm0
cmpq $0x3, %rax
jne 0x2f2f
testl %edx, %edx
js 0x31d9
movl %edx, -0x40(%rbp)
movl %edx, %eax
leaq (%rax,%rax,4), %rax
leaq 0x31fb(%rip), %rbx      # 0x6198
leaq (%rbx,%rax,8), %rax
movq %rax, -0x30(%rbp)
incl 0x14(%rax)
leaq 0x1154(%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 0x2fc6
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 0x3251
testl %eax, %eax
jne 0x32ba
leaq 0x1410(%rip), %rcx      # 0x44be
leaq -0x60(%rbp), %rdi
movl $0x1, %r8d
xorl %esi, %esi
xorl %edx, %edx
callq 0x2240
movq 0x2f08(%rip), %rbx      # 0x5fd0
leaq 0x13f1(%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 0x2ed6(%rip), %rdi      # 0x5fd0
callq 0x2030
movq %rax, %rdi
callq 0x2100
leaq 0x13e6(%rip), %rsi      # 0x44f4
jmp 0x3321
ucomisd 0xeed(%rip), %xmm2      # 0x4008
jne 0x31e2
jp 0x31e2
ucomisd 0xed9(%rip), %xmm1      # 0x4008
jne 0x31e2
jp 0x31e2
ucomisd 0xec5(%rip), %xmm0      # 0x4008
jne 0x31e2
jp 0x31e2
leaq -0x2d0(%rbp), %r14
movq %r14, -0x10(%r14)
leaq 0x14dd(%rip), %rsi      # 0x463e
leaq 0x14d8(%rip), %rdx      # 0x4640
leaq -0x2e0(%rbp), %rdi
callq 0x34be
movl 0x302e(%rip), %eax      # 0x61a8
cmpl 0x302c(%rip), %eax      # 0x61ac
jg 0x31b1
movl 0x3048(%rip), %eax      # 0x61d0
cmpl 0x3046(%rip), %eax      # 0x61d4
jg 0x31b1
movl 0x3062(%rip), %eax      # 0x61f8
cmpl 0x3060(%rip), %eax      # 0x61fc
jg 0x31b1
leaq 0x149c(%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
jmp 0x322d
cmpl $-0x1, %edx
je 0x314f
leaq -0x2d0(%rbp), %r14
movq %r14, -0x10(%r14)
leaq 0x1450(%rip), %rsi      # 0x4644
leaq 0x144a(%rip), %rdx      # 0x4645
leaq -0x2e0(%rbp), %rdi
callq 0x34be
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 0x33ce
movq -0x2d0(%rbp), %rsi
incq %rsi
callq 0x2180
jmp 0x33ce
leaq 0x12e6(%rip), %rcx      # 0x453e
leaq -0x60(%rbp), %rdi
movl $0x1, %r8d
xorl %esi, %esi
xorl %edx, %edx
callq 0x2240
movq 0x2d5e(%rip), %rbx      # 0x5fd0
leaq 0x12c7(%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 0x2d2c(%rip), %rdi      # 0x5fd0
callq 0x2030
movq %rax, %rdi
callq 0x2100
leaq 0x12bc(%rip), %rsi      # 0x4574
jmp 0x3321
leaq 0x12fd(%rip), %rcx      # 0x45be
leaq -0x60(%rbp), %rdi
movl $0x1, %r8d
xorl %esi, %esi
xorl %edx, %edx
callq 0x2240
movq 0x2cf5(%rip), %rbx      # 0x5fd0
leaq 0x12de(%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 0x2cc3(%rip), %rdi      # 0x5fd0
callq 0x2030
movq %rax, %rdi
callq 0x2100
leaq 0x12d3(%rip), %rsi      # 0x45f4
movq 0x2ca8(%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 0x2c7d(%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 0x3398
movq -0x50(%rbp), %rsi
incq %rsi
callq 0x2180
movq 0x2c29(%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
movq 0x2bfb(%rip), %rbx      # 0x5fd0
leaq 0x126a(%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 0x1276(%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 0x346d
jmp 0x3468
jmp 0x34a7
movq %rax, %rbx
jmp 0x34b6
movq %rax, %rbx
movq -0x2e0(%rbp), %rdi
cmpq %r14, %rdi
je 0x34b6
movq -0x2d0(%rbp), %rsi
incq %rsi
callq 0x2180
jmp 0x34b6
movq %rax, %rbx
movq -0x60(%rbp), %rdi
cmpq %r14, %rdi
je 0x34aa
movq -0x50(%rbp), %rsi
incq %rsi
callq 0x2180
jmp 0x34aa
movq %rax, %rbx
leaq -0x2e0(%rbp), %rdi
callq 0x2220
movq %rbx, %rdi
callq 0x2290