#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 (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,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,%rcx
mov    -0x38(%rbp),%rax
add    %rcx,%rax
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x30(%rbp),%rdi
add    %rax,%rdi
add    $0x2,%rdi
call   1070 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%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    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1080 <strcat@plt>
mov    -0x20(%rbp),%rdi
mov    $0x2f,%esi
call   1050 <strrchr@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     123d <rebuildname+0xcd>
mov    -0x28(%rbp),%rdi
mov    $0x2e,%esi
call   1050 <strrchr@plt>
mov    %rax,-0x28(%rbp)
jmp    124f <rebuildname+0xdf>
mov    -0x20(%rbp),%rdi
mov    $0x2e,%esi
call   1050 <strrchr@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     1270 <rebuildname+0x100>
mov    -0x28(%rbp),%rdi
add    $0x1,%rdi
mov    -0x18(%rbp),%rsi
call   1030 <strcpy@plt>
jmp    128d <rebuildname+0x11d>
mov    -0x20(%rbp),%rdi
lea    0xd95(%rip),%rsi
call   1080 <strcat@plt>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1080 <strcat@plt>
mov    -0x20(%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     1229 <rebuildname+0xb9>
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>
test   %rax,%rax
je     1202 <rebuildname+0x92>
add    $0x1,%rax
mov    %rax,%rdi
mov    %r14,%rsi
call   1030 <strcpy@plt>
jmp    121c <rebuildname+0xac>
lea    0xe07(%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    0x2db0(%rip),%rax
mov    (%rax),%edi
lea    0xdc7(%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*
store i8* %0, i8** %4
store i8* %1, i8** %5
store i8* %2, i8** %6
%9 = load i8*, i8** %4
%10 = call i64 @strlen(i8* %9)
%11 = load i8*, i8** %5
%12 = call i64 @strlen(i8* %11)
%13 = add nsw i64 %10, %12
%14 = load i8*, i8** %6
%15 = call i64 @strlen(i8* %14)
%16 = add nsw i64 %13, %15
%17 = add nsw i64 %16, 2
%18 = call i8* @malloc(i64 %17)
store i8* %18, i8** %7
%19 = load i8*, i8** %7
%20 = icmp eq i8* %19, null
br i1 %20, label %21, label %25
21: ; preds = %3
%22 = load i32, i32* @stderr
%23 = call i32 @fprintf(i32 %22, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%24 = call i32 @exit(i32 1)
unreachable
25: ; preds = %3
%26 = load i8*, i8** %7
%27 = load i8*, i8** %4
%28 = call i32 @strcpy(i8* %26, i8* %27)
%29 = load i8*, i8** %7
%30 = load i8*, i8** %5
%31 = call i32 @strcat(i8* %29, i8* %30)
%32 = load i8*, i8** %7
%33 = call i8* @strrchr(i8* %32, i8 signext 47)
store i8* %33, i8** %8
%34 = load i8*, i8** %8
%35 = icmp ne i8* %34, null
br i1 %35, label %36, label %39
36: ; preds = %25
%37 = load i8*, i8** %8
%38 = call i8* @strrchr(i8* %37, i8 signext 46)
store i8* %38, i8** %8
br label %42
39: ; preds = %25
%40 = load i8*, i8** %7
%41 = call i8* @strrchr(i8* %40, i8 signext 46)
store i8* %41, i8** %8
br label %42
42: ; preds = %39, %36
%43 = load i8*, i8** %8
%44 = icmp ne i8* %43, null
br i1 %44, label %45, label %50
45: ; preds = %42
%46 = load i8*, i8** %8
%47 = getelementptr inbounds i8, i8* %46, i64 1
%48 = load i8*, i8** %6
%49 = call i32 @strcpy(i8* %47, i8* %48)
br label %56
50: ; preds = %42
%51 = load i8*, i8** %7
%52 = call i32 @strcat(i8* %51, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%53 = load i8*, i8** %7
%54 = load i8*, i8** %6
%55 = call i32 @strcat(i8* %53, i8* %54)
br label %56
56: ; preds = %50, %45
%57 = load i8*, i8** %7
ret i8* %57
}

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 = icmp eq i8* %22, null
br i1 %23, label %27, label %24
24: ; preds = %16
%25 = getelementptr inbounds i8, i8* %22, i64 1
%26 = tail call i32 @strcpy(i8* %25, i8* %2)
br label %30
27: ; preds = %16
%28 = tail call i32 @strcat(i8* %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%29 = tail call i32 @strcat(i8* %10, i8* %2)
br label %30
30: ; preds = %27, %24
ret i8* %10
}