#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*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
char *malloc(scalar_t__);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcat(char *, char const *);
int /*<<< orphan*/ strcpy(char *, char const *);
scalar_t__ strlen(char const *);
char *strrchr(char *, char);
char *rebuildname(const char *r, const char *i, char *ext) {
  char *p = malloc(strlen(r) + strlen(i) + strlen(ext) + 2);
  char *t;
  if (p == NULL) {
    fprintf(stderr, "Out of memory.\n");
    exit(1);
  }
  strcpy(p, r);
  strcat(p, i);
  t = strrchr(p, '/');
  if (t)
    t = strrchr(t, '.');
  else
    t = strrchr(p, '.');
  if (*ext == 0) {
    *t = 0;
    return p;
  }
  if (t)
    strcpy(t + 1, ext);
  else {
    strcat(p, ".");
    strcat(p, ext);
  }
  return p;
}


x86 assembly (clang, -O0):
<rebuildname>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x40(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,%rcx
mov    -0x40(%rbp),%rax
add    %rcx,%rax
mov    %rax,-0x38(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x38(%rbp),%rdi
add    %rax,%rdi
add    $0x2,%rdi
call   1070 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11ef <rebuildname+0x7f>
mov    0x2e09(%rip),%rax
mov    (%rax),%edi
lea    0xe20(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1080 <strcat@plt>
mov    -0x28(%rbp),%rdi
mov    $0x2f,%esi
call   1050 <strrchr@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     123d <rebuildname+0xcd>
mov    -0x30(%rbp),%rdi
mov    $0x2e,%esi
call   1050 <strrchr@plt>
mov    %rax,-0x30(%rbp)
jmp    124f <rebuildname+0xdf>
mov    -0x28(%rbp),%rdi
mov    $0x2e,%esi
call   1050 <strrchr@plt>
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    1273 <rebuildname+0x103>
mov    -0x30(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12b9 <rebuildname+0x149>
cmpq   $0x0,-0x30(%rbp)
je     1294 <rebuildname+0x124>
mov    -0x30(%rbp),%rdi
add    $0x1,%rdi
mov    -0x20(%rbp),%rsi
call   1030 <strcpy@plt>
jmp    12b1 <rebuildname+0x141>
mov    -0x28(%rbp),%rdi
lea    0xd71(%rip),%rsi
call   1080 <strcat@plt>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1080 <strcat@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rebuildname>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%r12
call   1040 <strlen@plt>
mov    %rax,%r13
mov    %r15,%rdi
call   1040 <strlen@plt>
mov    %rax,%rbx
mov    %r14,%rdi
call   1040 <strlen@plt>
add    %r13,%rbx
lea    (%rax,%rbx,1),%rdi
add    $0x2,%rdi
call   1070 <malloc@plt>
test   %rax,%rax
je     1238 <rebuildname+0xc8>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r12,%rsi
call   1030 <strcpy@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
call   1080 <strcat@plt>
mov    %rbx,%rdi
mov    $0x2f,%esi
call   1050 <strrchr@plt>
test   %rax,%rax
mov    %rbx,%rdi
cmovne %rax,%rdi
mov    $0x2e,%esi
call   1050 <strrchr@plt>
cmpb   $0x0,(%r14)
je     120c <rebuildname+0x9c>
test   %rax,%rax
je     1211 <rebuildname+0xa1>
add    $0x1,%rax
mov    %rax,%rdi
mov    %r14,%rsi
call   1030 <strcpy@plt>
jmp    122b <rebuildname+0xbb>
movb   $0x0,(%rax)
jmp    122b <rebuildname+0xbb>
lea    0xdf8(%rip),%rsi
mov    %rbx,%rdi
call   1080 <strcat@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1080 <strcat@plt>
mov    %rbx,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret
mov    0x2da1(%rip),%rax
mov    (%rax),%edi
lea    0xdb8(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [16 x i8] c"Out of memory.\0A\00"
@.str.1 = [2 x i8] c".\00"
define i8* @rebuildname(i8* %0, i8* %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
store i8* %0, i8** %5
store i8* %1, i8** %6
store i8* %2, i8** %7
%10 = load i8*, i8** %5
%11 = call i64 @strlen(i8* %10)
%12 = load i8*, i8** %6
%13 = call i64 @strlen(i8* %12)
%14 = add nsw i64 %11, %13
%15 = load i8*, i8** %7
%16 = call i64 @strlen(i8* %15)
%17 = add nsw i64 %14, %16
%18 = add nsw i64 %17, 2
%19 = call i8* @malloc(i64 %18)
store i8* %19, i8** %8
%20 = load i8*, i8** %8
%21 = icmp eq i8* %20, null
br i1 %21, label %22, label %26
22: ; preds = %3
%23 = load i32, i32* @stderr
%24 = call i32 @fprintf(i32 %23, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%25 = call i32 @exit(i32 1)
unreachable
26: ; preds = %3
%27 = load i8*, i8** %8
%28 = load i8*, i8** %5
%29 = call i32 @strcpy(i8* %27, i8* %28)
%30 = load i8*, i8** %8
%31 = load i8*, i8** %6
%32 = call i32 @strcat(i8* %30, i8* %31)
%33 = load i8*, i8** %8
%34 = call i8* @strrchr(i8* %33, i8 signext 47)
store i8* %34, i8** %9
%35 = load i8*, i8** %9
%36 = icmp ne i8* %35, null
br i1 %36, label %37, label %40
37: ; preds = %26
%38 = load i8*, i8** %9
%39 = call i8* @strrchr(i8* %38, i8 signext 46)
store i8* %39, i8** %9
br label %43
40: ; preds = %26
%41 = load i8*, i8** %8
%42 = call i8* @strrchr(i8* %41, i8 signext 46)
store i8* %42, i8** %9
br label %43
43: ; preds = %40, %37
%44 = load i8*, i8** %7
%45 = load i8, i8* %44
%46 = sext i8 %45 to i32
%47 = icmp eq i32 %46, 0
br i1 %47, label %48, label %51
48: ; preds = %43
%49 = load i8*, i8** %9
store i8 0, i8* %49
%50 = load i8*, i8** %8
store i8* %50, i8** %4
br label %67
51: ; preds = %43
%52 = load i8*, i8** %9
%53 = icmp ne i8* %52, null
br i1 %53, label %54, label %59
54: ; preds = %51
%55 = load i8*, i8** %9
%56 = getelementptr inbounds i8, i8* %55, i64 1
%57 = load i8*, i8** %7
%58 = call i32 @strcpy(i8* %56, i8* %57)
br label %65
59: ; preds = %51
%60 = load i8*, i8** %8
%61 = call i32 @strcat(i8* %60, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%62 = load i8*, i8** %8
%63 = load i8*, i8** %7
%64 = call i32 @strcat(i8* %62, i8* %63)
br label %65
65: ; preds = %59, %54
%66 = load i8*, i8** %8
store i8* %66, i8** %4
br label %67
67: ; preds = %65, %48
%68 = load i8*, i8** %4
ret i8* %68
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [16 x i8] c"Out of memory.\0A\00"
@.str.1 = [2 x i8] c".\00"
define i8* @rebuildname(i8* %0, i8* %1, i8* %2) {
%4 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%5 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%6 = tail call i64 @strlen(i8* dereferenceable(1) %2)
%7 = add i64 %4, 2
%8 = add i64 %7, %5
%9 = add i64 %8, %6
%10 = tail call i8* @malloc(i64 %9)
%11 = icmp eq i8* %10, null
br i1 %11, label %12, label %16
12: ; preds = %3
%13 = load i32, i32* @stderr
%14 = tail call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%15 = tail call i32 @exit(i32 1)
unreachable
16: ; preds = %3
%17 = tail call i32 @strcpy(i8* %10, i8* %0)
%18 = tail call i32 @strcat(i8* %10, i8* %1)
%19 = tail call i8* @strrchr(i8* %10, i8 signext 47)
%20 = icmp eq i8* %19, null
%21 = select i1 %20, i8* %10, i8* %19
%22 = tail call i8* @strrchr(i8* %21, i8 signext 46)
%23 = load i8, i8* %2
%24 = icmp eq i8 %23, 0
br i1 %24, label %25, label %26
25: ; preds = %16
store i8 0, i8* %22
br label %34
26: ; preds = %16
%27 = icmp eq i8* %22, null
br i1 %27, label %31, label %28
28: ; preds = %26
%29 = getelementptr inbounds i8, i8* %22, i64 1
%30 = tail call i32 @strcpy(i8* %29, i8* %2)
br label %34
31: ; preds = %26
%32 = tail call i32 @strcat(i8* %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%33 = tail call i32 @strcat(i8* %10, i8* %2)
br label %34
34: ; preds = %28, %31, %25
ret i8* %10
}