// File: /workspace/github_c_makefile_star2_O2/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
pushq $0xd
popq %rdi
pushq $0x1
popq %rsi
callq 0x2110
pushq $0x2
popq %rdi
pushq $0x1
popq %rsi
xorl %edx, %edx
callq 0x2170
testl %eax, %eax
js 0x6864
movl %eax, %ebx
leaq -0x40(%rbp), %rdx
movw $0x2, -0x4(%rdx)
movl %r15d, %eax
rolw $0x8, %ax
movw %ax, -0x2(%rdx)
pushq $0x2
popq %rdi
movq %r14, %rsi
callq 0x22a0
testl %eax, %eax
jle 0x6874
movzwl %r15w, %edx
leaq 0xba7(%rip), %rdi       # 0x7330
movq %r14, %rsi
xorl %eax, %eax
callq 0x2030
leaq -0x44(%rbp), %rsi
pushq $0x10
popq %rdx
movl %ebx, %edi
callq 0x2120
testl %eax, %eax
js 0x687d
leaq 0xb9f(%rip), %rdi       # 0x734f
callq 0x22b0
pushq $0x10
popq %rdi
callq 0x2360
movq %rax, %r14
movl $0x800, %edi            # imm = 0x800
callq 0x2360
movq %rax, 0x8(%r14)
leaq -0x32(%rbp), %r13
leaq 0xb73(%rip), %r12       # 0x734c
pushq $0x3
popq %r15
leaq 0xb64(%rip), %rdi       # 0x7348
xorl %eax, %eax
callq 0x2030
movq 0x8(%r14), %rdi
movq 0x47fa(%rip), %rax      # 0xaff0
movq (%rax), %rdx
movl $0x7ff, %esi            # imm = 0x7FF
callq 0x2190
movq 0x8(%r14), %rdi
callq 0x2140
movl %eax, (%r14)
movl %ebx, %edi
movq %r14, %rsi
pushq $0x4
popq %rdx
xorl %ecx, %ecx
callq 0x2100
movl %ebx, %edi
movq %r13, %rsi
pushq $0x2
popq %rdx
callq 0x21f0
movb $0x0, -0x30(%rbp)
movq %r12, %rdi
movq %r13, %rsi
movq %r15, %rdx
callq 0x21c0
testl %eax, %eax
je 0x684f
leaq 0xb12(%rip), %rdi       # 0x735a
callq 0x22b0
jmp 0x67dd
movq 0x8(%r14), %rsi
movl (%r14), %edx
movl %ebx, %edi
xorl %ecx, %ecx
callq 0x2100
jmp 0x67dd
leaq 0xb3e(%rip), %rdi       # 0x73a9
callq 0x22b0
movb $-0x1, %al
jmp 0x688b
leaq 0xb01(%rip), %rdi       # 0x737c
jmp 0x6884
leaq 0xae5(%rip), %rdi       # 0x7369
callq 0x22b0
movb $0x1, %al
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop