#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 */
typedef int /*<<< orphan*/ cwd;

/* Variables and functions */
int /*<<< orphan*/ ENOENT;
int PATH_MAX;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ getcwd(char *, int);
int resolve_path(char *, char *, char *);
int /*<<< orphan*/ strcat(char *, char *);
char *strchr(char *, int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(char *, char *);
char *strdup(char const *);
char *realpath(const char *path, char *resolved_path) {
  char cwd[PATH_MAX];
  char *path_copy;
  int res;
  if (!*path) {
    errno = ENOENT;
    return NULL;
  }
  if (!getcwd(cwd, sizeof(cwd)))
    return NULL;
  strcpy(resolved_path, "/");
  if (resolve_path(cwd, resolved_path, resolved_path))
    return NULL;
  strcat(resolved_path, "/");
  path_copy = strdup(path);
  if (!path_copy)
    return NULL;
  res = resolve_path(path_copy, resolved_path, strchr(resolved_path, 0));
  free(path_copy);
  if (res)
    return NULL;
  return resolved_path;
}


x86 assembly (clang, -O0):
<realpath>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0x2e69(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x48(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x40(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
cmpb   $0x0,(%rax)
jne    11e3 <realpath+0x73>
mov    0x2e04(%rip),%rax
mov    (%rax),%ecx
mov    0x2e0b(%rip),%rax
mov    %ecx,(%rax)
movq   $0x0,-0x8(%rbp)
movl   $0x1,-0x38(%rbp)
jmp    12ed <realpath+0x17d>
mov    -0x40(%rbp),%rdi
mov    -0x48(%rbp),%rax
mov    %eax,%esi
call   1060 <getcwd@plt>
cmp    $0x0,%eax
jne    120f <realpath+0x9f>
movq   $0x0,-0x8(%rbp)
movl   $0x1,-0x38(%rbp)
jmp    12ed <realpath+0x17d>
mov    -0x18(%rbp),%rdi
lea    0xde6(%rip),%rsi
call   1040 <strcpy@plt>
mov    -0x40(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1070 <resolve_path@plt>
cmp    $0x0,%eax
je     124d <realpath+0xdd>
movq   $0x0,-0x8(%rbp)
movl   $0x1,-0x38(%rbp)
jmp    12ed <realpath+0x17d>
mov    -0x18(%rbp),%rdi
lea    0xda8(%rip),%rsi
call   1080 <strcat@plt>
mov    -0x10(%rbp),%rdi
call   1090 <strdup@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1289 <realpath+0x119>
movq   $0x0,-0x8(%rbp)
movl   $0x1,-0x38(%rbp)
jmp    12ed <realpath+0x17d>
mov    -0x30(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x18(%rbp),%rdi
xor    %esi,%esi
call   1050 <strchr@plt>
mov    -0x58(%rbp),%rdi
mov    -0x50(%rbp),%rsi
mov    %rax,%rdx
call   1070 <resolve_path@plt>
mov    %eax,-0x34(%rbp)
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
cmpl   $0x0,-0x34(%rbp)
je     12de <realpath+0x16e>
movq   $0x0,-0x8(%rbp)
movl   $0x1,-0x38(%rbp)
jmp    12ed <realpath+0x17d>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
movl   $0x1,-0x38(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
mov    -0x8(%rbp),%rax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<realpath>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    0x2e6c(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rbx
lea    0xf(%rsi),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
cmpb   $0x0,(%rdi)
je     1218 <realpath+0xa8>
mov    %rdi,%r15
mov    %rbx,%rdi
call   1060 <getcwd@plt>
test   %eax,%eax
je     122a <realpath+0xba>
lea    0xe4e(%rip),%rsi
mov    %r14,%rdi
call   1040 <strcpy@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r14,%rdx
call   1070 <resolve_path@plt>
test   %eax,%eax
jne    122a <realpath+0xba>
lea    0xe2d(%rip),%rsi
mov    %r14,%rdi
call   1080 <strcat@plt>
mov    %r15,%rdi
call   1090 <strdup@plt>
test   %rax,%rax
je     122a <realpath+0xba>
mov    %rax,%rbx
mov    %r14,%rdi
call   1050 <strlen@plt>
lea    (%rax,%r14,1),%rdx
mov    %rbx,%rdi
mov    %r14,%rsi
call   1070 <resolve_path@plt>
mov    %eax,%r15d
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %eax,%eax
test   %r15d,%r15d
cmove  %r14,%rax
jmp    122c <realpath+0xbc>
mov    0x2da9(%rip),%rax
mov    (%rax),%eax
mov    0x2db0(%rip),%rcx
mov    %eax,(%rcx)
xor    %eax,%eax
lea    -0x18(%rbp),%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@PATH_MAX = global i32 0
@ENOENT = global i32 0
@errno = global i32 0
@.str = [2 x i8] c"/\00"
define i8* @realpath(i8* nocapture %0, i8* %1) {
%3 = load i32, i32* @PATH_MAX
%4 = zext i32 %3 to i64
%5 = alloca i8, i64 %4
%6 = load i8, i8* %0
%7 = icmp eq i8 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %2
%9 = load i32, i32* @ENOENT
store i32 %9, i32* @errno
br label %28
10: ; preds = %2
%11 = call i32 @getcwd(i8* %5, i32 %3)
%12 = icmp eq i32 %11, 0
br i1 %12, label %28, label %13
13: ; preds = %10
%14 = call i32 @strcpy(i8* %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%15 = call i32 @resolve_path(i8* %5, i8* %1, i8* %1)
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %28
17: ; preds = %13
%18 = call i32 @strcat(i8* %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%19 = call i8* @strdup(i8* %0)
%20 = icmp eq i8* %19, null
br i1 %20, label %28, label %21
21: ; preds = %17
%22 = call i64 @strlen(i8* dereferenceable(1) %1)
%23 = getelementptr i8, i8* %1, i64 %22
%24 = call i32 @resolve_path(i8* %19, i8* %1, i8* %23)
%25 = call i32 @free(i8* %19)
%26 = icmp eq i32 %24, 0
%27 = select i1 %26, i8* %1, i8* null
br label %28
28: ; preds = %21, %17, %13, %10, %8
%29 = phi i8* [ null, %8 ], [ null, %10 ], [ null, %13 ], [ null, %17 ], [ %27, %21 ]
ret i8* %29
}