#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*/ free(char *);
char *malloc(int);
int /*<<< orphan*/ str_ends_with(char const *, char *);
scalar_t__ str_starts_with(char const *, char *);
int /*<<< orphan*/ strcat(char *, char const *);
int /*<<< orphan*/ strcpy(char *, char const *);
char *strdup(char const *);
int strlen(char const *);
char *path_join(const char *dir, const char *file) {
  int size = strlen(dir) + strlen(file) + 2;
  char *buf = malloc(size * sizeof(char));
  if (NULL == buf)
    return NULL;
  strcpy(buf, dir);
  if (!str_ends_with(dir, "/")) {
    strcat(buf, "/");
  }
  if (str_starts_with(file, "/")) {
    char *filecopy = strdup(file);
    if (NULL == filecopy) {
      free(buf);
      return NULL;
    }
    strcat(buf, ++filecopy);
    free(--filecopy);
  } else {
    strcat(buf, file);
  }
  return buf;
}


x86 assembly (clang, -O0):
<path_join>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,-0x34(%rbp)
mov    -0x18(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,%ecx
mov    -0x34(%rbp),%eax
add    %ecx,%eax
add    $0x2,%eax
mov    %eax,-0x1c(%rbp)
movslq -0x1c(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1080 <malloc@plt>
mov    %rax,-0x28(%rbp)
xor    %eax,%eax
cmp    -0x28(%rbp),%rax
jne    11de <path_join+0x5e>
movq   $0x0,-0x8(%rbp)
jmp    129d <path_join+0x11d>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x10(%rbp),%rdi
lea    0xe0a(%rip),%rsi
call   1050 <str_ends_with@plt>
cmp    $0x0,%eax
jne    1214 <path_join+0x94>
mov    -0x28(%rbp),%rdi
lea    0xdf1(%rip),%rsi
call   1090 <strcat@plt>
mov    -0x18(%rbp),%rdi
lea    0xde1(%rip),%rsi
call   1070 <str_starts_with@plt>
cmp    $0x0,%rax
je     1288 <path_join+0x108>
mov    -0x18(%rbp),%rdi
call   10a0 <strdup@plt>
mov    %rax,-0x30(%rbp)
xor    %eax,%eax
cmp    -0x30(%rbp),%rax
jne    125d <path_join+0xdd>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    129d <path_join+0x11d>
mov    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%rsi
add    $0x1,%rsi
mov    %rsi,-0x30(%rbp)
call   1090 <strcat@plt>
mov    -0x30(%rbp),%rdi
add    $0xffffffffffffffff,%rdi
mov    %rdi,-0x30(%rbp)
call   1030 <free@plt>
jmp    1295 <path_join+0x115>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1090 <strcat@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<path_join>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r12
call   1060 <strlen@plt>
mov    %eax,%ebx
mov    %r14,%rdi
call   1060 <strlen@plt>
lea    (%rbx,%rax,1),%edi
add    $0x2,%edi
call   1080 <malloc@plt>
test   %rax,%rax
je     1233 <path_join+0xb3>
mov    %rax,%r15
mov    %rax,%rdi
mov    %r12,%rsi
call   1040 <strcpy@plt>
lea    0xe3a(%rip),%rsi
mov    %r12,%rdi
call   1050 <str_ends_with@plt>
test   %eax,%eax
jne    11e1 <path_join+0x61>
lea    0xe27(%rip),%rsi
mov    %r15,%rdi
call   1090 <strcat@plt>
lea    0xe18(%rip),%rsi
mov    %r14,%rdi
call   1070 <str_starts_with@plt>
test   %rax,%rax
je     121e <path_join+0x9e>
mov    %r14,%rdi
call   10a0 <strdup@plt>
test   %rax,%rax
je     122b <path_join+0xab>
mov    %rax,%rbx
mov    %rax,%rsi
add    $0x1,%rsi
mov    %r15,%rdi
call   1090 <strcat@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
jmp    1236 <path_join+0xb6>
mov    %r15,%rdi
mov    %r14,%rsi
call   1090 <strcat@plt>
jmp    1236 <path_join+0xb6>
mov    %r15,%rdi
call   1030 <free@plt>
xor    %r15d,%r15d
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
@.str = [2 x i8] c"/\00"
define i8* @path_join(i8* %0, i8* %1) {
%3 = tail call i32 @strlen(i8* %0)
%4 = tail call i32 @strlen(i8* %1)
%5 = add i32 %3, 2
%6 = add i32 %5, %4
%7 = tail call i8* @malloc(i32 %6)
%8 = icmp eq i8* %7, null
br i1 %8, label %29, label %9
9: ; preds = %2
%10 = tail call i32 @strcpy(i8* %7, i8* %0)
%11 = tail call i32 @str_ends_with(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%12 = icmp eq i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %9
%14 = tail call i32 @strcat(i8* %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
br label %15
15: ; preds = %13, %9
%16 = tail call i64 @str_starts_with(i8* %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%17 = icmp eq i64 %16, 0
br i1 %17, label %27, label %18
18: ; preds = %15
%19 = tail call i8* @strdup(i8* %1)
%20 = icmp eq i8* %19, null
br i1 %20, label %21, label %23
21: ; preds = %18
%22 = tail call i32 @free(i8* %7)
br label %29
23: ; preds = %18
%24 = getelementptr inbounds i8, i8* %19, i64 1
%25 = tail call i32 @strcat(i8* %7, i8* %24)
%26 = tail call i32 @free(i8* %19)
br label %29
27: ; preds = %15
%28 = tail call i32 @strcat(i8* %7, i8* %1)
br label %29
29: ; preds = %27, %23, %21, %2
%30 = phi i8* [ null, %2 ], [ null, %21 ], [ %7, %23 ], [ %7, %27 ]
ret i8* %30
}