#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__ ERANGE;
scalar_t__ errno;
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ getcwd(char *, int);
char *malloc(int);
char *realloc(char *, int);
int /*<<< orphan*/ strcat(char *, char const *);
int /*<<< orphan*/ strcpy(char *, char const *);
int strlen(char const *);
char *path_absolute(const char *path) {
  int i, n;
  char *ret;
  if (path[0] != '/') {
    n = 256;
    ret = malloc(n);
    errno = 0;
    while (!getcwd(ret, n) && errno == ERANGE) {
      n += 256;
      ret = realloc(ret, n);
      errno = 0;
    }
    if (errno) {
      free(ret);
      return NULL;
    }
    i = strlen(path) + strlen(ret) + 2;
    if (i > n)
      ret = realloc(ret, i);
    strcat(ret, "/");
    strcat(ret, path);
  } else {
    ret = malloc(strlen(path) + 1);
    strcpy(ret, path);
  }
  return ret;
}


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    $0x2f,%eax
je     12a8 <path_absolute+0x138>
movl   $0x100,-0x18(%rbp)
mov    -0x18(%rbp),%edi
call   1070 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    0x2e32(%rip),%rax
movq   $0x0,(%rax)
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%esi
call   1060 <getcwd@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x21(%rbp)
jne    11e3 <path_absolute+0x73>
mov    0x2e08(%rip),%rax
mov    (%rax),%rax
mov    0x2e0e(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x21(%rbp)
mov    -0x21(%rbp),%al
test   $0x1,%al
jne    11f3 <path_absolute+0x83>
jmp    1221 <path_absolute+0xb1>
mov    -0x18(%rbp),%eax
add    $0x100,%eax
mov    %eax,-0x18(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%esi
call   1080 <realloc@plt>
mov    %rax,-0x20(%rbp)
mov    0x2dc3(%rip),%rax
movq   $0x0,(%rax)
jmp    11ad <path_absolute+0x3d>
mov    0x2db0(%rip),%rax
cmpq   $0x0,(%rax)
je     1248 <path_absolute+0xd8>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    12d4 <path_absolute+0x164>
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%eax
add    %ecx,%eax
add    $0x2,%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x18(%rbp),%eax
jle    1286 <path_absolute+0x116>
mov    -0x20(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1080 <realloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
lea    0xd6f(%rip),%rsi
call   1090 <strcat@plt>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1090 <strcat@plt>
jmp    12cc <path_absolute+0x15c>
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%edi
add    $0x1,%edi
call   1070 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<path_absolute>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdi,%r14
cmpb   $0x2f,(%rdi)
jne    11a3 <path_absolute+0x33>
mov    %r14,%rdi
call   1050 <strlen@plt>
lea    0x1(%rax),%edi
call   1070 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r14,%rsi
call   1040 <strcpy@plt>
jmp    1275 <path_absolute+0x105>
mov    $0x100,%ebp
mov    $0x100,%edi
call   1070 <malloc@plt>
mov    %rax,%rbx
mov    0x2e1c(%rip),%r15
movq   $0x0,(%r15)
mov    %rax,%rdi
mov    $0x100,%esi
call   1060 <getcwd@plt>
mov    (%r15),%rcx
test   %eax,%eax
jne    1221 <path_absolute+0xb1>
mov    0x2e0a(%rip),%r12
cmp    (%r12),%rcx
jne    1221 <path_absolute+0xb1>
mov    $0x100,%ebp
nopl   0x0(%rax)
add    $0x100,%ebp
mov    %rbx,%rdi
mov    %ebp,%esi
call   1080 <realloc@plt>
mov    %rax,%rbx
movq   $0x0,(%r15)
mov    %rax,%rdi
mov    %ebp,%esi
call   1060 <getcwd@plt>
mov    (%r15),%rcx
test   %eax,%eax
jne    1221 <path_absolute+0xb1>
cmp    (%r12),%rcx
je     11f0 <path_absolute+0x80>
test   %rcx,%rcx
je     1232 <path_absolute+0xc2>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %ebx,%ebx
jmp    1275 <path_absolute+0x105>
mov    %r14,%rdi
call   1050 <strlen@plt>
mov    %eax,%r15d
mov    %rbx,%rdi
call   1050 <strlen@plt>
lea    (%r15,%rax,1),%esi
add    $0x2,%esi
cmp    %ebp,%esi
jle    125b <path_absolute+0xeb>
mov    %rbx,%rdi
call   1080 <realloc@plt>
mov    %rax,%rbx
lea    0xd9e(%rip),%rsi
mov    %rbx,%rdi
call   1090 <strcat@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1090 <strcat@plt>
mov    %rbx,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@errno = global i64 0
@ERANGE = global i64 0
@.str = [2 x i8] c"/\00"
define i8* @path_absolute(i8* %0) {
%2 = load i8, i8* %0
%3 = icmp eq i8 %2, 47
br i1 %3, label %42, label %4
4: ; preds = %1
%5 = tail call dereferenceable_or_null(256) i8* @malloc(i32 256)
store i64 0, i64* @errno
%6 = tail call i32 @getcwd(i8* %5, i32 256)
%7 = icmp eq i32 %6, 0
%8 = load i64, i64* @errno
%9 = load i64, i64* @ERANGE
%10 = icmp eq i64 %8, %9
%11 = select i1 %7, i1 %10, i1 false
br i1 %11, label %12, label %23
12: ; preds = %4, %12
%13 = phi i8* [ %16, %12 ], [ %5, %4 ]
%14 = phi i32 [ %15, %12 ], [ 256, %4 ]
%15 = add nuw nsw i32 %14, 256
%16 = tail call i8* @realloc(i8* %13, i32 %15)
store i64 0, i64* @errno
%17 = tail call i32 @getcwd(i8* %16, i32 %15)
%18 = icmp eq i32 %17, 0
%19 = load i64, i64* @errno
%20 = load i64, i64* @ERANGE
%21 = icmp eq i64 %19, %20
%22 = select i1 %18, i1 %21, i1 false
br i1 %22, label %12, label %23
23: ; preds = %12, %4
%24 = phi i32 [ 256, %4 ], [ %15, %12 ]
%25 = phi i8* [ %5, %4 ], [ %16, %12 ]
%26 = phi i64 [ %8, %4 ], [ %19, %12 ]
%27 = icmp eq i64 %26, 0
br i1 %27, label %30, label %28
28: ; preds = %23
%29 = tail call i32 @free(i8* %25)
br label %47
30: ; preds = %23
%31 = tail call i32 @strlen(i8* %0)
%32 = tail call i32 @strlen(i8* %25)
%33 = add i32 %31, 2
%34 = add i32 %33, %32
%35 = icmp sgt i32 %34, %24
br i1 %35, label %36, label %38
36: ; preds = %30
%37 = tail call i8* @realloc(i8* %25, i32 %34)
br label %38
38: ; preds = %36, %30
%39 = phi i8* [ %37, %36 ], [ %25, %30 ]
%40 = tail call i32 @strcat(i8* %39, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%41 = tail call i32 @strcat(i8* %39, i8* %0)
br label %47
42: ; preds = %1
%43 = tail call i32 @strlen(i8* %0)
%44 = add nsw i32 %43, 1
%45 = tail call i8* @malloc(i32 %44)
%46 = tail call i32 @strcpy(i8* %45, i8* %0)
br label %47
47: ; preds = %38, %42, %28
%48 = phi i8* [ null, %28 ], [ %39, %38 ], [ %45, %42 ]
ret i8* %48
}