#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*/ errx(int, char *);
scalar_t__ strchr(char const *, char);
int /*<<< orphan*/ strcpy(char *, char const *);
int strlen(char const *);
char *xmalloc(int);
char *path_join(const char *path, const char *name) {
  int path_len, name_len;
  char *pathname;
  if (path == NULL || name == NULL)
    errx(101, "NULL argument in path_join");
  if (path[0] != '/')
    errx(102, "Relative path argument 1 in path_join");
  if (strchr(name, '/'))
    errx(103, "Relative path argument 2 in path_join");
  path_len = strlen(path);
  name_len = strlen(name);
  pathname = xmalloc(path_len + name_len + 2);
  strcpy(pathname, path);
  if (pathname[path_len - 1] != '/')
    pathname[path_len++] = '/';
  strcpy(pathname + path_len, name);
  return pathname;
}


x86 assembly (clang, -O0):
<path_join>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     1176 <path_join+0x26>
cmpq   $0x0,-0x10(%rbp)
jne    1187 <path_join+0x37>
mov    $0x65,%edi
lea    0xe7e(%rip),%rsi
call   1050 <errx@plt>
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
je     11a8 <path_join+0x58>
mov    $0x66,%edi
lea    0xe78(%rip),%rsi
call   1050 <errx@plt>
mov    -0x10(%rbp),%rdi
mov    $0x2f,%esi
call   1060 <strchr@plt>
cmp    $0x0,%rax
je     11d1 <path_join+0x81>
mov    $0x67,%edi
lea    0xe75(%rip),%rsi
call   1050 <errx@plt>
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%edi
add    -0x18(%rbp),%edi
add    $0x2,%edi
call   1070 <xmalloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x20(%rbp),%rax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x2f,%eax
je     1238 <path_join+0xe8>
mov    -0x20(%rbp),%rax
mov    -0x14(%rbp),%ecx
mov    %ecx,%edx
add    $0x1,%edx
mov    %edx,-0x14(%rbp)
movslq %ecx,%rcx
movb   $0x2f,(%rax,%rcx,1)
mov    -0x20(%rbp),%rdi
movslq -0x14(%rbp),%rax
add    %rax,%rdi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x20(%rbp),%rax
add    $0x20,%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,%r15
test   %rdi,%rdi
je     1168 <path_join+0x18>
test   %r14,%r14
jne    1179 <path_join+0x29>
lea    0xe91(%rip),%rsi
mov    $0x65,%edi
call   1050 <errx@plt>
cmpb   $0x2f,(%r15)
je     1190 <path_join+0x40>
lea    0xe95(%rip),%rsi
mov    $0x66,%edi
call   1050 <errx@plt>
mov    %r14,%rdi
mov    $0x2f,%esi
call   1060 <strchr@plt>
test   %rax,%rax
je     11b3 <path_join+0x63>
lea    0xe98(%rip),%rsi
mov    $0x67,%edi
call   1050 <errx@plt>
mov    %r15,%rdi
call   1040 <strlen@plt>
mov    %eax,%r12d
mov    %r14,%rdi
call   1040 <strlen@plt>
lea    (%r12,%rax,1),%edi
add    $0x2,%edi
call   1070 <xmalloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r15,%rsi
call   1030 <strcpy@plt>
movslq %r12d,%rax
cmpb   $0x2f,-0x1(%rax,%rbx,1)
je     11f2 <path_join+0xa2>
lea    0x1(%rax),%r12d
movb   $0x2f,(%rbx,%rax,1)
movslq %r12d,%rax
mov    %rbx,%rdi
add    %rax,%rdi
mov    %r14,%rsi
call   1030 <strcpy@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
@.str = [27 x i8] c"NULL argument in path_join\00"
@.str.1 = [38 x i8] c"Relative path argument 1 in path_join\00"
@.str.2 = [38 x i8] c"Relative path argument 2 in path_join\00"
define i8* @path_join(i8* %0, i8* %1) {
%3 = icmp eq i8* %0, null
%4 = icmp eq i8* %1, null
%5 = or i1 %3, %4
br i1 %5, label %6, label %8
6: ; preds = %2
%7 = tail call i32 @errx(i32 101, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
br label %8
8: ; preds = %2, %6
%9 = load i8, i8* %0
%10 = icmp eq i8 %9, 47
br i1 %10, label %13, label %11
11: ; preds = %8
%12 = tail call i32 @errx(i32 102, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0))
br label %13
13: ; preds = %11, %8
%14 = tail call i64 @strchr(i8* %1, i8 signext 47)
%15 = icmp eq i64 %14, 0
br i1 %15, label %18, label %16
16: ; preds = %13
%17 = tail call i32 @errx(i32 103, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0))
br label %18
18: ; preds = %16, %13
%19 = tail call i32 @strlen(i8* %0)
%20 = tail call i32 @strlen(i8* %1)
%21 = add i32 %19, 2
%22 = add i32 %21, %20
%23 = tail call i8* @xmalloc(i32 %22)
%24 = tail call i32 @strcpy(i8* %23, i8* %0)
%25 = add nsw i32 %19, -1
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds i8, i8* %23, i64 %26
%28 = load i8, i8* %27
%29 = icmp eq i8 %28, 47
br i1 %29, label %34, label %30
30: ; preds = %18
%31 = add nsw i32 %19, 1
%32 = sext i32 %19 to i64
%33 = getelementptr inbounds i8, i8* %23, i64 %32
store i8 47, i8* %33
br label %34
34: ; preds = %30, %18
%35 = phi i32 [ %31, %30 ], [ %19, %18 ]
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds i8, i8* %23, i64 %36
%38 = tail call i32 @strcpy(i8* %37, i8* %1)
ret i8* %23
}