// File: /workspace/github_cpp_makefile_star2_O1/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 $0x418, %rsp            # imm = 0x418
cmpl $0x1, %edi
je 0x24c4
cmpl $0x2, %edi
jne 0x24ca
movq 0x8(%rsi), %rdi
callq 0x2250
movl %eax, %ebp
jmp 0x24d4
movw $0x270f, %bp            # imm = 0x270F
jmp 0x24d4
movw $0xffff, %bp            # imm = 0xFFFF
jg 0x262f
movl $0x2, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x2060
cmpl $-0x1, %eax
je 0x262a
movl %eax, %ebx
movq %rsp, %r14
movl $0x10, %esi
movq %r14, %rdi
callq 0x22a0
movw $0x2, (%r14)
movl $0x0, 0x4(%r14)
rolw $0x8, %bp
movw %bp, 0x2(%r14)
movl %ebx, %edi
movq %r14, %rsi
callq 0x2cd4
movq 0x3aa8(%rip), %r14      # 0x5fd0
leaq 0x1b00(%rip), %rsi      # 0x402f
movl $0x31, %edx
movq %r14, %rdi
callq 0x21f0
movq 0x3a85(%rip), %rbp      # 0x5fc8
leaq 0x10(%rsp), %r12
movl $0xa71, %r13d           # imm = 0xA71
movl $0xa51, %r15d           # imm = 0xA51
movl $0xa, %edx
movq %r14, %rdi
leaq 0x1afe(%rip), %rsi      # 0x4061
callq 0x21f0
movq (%rbp), %rdx
movq %r12, %rdi
movl $0x400, %esi            # imm = 0x400
callq 0x2260
testq %rax, %rax
jne 0x258b
movq (%rbp), %rdi
callq 0x22c0
testl %eax, %eax
jne 0x25fe
movl 0x10(%rsp), %eax
xorl %r13d, %eax
movzbl 0x12(%rsp), %ecx
orw %ax, %cx
je 0x260f
movl 0x10(%rsp), %eax
xorl %r15d, %eax
movzbl 0x12(%rsp), %ecx
orw %ax, %cx
je 0x260f
cmpw $0xa, 0x10(%rsp)
je 0x2554
movq %r12, %rdi
callq 0x2100
movl %ebx, %edi
movq %r12, %rsi
movq %rax, %rdx
callq 0x31b4
movl $0x400, %edx            # imm = 0x400
movl %ebx, %edi
movq %r12, %rsi
callq 0x2220
testq %rax, %rax
js 0x2642
movb $0x0, 0x10(%rsp,%rax)
movq %r12, %rdi
callq 0x2100
movq %r14, %rdi
movq %r12, %rsi
movq %rax, %rdx
callq 0x21f0
jmp 0x2554
leaq 0x1a67(%rip), %rdi      # 0x406c
callq 0x2180
jmp 0x258b
movl %ebx, %edi
callq 0x22d0
xorl %eax, %eax
addq $0x418, %rsp            # imm = 0x418
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x22f0
movq 0x39ba(%rip), %rdi      # 0x5ff0
leaq 0x19c7(%rip), %rsi      # 0x4004
callq 0x21d0
xorl %edi, %edi
callq 0x2200
nopl (%rax)