// File: /workspace/github_c_makefile_star2_O1/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 0xf633(%rip), %rsi      # 0x23d6f
xorl %eax, %eax
callq 0x1dcf7
cmpl $-0x1, %eax
je 0x147da
movq %r14, 0x10(%rsp)
leaq 0x18(%r14), %r13
movb $0x1, %al
xorl %r15d, %r15d
xorl %r12d, %r12d
movl %eax, 0xc(%rsp)
movq %r15, %rcx
testb $0x1, %al
je 0x1476c
movq (%r13), %rcx
movq (%rcx), %r15
cmpq %r13, %rcx
je 0x1477c
movq -0x10(%rcx), %rbp
movq -0x8(%rcx), %r14
cmpq %r13, %rcx
je 0x14813
movq %rbx, %rdi
leaq 0xf628(%rip), %rsi      # 0x23db7
movq %rbp, %rdx
movq %r14, %rcx
xorl %eax, %eax
callq 0x1dcf7
cmpl $-0x1, %eax
je 0x148d9
movq %rbp, %rdi
leaq 0xf5b5(%rip), %rsi      # 0x23d64
callq 0x34e0
testl %eax, %eax
je 0x147d6
movq %rbp, %rdi
leaq 0xf59d(%rip), %rsi      # 0x23d5f
callq 0x34e0
testl %eax, %eax
movl $0x1, %eax
cmovel %eax, %r12d
xorl %eax, %eax
jmp 0x14761
xorl %eax, %eax
jmp 0x1475d
xorl %eax, %eax
callq 0x17d59
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
cmpl $0x3, (%rax)
jl 0x14932
leaq 0xf532(%rip), %rsi      # 0x23d28
leaq 0xf583(%rip), %rdx      # 0x23d80
leaq 0xf59b(%rip), %r8       # 0x23d9f
movl $0x3, %edi
movl $0x1d1, %ecx            # imm = 0x1D1
jmp 0x14909
testl %r12d, %r12d
movq 0x10(%rsp), %r14
jne 0x1483b
movq 0x10(%r14), %rdx
leaq 0xf598(%rip), %rsi      # 0x23dc0
movq %rbx, %rdi
xorl %eax, %eax
callq 0x1dcf7
cmpl $-0x1, %eax
je 0x14975
testb $0x1, 0xc(%rsp)
je 0x14873
cmpl $0x0, 0x34(%r14)
leaq 0xfc8f(%rip), %rax      # 0x244dd
leaq 0xe274(%rip), %rdx      # 0x22ac9
cmoveq %rax, %rdx
leaq 0xf56b(%rip), %rsi      # 0x23dcb
movq %rbx, %rdi
xorl %eax, %eax
callq 0x1dcf7
cmpl $-0x1, %eax
je 0x14943
movq (%r14), %rdi
leaq 0xd565(%rip), %rsi      # 0x21de2
callq 0x34e0
testl %eax, %eax
je 0x14912
leaq 0xf55f(%rip), %rsi      # 0x23df0
movq %rbx, %rdi
movl $0x2, %edx
callq 0x1dbf0
cmpl $-0x1, %eax
jne 0x1492c
xorl %eax, %eax
callq 0x17d59
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
cmpl $0x3, (%rax)
jl 0x14932
leaq 0xf469(%rip), %rsi      # 0x23d28
leaq 0xf4ba(%rip), %rdx      # 0x23d80
leaq 0xf4d2(%rip), %r8       # 0x23d9f
movl $0x3, %edi
movl $0x1fb, %ecx            # imm = 0x1FB
jmp 0x14909
xorl %eax, %eax
callq 0x17d59
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
cmpl $0x3, (%rax)
jl 0x14932
leaq 0xf437(%rip), %rsi      # 0x23d28
leaq 0xf488(%rip), %rdx      # 0x23d80
leaq 0xf4a0(%rip), %r8       # 0x23d9f
movl $0x3, %edi
movl $0x1d9, %ecx            # imm = 0x1D9
xorl %eax, %eax
callq 0x17d92
jmp 0x14932
movl 0x30(%r14), %edx
leaq 0xf4bf(%rip), %rsi      # 0x23ddc
movq %rbx, %rdi
xorl %eax, %eax
callq 0x1dcf7
cmpl $-0x1, %eax
je 0x149aa
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 0x17d59
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
cmpl $0x3, (%rax)
jl 0x14932
leaq 0xf3cd(%rip), %rsi      # 0x23d28
leaq 0xf41e(%rip), %rdx      # 0x23d80
leaq 0xf436(%rip), %r8       # 0x23d9f
movl $0x3, %edi
movl $0x1ee, %ecx            # imm = 0x1EE
jmp 0x14909
xorl %eax, %eax
callq 0x17d59
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
cmpl $0x3, (%rax)
jl 0x14932
leaq 0xf39b(%rip), %rsi      # 0x23d28
leaq 0xf3ec(%rip), %rdx      # 0x23d80
leaq 0xf404(%rip), %r8       # 0x23d9f
movl $0x3, %edi
movl $0x1e6, %ecx            # imm = 0x1E6
jmp 0x14909
xorl %eax, %eax
callq 0x17d59
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
cmpl $0x3, (%rax)
jl 0x14932
leaq 0xf362(%rip), %rsi      # 0x23d28
leaq 0xf3b3(%rip), %rdx      # 0x23d80
leaq 0xf3cb(%rip), %r8       # 0x23d9f
movl $0x3, %edi
movl $0x1f5, %ecx            # imm = 0x1F5
jmp 0x14909