// File: /workspace/github_c_makefile_star2_O2/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 $0x38, %rsp
movq %rsi, %r14
movq %rdi, %rbx
movq (%rsi), %rdx
movq 0x8(%rsi), %rcx
leaq 0xe814(%rip), %rsi      # 0x21d7b
xorl %eax, %eax
callq 0x1bf2d
cmpl $-0x1, %eax
je 0x1360c
xorps %xmm0, %xmm0
leaq 0x20(%rsp), %r15
movaps %xmm0, (%r15)
movb $0x1, %al
movl %eax, 0xc(%rsp)
leaq 0x18(%rsp), %r13
xorl %r12d, %r12d
movq %r14, %rdi
movq %r15, %rsi
leaq 0x10(%rsp), %rdx
movq %r13, %rcx
callq 0x134df
testl %eax, %eax
je 0x13641
movq 0x10(%rsp), %rdx
movq 0x18(%rsp), %rcx
movq %rbx, %rdi
leaq 0xe803(%rip), %rsi      # 0x21dc3
xorl %eax, %eax
callq 0x1bf2d
cmpl $-0x1, %eax
je 0x136f4
movq 0x10(%rsp), %rbp
movq %rbp, %rdi
leaq 0xe791(%rip), %rsi      # 0x21d70
callq 0x34f0
testl %eax, %eax
je 0x13602
movq %rbp, %rdi
leaq 0xe779(%rip), %rsi      # 0x21d6b
callq 0x34f0
testl %eax, %eax
pushq $0x1
popq %rax
cmovel %eax, %r12d
jmp 0x13591
movl $0x0, 0xc(%rsp)
jmp 0x13591
xorl %eax, %eax
callq 0x165f5
pushq $-0x1
popq %rbp
cmpl $0x3, (%rax)
jl 0x13749
leaq 0xe70e(%rip), %rsi      # 0x21d34
leaq 0xe75f(%rip), %rdx      # 0x21d8c
leaq 0xe777(%rip), %r8       # 0x21dab
pushq $0x3
popq %rdi
movl $0x1d1, %ecx            # imm = 0x1D1
jmp 0x13720
testl %r12d, %r12d
jne 0x13664
movq 0x10(%r14), %rdx
leaq 0xe77b(%rip), %rsi      # 0x21dcc
movq %rbx, %rdi
xorl %eax, %eax
callq 0x1bf2d
cmpl $-0x1, %eax
je 0x13788
testb $0x1, 0xc(%rsp)
je 0x1369c
cmpl $0x0, 0x34(%r14)
leaq 0xee72(%rip), %rax      # 0x224e9
leaq 0xd453(%rip), %rdx      # 0x20ad1
cmoveq %rax, %rdx
leaq 0xe74e(%rip), %rsi      # 0x21dd7
movq %rbx, %rdi
xorl %eax, %eax
callq 0x1bf2d
cmpl $-0x1, %eax
je 0x1375a
movq (%r14), %rdi
leaq 0xc71c(%rip), %rsi      # 0x1fdc2
callq 0x34f0
testl %eax, %eax
je 0x13729
leaq 0xe746(%rip), %rsi      # 0x21dfc
pushq $0x2
popq %rdx
movq %rbx, %rdi
callq 0x1be74
cmpl $-0x1, %eax
jne 0x13743
xorl %eax, %eax
callq 0x165f5
pushq $-0x1
popq %rbp
cmpl $0x3, (%rax)
jl 0x13749
leaq 0xe658(%rip), %rsi      # 0x21d34
leaq 0xe6a9(%rip), %rdx      # 0x21d8c
leaq 0xe6c1(%rip), %r8       # 0x21dab
pushq $0x3
popq %rdi
movl $0x1fb, %ecx            # imm = 0x1FB
jmp 0x13720
xorl %eax, %eax
callq 0x165f5
pushq $-0x1
popq %rbp
cmpl $0x3, (%rax)
jl 0x13749
leaq 0xe62a(%rip), %rsi      # 0x21d34
leaq 0xe67b(%rip), %rdx      # 0x21d8c
leaq 0xe693(%rip), %r8       # 0x21dab
pushq $0x3
popq %rdi
movl $0x1d9, %ecx            # imm = 0x1D9
xorl %eax, %eax
callq 0x1662e
jmp 0x13749
movl 0x30(%r14), %edx
leaq 0xe6b4(%rip), %rsi      # 0x21de8
movq %rbx, %rdi
xorl %eax, %eax
callq 0x1bf2d
cmpl $-0x1, %eax
je 0x137b9
movl 0x8(%rbx), %ebp
subl 0xc(%rbx), %ebp
movl %ebp, %eax
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
xorl %eax, %eax
callq 0x165f5
pushq $-0x1
popq %rbp
cmpl $0x3, (%rax)
jl 0x13749
leaq 0xe5c4(%rip), %rsi      # 0x21d34
leaq 0xe615(%rip), %rdx      # 0x21d8c
leaq 0xe62d(%rip), %r8       # 0x21dab
pushq $0x3
popq %rdi
movl $0x1ee, %ecx            # imm = 0x1EE
jmp 0x13720
xorl %eax, %eax
callq 0x165f5
pushq $-0x1
popq %rbp
cmpl $0x3, (%rax)
jl 0x13749
leaq 0xe596(%rip), %rsi      # 0x21d34
leaq 0xe5e7(%rip), %rdx      # 0x21d8c
leaq 0xe5ff(%rip), %r8       # 0x21dab
pushq $0x3
popq %rdi
movl $0x1e6, %ecx            # imm = 0x1E6
jmp 0x13720
xorl %eax, %eax
callq 0x165f5
pushq $-0x1
popq %rbp
cmpl $0x3, (%rax)
jl 0x13749
leaq 0xe565(%rip), %rsi      # 0x21d34
leaq 0xe5b6(%rip), %rdx      # 0x21d8c
leaq 0xe5ce(%rip), %r8       # 0x21dab
pushq $0x3
popq %rdi
movl $0x1f5, %ecx            # imm = 0x1F5
jmp 0x13720