// File: /workspace/github_c_makefile_star2_O1/tomka[P]cliaspora/http.c
// Function: http_gen_req
char *
http_gen_req(http_req_t *r)
{
	int    i, lc;
	char   *rq, *ln[16];
	size_t rqsz;

	rq = NULL; rqsz = 0; lc = 0;
	switch (r->type) {
	case HTTP_RQ_TYPE_GET:
		ln[lc++] = strduprintf(HTTP_TMPL_GET_RQ, r->url);
		ln[lc++] = strduprintf("X-Requested-With: XMLHttpRequest\r\n");
		break;
	case HTTP_RQ_TYPE_POST:
		ln[lc++] = strduprintf(HTTP_TMPL_POST_RQ, r->url);
		if (r->cl > 0)
			ln[lc++] = strduprintf(HTTP_TMPL_CONTENT_LEN, r->cl);
		break;
	case HTTP_RQ_TYPE_DELETE:
		ln[lc++] = strduprintf(HTTP_TMPL_DELETE_RQ, r->url);
		break;
	default:
		warnx("http_gen_req(): Invalid request type: %d", r->type);
		return (NULL);
	}
	if (r->host != NULL)
		ln[lc++] = strduprintf(HTTP_TMPL_HOST, r->host);
	if (r->location != NULL)
		ln[lc++] = strduprintf(HTTP_TMPL_LOCATION, r->location);
	if (r->cookie != NULL)
		ln[lc++] = strduprintf(HTTP_TMPL_COOKIE, r->cookie);
	if (r->ua != NULL)
		ln[lc++] = strduprintf(HTTP_TMPL_USER_AGENT, r->ua);
	if (r->cs != NULL)
		ln[lc++] = strduprintf(HTTP_TMPL_CHARSET, r->cs);
	else
		ln[lc++] = strduprintf(HTTP_TMPL_CHARSET, "UTF-8");
	if (r->ct != NULL)
		ln[lc++] = strduprintf(HTTP_TMPL_CONTENT_TYPE, r->ct);
	if (r->accept != NULL)
		ln[lc++] = strduprintf(HTTP_TMPL_ACCEPT, r->accept);
	ln[lc++] = strduprintf("Cache-Control: no-cache\r\n\r\n");
	for (i = 0; i < lc; i++) {
		if (ln[i] == NULL)
			goto error;
		if (strdupstrcat(&rq, &rqsz, ln[i], strlen(ln[i])) == NULL)
			goto error;
		free(ln[i]); ln[i] = NULL;
	}
	return (rq);
error:
	while (lc--)
		free(ln[lc]);
	free(rq);
	return (NULL);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x98, %rsp
movq %rdi, %rbx
xorl %eax, %eax
movq %rax, (%rsp)
movq %rax, 0x8(%rsp)
movl 0x4(%rdi), %esi
cmpl $0x3, %esi
je 0x8008
cmpl $0x2, %esi
je 0x7fd0
cmpl $0x1, %esi
jne 0x81a8
movq 0x8(%rbx), %rsi
leaq 0x3737(%rip), %rdi      # 0xb6eb
xorl %eax, %eax
callq 0x99a1
movq %rax, 0x10(%rsp)
leaq 0x3736(%rip), %rdi      # 0xb6fd
xorl %eax, %eax
callq 0x99a1
jmp 0x7ffb
movq 0x8(%rbx), %rsi
leaq 0x3745(%rip), %rdi      # 0xb720
xorl %eax, %eax
callq 0x99a1
movq %rax, 0x10(%rsp)
movl (%rbx), %esi
testl %esi, %esi
jle 0x801f
leaq 0x373f(%rip), %rdi      # 0xb733
xorl %eax, %eax
callq 0x99a1
movq %rax, 0x18(%rsp)
movl $0x2, %r14d
jmp 0x8025
movq 0x8(%rbx), %rsi
leaq 0x3735(%rip), %rdi      # 0xb748
xorl %eax, %eax
callq 0x99a1
movq %rax, 0x10(%rsp)
movl $0x1, %r14d
movq 0x30(%rbx), %rsi
testq %rsi, %rsi
je 0x8047
leaq 0x3751(%rip), %rdi      # 0xb786
xorl %eax, %eax
callq 0x99a1
movl %r14d, %ecx
incl %r14d
movq %rax, 0x10(%rsp,%rcx,8)
movq 0x38(%rbx), %rsi
testq %rsi, %rsi
je 0x8069
leaq 0x373a(%rip), %rdi      # 0xb791
xorl %eax, %eax
callq 0x99a1
movl %r14d, %ecx
incl %r14d
movq %rax, 0x10(%rsp,%rcx,8)
movq 0x40(%rbx), %rsi
testq %rsi, %rsi
je 0x808b
leaq 0x3727(%rip), %rdi      # 0xb7a0
xorl %eax, %eax
callq 0x99a1
movl %r14d, %ecx
incl %r14d
movq %rax, 0x10(%rsp,%rcx,8)
movq 0x10(%rbx), %rsi
testq %rsi, %rsi
je 0x80ad
leaq 0x3712(%rip), %rdi      # 0xb7ad
xorl %eax, %eax
callq 0x99a1
movl %r14d, %ecx
incl %r14d
movq %rax, 0x10(%rsp,%rcx,8)
movq 0x18(%rbx), %rax
testq %rax, %rax
movl %r14d, %r15d
leaq 0x1f68(%rip), %rsi      # 0xa026
cmovneq %rax, %rsi
leaq 0x36f5(%rip), %rdi      # 0xb7be
xorl %eax, %eax
callq 0x99a1
movq %rax, 0x10(%rsp,%r15,8)
leal 0x1(%r14), %r12d
movq 0x20(%rbx), %rsi
testq %rsi, %rsi
je 0x80ff
leaq 0x36e3(%rip), %rdi      # 0xb7cc
xorl %eax, %eax
callq 0x99a1
addl $0x2, %r14d
movl %r12d, %ecx
movq %rax, 0x10(%rsp,%rcx,8)
movl %r14d, %r12d
movq 0x28(%rbx), %rsi
testq %rsi, %rsi
je 0x8121
leaq 0x36d0(%rip), %rdi      # 0xb7df
xorl %eax, %eax
callq 0x99a1
movl %r12d, %ecx
incl %r12d
movq %rax, 0x10(%rsp,%rcx,8)
leaq 0x36c4(%rip), %rdi      # 0xb7ec
xorl %ebp, %ebp
xorl %eax, %eax
callq 0x99a1
movl %r12d, %r13d
movq %rax, 0x10(%rsp,%r13,8)
incl %r12d
movq %rsp, %rbx
leaq 0x8(%rsp), %r14
movq 0x10(%rsp,%rbp,8), %r15
testq %r15, %r15
je 0x818b
movq %r15, %rdi
callq 0x2150
movq %rbx, %rdi
movq %r14, %rsi
movq %r15, %rdx
movq %rax, %rcx
callq 0x98ec
testq %rax, %rax
je 0x818b
movq %r15, %rdi
callq 0x23c0
movq $0x0, 0x10(%rsp,%rbp,8)
incq %rbp
cmpq %rbp, %r12
jne 0x8144
movq (%rsp), %rbx
jmp 0x81b8
movq 0x10(%rsp,%r13,8), %rdi
callq 0x23c0
addq $-0x1, %r13
jb 0x818b
movq (%rsp), %rdi
callq 0x23c0
xorl %ebx, %ebx
jmp 0x81b8
leaq 0x35ae(%rip), %rdi      # 0xb75d
xorl %ebx, %ebx
xorl %eax, %eax
callq 0x22d0
movq %rbx, %rax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq