#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*/ errx(int, char *);
scalar_t__ malloc(unsigned int);
int /*<<< orphan*/ strcpy(char *, char const *);
int strlen(char const *);
char *strrchr(char const *, char);
char *extendfile(const char *path, const char *ext) {
  const char *file;
  char *res;
  const char *p;
  if ((file = strrchr(path, '/')) == NULL)
    file = path;
  else
    file++;
  res = (void *)malloc((unsigned)(strlen(file) + strlen(ext) + 1));
  if (res == NULL) {
    errx(1, "Out of memory");
  }
  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    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
mov    $0x2f,%esi
call   1060 <strrchr@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    1189 <extendfile+0x39>
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
jmp    1195 <extendfile+0x45>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x2c(%rbp),%edi
add    %eax,%edi
add    $0x1,%edi
call   1070 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11d7 <extendfile+0x87>
mov    $0x1,%edi
lea    0xe2e(%rip),%rsi
call   1050 <errx@plt>
mov    -0x18(%rbp),%rdi
mov    $0x2e,%esi
call   1060 <strrchr@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1215 <extendfile+0xc5>
mov    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x38(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rax
mov    -0x18(%rbp),%rcx
sub    %rcx,%rax
add    %rax,%rdi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x20(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<extendfile>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
mov    $0x2f,%esi
call   1060 <strrchr@plt>
lea    0x1(%rax),%r12
test   %rax,%rax
cmove  %rbx,%r12
mov    %r12,%rdi
call   1040 <strlen@plt>
mov    %eax,%ebx
mov    %r14,%rdi
call   1040 <strlen@plt>
lea    (%rbx,%rax,1),%edi
add    $0x1,%edi
call   1070 <malloc@plt>
mov    %rax,%r15
test   %rax,%rax
jne    11a9 <extendfile+0x59>
lea    0xe61(%rip),%rsi
mov    $0x1,%edi
call   1050 <errx@plt>
mov    %r12,%rdi
mov    $0x2e,%esi
call   1060 <strrchr@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11cc <extendfile+0x7c>
mov    %r12,%rdi
call   1040 <strlen@plt>
movslq %eax,%rbx
add    %r12,%rbx
mov    %r15,%rdi
mov    %r12,%rsi
call   1030 <strcpy@plt>
sub    %r12,%rbx
mov    %r15,%rdi
add    %rbx,%rdi
mov    %r14,%rsi
call   1030 <strcpy@plt>
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [14 x i8] c"Out of memory\00"
define i8* @extendfile(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 i8* @strrchr(i8* %8, i8 signext 47)
store i8* %9, i8** %5
%10 = icmp eq i8* %9, null
br i1 %10, label %11, label %13
11: ; preds = %2
%12 = load i8*, i8** %3
store i8* %12, i8** %5
br label %16
13: ; preds = %2
%14 = load i8*, i8** %5
%15 = getelementptr inbounds i8, i8* %14, i32 1
store i8* %15, i8** %5
br label %16
16: ; preds = %13, %11
%17 = load i8*, i8** %5
%18 = call i32 @strlen(i8* %17)
%19 = load i8*, i8** %4
%20 = call i32 @strlen(i8* %19)
%21 = add nsw i32 %18, %20
%22 = add nsw i32 %21, 1
%23 = call i64 @malloc(i32 %22)
%24 = inttoptr i64 %23 to i8*
store i8* %24, i8** %6
%25 = load i8*, i8** %6
%26 = icmp eq i8* %25, null
br i1 %26, label %27, label %29
27: ; preds = %16
%28 = call i32 @errx(i32 1, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %29
29: ; preds = %27, %16
%30 = load i8*, i8** %5
%31 = call i8* @strrchr(i8* %30, i8 signext 46)
store i8* %31, i8** %7
%32 = load i8*, i8** %7
%33 = icmp eq i8* %32, null
br i1 %33, label %34, label %40
34: ; preds = %29
%35 = load i8*, i8** %5
%36 = load i8*, i8** %5
%37 = call i32 @strlen(i8* %36)
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds i8, i8* %35, i64 %38
store i8* %39, i8** %7
br label %40
40: ; preds = %34, %29
%41 = load i8*, i8** %6
%42 = load i8*, i8** %5
%43 = call i32 @strcpy(i8* %41, i8* %42)
%44 = load i8*, i8** %6
%45 = load i8*, i8** %7
%46 = load i8*, i8** %5
%47 = ptrtoint i8* %45 to i64
%48 = ptrtoint i8* %46 to i64
%49 = sub i64 %47, %48
%50 = getelementptr inbounds i8, i8* %44, i64 %49
%51 = load i8*, i8** %4
%52 = call i32 @strcpy(i8* %50, i8* %51)
%53 = load i8*, i8** %6
ret i8* %53
}

LLVM IR (-O2):
@.str = [14 x i8] c"Out of memory\00"
define i8* @extendfile(i8* %0, i8* %1) {
%3 = tail call i8* @strrchr(i8* dereferenceable(1) %0, i8 signext 47)
%4 = icmp eq i8* %3, null
%5 = getelementptr inbounds i8, i8* %3, i64 1
%6 = select i1 %4, i8* %0, i8* %5
%7 = tail call i32 @strlen(i8* %6)
%8 = tail call i32 @strlen(i8* %1)
%9 = add i32 %7, 1
%10 = add i32 %9, %8
%11 = tail call i64 @malloc(i32 %10)
%12 = inttoptr i64 %11 to i8*
%13 = icmp eq i64 %11, 0
br i1 %13, label %14, label %16
14: ; preds = %2
%15 = tail call i32 @errx(i32 1, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %16
16: ; preds = %14, %2
%17 = tail call i8* @strrchr(i8* dereferenceable(1) %6, i8 signext 46)
%18 = icmp eq i8* %17, null
br i1 %18, label %19, label %23
19: ; preds = %16
%20 = tail call i32 @strlen(i8* %6)
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds i8, i8* %6, i64 %21
br label %23
23: ; preds = %19, %16
%24 = phi i8* [ %22, %19 ], [ %17, %16 ]
%25 = tail call i32 @strcpy(i8* %12, i8* %6)
%26 = ptrtoint i8* %24 to i64
%27 = ptrtoint i8* %6 to i64
%28 = sub i64 %26, %27
%29 = getelementptr inbounds i8, i8* %12, i64 %28
%30 = tail call i32 @strcpy(i8* %29, i8* %1)
ret i8* %12
}