#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 */
int /*<<< orphan*/ abort();
char *malloc(unsigned int);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
char *strrchr(char *, char);
char *extendfile(char *file, char *ext) {
  char *res;
  char *p;
  res = malloc((unsigned)(strlen(file) + strlen(ext) + 1));
  if (res == NULL) {
    abort();
  }
  p = strrchr(file, '.');
  if (p == NULL) {
    p = file + strlen(file);
  }
  (void)strcpy(res, file);
  (void)strcpy(res + (p - file), ext);
  return (res);
}


x86 assembly (clang, -O0):
<extendfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x24(%rbp),%edi
add    %eax,%edi
add    $0x1,%edi
call   1070 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1198 <extendfile+0x48>
mov    $0x0,%al
call   1030 <abort@plt>
mov    -0x8(%rbp),%rdi
mov    $0x2e,%esi
call   1060 <strrchr@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11d6 <extendfile+0x86>
mov    -0x8(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%ecx
mov    -0x30(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rax
mov    -0x8(%rbp),%rcx
sub    %rcx,%rax
add    %rax,%rdi
mov    -0x10(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x18(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<extendfile>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r12
call   1050 <strlen@plt>
mov    %eax,%ebx
mov    %r14,%rdi
call   1050 <strlen@plt>
lea    (%rbx,%rax,1),%edi
add    $0x1,%edi
call   1070 <malloc@plt>
test   %rax,%rax
je     11ce <extendfile+0x7e>
mov    %rax,%r15
mov    %r12,%rdi
mov    $0x2e,%esi
call   1060 <strrchr@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11a3 <extendfile+0x53>
mov    %r12,%rdi
call   1050 <strlen@plt>
movslq %eax,%rbx
add    %r12,%rbx
mov    %r15,%rdi
mov    %r12,%rsi
call   1040 <strcpy@plt>
sub    %r12,%rbx
mov    %r15,%rdi
add    %rbx,%rdi
mov    %r14,%rsi
call   1040 <strcpy@plt>
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
xor    %eax,%eax
call   1030 <abort@plt>

LLVM IR (-O0):
define i8* @extendfile(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
store i8* %0, i8** %3
store i8* %1, i8** %4
%7 = load i8*, i8** %3
%8 = call i32 @strlen(i8* %7)
%9 = load i8*, i8** %4
%10 = call i32 @strlen(i8* %9)
%11 = add nsw i32 %8, %10
%12 = add nsw i32 %11, 1
%13 = call i8* @malloc(i32 %12)
store i8* %13, i8** %5
%14 = load i8*, i8** %5
%15 = icmp eq i8* %14, null
br i1 %15, label %16, label %18
16: ; preds = %2
%17 = call i32 (...) @abort()
unreachable
18: ; preds = %2
%19 = load i8*, i8** %3
%20 = call i8* @strrchr(i8* %19, i8 signext 46)
store i8* %20, i8** %6
%21 = load i8*, i8** %6
%22 = icmp eq i8* %21, null
br i1 %22, label %23, label %29
23: ; preds = %18
%24 = load i8*, i8** %3
%25 = load i8*, i8** %3
%26 = call i32 @strlen(i8* %25)
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds i8, i8* %24, i64 %27
store i8* %28, i8** %6
br label %29
29: ; preds = %23, %18
%30 = load i8*, i8** %5
%31 = load i8*, i8** %3
%32 = call i32 @strcpy(i8* %30, i8* %31)
%33 = load i8*, i8** %5
%34 = load i8*, i8** %6
%35 = load i8*, i8** %3
%36 = ptrtoint i8* %34 to i64
%37 = ptrtoint i8* %35 to i64
%38 = sub i64 %36, %37
%39 = getelementptr inbounds i8, i8* %33, i64 %38
%40 = load i8*, i8** %4
%41 = call i32 @strcpy(i8* %39, i8* %40)
%42 = load i8*, i8** %5
ret i8* %42
}

LLVM IR (-O2):
define i8* @extendfile(i8* %0, i8* %1) {
%3 = tail call i32 @strlen(i8* %0)
%4 = tail call i32 @strlen(i8* %1)
%5 = add i32 %3, 1
%6 = add i32 %5, %4
%7 = tail call i8* @malloc(i32 %6)
%8 = icmp eq i8* %7, null
br i1 %8, label %9, label %11
9: ; preds = %2
%10 = tail call i32 (...) @abort()
unreachable
11: ; preds = %2
%12 = tail call i8* @strrchr(i8* dereferenceable(1) %0, i8 signext 46)
%13 = icmp eq i8* %12, null
br i1 %13, label %14, label %18
14: ; preds = %11
%15 = tail call i32 @strlen(i8* %0)
%16 = sext i32 %15 to i64
%17 = getelementptr inbounds i8, i8* %0, i64 %16
br label %18
18: ; preds = %14, %11
%19 = phi i8* [ %17, %14 ], [ %12, %11 ]
%20 = tail call i32 @strcpy(i8* %7, i8* %0)
%21 = ptrtoint i8* %19 to i64
%22 = ptrtoint i8* %0 to i64
%23 = sub i64 %21, %22
%24 = getelementptr inbounds i8, i8* %7, i64 %23
%25 = tail call i32 @strcpy(i8* %24, i8* %1)
ret i8* %7
}