// File: gty980218[P]ee450/client.cpp
// Function: main
int main(int argc,char* argv[]) {
	int sock;
	if((sock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0){
        fprintf(stderr,"Create TCP socket failed!\n");
        exit(1);
    }

	struct sockaddr_in servaddr;
	memset(&servaddr, 0, sizeof(servaddr));
	servaddr.sin_family = AF_INET;
	servaddr.sin_port = htons(34188); 
	servaddr.sin_addr.s_addr = inet_addr("127.0.0.1");
	
	if(connect(sock, (struct sockaddr*)&servaddr, sizeof(servaddr)) < 0){
        fprintf(stderr,"Connetc failed!");
        exit(1);
    }
	
	cout<<"The client is up and running"<<endl;

	char sendbuf[1024] = {0};
	char recvbuf[1024] = {0};
	string str=argv[1];
	strcpy(sendbuf,str.c_str());
	write(sock, sendbuf, strlen(sendbuf));
	cout<<"The client has sent query to Scheduler using TCP: client location "<<str<<endl;

	read(sock, recvbuf, sizeof(recvbuf));
	string response=recvbuf;
	if(response.compare("NF")==0){
		cout<<"Location "<<str<<" not found"<<endl;
	}else if(response.compare("N")==0){
		cout<<"Score = None, No assignment"<<endl;
	}else if(response.compare("OL")==0){
		cout<<"All hospitals are unavailable due to overload"<<endl;
	}else{
		cout<<"The client has received results from the Scheduler: assigned to Hospital "<<recvbuf<<endl;
	}
	close(sock);
	
	return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x910, %rsp            # imm = 0x910
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl $0x2, %edi
movl $0x1, %esi
movl $0x6, %edx
callq 0x2050
movl %eax, -0x14(%rbp)
cmpl $0x0, %eax
jge 0x2427
movq 0x2bcc(%rip), %rax      # 0x4fd8
movq (%rax), %rdi
leaq 0xbee(%rip), %rsi       # 0x3004
movb $0x0, %al
callq 0x21f0
movl $0x1, %edi
callq 0x21d0
leaq -0x24(%rbp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x20c0
movw $0x2, -0x24(%rbp)
movl $0x858c, %edi           # imm = 0x858C
callq 0x2030
movw %ax, -0x22(%rbp)
leaq 0xbcd(%rip), %rdi       # 0x301f
callq 0x2170
movl %eax, -0x20(%rbp)
movl -0x14(%rbp), %edi
leaq -0x24(%rbp), %rsi
movl $0x10, %edx
callq 0x2110
cmpl $0x0, %eax
jge 0x2492
movq 0x2b61(%rip), %rax      # 0x4fd8
movq (%rax), %rdi
leaq 0xba8(%rip), %rsi       # 0x3029
movb $0x0, %al
callq 0x21f0
movl $0x1, %edi
callq 0x21d0
movq 0x2b37(%rip), %rdi      # 0x4fd0
leaq 0xb99(%rip), %rsi       # 0x3039
callq 0x2160
movq %rax, %rdi
movq 0x2b11(%rip), %rsi      # 0x4fc0
callq 0x2180
leaq -0x430(%rbp), %rdi
xorl %esi, %esi
movl %esi, -0x8ac(%rbp)
movl $0x400, %edx            # imm = 0x400
movq %rdx, -0x8a8(%rbp)
callq 0x20c0
movl -0x8ac(%rbp), %esi
movq -0x8a8(%rbp), %rdx
leaq -0x830(%rbp), %rdi
callq 0x20c0
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x8a0(%rbp)
leaq -0x851(%rbp), %rdi
movq %rdi, -0x898(%rbp)
callq 0x2250
movq -0x8a0(%rbp), %rsi
movq -0x898(%rbp), %rdx
leaq -0x850(%rbp), %rdi
callq 0x2870
jmp 0x252b
leaq -0x851(%rbp), %rdi
callq 0x21a0
leaq -0x850(%rbp), %rdi
callq 0x20a0
movq %rax, %rsi
leaq -0x430(%rbp), %rdi
movq %rdi, -0x8b8(%rbp)
callq 0x2150
movq -0x8b8(%rbp), %rdi
movl -0x14(%rbp), %eax
movl %eax, -0x8bc(%rbp)
callq 0x20b0
movl -0x8bc(%rbp), %edi
movq -0x8b8(%rbp), %rsi
movq %rax, %rdx
callq 0x2080
jmp 0x2585
movq 0x2a44(%rip), %rdi      # 0x4fd0
leaq 0xac3(%rip), %rsi       # 0x3056
callq 0x2160
movq %rax, -0x8c8(%rbp)
jmp 0x25a1
movq -0x8c8(%rbp), %rdi
leaq -0x850(%rbp), %rsi
callq 0x2140
movq %rax, -0x8d0(%rbp)
jmp 0x25bd
movq -0x8d0(%rbp), %rdi
movq 0x29f5(%rip), %rsi      # 0x4fc0
callq 0x2180
jmp 0x25d2
movl -0x14(%rbp), %edi
leaq -0x830(%rbp), %rsi
movl $0x400, %edx            # imm = 0x400
callq 0x2200
jmp 0x25e8
leaq -0x889(%rbp), %rdi
movq %rdi, -0x8d8(%rbp)
callq 0x2250
movq -0x8d8(%rbp), %rdx
leaq -0x888(%rbp), %rdi
leaq -0x830(%rbp), %rsi
callq 0x2870
jmp 0x2617
leaq -0x889(%rbp), %rdi
callq 0x21a0
leaq -0x888(%rbp), %rdi
leaq 0xa68(%rip), %rsi       # 0x3099
callq 0x2070
cmpl $0x0, %eax
jne 0x272d
movq 0x298a(%rip), %rdi      # 0x4fd0
leaq 0xa4f(%rip), %rsi       # 0x309c
callq 0x2160
movq %rax, -0x8e0(%rbp)
jmp 0x265b
movq -0x8e0(%rbp), %rdi
leaq -0x850(%rbp), %rsi
callq 0x2140
movq %rax, -0x8e8(%rbp)
jmp 0x2677
movq -0x8e8(%rbp), %rdi
leaq 0xa21(%rip), %rsi       # 0x30a6
callq 0x2160
movq %rax, -0x8f0(%rbp)
jmp 0x2693
movq -0x8f0(%rbp), %rdi
movq 0x291f(%rip), %rsi      # 0x4fc0
callq 0x2180
jmp 0x26a8
jmp 0x2819
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x860(%rbp)
movl %eax, -0x864(%rbp)
leaq -0x851(%rbp), %rdi
callq 0x21a0
jmp 0x285a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x860(%rbp)
movl %eax, -0x864(%rbp)
jmp 0x284e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x860(%rbp)
movl %eax, -0x864(%rbp)
leaq -0x889(%rbp), %rdi
callq 0x21a0
jmp 0x284e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x860(%rbp)
movl %eax, -0x864(%rbp)
leaq -0x888(%rbp), %rdi
callq 0x20d0
jmp 0x284e
leaq -0x888(%rbp), %rdi
leaq 0x976(%rip), %rsi       # 0x30b1
callq 0x2070
cmpl $0x0, %eax
jne 0x277b
movq 0x2884(%rip), %rdi      # 0x4fd0
leaq 0x960(%rip), %rsi       # 0x30b3
callq 0x2160
movq %rax, -0x8f8(%rbp)
jmp 0x2761
movq -0x8f8(%rbp), %rdi
movq 0x2851(%rip), %rsi      # 0x4fc0
callq 0x2180
jmp 0x2776
jmp 0x2817
leaq -0x888(%rbp), %rdi
leaq 0x946(%rip), %rsi       # 0x30cf
callq 0x2070
cmpl $0x0, %eax
jne 0x27c6
movq 0x2836(%rip), %rdi      # 0x4fd0
leaq 0x931(%rip), %rsi       # 0x30d2
callq 0x2160
movq %rax, -0x900(%rbp)
jmp 0x27af
movq -0x900(%rbp), %rdi
movq 0x2803(%rip), %rsi      # 0x4fc0
callq 0x2180
jmp 0x27c4
jmp 0x2815
movq 0x2803(%rip), %rdi      # 0x4fd0
leaq 0x92c(%rip), %rsi       # 0x3100
callq 0x2160
movq %rax, -0x908(%rbp)
jmp 0x27e2
movq -0x908(%rbp), %rdi
leaq -0x830(%rbp), %rsi
callq 0x2160
movq %rax, -0x910(%rbp)
jmp 0x27fe
movq -0x910(%rbp), %rdi
movq 0x27b4(%rip), %rsi      # 0x4fc0
callq 0x2180
jmp 0x2813
jmp 0x2815
jmp 0x2817
jmp 0x2819
movl -0x14(%rbp), %edi
callq 0x2270
jmp 0x2823
movl $0x0, -0x4(%rbp)
leaq -0x888(%rbp), %rdi
callq 0x20d0
leaq -0x850(%rbp), %rdi
callq 0x20d0
movl -0x4(%rbp), %eax
addq $0x910, %rsp            # imm = 0x910
popq %rbp
retq
leaq -0x850(%rbp), %rdi
callq 0x20d0
movq -0x860(%rbp), %rdi
callq 0x2240
nopw %cs:(%rax,%rax)