// File: /workspace/github_c_makefile_star2_O0/TimeToogo[P]ff-proxy/src/http.c
// Function: ff_http_get_destination_host
char *ff_http_get_destination_host(struct ff_request *request)
{
    // @see https://stackoverflow.com/questions/8724954/what-is-the-maximum-number-of-characters-for-a-host-name-in-unix
    char *host_name = malloc(_POSIX_HOST_NAME_MAX + 1);
    char *http_request = (char *)request->payload->value;
    char *line = http_request;
    uint64_t payload_len = request->payload_length;
    uint64_t i = 0;

#define FF_HTTP_DESTINATION_CONSUME_CHAR(x)                                                          \
    line += (x);                                                                                     \
    i += (x);                                                                                        \
    if (i > payload_len || *line == '\0')                                                            \
    {                                                                                                \
        ff_log(FF_WARNING, "Encountered end of request payload before finding Host header");         \
        goto error;                                                                                  \
    }                                                                                                \
    if (i >= FF_HTTP_HOST_HEADER_MAX_SEARCH_LENGTH)                                                  \
    {                                                                                                \
        ff_log(FF_WARNING, "Reached char limit of request payload while searching for host header"); \
        goto error;                                                                                  \
    }

    if (payload_len == 0)
    {
        FF_HTTP_DESTINATION_CONSUME_CHAR(1); // Force error
    }

    while (1)
    {
        if (payload_len - i > 2 && line != http_request && (*line == '\n' || (*line == '\r' && *(line + 1) == '\n')))
        {
            // Subsequent new lines indicate request headers have finished
            ff_log(FF_WARNING, "Encountered end of request headers before finding Host header");
            goto error;
        }

        if (payload_len - i > 5 && strncasecmp(line, "host:", 5) == 0)
        {
            FF_HTTP_DESTINATION_CONSUME_CHAR(5);

            while (*line == ' ')
            {
                FF_HTTP_DESTINATION_CONSUME_CHAR(1);
            }

            int host_len = 0;

            // http://man7.org/linux/man-pages/man7/hostname.7.html
            while (i <= payload_len && (*line == '.' || *line == '-' || isalnum(*line)) && host_len <= 255)
            {
                *(host_name + host_len) = *line;
                line++;
                i++;
                host_len++;
            }

            *(host_name + host_len) = '\0';
            goto done;
        }

        // Advance to next line
        while (*line != '\n')
        {
            FF_HTTP_DESTINATION_CONSUME_CHAR(1);
        }

        FF_HTTP_DESTINATION_CONSUME_CHAR(1);
    }

error:
    FREE(host_name);
    return NULL;

done:
    ff_log(FF_DEBUG, "Found destination host: %s", host_name);
    return host_name;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x40, %rsp
movq %rdi, -0x10(%rbp)
movl $0x100, %edi            # imm = 0x100
callq 0x23e0
movq %rax, -0x18(%rbp)
movq -0x10(%rbp), %rax
movq 0x50(%rax), %rax
movq 0x8(%rax), %rax
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0x28(%rbp)
movq -0x10(%rbp), %rax
movq 0x40(%rax), %rax
movq %rax, -0x30(%rbp)
movq $0x0, -0x38(%rbp)
cmpq $0x0, -0x30(%rbp)
jne 0x4007
movq -0x28(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x28(%rbp)
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
cmpq -0x30(%rbp), %rax
ja 0x3fcb
movq -0x28(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x0, %eax
jne 0x3fe3
movl $0x3, %edi
leaq 0x3c17(%rip), %rsi      # 0x7bee
movb $0x0, %al
callq 0x4d70
jmp 0x42f9
cmpq $0x1fa0, -0x38(%rbp)    # imm = 0x1FA0
jb 0x4005
movl $0x3, %edi
leaq 0x3c33(%rip), %rsi      # 0x7c2c
movb $0x0, %al
callq 0x4d70
jmp 0x42f9
jmp 0x4007
jmp 0x4009
movq -0x30(%rbp), %rax
subq -0x38(%rbp), %rax
cmpq $0x2, %rax
jbe 0x405e
movq -0x28(%rbp), %rax
cmpq -0x20(%rbp), %rax
je 0x405e
movq -0x28(%rbp), %rax
movsbl (%rax), %eax
cmpl $0xa, %eax
je 0x4046
movq -0x28(%rbp), %rax
movsbl (%rax), %eax
cmpl $0xd, %eax
jne 0x405e
movq -0x28(%rbp), %rax
movsbl 0x1(%rax), %eax
cmpl $0xa, %eax
jne 0x405e
movl $0x3, %edi
leaq 0x3c20(%rip), %rsi      # 0x7c72
movb $0x0, %al
callq 0x4d70
jmp 0x42f9
movq -0x30(%rbp), %rax
subq -0x38(%rbp), %rax
cmpq $0x5, %rax
jbe 0x421d
movq -0x28(%rbp), %rdi
leaq 0x3c35(%rip), %rsi      # 0x7cb0
movl $0x5, %edx
callq 0x2390
cmpl $0x0, %eax
jne 0x421d
movq -0x28(%rbp), %rax
addq $0x5, %rax
movq %rax, -0x28(%rbp)
movq -0x38(%rbp), %rax
addq $0x5, %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
cmpq -0x30(%rbp), %rax
ja 0x40bc
movq -0x28(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x0, %eax
jne 0x40d4
movl $0x3, %edi
leaq 0x3b26(%rip), %rsi      # 0x7bee
movb $0x0, %al
callq 0x4d70
jmp 0x42f9
cmpq $0x1fa0, -0x38(%rbp)    # imm = 0x1FA0
jb 0x40f6
movl $0x3, %edi
leaq 0x3b42(%rip), %rsi      # 0x7c2c
movb $0x0, %al
callq 0x4d70
jmp 0x42f9
jmp 0x40f8
movq -0x28(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x20, %eax
jne 0x416e
movq -0x28(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x28(%rbp)
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
cmpq -0x30(%rbp), %rax
ja 0x4132
movq -0x28(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x0, %eax
jne 0x414a
movl $0x3, %edi
leaq 0x3ab0(%rip), %rsi      # 0x7bee
movb $0x0, %al
callq 0x4d70
jmp 0x42f9
cmpq $0x1fa0, -0x38(%rbp)    # imm = 0x1FA0
jb 0x416c
movl $0x3, %edi
leaq 0x3acc(%rip), %rsi      # 0x7c2c
movb $0x0, %al
callq 0x4d70
jmp 0x42f9
jmp 0x40f8
movl $0x0, -0x3c(%rbp)
movq -0x38(%rbp), %rcx
xorl %eax, %eax
cmpq -0x30(%rbp), %rcx
movb %al, -0x3d(%rbp)
ja 0x41cc
movq -0x28(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x2e, %eax
je 0x41bf
movq -0x28(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x2d, %eax
je 0x41bf
callq 0x22c0
movq (%rax), %rax
movq -0x28(%rbp), %rcx
movsbl (%rcx), %ecx
movslq %ecx, %rcx
movzwl (%rax,%rcx,2), %ecx
andl $0x8, %ecx
xorl %eax, %eax
cmpl $0x0, %ecx
movb %al, -0x3d(%rbp)
je 0x41cc
cmpl $0xff, -0x3c(%rbp)
setle %al
movb %al, -0x3d(%rbp)
movb -0x3d(%rbp), %al
testb $0x1, %al
jne 0x41d5
jmp 0x420c
movq -0x28(%rbp), %rax
movb (%rax), %dl
movq -0x18(%rbp), %rax
movslq -0x3c(%rbp), %rcx
movb %dl, (%rax,%rcx)
movq -0x28(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x28(%rbp)
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
movl -0x3c(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x3c(%rbp)
jmp 0x4175
movq -0x18(%rbp), %rax
movslq -0x3c(%rbp), %rcx
movb $0x0, (%rax,%rcx)
jmp 0x4316
jmp 0x421f
movq -0x28(%rbp), %rax
movsbl (%rax), %eax
cmpl $0xa, %eax
je 0x4292
movq -0x28(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x28(%rbp)
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
cmpq -0x30(%rbp), %rax
ja 0x4259
movq -0x28(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x0, %eax
jne 0x4271
movl $0x3, %edi
leaq 0x3989(%rip), %rsi      # 0x7bee
movb $0x0, %al
callq 0x4d70
jmp 0x42f9
cmpq $0x1fa0, -0x38(%rbp)    # imm = 0x1FA0
jb 0x4290
movl $0x3, %edi
leaq 0x39a5(%rip), %rsi      # 0x7c2c
movb $0x0, %al
callq 0x4d70
jmp 0x42f9
jmp 0x421f
movq -0x28(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x28(%rbp)
movq -0x38(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rax
cmpq -0x30(%rbp), %rax
ja 0x42c0
movq -0x28(%rbp), %rax
movsbl (%rax), %eax
cmpl $0x0, %eax
jne 0x42d5
movl $0x3, %edi
leaq 0x3922(%rip), %rsi      # 0x7bee
movb $0x0, %al
callq 0x4d70
jmp 0x42f9
cmpq $0x1fa0, -0x38(%rbp)    # imm = 0x1FA0
jb 0x42f4
movl $0x3, %edi
leaq 0x3941(%rip), %rsi      # 0x7c2c
movb $0x0, %al
callq 0x4d70
jmp 0x42f9
jmp 0x4009
jmp 0x42fb
movq -0x18(%rbp), %rdi
callq 0x2350
movq $0x0, -0x18(%rbp)
movq $0x0, -0x8(%rbp)
jmp 0x4335
movq -0x18(%rbp), %rdx
movl $0x1, %edi
leaq 0x3990(%rip), %rsi      # 0x7cb6
movb $0x0, %al
callq 0x4d70
movq -0x18(%rbp), %rax
movq %rax, -0x8(%rbp)
movq -0x8(%rbp), %rax
addq $0x40, %rsp
popq %rbp
retq
nop