// File: gty980218[P]ee450/scheduler.cpp
// Function: main
int main(){
    //initailize UDP sock
    int sock_udp;
    if((sock_udp=socket(PF_INET,SOCK_DGRAM,0))<0){
        fprintf(stderr,"Create UDP socket failed!\n");
        exit(1);
    }
    struct sockaddr_in scheaddr;
    memset(&scheaddr, 0, sizeof(scheaddr));
	scheaddr.sin_family = AF_INET;                          //use IPv4
	scheaddr.sin_port = htons(33188);
	scheaddr.sin_addr.s_addr = htonl(INADDR_ANY);
    if(bind(sock_udp, (struct sockaddr*)& scheaddr, sizeof(scheaddr)) < 0){
        fprintf(stderr,"Binding UDP failed,check the availability of port!\n");
        exit(1);
    }
    cout<<"The Scheduler is up and running."<<endl;
    //initialize sheduler
    init_sche(sock_udp);
    //initialize TCP sock
    int sock_tcp;
    if((sock_tcp=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 = htonl(INADDR_ANY);
    if(bind(sock_tcp, (struct sockaddr*)& servaddr, sizeof(servaddr)) < 0){
        fprintf(stderr,"Binding TCP failed,check the availability of port!\n");
        exit(1);
    }
    if(listen(sock_tcp,SOMAXCONN)<0){
        fprintf(stderr,"Listen failed!");
        exit(1);
    }
    //connect tcp
    tcp_conn(sock_tcp,sock_udp);

    close(sock_tcp);
    close(sock_udp); 
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x30, %rsp
movl $0x2, %esi
xorl %edx, %edx
movl %esi, %edi
callq 0x2080
movl %eax, -0x4(%rbp)
cmpl $0x0, %eax
jge 0x2630
movq 0x39c3(%rip), %rax      # 0x5fd8
movq (%rax), %rdi
leaq 0x19f1(%rip), %rsi      # 0x4010
movb $0x0, %al
callq 0x22d0
movl $0x1, %edi
callq 0x2260
leaq -0x14(%rbp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x2110
movw $0x2, -0x14(%rbp)
movl $0x81a4, %edi           # imm = 0x81A4
callq 0x2030
movw %ax, -0x12(%rbp)
xorl %edi, %edi
callq 0x2170
movl %eax, -0x10(%rbp)
movl -0x4(%rbp), %edi
leaq -0x14(%rbp), %rsi
movl $0x10, %edx
callq 0x2050
cmpl $0x0, %eax
jge 0x2696
movq 0x395d(%rip), %rax      # 0x5fd8
movq (%rax), %rdi
leaq 0x19a6(%rip), %rsi      # 0x402b
movb $0x0, %al
callq 0x22d0
movl $0x1, %edi
callq 0x2260
movq 0x3933(%rip), %rdi      # 0x5fd0
leaq 0x19bb(%rip), %rsi      # 0x405f
callq 0x21d0
movq %rax, %rdi
movq 0x390d(%rip), %rsi      # 0x5fc0
callq 0x21e0
movl -0x4(%rbp), %edi
callq 0x27c0
movl $0x2, %edi
movl $0x1, %esi
movl $0x6, %edx
callq 0x2080
movl %eax, -0x18(%rbp)
cmpl $0x0, %eax
jge 0x26fe
movq 0x38f5(%rip), %rax      # 0x5fd8
movq (%rax), %rdi
leaq 0x1993(%rip), %rsi      # 0x4080
movb $0x0, %al
callq 0x22d0
movl $0x1, %edi
callq 0x2260
leaq -0x28(%rbp), %rdi
xorl %esi, %esi
movl $0x10, %edx
callq 0x2110
movw $0x2, -0x28(%rbp)
movl $0x858c, %edi           # imm = 0x858C
callq 0x2030
movw %ax, -0x26(%rbp)
xorl %edi, %edi
callq 0x2170
movl %eax, -0x24(%rbp)
movl -0x18(%rbp), %edi
leaq -0x28(%rbp), %rsi
movl $0x10, %edx
callq 0x2050
cmpl $0x0, %eax
jge 0x2764
movq 0x388f(%rip), %rax      # 0x5fd8
movq (%rax), %rdi
leaq 0x1948(%rip), %rsi      # 0x409b
movb $0x0, %al
callq 0x22d0
movl $0x1, %edi
callq 0x2260
movl -0x18(%rbp), %edi
movl $0x1000, %esi           # imm = 0x1000
callq 0x2240
cmpl $0x0, %eax
jge 0x2798
movq 0x385b(%rip), %rax      # 0x5fd8
movq (%rax), %rdi
leaq 0x1948(%rip), %rsi      # 0x40cf
movb $0x0, %al
callq 0x22d0
movl $0x1, %edi
callq 0x2260
movl -0x18(%rbp), %edi
movl -0x4(%rbp), %esi
callq 0x2bc0
movl -0x18(%rbp), %edi
callq 0x23c0
movl -0x4(%rbp), %edi
callq 0x23c0
xorl %eax, %eax
addq $0x30, %rsp
popq %rbp
retq
nopl (%rax,%rax)