// File: /workspace/github_c_makefile_star2_O3/ammarfaizi2[P]riyuu/src/riyuu/client/riyuu_client.c
// Function: riyuu_client_connect(char*, unsigned short, char**)
uint8_t riyuu_client_connect(char *host_addr, uint16_t port, char **error)
{
    int sock = 0;
    struct sockaddr_in serv_addr;
    riyuu_data *data;
    size_t inlen;
    size_t reqlen;
    size_t reslen;

    signal(SIGPIPE, SIG_IGN);

    if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)  { 
        printf("Socket creation error\n");
        return -1; 
    }

    serv_addr.sin_family = AF_INET;
    serv_addr.sin_port = htons(port);

    if (inet_pton(AF_INET, host_addr, &serv_addr.sin_addr) <= 0) {
        printf("Invalid address or address is not supported.\n");
        return 1;
    }

    printf("Connecting to %s:%d...\n", host_addr, port);
    if (connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
        printf("Connection failed!\n");
        return 1;
    }
    printf("Connected!\n");

    data = (riyuu_data *)malloc(sizeof(riyuu_data));
    data->data = (char *)malloc(sizeof(char) * 2048);

    while (1) {
        char buff[10];
        printf(">> ");
        fgets(data->data, 2047, stdin);
        data->data_size = (uint32_t)strlen(data->data);

        send(sock, &(data->data_size), sizeof(uint32_t), 0);
        read(sock, buff, 2);
        buff[2] = 0;

        if (!strcmp("OK", buff)) {
            send(sock, data->data, data->data_size, 0);
        } else {
            printf("Response error\n");
        }
    }
    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x28, %rsp
movl %esi, %r15d
movq %rdi, %r14
movl $0x1, %esi
movl $0xd, %edi
callq 0x2110
movl $0x2, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x2170
testl %eax, %eax
js 0x72d2
movl %eax, %ebx
leaq -0x40(%rbp), %rdx
movw $0x2, -0x4(%rdx)
movl %r15d, %eax
rolw $0x8, %ax
movw %ax, -0x2(%rdx)
movl $0x2, %edi
movq %r14, %rsi
callq 0x2290
testl %eax, %eax
jle 0x72e2
movzwl %r15w, %edx
leaq 0x112d(%rip), %rdi      # 0x8330
movq %r14, %rsi
xorl %eax, %eax
callq 0x2030
leaq -0x44(%rbp), %rsi
movl %ebx, %edi
movl $0x10, %edx
callq 0x2120
testl %eax, %eax
js 0x72eb
leaq 0x1123(%rip), %rdi      # 0x834f
callq 0x22a0
movl $0x10, %edi
callq 0x2350
movq %rax, %r14
movl $0x800, %edi            # imm = 0x800
callq 0x2350
movq %rax, 0x8(%r14)
leaq 0x10f5(%rip), %r15      # 0x8348
movq 0x4d96(%rip), %r13      # 0xbff0
leaq -0x32(%rbp), %r12
movq %r15, %rdi
xorl %eax, %eax
callq 0x2030
movq 0x8(%r14), %rdi
movq (%r13), %rdx
movl $0x7ff, %esi            # imm = 0x7FF
callq 0x2190
movq 0x8(%r14), %rdi
callq 0x2140
movl %eax, (%r14)
movl $0x4, %edx
movl %ebx, %edi
movq %r14, %rsi
xorl %ecx, %ecx
callq 0x2100
movl $0x2, %edx
movl %ebx, %edi
movq %r12, %rsi
callq 0x21e0
movb $0x0, -0x30(%rbp)
cmpw $0x4b4f, -0x32(%rbp)    # imm = 0x4B4F
je 0x72c0
leaq 0x10a1(%rip), %rdi      # 0x835a
callq 0x22a0
jmp 0x725e
movq 0x8(%r14), %rsi
movl (%r14), %edx
movl %ebx, %edi
xorl %ecx, %ecx
callq 0x2100
jmp 0x725e
leaq 0x10d0(%rip), %rdi      # 0x83a9
callq 0x22a0
movb $-0x1, %al
jmp 0x72f9
leaq 0x1093(%rip), %rdi      # 0x837c
jmp 0x72f2
leaq 0x1077(%rip), %rdi      # 0x8369
callq 0x22a0
movb $0x1, %al
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq