// File: /workspace/github_cpp_makefile_star2_O2/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)
pushq $0x40
popq %rdx
movl %edi, -0x34(%rbp)
callq 0x21d0
cmpl $-0x1, %eax
je 0x2fd4
movq 0x363e(%rip), %rdi      # 0x5fd0
leaq 0x17b3(%rip), %rsi      # 0x414c
callq 0x2160
leaq -0xe0(%rbp), %rsi
movq %rax, %rdi
callq 0x2160
leaq 0x17c7(%rip), %rsi      # 0x417b
movq %rax, %rdi
callq 0x2160
movq %rax, %rdi
callq 0x20e0
leaq 0x37a5(%rip), %r13      # 0x6170
xorl %r12d, %r12d
leaq 0x1cb6(%rip), %r15      # 0x468b
cmpq $0x3, %r12
je 0x2bad
movl 0x10(%r13), %eax
cmpl 0x14(%r13), %eax
jle 0x2ba1
xorpd %xmm0, %xmm0
movapd %xmm0, -0x70(%rbp)
movapd %xmm0, -0x80(%rbp)
movapd %xmm0, -0x90(%rbp)
movapd %xmm0, -0xa0(%rbp)
leaq -0xe0(%rbp), %rbx
movq %rbx, %rdi
callq 0x20c0
movl %r14d, %edi
movq %rbx, %rsi
movq %rax, %rdx
xorl %ecx, %ecx
movq %r13, %r8
pushq $0x10
popq %r9
callq 0x2030
movl %r14d, %edi
leaq -0xa0(%rbp), %rbx
movq %rbx, %rsi
pushq $0x40
popq %rdx
xorl %ecx, %ecx
xorl %r8d, %r8d
xorl %r9d, %r9d
callq 0x2120
movq %rbx, %rdi
movq %r15, %rsi
callq 0x2210
movq %rax, %rdi
callq 0x21b0
movsd %xmm0, -0x40(%rbp)
xorl %edi, %edi
movq %r15, %rsi
callq 0x2210
movq %rax, %rdi
callq 0x21b0
movsd -0x40(%rbp), %xmm1
movsd %xmm1, 0x18(%r13)
movsd %xmm0, 0x20(%r13)
cmpl $0x1, %r12d
movsd %xmm0, -0x30(%rbp)
je 0x2ad8
testl %r12d, %r12d
movq 0x3532(%rip), %rbx      # 0x5fd0
movq %rbx, %rdi
jne 0x2b13
leaq 0x16fc(%rip), %rsi      # 0x41a6
callq 0x2160
movq %rax, %rdi
callq 0x20e0
movsd -0x30(%rbp), %xmm0
ucomisd 0x1544(%rip), %xmm0     # 0x4008
jne 0x2acc
jnp 0x2b72
movq %rbx, %rdi
leaq 0x1720(%rip), %rsi      # 0x41f6
jmp 0x2b42
movq 0x34f1(%rip), %rbx      # 0x5fd0
movq %rbx, %rdi
leaq 0x17d0(%rip), %rsi      # 0x42b9
callq 0x2160
movq %rax, %rdi
callq 0x20e0
movsd -0x30(%rbp), %xmm0
ucomisd 0x1505(%rip), %xmm0     # 0x4008
jne 0x2b07
jnp 0x2b7e
movq %rbx, %rdi
leaq 0x17f8(%rip), %rsi      # 0x4309
jmp 0x2b42
leaq 0x189d(%rip), %rsi      # 0x43b7
callq 0x2160
movq %rax, %rdi
callq 0x20e0
movsd -0x30(%rbp), %xmm0
ucomisd 0x14d4(%rip), %xmm0     # 0x4008
jne 0x2b38
jnp 0x2b8a
movq %rbx, %rdi
leaq 0x18c5(%rip), %rsi      # 0x4407
callq 0x2160
movq %rax, %rdi
movsd -0x40(%rbp), %xmm0
callq 0x2220
movq %rax, %rdi
leaq 0x16e1(%rip), %rsi      # 0x423f
callq 0x2160
movq %rax, %rdi
movsd -0x30(%rbp), %xmm0
callq 0x2220
jmp 0x2b99
movq %rbx, %rdi
leaq 0x16d8(%rip), %rsi      # 0x4254
jmp 0x2b94
movq %rbx, %rdi
leaq 0x17ca(%rip), %rsi      # 0x4352
jmp 0x2b94
movq %rbx, %rdi
leaq 0x18bc(%rip), %rsi      # 0x4450
callq 0x2160
movq %rax, %rdi
callq 0x20e0
incq %r12
addq $0x28, %r13
jmp 0x29d5
movsd 0x35db(%rip), %xmm2     # 0x6190
ucomisd 0x144b(%rip), %xmm2     # 0x4008
movsd 0x35f3(%rip), %xmm1     # 0x61b8
movsd 0x3613(%rip), %xmm0     # 0x61e0
jne 0x2bd5
jnp 0x2ca3
ucomisd 0x142b(%rip), %xmm1     # 0x4008
jne 0x2be5
jnp 0x2ca3
ucomisd 0x141b(%rip), %xmm0     # 0x4008
jne 0x2bf5
jnp 0x2ca3
movsd 0x358b(%rip), %xmm0     # 0x6188
ucomisd 0x1403(%rip), %xmm0     # 0x4008
jne 0x2c35
jp 0x2c35
movsd 0x359f(%rip), %xmm0     # 0x61b0
ucomisd 0x13ef(%rip), %xmm0     # 0x4008
jne 0x2c35
jp 0x2c35
movsd 0x35b3(%rip), %xmm0     # 0x61d8
ucomisd 0x13db(%rip), %xmm0     # 0x4008
jne 0x2c35
jnp 0x2cdf
xorpd %xmm0, %xmm0
xorl %eax, %eax
leaq 0x354e(%rip), %rcx      # 0x6190
xorl %r12d, %r12d
cmpq $0x3, %rax
je 0x2d5c
movsd -0x8(%rcx), %xmm2
ucomisd 0x13ac(%rip), %xmm2     # 0x4008
jne 0x2c60
jnp 0x2c8a
ucomisd %xmm0, %xmm2
jbe 0x2c6f
movsd (%rcx), %xmm3
movl %eax, %r12d
jmp 0x2c92
ucomisd %xmm2, %xmm0
jne 0x2c8a
jp 0x2c8a
movsd (%rcx), %xmm3
ucomisd %xmm1, %xmm3
jbe 0x2c8a
movl %eax, %r12d
movapd %xmm0, %xmm2
jmp 0x2c92
movapd %xmm0, %xmm2
movapd %xmm1, %xmm3
incq %rax
addq $0x28, %rcx
movapd %xmm2, %xmm0
movapd %xmm3, %xmm1
jmp 0x2c45
ucomisd 0x135d(%rip), %xmm2     # 0x4008
jne 0x2e91
jp 0x2e91
ucomisd 0x1349(%rip), %xmm1     # 0x4008
jne 0x2e91
jp 0x2e91
ucomisd 0x1335(%rip), %xmm0     # 0x4008
jne 0x2e91
jp 0x2e91
leaq 0x1958(%rip), %rsi      # 0x463e
leaq -0x2e0(%rbp), %rdi
leaq -0x60(%rbp), %rdx
callq 0x303a
movl 0x3484(%rip), %eax      # 0x6180
cmpl 0x3482(%rip), %eax      # 0x6184
jg 0x2d33
movl 0x349e(%rip), %eax      # 0x61a8
cmpl 0x349c(%rip), %eax      # 0x61ac
jg 0x2d33
movl 0x34b8(%rip), %eax      # 0x61d0
cmpl 0x34b6(%rip), %eax      # 0x61d4
jg 0x2d33
leaq 0x191a(%rip), %rsi      # 0x4641
leaq -0x2e0(%rbp), %rdi
callq 0x2260
movq -0x2e0(%rbp), %rsi
leaq -0xe0(%rbp), %rbx
movq %rbx, %rdi
callq 0x2150
pushq $0x40
popq %rdx
movl -0x34(%rbp), %edi
movq %rbx, %rsi
callq 0x20b0
jmp 0x2ecc
testl %r12d, %r12d
js 0x2e87
movl %r12d, %eax
imulq $0x28, %rax, %rax
leaq 0x33fd(%rip), %r13      # 0x6170
leaq (%rax,%r13), %rcx
movq %rcx, -0x30(%rbp)
incl 0x14(%rax,%r13)
leaq 0x137c(%rip), %rsi      # 0x4103
leaq -0x2e0(%rbp), %r15
pushq $0x10
popq %rdx
movq %r15, %rdi
callq 0x2170
xorl %ebx, %ebx
cmpq $0x78, %rbx
je 0x2dbc
movl 0x14(%rbx,%r13), %esi
movq %r15, %rdi
callq 0x2230
movq %rax, %rdi
callq 0x20e0
addq $0x28, %rbx
jmp 0x2d9b
leaq -0x2e0(%rbp), %rdi
callq 0x21c0
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, 0x29(%rsi)
andq $0x0, 0x38(%rsi)
movupd %xmm0, 0x19(%rsi)
movabsq $0x64656e6769737361, %rax # imm = 0x64656E6769737361
movq %rax, (%rsi)
movb $0x0, 0x8(%rsi)
pushq $0x40
popq %rdx
pushq $0x10
popq %r9
movl %r14d, %edi
xorl %ecx, %ecx
movq -0x30(%rbp), %r8
callq 0x2030
leaq -0x50(%rbp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
cmpl $0x1, %r12d
je 0x2f1f
testl %r12d, %r12d
jne 0x2f53
leaq 0x1667(%rip), %rsi      # 0x44be
leaq -0x60(%rbp), %rdi
callq 0x2260
movq 0x3169(%rip), %rdi      # 0x5fd0
leaq 0x1652(%rip), %rsi      # 0x44c0
callq 0x2160
movq %rax, %rdi
callq 0x20e0
leaq 0x1672(%rip), %rsi      # 0x44f4
jmp 0x2f85
cmpl $-0x1, %r12d
je 0x2cdf
leaq 0x17ac(%rip), %rsi      # 0x4644
leaq -0x2e0(%rbp), %rdi
leaq -0x60(%rbp), %rdx
callq 0x303a
movq -0x2e0(%rbp), %rsi
leaq -0xe0(%rbp), %rbx
movq %rbx, %rdi
callq 0x2150
pushq $0x40
popq %rdx
movl -0x34(%rbp), %edi
movq %rbx, %rsi
callq 0x20b0
leaq -0x2e0(%rbp), %rdi
callq 0x20d0
movq 0x30f1(%rip), %rbx      # 0x5fd0
leaq 0x1760(%rip), %rsi      # 0x4646
movq %rbx, %rdi
callq 0x2160
movq %rax, %rdi
callq 0x20e0
leaq 0x1790(%rip), %rsi      # 0x468d
movq %rbx, %rdi
callq 0x2160
movq %rax, %rdi
callq 0x20e0
addq $0x2b8, %rsp            # imm = 0x2B8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x1618(%rip), %rsi      # 0x453e
leaq -0x60(%rbp), %rdi
callq 0x2260
movq 0x309a(%rip), %rdi      # 0x5fd0
leaq 0x1603(%rip), %rsi      # 0x4540
callq 0x2160
movq %rax, %rdi
callq 0x20e0
leaq 0x1623(%rip), %rsi      # 0x4574
jmp 0x2f85
leaq 0x1664(%rip), %rsi      # 0x45be
leaq -0x60(%rbp), %rdi
callq 0x2260
movq 0x3066(%rip), %rdi      # 0x5fd0
leaq 0x164f(%rip), %rsi      # 0x45c0
callq 0x2160
movq %rax, %rdi
callq 0x20e0
leaq 0x166f(%rip), %rsi      # 0x45f4
movq 0x3044(%rip), %rdi      # 0x5fd0
callq 0x2160
movq %rax, %rdi
callq 0x20e0
movq -0x60(%rbp), %rsi
leaq -0xe0(%rbp), %rbx
movq %rbx, %rdi
callq 0x2150
pushq $0x40
popq %rdx
movl -0x34(%rbp), %edi
movq %rbx, %rsi
callq 0x20b0
leaq -0x60(%rbp), %rdi
callq 0x20d0
leaq -0x2e0(%rbp), %rdi
callq 0x21e0
jmp 0x2ed8
movq 0x2ffd(%rip), %rax      # 0x5fd8
movq (%rax), %rcx
leaq 0x1154(%rip), %rdi      # 0x4139
pushq $0x12
popq %rsi
pushq $0x1
popq %rdx
callq 0x2270
pushq $0x1
popq %rdi
callq 0x21a0
jmp 0x3003
jmp 0x2ffe
jmp 0x3022
movq %rax, %rbx
jmp 0x3031
movq %rax, %rbx
leaq -0x2e0(%rbp), %rdi
callq 0x20d0
jmp 0x3031
movq %rax, %rbx
leaq -0x60(%rbp), %rdi
callq 0x20d0
jmp 0x3025
movq %rax, %rbx
leaq -0x2e0(%rbp), %rdi
callq 0x21e0
movq %rbx, %rdi
callq 0x2240
nop