#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__ malloc(int);
int /*<<< orphan*/ memset(void *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ sprintf(char *, char *, char const *, char const *,
                           char const *);
int strlen(char const *);
char *concatenate_fname(const char *path, const char *base, const char *ext) {
  int path_len = strlen(path);
  int base_len = strlen(base);
  int ext_len = strlen(ext);
  int char_count = path_len + base_len + ext_len;
  char *fname = (char *)malloc((char_count + 1) * sizeof(char));
  memset((void *)fname, 0, char_count + 1);
  if (fname == NULL) {
    printf("Failed to Allocate\n");
    return NULL;
  }
  sprintf(fname, "%s%s%s", path, base, ext);
  return fname;
}


x86 assembly (clang, -O0):
<concatenate_fname>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x24(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x24(%rbp),%eax
add    -0x28(%rbp),%eax
add    -0x2c(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0x30(%rbp),%eax
add    $0x1,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
mov    -0x30(%rbp),%edx
add    $0x1,%edx
xor    %esi,%esi
call   1050 <memset@plt>
cmpq   $0x0,-0x38(%rbp)
jne    11e0 <concatenate_fname+0x90>
lea    0xe32(%rip),%rdi
call   1040 <printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    1204 <concatenate_fname+0xb4>
mov    -0x38(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rcx
mov    -0x20(%rbp),%r8
lea    0xe1d(%rip),%rsi
call   1070 <sprintf@plt>
mov    -0x38(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<concatenate_fname>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%r12
call   1040 <strlen@plt>
mov    %eax,%ebp
mov    %r15,%rdi
call   1040 <strlen@plt>
mov    %eax,%ebx
mov    %r14,%rdi
call   1040 <strlen@plt>
add    %ebp,%ebx
lea    (%rax,%rbx,1),%ebp
add    $0x1,%ebp
mov    %ebp,%edi
call   1060 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
xor    %esi,%esi
mov    %ebp,%edx
call   1050 <memset@plt>
test   %rbx,%rbx
je     11b7 <concatenate_fname+0x67>
lea    0xe5c(%rip),%rsi
mov    %rbx,%rdi
mov    %r12,%rdx
mov    %r15,%rcx
mov    %r14,%r8
call   1070 <sprintf@plt>
jmp    11c5 <concatenate_fname+0x75>
lea    0xe49(%rip),%rdi
call   1030 <puts@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [20 x i8] c"Failed to Allocate\0A\00"
@.str.1 = [7 x i8] c"%s%s%s\00"
define i8* @concatenate_fname(i8* %0, i8* %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i8*
store i8* %0, i8** %5
store i8* %1, i8** %6
store i8* %2, i8** %7
%13 = load i8*, i8** %5
%14 = call i32 @strlen(i8* %13)
store i32 %14, i32* %8
%15 = load i8*, i8** %6
%16 = call i32 @strlen(i8* %15)
store i32 %16, i32* %9
%17 = load i8*, i8** %7
%18 = call i32 @strlen(i8* %17)
store i32 %18, i32* %10
%19 = load i32, i32* %8
%20 = load i32, i32* %9
%21 = add nsw i32 %19, %20
%22 = load i32, i32* %10
%23 = add nsw i32 %21, %22
store i32 %23, i32* %11
%24 = load i32, i32* %11
%25 = add nsw i32 %24, 1
%26 = sext i32 %25 to i64
%27 = mul i64 %26, 1
%28 = trunc i64 %27 to i32
%29 = call i64 @malloc(i32 %28)
%30 = inttoptr i64 %29 to i8*
store i8* %30, i8** %12
%31 = load i8*, i8** %12
%32 = load i32, i32* %11
%33 = add nsw i32 %32, 1
%34 = call i32 @memset(i8* %31, i32 0, i32 %33)
%35 = load i8*, i8** %12
%36 = icmp eq i8* %35, null
br i1 %36, label %37, label %39
37: ; preds = %3
%38 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
store i8* null, i8** %4
br label %46
39: ; preds = %3
%40 = load i8*, i8** %12
%41 = load i8*, i8** %5
%42 = load i8*, i8** %6
%43 = load i8*, i8** %7
%44 = call i32 @sprintf(i8* %40, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i8* %41, i8* %42, i8* %43)
%45 = load i8*, i8** %12
store i8* %45, i8** %4
br label %46
46: ; preds = %39, %37
%47 = load i8*, i8** %4
ret i8* %47
}

LLVM IR (-O2):
@.str.1 = [7 x i8] c"%s%s%s\00"
@str = [19 x i8] c"Failed to Allocate\00"
define i8* @concatenate_fname(i8* %0, i8* %1, i8* %2) {
%4 = tail call i32 @strlen(i8* %0)
%5 = tail call i32 @strlen(i8* %1)
%6 = tail call i32 @strlen(i8* %2)
%7 = add i32 %4, 1
%8 = add i32 %7, %5
%9 = add i32 %8, %6
%10 = tail call i64 @malloc(i32 %9)
%11 = inttoptr i64 %10 to i8*
%12 = tail call i32 @memset(i8* %11, i32 0, i32 %9)
%13 = icmp eq i64 %10, 0
br i1 %13, label %14, label %16
14: ; preds = %3
%15 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str, i64 0, i64 0))
br label %18
16: ; preds = %3
%17 = tail call i32 @sprintf(i8* %11, i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i8* %0, i8* %1, i8* %2)
br label %18
18: ; preds = %16, %14
%19 = phi i8* [ null, %14 ], [ %11, %16 ]
ret i8* %19
}