// File: Rjerk[P]ratabase/client.cpp
// Function: main
int main(int argc, char** argv)
{
    int port = -1;
    if (argc == 1) {
        port = DEFAULT_PORT;
    } else if (argc == 2) {
        port = atoi(argv[1]);
    } else if (argc > 2) {
        std::cerr << "usage: ./client [ <port> ]\n";
        exit(0);
    }

    int sock = ::socket(PF_INET, SOCK_STREAM, 0);
    if (sock == -1) {
        perror("socket() error");
        exit(0);
    }

    struct sockaddr_in serv_adr;
    ::bzero(&serv_adr, sizeof(serv_adr));
    serv_adr.sin_family = AF_INET;
    serv_adr.sin_addr.s_addr = htonl(INADDR_ANY);
    serv_adr.sin_port = htons(port);

    sockets::connect(sock, (struct sockaddr*) &serv_adr);

    char message[BUF_SIZE];
    std::cout << "Welcome to use Ratabase (use 'q' or 'Q' to quit)\n";
    while (1) {
        std::cout << "Ratabase> ";
        if (fgets(message, BUF_SIZE, stdin) == NULL && ferror(stdin)) {
            perror("fgets error");
        }
        if (!strcmp(message, "q\n") || !strcmp(message, "Q\n")) {
            break;
        } else if (!strcmp(message, "\n")) {
            continue;
        }
        ssize_t ret = sockets::write(sock, message, strlen(message));
        ret = ::read(sock, message, BUF_SIZE);
        if (ret < 0) {
            exit(0);
        }
        message[ret] = '\0';
        std::cout << message;
    }

    ::close(sock);
}

// Assembly:
subq $0x448, %rsp            # imm = 0x448
movl $0x0, 0x444(%rsp)
movl %edi, 0x440(%rsp)
movq %rsi, 0x438(%rsp)
movl $0xffffffff, 0x434(%rsp) # imm = 0xFFFFFFFF
cmpl $0x1, 0x440(%rsp)
jne 0x2533
movl $0x270f, 0x434(%rsp) # imm = 0x270F
jmp 0x257f
cmpl $0x2, 0x440(%rsp)
jne 0x2557
movq 0x438(%rsp), %rax
movq 0x8(%rax), %rdi
callq 0x22a0
movl %eax, 0x434(%rsp)
jmp 0x257d
cmpl $0x2, 0x440(%rsp)
jle 0x257b
movq 0x4a88(%rip), %rdi      # 0x6ff0
leaq 0x1a95(%rip), %rsi      # 0x4004
callq 0x2200
xorl %edi, %edi
callq 0x2240
jmp 0x257d
jmp 0x257f
movl $0x2, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x2070
movl %eax, 0x430(%rsp)
cmpl $-0x1, 0x430(%rsp)
jne 0x25b4
leaq 0x1a78(%rip), %rdi      # 0x4020
callq 0x21b0
xorl %edi, %edi
callq 0x2240
leaq 0x420(%rsp), %rdi
movl $0x10, %esi
callq 0x22d0
movw $0x2, 0x420(%rsp)
xorl %edi, %edi
callq 0x2190
movl %eax, 0x424(%rsp)
movl 0x434(%rsp), %eax
movzwl %ax, %edi
callq 0x2040
movw %ax, 0x422(%rsp)
movl 0x430(%rsp), %edi
leaq 0x420(%rsp), %rsi
callq 0x3520
movq 0x49c0(%rip), %rdi      # 0x6fd0
leaq 0x1a18(%rip), %rsi      # 0x402f
callq 0x2200
movq 0x49ad(%rip), %rdi      # 0x6fd0
leaq 0x1a37(%rip), %rsi      # 0x4061
callq 0x2200
leaq 0x20(%rsp), %rdi
movq 0x498d(%rip), %rax      # 0x6fc8
movq (%rax), %rdx
movl $0x400, %esi            # imm = 0x400
callq 0x22b0
cmpq $0x0, %rax
jne 0x266e
movq 0x4973(%rip), %rax      # 0x6fc8
movq (%rax), %rdi
callq 0x2300
cmpl $0x0, %eax
je 0x266e
leaq 0x1a03(%rip), %rdi      # 0x406c
callq 0x21b0
leaq 0x20(%rsp), %rdi
leaq 0x19fe(%rip), %rsi      # 0x4078
callq 0x2270
cmpl $0x0, %eax
je 0x269a
leaq 0x20(%rsp), %rdi
leaq 0x19eb(%rip), %rsi      # 0x407b
callq 0x2270
cmpl $0x0, %eax
jne 0x269f
jmp 0x273b
leaq 0x20(%rsp), %rdi
leaq 0x19b4(%rip), %rsi      # 0x405f
callq 0x2270
cmpl $0x0, %eax
jne 0x26ba
jmp 0x261c
jmp 0x26bc
movl 0x430(%rsp), %eax
movl %eax, 0xc(%rsp)
leaq 0x20(%rsp), %rax
movq %rax, 0x10(%rsp)
leaq 0x20(%rsp), %rdi
callq 0x20f0
movl 0xc(%rsp), %edi
movq 0x10(%rsp), %rsi
movq %rax, %rdx
callq 0x3bb0
movq %rax, 0x18(%rsp)
movl 0x430(%rsp), %edi
leaq 0x20(%rsp), %rsi
movl $0x400, %edx            # imm = 0x400
callq 0x2280
movq %rax, 0x18(%rsp)
cmpq $0x0, 0x18(%rsp)
jge 0x271b
xorl %edi, %edi
callq 0x2240
movq 0x18(%rsp), %rax
movb $0x0, 0x20(%rsp,%rax)
leaq 0x20(%rsp), %rsi
movq 0x489f(%rip), %rdi      # 0x6fd0
callq 0x2200
jmp 0x261c
movl 0x430(%rsp), %edi
callq 0x2310
movl 0x444(%rsp), %eax
addq $0x448, %rsp            # imm = 0x448
retq
nopw %cs:(%rax,%rax)