#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__ calloc(int, int);
int /*<<< orphan*/ strcat(char *, char *);
scalar_t__ strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, int);
char *AppendToBasename(char *filename, char *extra) {
  char *resultfilename;
  int i, n;
  n = (int)strlen(filename);
  for (i = (n - 1); i > 0; i--) {
    if (filename[i] == '.')
      break;
  }
  if (i == 0)
    i = n;
  n = (int)strlen(extra);
  resultfilename = (char *)calloc((i + n + 1), sizeof(char));
  strncpy(resultfilename, filename, i);
  resultfilename[i] = '\0';
  strcat(resultfilename, extra);
  return resultfilename;
}


x86 assembly (clang, -O0):
<AppendToBasename>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jle    119c <AppendToBasename+0x5c>
mov    -0x8(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x2e,%eax
jne    1189 <AppendToBasename+0x49>
jmp    119c <AppendToBasename+0x5c>
jmp    118e <AppendToBasename+0x4e>
mov    -0x1c(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x1c(%rbp)
jmp    1165 <AppendToBasename+0x25>
cmpl   $0x0,-0x1c(%rbp)
jne    11ac <AppendToBasename+0x6c>
mov    -0x20(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%edi
add    -0x20(%rbp),%edi
add    $0x1,%edi
mov    $0x1,%esi
call   1050 <calloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1030 <strncpy@plt>
mov    -0x18(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1060 <strcat@plt>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<AppendToBasename>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
call   1040 <strlen@plt>
mov    %rax,%r15
mov    %r15d,%ecx
test   %r15d,%r15d
mov    $0x1,%eax
cmovle %r15d,%eax
add    $0xffffffff,%eax
nopl   0x0(%rax,%rax,1)
cmp    $0x2,%ecx
jl     1183 <AppendToBasename+0x43>
add    $0xffffffffffffffff,%rcx
mov    %ecx,%edx
cmpb   $0x2e,(%rbx,%rdx,1)
jne    1170 <AppendToBasename+0x30>
mov    %ecx,%eax
test   %eax,%eax
cmovne %eax,%r15d
mov    %r14,%rdi
call   1040 <strlen@plt>
lea    (%r15,%rax,1),%edi
add    $0x1,%edi
mov    $0x1,%esi
call   1050 <calloc@plt>
mov    %rax,%r12
mov    %rax,%rdi
mov    %rbx,%rsi
mov    %r15d,%edx
call   1030 <strncpy@plt>
movslq %r15d,%rax
movb   $0x0,(%r12,%rax,1)
mov    %r12,%rdi
mov    %r14,%rsi
call   1060 <strcat@plt>
mov    %r12,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @AppendToBasename(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %3
store i8* %1, i8** %4
%8 = load i8*, i8** %3
%9 = call i64 @strlen(i8* %8)
%10 = trunc i64 %9 to i32
store i32 %10, i32* %7
%11 = load i32, i32* %7
%12 = sub nsw i32 %11, 1
store i32 %12, i32* %6
br label %13
13: ; preds = %26, %2
%14 = load i32, i32* %6
%15 = icmp sgt i32 %14, 0
br i1 %15, label %16, label %29
16: ; preds = %13
%17 = load i8*, i8** %3
%18 = load i32, i32* %6
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds i8, i8* %17, i64 %19
%21 = load i8, i8* %20
%22 = sext i8 %21 to i32
%23 = icmp eq i32 %22, 46
br i1 %23, label %24, label %25
24: ; preds = %16
br label %29
25: ; preds = %16
br label %26
26: ; preds = %25
%27 = load i32, i32* %6
%28 = add nsw i32 %27, -1
store i32 %28, i32* %6
br label %13
29: ; preds = %24, %13
%30 = load i32, i32* %6
%31 = icmp eq i32 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %29
%33 = load i32, i32* %7
store i32 %33, i32* %6
br label %34
34: ; preds = %32, %29
%35 = load i8*, i8** %4
%36 = call i64 @strlen(i8* %35)
%37 = trunc i64 %36 to i32
store i32 %37, i32* %7
%38 = load i32, i32* %6
%39 = load i32, i32* %7
%40 = add nsw i32 %38, %39
%41 = add nsw i32 %40, 1
%42 = call i64 @calloc(i32 %41, i32 1)
%43 = inttoptr i64 %42 to i8*
store i8* %43, i8** %5
%44 = load i8*, i8** %5
%45 = load i8*, i8** %3
%46 = load i32, i32* %6
%47 = call i32 @strncpy(i8* %44, i8* %45, i32 %46)
%48 = load i8*, i8** %5
%49 = load i32, i32* %6
%50 = sext i32 %49 to i64
%51 = getelementptr inbounds i8, i8* %48, i64 %50
store i8 0, i8* %51
%52 = load i8*, i8** %5
%53 = load i8*, i8** %4
%54 = call i32 @strcat(i8* %52, i8* %53)
%55 = load i8*, i8** %5
ret i8* %55
}

LLVM IR (-O2):
define i8* @AppendToBasename(i8* %0, i8* %1) {
%3 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%4 = trunc i64 %3 to i32
%5 = and i64 %3, 4294967295
%6 = call i32 @llvm.smin.i32(i32 %4, i32 1)
%7 = add i32 %6, -1
br label %8
8: ; preds = %13, %2
%9 = phi i64 [ %10, %13 ], [ %5, %2 ]
%10 = add nsw i64 %9, -1
%11 = trunc i64 %9 to i32
%12 = icmp sgt i32 %11, 1
br i1 %12, label %13, label %20
13: ; preds = %8
%14 = and i64 %10, 4294967295
%15 = getelementptr inbounds i8, i8* %0, i64 %14
%16 = load i8, i8* %15
%17 = icmp eq i8 %16, 46
br i1 %17, label %18, label %8
18: ; preds = %13
%19 = trunc i64 %10 to i32
br label %20
20: ; preds = %8, %18
%21 = phi i32 [ %19, %18 ], [ %7, %8 ]
%22 = icmp eq i32 %21, 0
%23 = select i1 %22, i32 %4, i32 %21
%24 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%25 = trunc i64 %24 to i32
%26 = add i32 %23, 1
%27 = add i32 %26, %25
%28 = tail call i64 @calloc(i32 %27, i32 1)
%29 = inttoptr i64 %28 to i8*
%30 = tail call i32 @strncpy(i8* %29, i8* %0, i32 %23)
%31 = sext i32 %23 to i64
%32 = getelementptr inbounds i8, i8* %29, i64 %31
store i8 0, i8* %32
%33 = tail call i32 @strcat(i8* %29, i8* %1)
ret i8* %29
}