// File: /workspace/github_cpp_makefile_star2_O1/Computer-Kids-Club[P]screenshare-pro-backend/http.cpp
// Function: parse_header[abi:cxx11](char const*, char const*)
std::unordered_map<std::string, std::string> parse_header(const char *msg, const char *msg_end) {
    const char *head = msg;
    const char *tail = msg;

    std::unordered_map<std::string, std::string> http_request;

    // Find request type
    while (tail != msg_end && *tail != ' ')
        ++tail;
    http_request["Type"] = std::string(head, tail);

    // Find path
    while (tail != msg_end && *tail == ' ')
        ++tail;
    head = tail;
    while (tail != msg_end && *tail != ' ')
        ++tail;
    http_request["Path"] = std::string(head, tail);

    // Find HTTP version
    while (tail != msg_end && *tail == ' ')
        ++tail;
    head = tail;
    while (tail != msg_end && *tail != '\r')
        ++tail;
    http_request["Version"] = std::string(head, tail);
    if (tail != msg_end)
        ++tail;  // skip '\r'
    // TODO: what about the trailing '\n'?

    // Map all headers from a key to a value
    head = tail;
    while (head < msg_end && *head != '\r') {
        tail = head;
        while (tail < msg_end && *tail != '\r')
            ++tail;
        const char *colon = (const char *)memchr(head, ':', tail - head);
        if (colon == NULL) {
            // TODO: malformed headers, what should happen?
            break;
        }
        const char *value = colon + 1;
        while (value < tail && *value == ' ')
            ++value;
        if (head < colon && value < tail && tail <= msg_end) {
            http_request[std::string(head, colon)] = std::string(value, tail);
        }
        head = tail + 1;
        while (head < msg_end && (*head == '\n' || *head == '\r' || *head == ' '))
            head++;
        //printf("%d\n", head - msg);
        // TODO: what about the trailing '\n'?
    }

    return http_request;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x58, %rsp
leaq 0x30(%rdi), %rax
movq %rax, (%rdi)
movq $0x1, 0x8(%rdi)
xorps %xmm0, %xmm0
movups %xmm0, 0x10(%rdi)
movl $0x3f800000, 0x20(%rdi) # imm = 0x3F800000
movq %rdi, -0x70(%rbp)
movups %xmm0, 0x28(%rdi)
movq %rsi, %r15
cmpq %rdx, %rsi
je 0x5e4e
movq %rsi, %r15
cmpb $0x20, (%r15)
je 0x5e4e
incq %r15
cmpq %rdx, %r15
jne 0x5e3d
movq %rdx, %r15
movq %rdx, -0x78(%rbp)
leaq -0x38(%rbp), %rax
movq %rax, -0x10(%rax)
leaq -0x48(%rbp), %rdi
movq %r15, %rdx
callq 0x48f0
leaq -0x58(%rbp), %rax
movq %rax, -0x10(%rax)
leaq 0x1716(%rip), %rsi      # 0x758b
leaq 0x1713(%rip), %rdx      # 0x758f
leaq -0x68(%rbp), %rdi
callq 0x48f0
leaq -0x68(%rbp), %rsi
movq -0x70(%rbp), %rdi
callq 0x58d0
leaq -0x48(%rbp), %rsi
movq %rax, %rdi
callq 0x2280
movq -0x68(%rbp), %rdi
leaq -0x58(%rbp), %rax
cmpq %rax, %rdi
je 0x5eb7
movq -0x58(%rbp), %rsi
incq %rsi
callq 0x2200
movq -0x48(%rbp), %rdi
leaq -0x38(%rbp), %rax
cmpq %rax, %rdi
je 0x5ed0
movq -0x38(%rbp), %rsi
incq %rsi
callq 0x2200
movq -0x78(%rbp), %rax
cmpq %rax, %r15
je 0x5eea
cmpb $0x20, (%r15)
jne 0x5eea
incq %r15
cmpq %rax, %r15
jne 0x5ed9
movq %rax, %r15
movq %r15, %r12
cmpq %rax, %r15
je 0x5f07
movq %r15, %r12
cmpb $0x20, (%r12)
je 0x5f07
incq %r12
cmpq %rax, %r12
jne 0x5ef5
movq %rax, %r12
leaq -0x48(%rbp), %rdi
leaq -0x38(%rbp), %rax
movq %rax, (%rdi)
movq %r15, %rsi
movq %r12, %rdx
callq 0x48f0
leaq -0x68(%rbp), %rdi
leaq -0x58(%rbp), %rax
movq %rax, (%rdi)
leaq 0x1661(%rip), %rsi      # 0x7590
leaq 0x165e(%rip), %rdx      # 0x7594
callq 0x48f0
leaq -0x68(%rbp), %rsi
movq -0x70(%rbp), %rdi
callq 0x58d0
leaq -0x48(%rbp), %rsi
movq %rax, %rdi
callq 0x2280
movq -0x68(%rbp), %rdi
leaq -0x58(%rbp), %rax
cmpq %rax, %rdi
je 0x5f6d
movq -0x58(%rbp), %rsi
incq %rsi
callq 0x2200
movq -0x48(%rbp), %rdi
leaq -0x38(%rbp), %rax
cmpq %rax, %rdi
je 0x5f86
movq -0x38(%rbp), %rsi
incq %rsi
callq 0x2200
movq -0x78(%rbp), %rcx
cmpq %rcx, %r12
je 0x5fa1
cmpb $0x20, (%r12)
jne 0x5fa1
incq %r12
cmpq %rcx, %r12
jne 0x5f8f
movq %rcx, %r12
cmpq %rcx, %r12
setne %bl
je 0x5fd1
cmpb $0xd, (%r12)
movq %r12, %r15
je 0x5fd9
leaq 0x1(%r12), %rax
cmpq %rcx, %rax
setne %bl
je 0x5fd6
leaq 0x1(%rax), %r15
cmpb $0xd, (%rax)
movq %r15, %rax
jne 0x5fb8
decq %r15
jmp 0x5fd9
movq %r12, %r15
jmp 0x5fd9
movq %rcx, %r15
leaq -0x48(%rbp), %rdi
leaq -0x38(%rbp), %rax
movq %rax, (%rdi)
movq %r12, %rsi
movq %r15, %rdx
callq 0x48f0
leaq -0x68(%rbp), %rdi
leaq -0x58(%rbp), %rax
movq %rax, (%rdi)
leaq 0x1594(%rip), %rsi      # 0x7595
leaq 0x1594(%rip), %rdx      # 0x759c
callq 0x48f0
leaq -0x68(%rbp), %rsi
movq -0x70(%rbp), %rdi
callq 0x58d0
leaq -0x48(%rbp), %rsi
movq %rax, %rdi
callq 0x2280
movq -0x68(%rbp), %rdi
leaq -0x58(%rbp), %rax
cmpq %rax, %rdi
je 0x603f
movq -0x58(%rbp), %rsi
incq %rsi
callq 0x2200
movq -0x48(%rbp), %rdi
leaq -0x38(%rbp), %rax
cmpq %rax, %rdi
je 0x6058
movq -0x38(%rbp), %rsi
incq %rsi
callq 0x2200
movzbl %bl, %eax
addq %rax, %r15
movq -0x78(%rbp), %rdx
leaq -0x1(%rdx), %rax
movq %rax, -0x80(%rbp)
movabsq $0x100002400, %rbx      # imm = 0x100002400
cmpq %rdx, %r15
jae 0x61ad
cmpb $0xd, (%r15)
je 0x61ad
movq %rdx, %rax
subq %r15, %rax
xorl %r14d, %r14d
cmpb $0xd, (%r15,%r14)
je 0x60a4
incq %r14
cmpq %r14, %rax
jne 0x6090
movq %rdx, %r14
jmp 0x60a7
addq %r15, %r14
movq %r14, %rdx
subq %r15, %rdx
movq %r15, %rdi
movl $0x3a, %esi
callq 0x2220
movq %rax, %r12
testq %rax, %rax
je 0x61a0
leaq 0x1(%r12), %rax
movq -0x78(%rbp), %rdx
movq %rax, %rsi
cmpq %r14, %rax
jae 0x60e0
leaq 0x1(%rsi), %rax
cmpb $0x20, (%rsi)
je 0x60cf
cmpq %rdx, %r14
ja 0x6173
cmpq %r12, %r15
jae 0x6173
cmpq %r14, %rsi
jae 0x6173
leaq -0x38(%rbp), %rax
movq %rax, -0x48(%rbp)
leaq -0x48(%rbp), %rdi
movq %r14, %rdx
callq 0x48f0
leaq -0x58(%rbp), %rax
movq %rax, -0x68(%rbp)
leaq -0x68(%rbp), %r13
movq %r13, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x48f0
movq -0x70(%rbp), %rdi
movq %r13, %rsi
callq 0x58d0
movq %rax, %rdi
leaq -0x48(%rbp), %rsi
callq 0x2280
movq -0x68(%rbp), %rdi
leaq -0x58(%rbp), %rax
cmpq %rax, %rdi
je 0x6156
movq -0x58(%rbp), %rsi
incq %rsi
callq 0x2200
movq -0x48(%rbp), %rdi
leaq -0x38(%rbp), %rax
cmpq %rax, %rdi
je 0x616f
movq -0x38(%rbp), %rsi
incq %rsi
callq 0x2200
movq -0x78(%rbp), %rdx
leaq 0x1(%r14), %r15
cmpq %rdx, %r15
jae 0x61a4
movq -0x80(%rbp), %rax
subq %r14, %rax
movzbl (%r15), %ecx
cmpq $0x20, %rcx
ja 0x61a4
btq %rcx, %rbx
jae 0x61a4
incq %r15
decq %rax
jne 0x6183
movq %rdx, %r15
jmp 0x61a4
movq -0x78(%rbp), %rdx
testq %r12, %r12
jne 0x6074
movq -0x70(%rbp), %rax
addq $0x58, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x61d2
jmp 0x61f0
jmp 0x620e
jmp 0x61d2
jmp 0x61f0
jmp 0x620e
jmp 0x61d2
jmp 0x61f0
jmp 0x620e
movq %rax, %r14
movq -0x68(%rbp), %rdi
leaq -0x58(%rbp), %rax
cmpq %rax, %rdi
je 0x61f3
movq -0x58(%rbp), %rsi
incq %rsi
callq 0x2200
jmp 0x61f3
movq %rax, %r14
movq -0x48(%rbp), %rdi
leaq -0x38(%rbp), %rax
cmpq %rax, %rdi
je 0x6211
movq -0x38(%rbp), %rsi
incq %rsi
callq 0x2200
jmp 0x6211
movq %rax, %r14
movq -0x70(%rbp), %rdi
callq 0x4208
movq %r14, %rdi
callq 0x2320
nop