#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 *realpath(char *, int /*<<< orphan*/ *);
char *sprintf_alloc(char *, char const *, char const *);
char *strdup(char const *);
char *abs_path(const char *path, const char *base_dir) {
  if (path == NULL) {
    return NULL;
  } else if (path[0] == '/') {
    return strdup(path);
  } else {
    char *full_path;
    if (base_dir == NULL) {
      char *cwd = realpath("./", NULL);
      full_path = sprintf_alloc("%s/%s", cwd, path);
      free(cwd);
    } else {
      full_path = sprintf_alloc("%s/%s", base_dir, path);
    }
    return full_path;
  }
}


x86 assembly (clang, -O0):
<abs_path>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1168 <abs_path+0x28>
movq   $0x0,-0x8(%rbp)
jmp    11ef <abs_path+0xaf>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
jne    118a <abs_path+0x4a>
mov    -0x10(%rbp),%rdi
call   1060 <strdup@plt>
mov    %rax,-0x8(%rbp)
jmp    11ef <abs_path+0xaf>
cmpq   $0x0,-0x18(%rbp)
jne    11cf <abs_path+0x8f>
lea    0xe64(%rip),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1050 <realpath@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rsi
mov    -0x10(%rbp),%rdx
lea    0xe4b(%rip),%rdi
call   1040 <sprintf_alloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
jmp    11e7 <abs_path+0xa7>
mov    -0x18(%rbp),%rsi
mov    -0x10(%rbp),%rdx
lea    0xe25(%rip),%rdi
call   1040 <sprintf_alloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<abs_path>:
push   %r14
push   %rbx
push   %rax
test   %rdi,%rdi
je     1160 <abs_path+0x20>
mov    %rdi,%rbx
cmpb   $0x2f,(%rdi)
jne    1164 <abs_path+0x24>
mov    %rbx,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1060 <strdup@plt>
xor    %eax,%eax
jmp    11b0 <abs_path+0x70>
test   %rsi,%rsi
je     117f <abs_path+0x3f>
lea    0xe93(%rip),%rdi
mov    %rbx,%rdx
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1040 <sprintf_alloc@plt>
lea    0xe7a(%rip),%rdi
xor    %esi,%esi
call   1050 <realpath@plt>
mov    %rax,%r14
lea    0xe6c(%rip),%rdi
mov    %rax,%rsi
mov    %rbx,%rdx
call   1040 <sprintf_alloc@plt>
mov    %rax,%rbx
mov    %r14,%rdi
call   1030 <free@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [3 x i8] c"./\00"
@.str.1 = [6 x i8] c"%s/%s\00"
define i8* @abs_path(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
%8 = load i8*, i8** %4
%9 = icmp eq i8* %8, null
br i1 %9, label %10, label %11
10: ; preds = %2
store i8* null, i8** %3
br label %36
11: ; preds = %2
%12 = load i8*, i8** %4
%13 = getelementptr inbounds i8, i8* %12, i64 0
%14 = load i8, i8* %13
%15 = sext i8 %14 to i32
%16 = icmp eq i32 %15, 47
br i1 %16, label %17, label %20
17: ; preds = %11
%18 = load i8*, i8** %4
%19 = call i8* @strdup(i8* %18)
store i8* %19, i8** %3
br label %36
20: ; preds = %11
%21 = load i8*, i8** %5
%22 = icmp eq i8* %21, null
br i1 %22, label %23, label %30
23: ; preds = %20
%24 = call i8* @realpath(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* null)
store i8* %24, i8** %7
%25 = load i8*, i8** %7
%26 = load i8*, i8** %4
%27 = call i8* @sprintf_alloc(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %25, i8* %26)
store i8* %27, i8** %6
%28 = load i8*, i8** %7
%29 = call i32 @free(i8* %28)
br label %34
30: ; preds = %20
%31 = load i8*, i8** %5
%32 = load i8*, i8** %4
%33 = call i8* @sprintf_alloc(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %31, i8* %32)
store i8* %33, i8** %6
br label %34
34: ; preds = %30, %23
%35 = load i8*, i8** %6
store i8* %35, i8** %3
br label %36
36: ; preds = %34, %17, %10
%37 = load i8*, i8** %3
ret i8* %37
}

LLVM IR (-O2):
@.str = [3 x i8] c"./\00"
@.str.1 = [6 x i8] c"%s/%s\00"
define i8* @abs_path(i8* %0, i8* %1) {
%3 = icmp eq i8* %0, null
br i1 %3, label %17, label %4
4: ; preds = %2
%5 = load i8, i8* %0
%6 = icmp eq i8 %5, 47
br i1 %6, label %7, label %9
7: ; preds = %4
%8 = tail call i8* @strdup(i8* %0)
br label %17
9: ; preds = %4
%10 = icmp eq i8* %1, null
br i1 %10, label %11, label %15
11: ; preds = %9
%12 = tail call i8* @realpath(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* null)
%13 = tail call i8* @sprintf_alloc(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %12, i8* %0)
%14 = tail call i32 @free(i8* %12)
br label %17
15: ; preds = %9
%16 = tail call i8* @sprintf_alloc(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %1, i8* %0)
br label %17
17: ; preds = %11, %15, %2, %7
%18 = phi i8* [ %8, %7 ], [ null, %2 ], [ %13, %11 ], [ %16, %15 ]
ret i8* %18
}