// File: /workspace/github_c_makefile_star2_O0/giometti[P]mastering_UNIX_programming/chapter_06/psprintf.c
// Function: psprintf
static int psprintf(char **str, const char *name,
		const char *fmt, ...)
{
	int size;
	char *nfmt;
	va_list ap;
	int ret;

	/* Compute the new format size.
	 *
	 * The new format string is formed as:
	 *     <name>: <fmt>\0
	 * so it must be computed as:
	 */
	size = strlen(name) + strlen(fmt) + 3;

	/* Allocate the new format */
	nfmt = malloc(size);
	if (!nfmt)
		return -1;

	/* Build the new format as "<name>: <fmt>".
	 *
	 * Note that nft string has the right size so
	 * we can safely not check the return value
	 */
	snprintf(nfmt, size, "%s: %s", name, fmt);
	
	/* Compute the output size */
	va_start(ap, fmt);
	ret = vsnprintf(NULL, 0, nfmt, ap);
	va_end(ap);
	if (ret < 0)
		goto free_nfmt;
	size = ret + 1; /* for '\0' */

	/* Allocate the output buffer */
	*str = malloc(size);
	if (*str == NULL) {
		ret = -1;
		goto free_nfmt;
	}

	/* Build the output string */
	va_start(ap, fmt);
	ret = vsnprintf(*str, size, nfmt, ap);
	va_end(ap);
	if (ret < 0)
		goto free_nfmt;

free_nfmt:
	free(nfmt);

	return ret;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x120, %rsp            # imm = 0x120
testb %al, %al
je 0x12a1
movaps %xmm0, -0xe0(%rbp)
movaps %xmm1, -0xd0(%rbp)
movaps %xmm2, -0xc0(%rbp)
movaps %xmm3, -0xb0(%rbp)
movaps %xmm4, -0xa0(%rbp)
movaps %xmm5, -0x90(%rbp)
movaps %xmm6, -0x80(%rbp)
movaps %xmm7, -0x70(%rbp)
movq %r9, -0xe8(%rbp)
movq %r8, -0xf0(%rbp)
movq %rcx, -0xf8(%rbp)
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq -0x18(%rbp), %rdi
callq 0x1050
movq %rax, -0x118(%rbp)
movq -0x20(%rbp), %rdi
callq 0x1050
movq %rax, %rcx
movq -0x118(%rbp), %rax
addq %rcx, %rax
addq $0x3, %rax
movl %eax, -0x24(%rbp)
movslq -0x24(%rbp), %rdi
callq 0x1090
movq %rax, -0x30(%rbp)
cmpq $0x0, -0x30(%rbp)
jne 0x130f
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x13ff
movq -0x30(%rbp), %rdi
movslq -0x24(%rbp), %rsi
movq -0x18(%rbp), %rcx
movq -0x20(%rbp), %r8
leaq 0xd15(%rip), %rdx       # 0x203b
movb $0x0, %al
callq 0x1060
leaq -0x50(%rbp), %rax
leaq -0x110(%rbp), %rcx
movq %rcx, 0x10(%rax)
leaq 0x10(%rbp), %rcx
movq %rcx, 0x8(%rax)
movl $0x30, 0x4(%rax)
movl $0x18, (%rax)
movq -0x30(%rbp), %rdx
leaq -0x50(%rbp), %rcx
xorl %eax, %eax
movl %eax, %esi
movq %rsi, %rdi
callq 0x1040
movl %eax, -0x54(%rbp)
leaq -0x50(%rbp), %rax
cmpl $0x0, -0x54(%rbp)
jge 0x1374
jmp 0x13f0
movl -0x54(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x24(%rbp)
movslq -0x24(%rbp), %rdi
callq 0x1090
movq %rax, %rcx
movq -0x10(%rbp), %rax
movq %rcx, (%rax)
movq -0x10(%rbp), %rax
cmpq $0x0, (%rax)
jne 0x13a3
movl $0xffffffff, -0x54(%rbp) # imm = 0xFFFFFFFF
jmp 0x13f0
leaq -0x50(%rbp), %rax
leaq -0x110(%rbp), %rcx
movq %rcx, 0x10(%rax)
leaq 0x10(%rbp), %rcx
movq %rcx, 0x8(%rax)
movl $0x30, 0x4(%rax)
movl $0x18, (%rax)
movq -0x10(%rbp), %rax
movq (%rax), %rdi
movslq -0x24(%rbp), %rsi
movq -0x30(%rbp), %rdx
leaq -0x50(%rbp), %rcx
callq 0x1040
movl %eax, -0x54(%rbp)
leaq -0x50(%rbp), %rax
cmpl $0x0, -0x54(%rbp)
jge 0x13ee
jmp 0x13f0
jmp 0x13f0
movq -0x30(%rbp), %rdi
callq 0x1030
movl -0x54(%rbp), %eax
movl %eax, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x120, %rsp            # imm = 0x120
popq %rbp
retq