// File: /workspace/github_c_makefile_star2_O0/TimeToogo[P]ff-proxy/src/http.c
// Function: ff_http_send_request_unencrypted
bool ff_http_send_request_unencrypted(struct ff_request *request, char *host_name)
{
    bool ret;
    struct addrinfo hints;
    struct addrinfo *res = NULL;
    char formatted_address[INET6_ADDRSTRLEN] = {0};
    struct timeval timeout = {.tv_sec = FF_HTTP_RESPONSE_MAX_WAIT_SECS, .tv_usec = 0};
    int err;
    int sockfd = 0;
    ssize_t chunk = 0;
    uint32_t sent = 0;
    uint32_t received = 0;
    char response[FF_HTTP_RESPONSE_BUFF_SIZE] = {0};

    if (host_name == NULL)
    {
        goto error;
    }

    ff_log(FF_DEBUG, "Performing DNS lookup of %s", host_name);
    memset(&hints, 0, sizeof(hints));
    hints.ai_family = AF_UNSPEC;
    hints.ai_socktype = SOCK_STREAM;
    err = getaddrinfo(host_name, "80", &hints, &res);
    if (err)
    {
        ff_log(FF_WARNING, "Failed to perform DNS lookup for host: %s", host_name);
        goto error;
    }

    // TODO: filter out private IP ranges

    sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
    if (sockfd < 0)
    {
        ff_log(FF_ERROR, "Failed to open socket");
        goto error;
    }

    setsockopt(sockfd, SOL_SOCKET, SO_RCVTIMEO, (void *)&timeout, sizeof(timeout));

    if (res->ai_family == AF_INET)
    {
        inet_ntop(AF_INET, &((struct sockaddr_in *)res->ai_addr)->sin_addr, formatted_address, INET_ADDRSTRLEN);
    }
    else
    {
        inet_ntop(AF_INET6, &((struct sockaddr_in6 *)res->ai_addr)->sin6_addr, formatted_address, INET6_ADDRSTRLEN);
    }

    ff_log(FF_DEBUG, "Resolved host %s to ip address %s", host_name, formatted_address);

    err = connect(sockfd, res->ai_addr, res->ai_addrlen);
    if (err)
    {
        ff_log(FF_WARNING, "Failed to connect to host: %s", host_name);
        goto error;
    }

    do
    {
        chunk = write(sockfd, request->payload->value + sent, request->payload_length - sent);

        if (chunk < 0)
        {
            ff_log(FF_WARNING, "Failed to write to socket: %s (%d bytes remaining)", host_name, request->payload_length - sent);
            goto error;
        }

        if (chunk == 0)
        {
            break;
        }

        sent += (uint32_t)chunk;
    } while (sent < request->payload_length);

    ff_log(FF_DEBUG, "Finished sending request to %s over HTTP (%d bytes sent)", host_name, sent);

    do
    {
        chunk = recv(sockfd, response + received, sizeof(response) - received - 1, 0);

        if (chunk < 0)
        {
            ff_log(FF_WARNING, "Failed to read response from socket for host: %s (%d bytes received)", host_name, received);
            goto error;
        }

        if (chunk == 0)
        {
            break;
        }

        received += (uint32_t)chunk;

        if (received > 0 && strncasecmp(response, "HTTP/", 5) == 0)
        {
            break;
        }
    } while (received < sizeof(response) - 1);

    ff_log(FF_DEBUG, "Finished receiving response from %s (%d bytes received)", host_name, received);
    size_t response_header_length = strchr((char *)response, '\n') - response;
    ff_log(FF_DEBUG, "Response: %.*s", response_header_length > 100l ? 100 : (int)response_header_length, response);
    goto done;

error:
    ret = false;
    goto cleanup;

done:
    ret = true;
    goto cleanup;

cleanup:
    if (res != NULL)
    {
        freeaddrinfo(res);
    }

    if (sockfd != 0)
    {
        close(sockfd);
    }

    return ret;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x10d0, %rsp           # imm = 0x10D0
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq $0x0, -0x50(%rbp)
leaq -0x80(%rbp), %rdi
xorl %esi, %esi
movl $0x2e, %edx
callq 0x21a0
movq 0x2ede(%rip), %rax      # 0x77f0
movq %rax, -0x90(%rbp)
movq 0x2ed8(%rip), %rax      # 0x77f8
movq %rax, -0x88(%rbp)
movl $0x0, -0x98(%rbp)
movq $0x0, -0xa0(%rbp)
movl $0x0, -0xa4(%rbp)
movl $0x0, -0xa8(%rbp)
leaq -0x10b0(%rbp), %rdi
xorl %esi, %esi
movl $0x1000, %edx           # imm = 0x1000
callq 0x21a0
cmpq $0x0, -0x10(%rbp)
jne 0x496f
jmp 0x4d05
movq -0x10(%rbp), %rdx
movl $0x1, %edi
leaq 0x2e81(%rip), %rsi      # 0x7800
movb $0x0, %al
callq 0x4d70
leaq -0x48(%rbp), %rdi
xorl %esi, %esi
movl $0x30, %edx
callq 0x21a0
movl $0x0, -0x44(%rbp)
movl $0x1, -0x40(%rbp)
movq -0x10(%rbp), %rdi
leaq 0x2e6d(%rip), %rsi      # 0x781c
leaq -0x48(%rbp), %rdx
leaq -0x50(%rbp), %rcx
callq 0x2380
movl %eax, -0x94(%rbp)
cmpl $0x0, -0x94(%rbp)
je 0x49e7
movq -0x10(%rbp), %rdx
movl $0x3, %edi
leaq 0x2e44(%rip), %rsi      # 0x781f
movb $0x0, %al
callq 0x4d70
jmp 0x4d05
movq -0x50(%rbp), %rax
movl 0x4(%rax), %edi
movq -0x50(%rbp), %rax
movl 0x8(%rax), %esi
movq -0x50(%rbp), %rax
movl 0xc(%rax), %edx
callq 0x20a0
movl %eax, -0x98(%rbp)
cmpl $0x0, -0x98(%rbp)
jge 0x4a28
movl $0x4, %edi
leaq 0x2e2d(%rip), %rsi      # 0x7849
movb $0x0, %al
callq 0x4d70
jmp 0x4d05
movl -0x98(%rbp), %edi
movl $0x1, %esi
movl $0x14, %edx
leaq -0x90(%rbp), %rcx
movl $0x10, %r8d
callq 0x24a0
movq -0x50(%rbp), %rax
cmpl $0x2, 0x4(%rax)
jne 0x4a75
movq -0x50(%rbp), %rax
movq 0x18(%rax), %rsi
addq $0x4, %rsi
leaq -0x80(%rbp), %rdx
movl $0x2, %edi
movl $0x10, %ecx
callq 0x22b0
jmp 0x4a94
movq -0x50(%rbp), %rax
movq 0x18(%rax), %rsi
addq $0x8, %rsi
leaq -0x80(%rbp), %rdx
movl $0xa, %edi
movl $0x2e, %ecx
callq 0x22b0
movq -0x10(%rbp), %rdx
leaq -0x80(%rbp), %rcx
movl $0x1, %edi
leaq 0x2db7(%rip), %rsi      # 0x785f
movb $0x0, %al
callq 0x4d70
movl -0x98(%rbp), %edi
movq -0x50(%rbp), %rax
movq 0x18(%rax), %rax
movq %rax, -0x10b8(%rbp)
movq -0x50(%rbp), %rax
movl 0x10(%rax), %edx
movq -0x10b8(%rbp), %rsi
callq 0x2280
movl %eax, -0x94(%rbp)
cmpl $0x0, -0x94(%rbp)
je 0x4b02
movq -0x10(%rbp), %rdx
movl $0x3, %edi
leaq 0x2d8b(%rip), %rsi      # 0x7881
movb $0x0, %al
callq 0x4d70
jmp 0x4d05
jmp 0x4b04
movl -0x98(%rbp), %edi
movq -0x8(%rbp), %rax
movq 0x50(%rax), %rax
movq 0x8(%rax), %rsi
movl -0xa4(%rbp), %eax
addq %rax, %rsi
movq -0x8(%rbp), %rax
movq 0x40(%rax), %rdx
movl -0xa4(%rbp), %eax
subq %rax, %rdx
callq 0x20f0
movq %rax, -0xa0(%rbp)
cmpq $0x0, -0xa0(%rbp)
jge 0x4b73
movq -0x10(%rbp), %rdx
movq -0x8(%rbp), %rax
movq 0x40(%rax), %rcx
movl -0xa4(%rbp), %eax
subq %rax, %rcx
movl $0x3, %edi
leaq 0x2d38(%rip), %rsi      # 0x789f
movb $0x0, %al
callq 0x4d70
jmp 0x4d05
cmpq $0x0, -0xa0(%rbp)
jne 0x4b7f
jmp 0x4ba6
movq -0xa0(%rbp), %rax
addl -0xa4(%rbp), %eax
movl %eax, -0xa4(%rbp)
movl -0xa4(%rbp), %eax
movq -0x8(%rbp), %rcx
cmpq 0x40(%rcx), %rax
jb 0x4b04
movq -0x10(%rbp), %rdx
movl -0xa4(%rbp), %ecx
movl $0x1, %edi
leaq 0x2d16(%rip), %rsi      # 0x78d2
movb $0x0, %al
callq 0x4d70
movl -0x98(%rbp), %edi
leaq -0x10b0(%rbp), %rsi
movl -0xa8(%rbp), %eax
addq %rax, %rsi
movl -0xa8(%rbp), %eax
movl $0x1000, %edx           # imm = 0x1000
subq %rax, %rdx
subq $0x1, %rdx
xorl %ecx, %ecx
callq 0x20d0
movq %rax, -0xa0(%rbp)
cmpq $0x0, -0xa0(%rbp)
jge 0x4c25
movq -0x10(%rbp), %rdx
movl -0xa8(%rbp), %ecx
movl $0x3, %edi
leaq 0x2cf2(%rip), %rsi      # 0x790b
movb $0x0, %al
callq 0x4d70
jmp 0x4d05
cmpq $0x0, -0xa0(%rbp)
jne 0x4c31
jmp 0x4c80
movq -0xa0(%rbp), %rax
addl -0xa8(%rbp), %eax
movl %eax, -0xa8(%rbp)
cmpl $0x0, -0xa8(%rbp)
jbe 0x4c6c
leaq -0x10b0(%rbp), %rdi
leaq 0x2cf5(%rip), %rsi      # 0x7950
movl $0x5, %edx
callq 0x2390
cmpl $0x0, %eax
jne 0x4c6c
jmp 0x4c80
jmp 0x4c6e
movl -0xa8(%rbp), %eax
cmpq $0xfff, %rax            # imm = 0xFFF
jb 0x4bc3
movq -0x10(%rbp), %rdx
movl -0xa8(%rbp), %ecx
movl $0x1, %edi
leaq 0x2cc0(%rip), %rsi      # 0x7956
movb $0x0, %al
callq 0x4d70
leaq -0x10b0(%rbp), %rdi
movl $0xa, %esi
callq 0x2100
leaq -0x10b0(%rbp), %rcx
subq %rcx, %rax
movq %rax, -0x10c0(%rbp)
cmpq $0x64, -0x10c0(%rbp)
jbe 0x4cd6
movl $0x64, %eax
movl %eax, -0x10c4(%rbp)
jmp 0x4ce3
movq -0x10c0(%rbp), %rax
movl %eax, -0x10c4(%rbp)
movl -0x10c4(%rbp), %edx
leaq -0x10b0(%rbp), %rcx
movl $0x1, %edi
leaq 0x2c92(%rip), %rsi      # 0x798e
movb $0x0, %al
callq 0x4d70
jmp 0x4d0b
movb $0x0, -0x11(%rbp)
jmp 0x4d0f
movb $0x1, -0x11(%rbp)
cmpq $0x0, -0x50(%rbp)
je 0x4d1f
movq -0x50(%rbp), %rdi
callq 0x23b0
cmpl $0x0, -0x98(%rbp)
je 0x4d33
movl -0x98(%rbp), %edi
callq 0x2520
movb -0x11(%rbp), %al
andb $0x1, %al
addq $0x10d0, %rsp           # imm = 0x10D0
popq %rbp
retq
nopw %cs:(%rax,%rax)
nopl (%rax,%rax)