// File: /workspace/github_c_makefile_star2_O3/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 0x7eb8
cmpl $0x2, %esi
je 0x7e80
cmpl $0x1, %esi
jne 0x8058
movq 0x8(%rbx), %rsi
leaq 0x3867(%rip), %rdi      # 0xb6cb
xorl %eax, %eax
callq 0x9772
movq %rax, 0x10(%rsp)
leaq 0x3866(%rip), %rdi      # 0xb6dd
xorl %eax, %eax
callq 0x9772
jmp 0x7eab
movq 0x8(%rbx), %rsi
leaq 0x3875(%rip), %rdi      # 0xb700
xorl %eax, %eax
callq 0x9772
movq %rax, 0x10(%rsp)
movl (%rbx), %esi
testl %esi, %esi
jle 0x7ecf
leaq 0x386f(%rip), %rdi      # 0xb713
xorl %eax, %eax
callq 0x9772
movq %rax, 0x18(%rsp)
movl $0x2, %r14d
jmp 0x7ed5
movq 0x8(%rbx), %rsi
leaq 0x3865(%rip), %rdi      # 0xb728
xorl %eax, %eax
callq 0x9772
movq %rax, 0x10(%rsp)
movl $0x1, %r14d
movq 0x30(%rbx), %rsi
testq %rsi, %rsi
je 0x7ef7
leaq 0x3881(%rip), %rdi      # 0xb766
xorl %eax, %eax
callq 0x9772
movl %r14d, %ecx
incl %r14d
movq %rax, 0x10(%rsp,%rcx,8)
movq 0x38(%rbx), %rsi
testq %rsi, %rsi
je 0x7f19
leaq 0x386a(%rip), %rdi      # 0xb771
xorl %eax, %eax
callq 0x9772
movl %r14d, %ecx
incl %r14d
movq %rax, 0x10(%rsp,%rcx,8)
movq 0x40(%rbx), %rsi
testq %rsi, %rsi
je 0x7f3b
leaq 0x3857(%rip), %rdi      # 0xb780
xorl %eax, %eax
callq 0x9772
movl %r14d, %ecx
incl %r14d
movq %rax, 0x10(%rsp,%rcx,8)
movq 0x10(%rbx), %rsi
testq %rsi, %rsi
je 0x7f5d
leaq 0x3842(%rip), %rdi      # 0xb78d
xorl %eax, %eax
callq 0x9772
movl %r14d, %ecx
incl %r14d
movq %rax, 0x10(%rsp,%rcx,8)
movq 0x18(%rbx), %rax
testq %rax, %rax
movl %r14d, %r15d
leaq 0x20b8(%rip), %rsi      # 0xa026
cmovneq %rax, %rsi
leaq 0x3825(%rip), %rdi      # 0xb79e
xorl %eax, %eax
callq 0x9772
movq %rax, 0x10(%rsp,%r15,8)
leal 0x1(%r14), %r12d
movq 0x20(%rbx), %rsi
testq %rsi, %rsi
je 0x7faf
leaq 0x3813(%rip), %rdi      # 0xb7ac
xorl %eax, %eax
callq 0x9772
addl $0x2, %r14d
movl %r12d, %ecx
movq %rax, 0x10(%rsp,%rcx,8)
movl %r14d, %r12d
movq 0x28(%rbx), %rsi
testq %rsi, %rsi
je 0x7fd1
leaq 0x3800(%rip), %rdi      # 0xb7bf
xorl %eax, %eax
callq 0x9772
movl %r12d, %ecx
incl %r12d
movq %rax, 0x10(%rsp,%rcx,8)
leaq 0x37f4(%rip), %rdi      # 0xb7cc
xorl %ebp, %ebp
xorl %eax, %eax
callq 0x9772
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 0x803b
movq %r15, %rdi
callq 0x2150
movq %rbx, %rdi
movq %r14, %rsi
movq %r15, %rdx
movq %rax, %rcx
callq 0x96b8
testq %rax, %rax
je 0x803b
movq %r15, %rdi
callq 0x23c0
movq $0x0, 0x10(%rsp,%rbp,8)
incq %rbp
cmpq %rbp, %r12
jne 0x7ff4
movq (%rsp), %rbx
jmp 0x8068
movq 0x10(%rsp,%r13,8), %rdi
callq 0x23c0
addq $-0x1, %r13
jb 0x803b
movq (%rsp), %rdi
callq 0x23c0
xorl %ebx, %ebx
jmp 0x8068
leaq 0x36de(%rip), %rdi      # 0xb73d
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