// File: /workspace/github_cpp_makefile_star2_O2/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x428, %rsp            # imm = 0x428
cmpl $0x1, %edi
je 0x2494
cmpl $0x2, %edi
jne 0x249a
movq 0x8(%rsi), %rdi
callq 0x2240
movl %eax, %ebx
jmp 0x24a4
movw $0x270f, %bx            # imm = 0x270F
jmp 0x24a4
movw $0xffff, %bx            # imm = 0xFFFF
jg 0x25f3
pushq $0x2
popq %rdi
pushq $0x1
popq %rsi
xorl %edx, %edx
callq 0x2060
cmpl $-0x1, %eax
je 0x25e5
movl %eax, %ebp
leaq 0x10(%rsp), %r14
pushq $0x10
popq %rsi
movq %r14, %rdi
callq 0x2280
movw $0x2, (%r14)
andl $0x0, 0x4(%r14)
rolw $0x8, %bx
movw %bx, 0x2(%r14)
movl %ebp, 0xc(%rsp)
movl %ebp, %edi
movq %r14, %rsi
callq 0x2ac2
leaq 0xb3a(%rip), %rsi       # 0x302f
movq 0x2ad4(%rip), %r14      # 0x4fd0
movq 0x2ac5(%rip), %r15      # 0x4fc8
leaq 0x20(%rsp), %r12
leaq 0xb69(%rip), %rbp       # 0x3078
pushq $0x3
popq %r13
leaq 0xb61(%rip), %rbx       # 0x307b
movq %r14, %rdi
callq 0x21c0
movq %r14, %rdi
leaq 0xb35(%rip), %rsi       # 0x3061
callq 0x21c0
movq (%r15), %rdx
movq %r12, %rdi
movl $0x400, %esi            # imm = 0x400
callq 0x2250
testq %rax, %rax
jne 0x2552
movq (%r15), %rdi
callq 0x22a0
testl %eax, %eax
jne 0x2580
movq %r12, %rdi
movq %rbp, %rsi
movq %r13, %rdx
callq 0x21d0
testl %eax, %eax
je 0x25c8
movq %r12, %rdi
movq %rbx, %rsi
movq %r13, %rdx
callq 0x21d0
testl %eax, %eax
je 0x25c8
cmpw $0xa, 0x20(%rsp)
je 0x2522
jmp 0x258e
leaq 0xae5(%rip), %rdi       # 0x306c
callq 0x2180
jmp 0x2552
movq %r12, %rdi
callq 0x20f0
movl 0xc(%rsp), %edi
movq %r12, %rsi
movq %rax, %rdx
callq 0x2eec
movl $0x400, %edx            # imm = 0x400
movl 0xc(%rsp), %edi
movq %r12, %rsi
callq 0x2220
testq %rax, %rax
js 0x2606
movb $0x0, 0x20(%rsp,%rax)
movq %r12, %rsi
jmp 0x251a
movl 0xc(%rsp), %edi
callq 0x22b0
xorl %eax, %eax
addq $0x428, %rsp            # imm = 0x428
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xa34(%rip), %rdi       # 0x3020
callq 0x2180
jmp 0x2606
movq 0x29f6(%rip), %rdi      # 0x4ff0
leaq 0xa03(%rip), %rsi       # 0x3004
callq 0x21c0
xorl %edi, %edi
callq 0x21f0
nopl (%rax)