// File: /workspace/github_c_makefile_star2_O1/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, %r12d
movq %rdi, %r15
movl $0x1, %esi
movl $0xd, %edi
callq 0x2110
movl $0x2, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x2170
testl %eax, %eax
js 0x7465
movl %eax, %ebx
leaq -0x40(%rbp), %rdx
movw $0x2, -0x4(%rdx)
movl %r12d, %eax
rolw $0x8, %ax
movw %ax, -0x2(%rdx)
movl $0x2, %edi
movq %r15, %rsi
callq 0x2290
movb $0x1, %r14b
testl %eax, %eax
jle 0x7471
movzwl %r12w, %edx
leaq 0xfa2(%rip), %rdi       # 0x8338
movq %r15, %rsi
xorl %eax, %eax
callq 0x2030
leaq -0x44(%rbp), %rsi
movl %ebx, %edi
movl $0x10, %edx
callq 0x2120
testl %eax, %eax
js 0x747a
leaq 0xf98(%rip), %rdi       # 0x8357
callq 0x22a0
movl $0x10, %edi
callq 0x2350
movq %rax, %r14
movl $0x800, %edi            # imm = 0x800
callq 0x2350
movq %rax, 0x8(%r14)
leaq 0xf6a(%rip), %r15       # 0x8350
movq 0x4c03(%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 0x7453
leaq 0xf16(%rip), %rdi       # 0x8362
callq 0x22a0
jmp 0x73f1
movq 0x8(%r14), %rsi
movl (%r14), %edx
movl %ebx, %edi
xorl %ecx, %ecx
callq 0x2100
jmp 0x73f1
movb $-0x1, %r14b
leaq 0xf42(%rip), %rdi       # 0x83b1
jmp 0x7481
leaq 0xf0c(%rip), %rdi       # 0x8384
jmp 0x7481
leaq 0xef0(%rip), %rdi       # 0x8371
callq 0x22a0
movl %r14d, %eax
addq $0x28, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq