#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);
char *findExt(char *);
int /*<<< orphan*/ strcat(char *, char const *);
int /*<<< orphan*/ strcpy(char *, char const *);
int strlen(char const *);
char *appendExt(const char *name, const char *newExt) {
  char *ret, *ext;
  int l_name, l_newExt;
  l_name = strlen(name);
  l_newExt = newExt ? strlen(newExt) : 0;
  ret = (char *)MALLOC(sizeof(char) * (l_name + l_newExt + 2));
  strcpy(ret, name);
  if (newExt == NULL)
    return ret;
  ext = findExt(ret);
  if (ext != NULL)
    *ext = '\0';
  if (ret[strlen(ret) - 1] == '.')
    ret[strlen(ret) - 1] = '\0';
  if (strlen(newExt) > 0) {
    if (newExt[0] != '.')
      strcat(ret, ".");
    strcat(ret, newExt);
  }
  return ret;
}


x86 assembly (clang, -O0):
<appendExt>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x2c(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1188 <appendExt+0x38>
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x34(%rbp)
jmp    1192 <appendExt+0x42>
xor    %eax,%eax
mov    %eax,-0x34(%rbp)
jmp    1192 <appendExt+0x42>
mov    -0x34(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0x2c(%rbp),%eax
add    -0x30(%rbp),%eax
add    $0x2,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   1050 <MALLOC@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
cmpq   $0x0,-0x18(%rbp)
jne    11d7 <appendExt+0x87>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1288 <appendExt+0x138>
mov    -0x20(%rbp),%rdi
call   1070 <findExt@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     11f6 <appendExt+0xa6>
mov    -0x28(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x20(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x40(%rbp),%rax
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x2e,%eax
jne    1241 <appendExt+0xf1>
mov    -0x20(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x48(%rbp),%rax
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
cmp    $0x0,%eax
jle    1280 <appendExt+0x130>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2e,%eax
je     1273 <appendExt+0x123>
mov    -0x20(%rbp),%rdi
lea    0xd92(%rip),%rsi
call   1060 <strcat@plt>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1060 <strcat@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<appendExt>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %rdi,%r15
call   1040 <strlen@plt>
mov    %eax,%ebx
test   %r14,%r14
je     1171 <appendExt+0x21>
mov    %r14,%rdi
call   1040 <strlen@plt>
jmp    1173 <appendExt+0x23>
xor    %eax,%eax
lea    (%rbx,%rax,1),%edi
add    $0x2,%edi
call   1050 <MALLOC@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r15,%rsi
call   1030 <strcpy@plt>
test   %r14,%r14
je     11ed <appendExt+0x9d>
mov    %rbx,%rdi
call   1070 <findExt@plt>
test   %rax,%rax
je     11a1 <appendExt+0x51>
movb   $0x0,(%rax)
mov    %rbx,%rdi
call   1040 <strlen@plt>
cltq
cmpb   $0x2e,-0x1(%rax,%rbx,1)
jne    11c1 <appendExt+0x71>
mov    %rbx,%rdi
call   1040 <strlen@plt>
cltq
movb   $0x0,-0x1(%rax,%rbx,1)
mov    %r14,%rdi
call   1040 <strlen@plt>
test   %eax,%eax
jle    11ed <appendExt+0x9d>
cmpb   $0x2e,(%r14)
je     11e2 <appendExt+0x92>
lea    0xe26(%rip),%rsi
mov    %rbx,%rdi
call   1060 <strcat@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1060 <strcat@plt>
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

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

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