#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 */
char *MALLOC(int);
char *STRDUP(char *);
char *findExt(char *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ strcat(char *, char const *);
int /*<<< orphan*/ strcpy(char *, char const *);
int strlen(char const *);
char *appendToBasename(const char *inFile, const char *suffix) {
  char *ret = MALLOC(sizeof(char) * (strlen(inFile) + strlen(suffix) + 20));
  strcpy(ret, inFile);
  char *p = findExt(ret);
  if (p && p != ret) {
    char *ext;
    *p++ = '\0';
    ext = STRDUP(p);
    strcat(ret, suffix);
    strcat(ret, ".");
    strcat(ret, ext);
    free(ext);
  } else if (ret[strlen(ret) - 1] == '.') {
    ret[strlen(ret) - 1] = '\0';
    strcat(ret, suffix);
  } else {
    strcat(ret, suffix);
  }
  return ret;
}


x86 assembly (clang, -O0):
<appendToBasename>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%ecx
mov    -0x2c(%rbp),%eax
add    %ecx,%eax
add    $0x14,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   1060 <MALLOC@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x18(%rbp),%rdi
call   1090 <findExt@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     123a <appendToBasename+0xca>
mov    -0x20(%rbp),%rax
cmp    -0x18(%rbp),%rax
je     123a <appendToBasename+0xca>
mov    -0x20(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x20(%rbp)
movb   $0x0,(%rax)
mov    -0x20(%rbp),%rdi
call   1070 <STRDUP@plt>
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1080 <strcat@plt>
mov    -0x18(%rbp),%rdi
lea    0xde6(%rip),%rsi
call   1080 <strcat@plt>
mov    -0x18(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1080 <strcat@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
jmp    12a9 <appendToBasename+0x139>
mov    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%ecx
mov    -0x38(%rbp),%rax
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x2e,%eax
jne    1297 <appendToBasename+0x127>
mov    -0x18(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%ecx
mov    -0x40(%rbp),%rax
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1080 <strcat@plt>
jmp    12a4 <appendToBasename+0x134>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1080 <strcat@plt>
jmp    12a9 <appendToBasename+0x139>
mov    -0x18(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<appendToBasename>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %rdi,%r15
call   1050 <strlen@plt>
mov    %eax,%ebx
mov    %r14,%rdi
call   1050 <strlen@plt>
lea    (%rbx,%rax,1),%edi
add    $0x14,%edi
call   1060 <MALLOC@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r15,%rsi
call   1040 <strcpy@plt>
mov    %rbx,%rdi
call   1090 <findExt@plt>
test   %rax,%rax
je     11f6 <appendToBasename+0x86>
cmp    %rbx,%rax
je     11f6 <appendToBasename+0x86>
mov    %rax,%rdi
add    $0x1,%rdi
movb   $0x0,(%rax)
call   1070 <STRDUP@plt>
mov    %rax,%r15
mov    %rbx,%rdi
mov    %r14,%rsi
call   1080 <strcat@plt>
lea    0xe27(%rip),%rsi
mov    %rbx,%rdi
call   1080 <strcat@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
call   1080 <strcat@plt>
mov    %r15,%rdi
call   1030 <free@plt>
jmp    1221 <appendToBasename+0xb1>
mov    %rbx,%rdi
call   1050 <strlen@plt>
cltq
cmpb   $0x2e,-0x1(%rax,%rbx,1)
jne    1216 <appendToBasename+0xa6>
mov    %rbx,%rdi
call   1050 <strlen@plt>
cltq
movb   $0x0,-0x1(%rax,%rbx,1)
mov    %rbx,%rdi
mov    %r14,%rsi
call   1080 <strcat@plt>
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [2 x i8] c".\00"
define i8* @appendToBasename(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
store i8* %0, i8** %3
store i8* %1, i8** %4
%8 = load i8*, i8** %3
%9 = call i32 @strlen(i8* %8)
%10 = load i8*, i8** %4
%11 = call i32 @strlen(i8* %10)
%12 = add nsw i32 %9, %11
%13 = add nsw i32 %12, 20
%14 = sext i32 %13 to i64
%15 = mul i64 1, %14
%16 = trunc i64 %15 to i32
%17 = call i8* @MALLOC(i32 %16)
store i8* %17, i8** %5
%18 = load i8*, i8** %5
%19 = load i8*, i8** %3
%20 = call i32 @strcpy(i8* %18, i8* %19)
%21 = load i8*, i8** %5
%22 = call i8* @findExt(i8* %21)
store i8* %22, i8** %6
%23 = load i8*, i8** %6
%24 = icmp ne i8* %23, null
br i1 %24, label %25, label %44
25: ; preds = %2
%26 = load i8*, i8** %6
%27 = load i8*, i8** %5
%28 = icmp ne i8* %26, %27
br i1 %28, label %29, label %44
29: ; preds = %25
%30 = load i8*, i8** %6
%31 = getelementptr inbounds i8, i8* %30, i32 1
store i8* %31, i8** %6
store i8 0, i8* %30
%32 = load i8*, i8** %6
%33 = call i8* @STRDUP(i8* %32)
store i8* %33, i8** %7
%34 = load i8*, i8** %5
%35 = load i8*, i8** %4
%36 = call i32 @strcat(i8* %34, i8* %35)
%37 = load i8*, i8** %5
%38 = call i32 @strcat(i8* %37, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%39 = load i8*, i8** %5
%40 = load i8*, i8** %7
%41 = call i32 @strcat(i8* %39, i8* %40)
%42 = load i8*, i8** %7
%43 = call i32 @free(i8* %42)
br label %69
44: ; preds = %25, %2
%45 = load i8*, i8** %5
%46 = load i8*, i8** %5
%47 = call i32 @strlen(i8* %46)
%48 = sub nsw i32 %47, 1
%49 = sext i32 %48 to i64
%50 = getelementptr inbounds i8, i8* %45, i64 %49
%51 = load i8, i8* %50
%52 = sext i8 %51 to i32
%53 = icmp eq i32 %52, 46
br i1 %53, label %54, label %64
54: ; preds = %44
%55 = load i8*, i8** %5
%56 = load i8*, i8** %5
%57 = call i32 @strlen(i8* %56)
%58 = sub nsw i32 %57, 1
%59 = sext i32 %58 to i64
%60 = getelementptr inbounds i8, i8* %55, i64 %59
store i8 0, i8* %60
%61 = load i8*, i8** %5
%62 = load i8*, i8** %4
%63 = call i32 @strcat(i8* %61, i8* %62)
br label %68
64: ; preds = %44
%65 = load i8*, i8** %5
%66 = load i8*, i8** %4
%67 = call i32 @strcat(i8* %65, i8* %66)
br label %68
68: ; preds = %64, %54
br label %69
69: ; preds = %68, %29
%70 = load i8*, i8** %5
ret i8* %70
}

LLVM IR (-O2):
@.str = [2 x i8] c".\00"
define i8* @appendToBasename(i8* %0, i8* %1) {
%3 = tail call i32 @strlen(i8* %0)
%4 = tail call i32 @strlen(i8* %1)
%5 = add i32 %3, 20
%6 = add i32 %5, %4
%7 = tail call i8* @MALLOC(i32 %6)
%8 = tail call i32 @strcpy(i8* %7, i8* %0)
%9 = tail call i8* @findExt(i8* %7)
%10 = icmp eq i8* %9, null
%11 = icmp eq i8* %9, %7
%12 = or i1 %10, %11
br i1 %12, label %20, label %13
13: ; preds = %2
%14 = getelementptr inbounds i8, i8* %9, i64 1
store i8 0, i8* %9
%15 = tail call i8* @STRDUP(i8* %14)
%16 = tail call i32 @strcat(i8* %7, i8* %1)
%17 = tail call i32 @strcat(i8* %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%18 = tail call i32 @strcat(i8* %7, i8* %15)
%19 = tail call i32 @free(i8* %15)
br label %35
20: ; preds = %2
%21 = tail call i32 @strlen(i8* %7)
%22 = add nsw i32 %21, -1
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds i8, i8* %7, i64 %23
%25 = load i8, i8* %24
%26 = icmp eq i8 %25, 46
br i1 %26, label %27, label %33
27: ; preds = %20
%28 = tail call i32 @strlen(i8* %7)
%29 = add nsw i32 %28, -1
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds i8, i8* %7, i64 %30
store i8 0, i8* %31
%32 = tail call i32 @strcat(i8* %7, i8* %1)
br label %35
33: ; preds = %20
%34 = tail call i32 @strcat(i8* %7, i8* %1)
br label %35
35: ; preds = %27, %33, %13
ret i8* %7
}