// File: /workspace/github_c_makefile_star2_O3/GangZhuo[P]dohclient/src/http.c
// Function: http_request_headers_serialize
int http_request_headers_serialize(/*write stream*/stream_t* s, http_request_t* req)
{
	struct dliterator_t it;
	const char* name;
	const char* value;
	int have_connection = FALSE;
	int have_host = FALSE;

	if (stream_appendf(s,
		"%s %s HTTP/1.1\r\n",
		req->method,
		req->path) == -1) {
		loge("stream_appendf() error\n");
		return -1;
	}

	dliterator_reset(&it);

	while (http_request_header_next(req, &it, &name, &value)) {
		if (stream_appendf(s, "%s: %s\r\n", name, value) == -1) {
			loge("stream_appendf() error\n");
			return -1;
		}
		if (strcmp(name, "Connection") == 0) {
			have_connection = TRUE;
		}
		else if (strcmp(name, "Host") == 0) {
			have_host = TRUE;
		}
	}

	if (!have_host) {
		if (stream_appendf(s, "Host: %s\r\n", req->host) == -1) {
			loge("stream_appendf() error\n");
			return -1;
		}
	}

	if (!have_connection) {
		if (stream_appendf(s, "Connection: %s\r\n",
			req->keep_alive ? "keep-alive" : "close") == -1) {
			loge("stream_appendf() error\n");
			return -1;
		}
	}

	if (strcmp(req->method, "POST") == 0) {
		if (stream_appendf(s, "Content-Length: %d\r\n\r\n", req->data_len) == -1) {
			loge("stream_appendf() error\n");
			return -1;
		}
	}
	else {
		if (stream_appends(s, "\r\n", 2) == -1) {
			loge("stream_appendf() error\n");
			return -1;
		}
	}

	return s->size - s->pos;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rsi, %r14
movq %rdi, %rbx
movq (%rsi), %rdx
movq 0x8(%rsi), %rcx
leaq 0xfb79(%rip), %rsi      # 0x23d7b
xorl %eax, %eax
callq 0x1d504
cmpl $-0x1, %eax
je 0x1429a
movq %r14, 0x10(%rsp)
addq $0x18, %r14
movb $0x1, %al
leaq 0xfb9f(%rip), %r15      # 0x23dc3
xorl %r12d, %r12d
xorl %r13d, %r13d
movl %eax, 0xc(%rsp)
movq %r12, %rcx
testb $0x1, %al
je 0x14238
movq (%r14), %rcx
cmpq %r14, %rcx
je 0x142d3
movq (%rcx), %r12
movq -0x10(%rcx), %rbp
movq -0x8(%rcx), %rcx
movq %rbx, %rdi
movq %r15, %rsi
movq %rbp, %rdx
xorl %eax, %eax
callq 0x1d504
cmpl $-0x1, %eax
je 0x14399
movq %rbp, %rdi
leaq 0xfb01(%rip), %rsi      # 0x23d70
callq 0x34e0
testl %eax, %eax
je 0x14296
movq %rbp, %rdi
leaq 0xfae9(%rip), %rsi      # 0x23d6b
callq 0x34e0
testl %eax, %eax
movl $0x1, %eax
cmovel %eax, %r13d
xorl %eax, %eax
jmp 0x1422e
xorl %eax, %eax
jmp 0x1422a
xorl %eax, %eax
callq 0x176e5
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
cmpl $0x3, (%rax)
jl 0x143f2
leaq 0xfa7e(%rip), %rsi      # 0x23d34
leaq 0xfacf(%rip), %rdx      # 0x23d8c
leaq 0xfae7(%rip), %r8       # 0x23dab
movl $0x3, %edi
movl $0x1d1, %ecx            # imm = 0x1D1
jmp 0x143c9
testl %r13d, %r13d
movq 0x10(%rsp), %r14
jne 0x142fb
movq 0x10(%r14), %rdx
leaq 0xfae4(%rip), %rsi      # 0x23dcc
movq %rbx, %rdi
xorl %eax, %eax
callq 0x1d504
cmpl $-0x1, %eax
je 0x14435
testb $0x1, 0xc(%rsp)
je 0x14333
cmpl $0x0, 0x34(%r14)
leaq 0x101db(%rip), %rax     # 0x244e9
leaq 0xe7bc(%rip), %rdx      # 0x22ad1
cmoveq %rax, %rdx
leaq 0xfab7(%rip), %rsi      # 0x23dd7
movq %rbx, %rdi
xorl %eax, %eax
callq 0x1d504
cmpl $-0x1, %eax
je 0x14403
movq (%r14), %rdi
leaq 0xda85(%rip), %rsi      # 0x21dc2
callq 0x34e0
testl %eax, %eax
je 0x143d2
leaq 0xfaab(%rip), %rsi      # 0x23dfc
movq %rbx, %rdi
movl $0x2, %edx
callq 0x1d3fd
cmpl $-0x1, %eax
jne 0x143ec
xorl %eax, %eax
callq 0x176e5
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
cmpl $0x3, (%rax)
jl 0x143f2
leaq 0xf9b5(%rip), %rsi      # 0x23d34
leaq 0xfa06(%rip), %rdx      # 0x23d8c
leaq 0xfa1e(%rip), %r8       # 0x23dab
movl $0x3, %edi
movl $0x1fb, %ecx            # imm = 0x1FB
jmp 0x143c9
xorl %eax, %eax
callq 0x176e5
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
cmpl $0x3, (%rax)
jl 0x143f2
leaq 0xf983(%rip), %rsi      # 0x23d34
leaq 0xf9d4(%rip), %rdx      # 0x23d8c
leaq 0xf9ec(%rip), %r8       # 0x23dab
movl $0x3, %edi
movl $0x1d9, %ecx            # imm = 0x1D9
xorl %eax, %eax
callq 0x1771e
jmp 0x143f2
movl 0x30(%r14), %edx
leaq 0xfa0b(%rip), %rsi      # 0x23de8
movq %rbx, %rdi
xorl %eax, %eax
callq 0x1d504
cmpl $-0x1, %eax
je 0x1446a
movl 0x8(%rbx), %ebp
subl 0xc(%rbx), %ebp
movl %ebp, %eax
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %eax, %eax
callq 0x176e5
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
cmpl $0x3, (%rax)
jl 0x143f2
leaq 0xf919(%rip), %rsi      # 0x23d34
leaq 0xf96a(%rip), %rdx      # 0x23d8c
leaq 0xf982(%rip), %r8       # 0x23dab
movl $0x3, %edi
movl $0x1ee, %ecx            # imm = 0x1EE
jmp 0x143c9
xorl %eax, %eax
callq 0x176e5
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
cmpl $0x3, (%rax)
jl 0x143f2
leaq 0xf8e7(%rip), %rsi      # 0x23d34
leaq 0xf938(%rip), %rdx      # 0x23d8c
leaq 0xf950(%rip), %r8       # 0x23dab
movl $0x3, %edi
movl $0x1e6, %ecx            # imm = 0x1E6
jmp 0x143c9
xorl %eax, %eax
callq 0x176e5
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
cmpl $0x3, (%rax)
jl 0x143f2
leaq 0xf8ae(%rip), %rsi      # 0x23d34
leaq 0xf8ff(%rip), %rdx      # 0x23d8c
leaq 0xf917(%rip), %r8       # 0x23dab
movl $0x3, %edi
movl $0x1f5, %ecx            # imm = 0x1F5
jmp 0x143c9