#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 */
scalar_t__ EINVAL;
scalar_t__ ENOENT;
scalar_t__ errno;
int /*<<< orphan*/ free(char *);
char *path_basename(char const *);
char *path_dirname(char const *);
char *path_join(char *, char const *);
char *realpath(char const *, int /*<<< orphan*/ *);
char *path_absolute(const char *path) {
  if ((path[0] == '\0')) {
    errno = EINVAL;
    return NULL;
  }
  char *abs = realpath(path, NULL);
  if (abs || errno != ENOENT) {
    return abs;
  }
  const char *base = path_basename(path);
  char *dir_relative = path_dirname(path);
  char *dir = realpath(dir_relative, NULL);
  free(dir_relative);
  if (!dir) {
    errno = ENOENT;
    return NULL;
  }
  abs = path_join(dir, base);
  free(dir);
  return abs;
}


x86 assembly (clang, -O0):
<path_absolute>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    118d <path_absolute+0x3d>
mov    0x2e65(%rip),%rax
mov    (%rax),%rcx
mov    0x2e63(%rip),%rax
mov    %rcx,(%rax)
movq   $0x0,-0x8(%rbp)
jmp    1252 <path_absolute+0x102>
mov    -0x10(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1070 <realpath@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11c3 <path_absolute+0x73>
mov    0x2e30(%rip),%rax
mov    (%rax),%rax
mov    0x2e0e(%rip),%rcx
cmp    (%rcx),%rax
je     11d0 <path_absolute+0x80>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1252 <path_absolute+0x102>
mov    -0x10(%rbp),%rdi
call   1050 <path_basename@plt>
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <path_dirname@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1070 <realpath@plt>
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rdi
call   1040 <free@plt>
cmpq   $0x0,-0x30(%rbp)
jne    1230 <path_absolute+0xe0>
mov    0x2db2(%rip),%rax
mov    (%rax),%rcx
mov    0x2dc0(%rip),%rax
mov    %rcx,(%rax)
movq   $0x0,-0x8(%rbp)
jmp    1252 <path_absolute+0x102>
mov    -0x30(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1060 <path_join@plt>
mov    %rax,-0x18(%rbp)
mov    -0x30(%rbp),%rdi
call   1040 <free@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<path_absolute>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
cmpb   $0x0,(%rdi)
je     11d0 <path_absolute+0x80>
mov    %rdi,%rbx
xor    %esi,%esi
call   1070 <realpath@plt>
test   %rax,%rax
jne    11e6 <path_absolute+0x96>
mov    0x2e6c(%rip),%r12
mov    (%r12),%rax
mov    0x2e49(%rip),%r13
cmp    0x0(%r13),%rax
jne    11e4 <path_absolute+0x94>
mov    %rbx,%rdi
call   1050 <path_basename@plt>
mov    %rax,%r14
mov    %rbx,%rdi
call   1030 <path_dirname@plt>
mov    %rax,%rbx
mov    %rax,%rdi
xor    %esi,%esi
call   1070 <realpath@plt>
mov    %rax,%r15
mov    %rbx,%rdi
call   1040 <free@plt>
test   %r15,%r15
je     11f0 <path_absolute+0xa0>
mov    %r15,%rdi
mov    %r14,%rsi
call   1060 <path_join@plt>
mov    %rax,%rbx
mov    %r15,%rdi
call   1040 <free@plt>
mov    %rbx,%rax
jmp    11e6 <path_absolute+0x96>
mov    0x2e01(%rip),%rax
mov    (%rax),%rax
mov    0x2dff(%rip),%rcx
mov    %rax,(%rcx)
xor    %eax,%eax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret
mov    0x0(%r13),%rax
mov    %rax,(%r12)
jmp    11e4 <path_absolute+0x94>

LLVM IR (-O0):
@EINVAL = global i64 0
@errno = global i64 0
@ENOENT = global i64 0
define i8* @path_absolute(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
store i8* %0, i8** %3
%8 = load i8*, i8** %3
%9 = getelementptr inbounds i8, i8* %8, i64 0
%10 = load i8, i8* %9
%11 = sext i8 %10 to i32
%12 = icmp eq i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %1
%14 = load i64, i64* @EINVAL
store i64 %14, i64* @errno
store i8* null, i8** %2
br label %46
15: ; preds = %1
%16 = load i8*, i8** %3
%17 = call i8* @realpath(i8* %16, i32* null)
store i8* %17, i8** %4
%18 = load i8*, i8** %4
%19 = icmp ne i8* %18, null
br i1 %19, label %24, label %20
20: ; preds = %15
%21 = load i64, i64* @errno
%22 = load i64, i64* @ENOENT
%23 = icmp ne i64 %21, %22
br i1 %23, label %24, label %26
24: ; preds = %20, %15
%25 = load i8*, i8** %4
store i8* %25, i8** %2
br label %46
26: ; preds = %20
%27 = load i8*, i8** %3
%28 = call i8* @path_basename(i8* %27)
store i8* %28, i8** %5
%29 = load i8*, i8** %3
%30 = call i8* @path_dirname(i8* %29)
store i8* %30, i8** %6
%31 = load i8*, i8** %6
%32 = call i8* @realpath(i8* %31, i32* null)
store i8* %32, i8** %7
%33 = load i8*, i8** %6
%34 = call i32 @free(i8* %33)
%35 = load i8*, i8** %7
%36 = icmp ne i8* %35, null
br i1 %36, label %39, label %37
37: ; preds = %26
%38 = load i64, i64* @ENOENT
store i64 %38, i64* @errno
store i8* null, i8** %2
br label %46
39: ; preds = %26
%40 = load i8*, i8** %7
%41 = load i8*, i8** %5
%42 = call i8* @path_join(i8* %40, i8* %41)
store i8* %42, i8** %4
%43 = load i8*, i8** %7
%44 = call i32 @free(i8* %43)
%45 = load i8*, i8** %4
store i8* %45, i8** %2
br label %46
46: ; preds = %39, %37, %24, %13
%47 = load i8*, i8** %2
ret i8* %47
}

LLVM IR (-O2):
@EINVAL = global i64 0
@errno = global i64 0
@ENOENT = global i64 0
define i8* @path_absolute(i8* %0) {
%2 = load i8, i8* %0
%3 = icmp eq i8 %2, 0
br i1 %3, label %4, label %6
4: ; preds = %1
%5 = load i64, i64* @EINVAL
store i64 %5, i64* @errno
br label %24
6: ; preds = %1
%7 = tail call i8* @realpath(i8* %0, i32* null)
%8 = icmp eq i8* %7, null
br i1 %8, label %9, label %24
9: ; preds = %6
%10 = load i64, i64* @errno
%11 = load i64, i64* @ENOENT
%12 = icmp eq i64 %10, %11
br i1 %12, label %13, label %24
13: ; preds = %9
%14 = tail call i8* @path_basename(i8* %0)
%15 = tail call i8* @path_dirname(i8* %0)
%16 = tail call i8* @realpath(i8* %15, i32* null)
%17 = tail call i32 @free(i8* %15)
%18 = icmp eq i8* %16, null
br i1 %18, label %19, label %21
19: ; preds = %13
%20 = load i64, i64* @ENOENT
store i64 %20, i64* @errno
br label %24
21: ; preds = %13
%22 = tail call i8* @path_join(i8* %16, i8* %14)
%23 = tail call i32 @free(i8* %16)
br label %24
24: ; preds = %9, %6, %21, %19, %4
%25 = phi i8* [ null, %4 ], [ null, %9 ], [ %7, %6 ], [ %22, %21 ], [ null, %19 ]
ret i8* %25
}