#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*/ strcpy(char *, char const *);
int strlen(char const *);
char *strrchr(char const *, char);
char *xmalloc(int);
char *extendfile(const char *path, const char *ext) {
  char *res;
  const char *p;
  const char *file;
  if ((file = strrchr(path, '/')) == NULL)
    file = path;
  else
    file++;
  res = xmalloc(strlen(file) + strlen(ext) + 1);
  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   1050 <strrchr@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    1179 <extendfile+0x39>
mov    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
jmp    1185 <extendfile+0x45>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%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   1060 <xmalloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x28(%rbp),%rdi
mov    $0x2e,%esi
call   1050 <strrchr@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11e9 <extendfile+0xa9>
mov    -0x28(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x28(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x38(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rax
mov    -0x28(%rbp),%rcx
sub    %rcx,%rax
add    %rax,%rdi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x18(%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   1050 <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   1060 <xmalloc@plt>
mov    %rax,%r15
mov    %r12,%rdi
mov    $0x2e,%esi
call   1050 <strrchr@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11a6 <extendfile+0x66>
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):
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** %7
%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** %7
br label %16
13: ; preds = %2
%14 = load i8*, i8** %7
%15 = getelementptr inbounds i8, i8* %14, i32 1
store i8* %15, i8** %7
br label %16
16: ; preds = %13, %11
%17 = load i8*, i8** %7
%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 i8* @xmalloc(i32 %22)
store i8* %23, i8** %5
%24 = load i8*, i8** %7
%25 = call i8* @strrchr(i8* %24, i8 signext 46)
store i8* %25, i8** %6
%26 = load i8*, i8** %6
%27 = icmp eq i8* %26, null
br i1 %27, label %28, label %34
28: ; preds = %16
%29 = load i8*, i8** %7
%30 = load i8*, i8** %7
%31 = call i32 @strlen(i8* %30)
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds i8, i8* %29, i64 %32
store i8* %33, i8** %6
br label %34
34: ; preds = %28, %16
%35 = load i8*, i8** %5
%36 = load i8*, i8** %7
%37 = call i32 @strcpy(i8* %35, i8* %36)
%38 = load i8*, i8** %5
%39 = load i8*, i8** %6
%40 = load i8*, i8** %7
%41 = ptrtoint i8* %39 to i64
%42 = ptrtoint i8* %40 to i64
%43 = sub i64 %41, %42
%44 = getelementptr inbounds i8, i8* %38, i64 %43
%45 = load i8*, i8** %4
%46 = call i32 @strcpy(i8* %44, i8* %45)
%47 = load i8*, i8** %5
ret i8* %47
}

LLVM IR (-O2):
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 i8* @xmalloc(i32 %10)
%12 = tail call i8* @strrchr(i8* dereferenceable(1) %6, i8 signext 46)
%13 = icmp eq i8* %12, null
br i1 %13, label %14, label %18
14: ; preds = %2
%15 = tail call i32 @strlen(i8* %6)
%16 = sext i32 %15 to i64
%17 = getelementptr inbounds i8, i8* %6, i64 %16
br label %18
18: ; preds = %14, %2
%19 = phi i8* [ %17, %14 ], [ %12, %2 ]
%20 = tail call i32 @strcpy(i8* %11, i8* %6)
%21 = ptrtoint i8* %19 to i64
%22 = ptrtoint i8* %6 to i64
%23 = sub i64 %21, %22
%24 = getelementptr inbounds i8, i8* %11, i64 %23
%25 = tail call i32 @strcpy(i8* %24, i8* %1)
ret i8* %11
}