// File: /workspace/github_c_makefile_star2_O2/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
andq $0x0, (%rsp)
andq $0x0, 0x8(%rsp)
movq %rdi, %rbx
movl 0x4(%rdi), %esi
cmpl $0x3, %esi
je 0x7820
cmpl $0x2, %esi
je 0x77ec
cmpl $0x1, %esi
jne 0x79c0
movq 0x8(%rbx), %rsi
leaq 0x3efb(%rip), %rdi      # 0xb6cb
xorl %eax, %eax
callq 0x918a
movq %rax, 0x10(%rsp)
leaq 0x3efa(%rip), %rdi      # 0xb6dd
xorl %eax, %eax
callq 0x918a
jmp 0x7817
movq 0x8(%rbx), %rsi
leaq 0x3f09(%rip), %rdi      # 0xb700
xorl %eax, %eax
callq 0x918a
movq %rax, 0x10(%rsp)
movl (%rbx), %esi
testl %esi, %esi
jle 0x7837
leaq 0x3f03(%rip), %rdi      # 0xb713
xorl %eax, %eax
callq 0x918a
movq %rax, 0x18(%rsp)
pushq $0x2
jmp 0x7839
movq 0x8(%rbx), %rsi
leaq 0x3efd(%rip), %rdi      # 0xb728
xorl %eax, %eax
callq 0x918a
movq %rax, 0x10(%rsp)
pushq $0x1
popq %r14
movq 0x30(%rbx), %rsi
testq %rsi, %rsi
je 0x785d
leaq 0x3f1b(%rip), %rdi      # 0xb766
xorl %eax, %eax
callq 0x918a
movl %r14d, %ecx
incl %r14d
movq %rax, 0x10(%rsp,%rcx,8)
movq 0x38(%rbx), %rsi
testq %rsi, %rsi
je 0x787f
leaq 0x3f04(%rip), %rdi      # 0xb771
xorl %eax, %eax
callq 0x918a
movl %r14d, %ecx
incl %r14d
movq %rax, 0x10(%rsp,%rcx,8)
movq 0x40(%rbx), %rsi
testq %rsi, %rsi
je 0x78a1
leaq 0x3ef1(%rip), %rdi      # 0xb780
xorl %eax, %eax
callq 0x918a
movl %r14d, %ecx
incl %r14d
movq %rax, 0x10(%rsp,%rcx,8)
movq 0x10(%rbx), %rsi
testq %rsi, %rsi
je 0x78c3
leaq 0x3edc(%rip), %rdi      # 0xb78d
xorl %eax, %eax
callq 0x918a
movl %r14d, %ecx
incl %r14d
movq %rax, 0x10(%rsp,%rcx,8)
movq 0x18(%rbx), %rax
testq %rax, %rax
movl %r14d, %r15d
leaq 0x2752(%rip), %rsi      # 0xa026
cmovneq %rax, %rsi
leaq 0x3ebf(%rip), %rdi      # 0xb79e
xorl %eax, %eax
callq 0x918a
movq %rax, 0x10(%rsp,%r15,8)
leal 0x1(%r14), %r12d
movq 0x20(%rbx), %rsi
testq %rsi, %rsi
je 0x7915
leaq 0x3ead(%rip), %rdi      # 0xb7ac
xorl %eax, %eax
callq 0x918a
addl $0x2, %r14d
movl %r12d, %ecx
movq %rax, 0x10(%rsp,%rcx,8)
movl %r14d, %r12d
movq 0x28(%rbx), %rsi
testq %rsi, %rsi
je 0x7937
leaq 0x3e9a(%rip), %rdi      # 0xb7bf
xorl %eax, %eax
callq 0x918a
movl %r12d, %ecx
incl %r12d
movq %rax, 0x10(%rsp,%rcx,8)
leaq 0x3e8e(%rip), %rdi      # 0xb7cc
xorl %ebp, %ebp
xorl %eax, %eax
callq 0x918a
movl %r12d, %r13d
incl %r12d
movq %rax, 0x10(%rsp,%r13,8)
movq %rsp, %rbx
leaq 0x8(%rsp), %r14
cmpq %rbp, %r12
je 0x79ba
movq 0x10(%rsp,%rbp,8), %r15
testq %r15, %r15
je 0x79a7
movq %r15, %rdi
callq 0x2150
movq %rbx, %rdi
movq %r14, %rsi
movq %r15, %rdx
movq %rax, %rcx
callq 0x90d0
testq %rax, %rax
je 0x79a7
movq %r15, %rdi
callq 0x23d0
andq $0x0, 0x10(%rsp,%rbp,8)
incq %rbp
jmp 0x795a
movq 0x10(%rsp,%r13,8), %rdi
callq 0x23d0
decq %r13
cmpq $-0x1, %r13
jne 0x799a
movq (%rsp), %rdi
callq 0x23d0
xorl %ebx, %ebx
jmp 0x79d0
movq (%rsp), %rbx
jmp 0x79d0
leaq 0x3d76(%rip), %rdi      # 0xb73d
xorl %ebx, %ebx
xorl %eax, %eax
callq 0x22e0
movq %rbx, %rax
addq $0x98, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq