#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */

/* Variables and functions */
scalar_t__ snprintf(int /*<<< orphan*/ *, int /*<<< orphan*/, char *,
                    char const *, char const *, char const *);
int /*<<< orphan*/ sprintf(char *, char *, char const *, char const *,
                           char const *);
char *xmalloc(scalar_t__);
char *build_filename(const char *type, const char *modstr, const char *mtrstr) {
  char *fn =
      xmalloc(snprintf(NULL, 0, "%s_%s_%s.db", type, modstr, mtrstr) + 1);
  sprintf(fn, "%s_%s_%s.db", type, modstr, mtrstr);
  return fn;
}


x86 assembly (clang, -O0):
<build_filename>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rcx
mov    -0x10(%rbp),%r8
mov    -0x18(%rbp),%r9
xor    %eax,%eax
mov    %eax,%edi
xor    %esi,%esi
lea    0xea3(%rip),%rdx
call   1030 <snprintf@plt>
mov    %rax,%rdi
add    $0x1,%rdi
call   1040 <xmalloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%rdx
mov    -0x10(%rbp),%rcx
mov    -0x18(%rbp),%r8
lea    0xe77(%rip),%rsi
call   1050 <sprintf@plt>
mov    -0x20(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<build_filename>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%r13
lea    0xeb7(%rip),%r12
xor    %edi,%edi
xor    %esi,%esi
mov    %r12,%rdx
mov    %r13,%rcx
mov    %r15,%r8
mov    %r14,%r9
call   1030 <snprintf@plt>
lea    0x1(%rax),%rdi
call   1040 <xmalloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r12,%rsi
mov    %r13,%rdx
mov    %r15,%rcx
mov    %r14,%r8
call   1050 <sprintf@plt>
mov    %rbx,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [12 x i8] c"%s_%s_%s.db\00"
define i8* @build_filename(i8* %0, i8* %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
store i8* %2, i8** %6
%8 = load i8*, i8** %4
%9 = load i8*, i8** %5
%10 = load i8*, i8** %6
%11 = call i64 @snprintf(i32* null, i32 0, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* %8, i8* %9, i8* %10)
%12 = add nsw i64 %11, 1
%13 = call i8* @xmalloc(i64 %12)
store i8* %13, i8** %7
%14 = load i8*, i8** %7
%15 = load i8*, i8** %4
%16 = load i8*, i8** %5
%17 = load i8*, i8** %6
%18 = call i32 @sprintf(i8* %14, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* %15, i8* %16, i8* %17)
%19 = load i8*, i8** %7
ret i8* %19
}

LLVM IR (-O2):
@.str = [12 x i8] c"%s_%s_%s.db\00"
define i8* @build_filename(i8* %0, i8* %1, i8* %2) {
%4 = tail call i64 @snprintf(i32* null, i32 0, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %1, i8* %2)
%5 = add nsw i64 %4, 1
%6 = tail call i8* @xmalloc(i64 %5)
%7 = tail call i32 @sprintf(i8* dereferenceable(1) %6, i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %1, i8* %2)
ret i8* %6
}