#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_FAILURE;
char *calloc(int, scalar_t__);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ sprintf(char *, char *, char *, ...);
int /*<<< orphan*/ stderr;
scalar_t__ strcmp(char *, char *);
scalar_t__ strlen(char *);
char *append_path(char *path, char *last) {
  char *ret = calloc(1, strlen(path) + strlen(last) + 2);
  if (ret == NULL) {
    fprintf(stderr, "Unable to allocate memory.\n");
    exit(EXIT_FAILURE);
  }
  if (strcmp(path, "/"))
    sprintf(ret, "%s/%s", path, last);
  else
    sprintf(ret, "/%s", last);
  return ret;
}


x86 assembly (clang, -O0):
<append_path>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x20(%rbp),%rsi
add    %rax,%rsi
add    $0x2,%rsi
mov    $0x1,%edi
call   1040 <calloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11cd <append_path+0x6d>
mov    0x2e2f(%rip),%rax
mov    (%rax),%edi
lea    0xe46(%rip),%rsi
call   1060 <fprintf@plt>
mov    0x2e12(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    -0x8(%rbp),%rdi
lea    0xe44(%rip),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%rax
je     1206 <append_path+0xa6>
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rdx
mov    -0x10(%rbp),%rcx
lea    0xe24(%rip),%rsi
mov    $0x0,%al
call   1070 <sprintf@plt>
jmp    121c <append_path+0xbc>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rdx
lea    0xe0b(%rip),%rsi
mov    $0x0,%al
call   1070 <sprintf@plt>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<append_path>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %rdi,%r15
call   1030 <strlen@plt>
mov    %rax,%rbx
mov    %r14,%rdi
call   1030 <strlen@plt>
lea    (%rbx,%rax,1),%rsi
add    $0x2,%rsi
mov    $0x1,%edi
call   1040 <calloc@plt>
test   %rax,%rax
je     11df <append_path+0x7f>
mov    %rax,%rbx
lea    0xe80(%rip),%rsi
mov    %r15,%rdi
call   1050 <strcmp@plt>
test   %rax,%rax
je     11c2 <append_path+0x62>
lea    0xe6e(%rip),%rsi
mov    %rbx,%rdi
mov    %r15,%rdx
mov    %r14,%rcx
xor    %eax,%eax
call   1070 <sprintf@plt>
jmp    11d6 <append_path+0x76>
lea    0xe57(%rip),%rsi
mov    %rbx,%rdi
mov    %r14,%rdx
xor    %eax,%eax
call   1070 <sprintf@plt>
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret
mov    0x2dfa(%rip),%rax
mov    (%rax),%edi
lea    0xe11(%rip),%rsi
call   1060 <fprintf@plt>
mov    0x2ddd(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [28 x i8] c"Unable to allocate memory.\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [2 x i8] c"/\00"
@.str.2 = [6 x i8] c"%s/%s\00"
@.str.3 = [4 x i8] c"/%s\00"
define i8* @append_path(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
store i8* %0, i8** %3
store i8* %1, i8** %4
%6 = load i8*, i8** %3
%7 = call i64 @strlen(i8* %6)
%8 = load i8*, i8** %4
%9 = call i64 @strlen(i8* %8)
%10 = add nsw i64 %7, %9
%11 = add nsw i64 %10, 2
%12 = call i8* @calloc(i32 1, i64 %11)
store i8* %12, i8** %5
%13 = load i8*, i8** %5
%14 = icmp eq i8* %13, null
br i1 %14, label %15, label %20
15: ; preds = %2
%16 = load i32, i32* @stderr
%17 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
%18 = load i32, i32* @EXIT_FAILURE
%19 = call i32 @exit(i32 %18)
unreachable
20: ; preds = %2
%21 = load i8*, i8** %3
%22 = call i64 @strcmp(i8* %21, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%23 = icmp ne i64 %22, 0
br i1 %23, label %24, label %29
24: ; preds = %20
%25 = load i8*, i8** %5
%26 = load i8*, i8** %3
%27 = load i8*, i8** %4
%28 = call i32 (i8*, i8*, i8*, ...) @sprintf(i8* %25, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %26, i8* %27)
br label %33
29: ; preds = %20
%30 = load i8*, i8** %5
%31 = load i8*, i8** %4
%32 = call i32 (i8*, i8*, i8*, ...) @sprintf(i8* %30, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* %31)
br label %33
33: ; preds = %29, %24
%34 = load i8*, i8** %5
ret i8* %34
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [28 x i8] c"Unable to allocate memory.\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [2 x i8] c"/\00"
@.str.2 = [6 x i8] c"%s/%s\00"
@.str.3 = [4 x i8] c"/%s\00"
define i8* @append_path(i8* %0, i8* %1) {
%3 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%4 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%5 = add i64 %3, 2
%6 = add i64 %5, %4
%7 = tail call i8* @calloc(i32 1, i64 %6)
%8 = icmp eq i8* %7, null
br i1 %8, label %9, label %14
9: ; preds = %2
%10 = load i32, i32* @stderr
%11 = tail call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
%12 = load i32, i32* @EXIT_FAILURE
%13 = tail call i32 @exit(i32 %12)
unreachable
14: ; preds = %2
%15 = tail call i64 @strcmp(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%16 = icmp eq i64 %15, 0
br i1 %16, label %19, label %17
17: ; preds = %14
%18 = tail call i32 (i8*, i8*, i8*, ...) @sprintf(i8* %7, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %0, i8* %1)
br label %21
19: ; preds = %14
%20 = tail call i32 (i8*, i8*, i8*, ...) @sprintf(i8* %7, i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* %1)
br label %21
21: ; preds = %19, %17
ret i8* %7
}