#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)
    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    $0x2e,%esi
call   1050 <strrchr@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     123c <rebuildname+0xcc>
mov    -0x28(%rbp),%rdi
add    $0x1,%rdi
mov    -0x18(%rbp),%rsi
call   1030 <strcpy@plt>
jmp    1259 <rebuildname+0xe9>
mov    -0x20(%rbp),%rdi
lea    0xdc9(%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     1215 <rebuildname+0xa5>
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    $0x2e,%esi
call   1050 <strrchr@plt>
test   %rax,%rax
je     11ee <rebuildname+0x7e>
add    $0x1,%rax
mov    %rax,%rdi
mov    %r14,%rsi
call   1030 <strcpy@plt>
jmp    1208 <rebuildname+0x98>
lea    0xe1b(%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    0x2dc4(%rip),%rax
mov    (%rax),%edi
lea    0xddb(%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 46)
store i8* %33, i8** %8
%34 = load i8*, i8** %8
%35 = icmp ne i8* %34, null
br i1 %35, label %36, label %41
36: ; preds = %25
%37 = load i8*, i8** %8
%38 = getelementptr inbounds i8, i8* %37, i64 1
%39 = load i8*, i8** %6
%40 = call i32 @strcpy(i8* %38, i8* %39)
br label %47
41: ; preds = %25
%42 = load i8*, i8** %7
%43 = call i32 @strcat(i8* %42, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%44 = load i8*, i8** %7
%45 = load i8*, i8** %6
%46 = call i32 @strcat(i8* %44, i8* %45)
br label %47
47: ; preds = %41, %36
%48 = load i8*, i8** %7
ret i8* %48
}

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