// File: /workspace/github_c_makefile_star2_O0/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
subq $0x70, %rsp
movw %si, %ax
movq %rdi, -0x10(%rbp)
movw %ax, -0x12(%rbp)
movq %rdx, -0x20(%rbp)
movl $0x0, -0x24(%rbp)
movl $0xd, %edi
movl $0x1, %esi
callq 0x2130
movl $0x2, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x21a0
movl %eax, -0x24(%rbp)
cmpl $0x0, %eax
jge 0xa74d
leaq 0x1d28(%rip), %rdi      # 0xc465
movb $0x0, %al
callq 0x2030
movb $-0x1, -0x1(%rbp)
jmp 0xa8b1
movw $0x2, -0x34(%rbp)
movzwl -0x12(%rbp), %edi
callq 0x21f0
movw %ax, -0x32(%rbp)
movq -0x10(%rbp), %rsi
leaq -0x34(%rbp), %rdx
addq $0x4, %rdx
movl $0x2, %edi
callq 0x22d0
cmpl $0x0, %eax
jg 0xa792
leaq 0x1cfa(%rip), %rdi      # 0xc47c
movb $0x0, %al
callq 0x2030
movb $0x1, -0x1(%rbp)
jmp 0xa8b1
movq -0x10(%rbp), %rsi
movzwl -0x12(%rbp), %edx
leaq 0x1d09(%rip), %rdi      # 0xc4aa
movb $0x0, %al
callq 0x2030
movl -0x24(%rbp), %edi
leaq -0x34(%rbp), %rsi
movl $0x10, %edx
callq 0x2140
cmpl $0x0, %eax
jge 0xa7d5
leaq 0x1cfd(%rip), %rdi      # 0xc4c2
movb $0x0, %al
callq 0x2030
movb $0x1, -0x1(%rbp)
jmp 0xa8b1
leaq 0x1cfa(%rip), %rdi      # 0xc4d6
movb $0x0, %al
callq 0x2030
movl $0x10, %edi
callq 0x2370
movq %rax, -0x40(%rbp)
movl $0x800, %edi            # imm = 0x800
callq 0x2370
movq %rax, %rcx
movq -0x40(%rbp), %rax
movq %rcx, 0x8(%rax)
leaq 0x1cd5(%rip), %rdi      # 0xc4e2
movb $0x0, %al
callq 0x2030
movq -0x40(%rbp), %rax
movq 0x8(%rax), %rdi
movq 0x57cd(%rip), %rax      # 0xfff0
movq (%rax), %rdx
movl $0x7ff, %esi            # imm = 0x7FF
callq 0x21c0
movq -0x40(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x2160
movl %eax, %ecx
movq -0x40(%rbp), %rax
movl %ecx, (%rax)
movl -0x24(%rbp), %edi
movq -0x40(%rbp), %rsi
movl $0x4, %edx
xorl %ecx, %ecx
callq 0x2120
movl -0x24(%rbp), %edi
leaq -0x62(%rbp), %rsi
movl $0x2, %edx
callq 0x2220
movb $0x0, -0x60(%rbp)
leaq -0x62(%rbp), %rsi
leaq 0x1c6e(%rip), %rdi      # 0xc4e6
callq 0x2250
cmpl $0x0, %eax
jne 0xa89e
movl -0x24(%rbp), %edi
movq -0x40(%rbp), %rax
movq 0x8(%rax), %rsi
movq -0x40(%rbp), %rax
movl (%rax), %eax
movl %eax, %edx
xorl %ecx, %ecx
callq 0x2120
jmp 0xa8ac
leaq 0x1c44(%rip), %rdi      # 0xc4e9
movb $0x0, %al
callq 0x2030
jmp 0xa806
movb -0x1(%rbp), %al
addq $0x70, %rsp
popq %rbp
retq
nopw (%rax,%rax)