#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*/ O_RDONLY;
scalar_t__ PATH_MAX;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fchdir(int);
char *malloc(scalar_t__);
char *myrealpath(char const *, char *, int);
int open(char *, int /*<<< orphan*/);
char *realpath(const char *file, char *dest) {
  int fd = open(".", O_RDONLY);
  char *res;
  if (!dest)
    dest = malloc(PATH_MAX + 1);
  if (!dest)
    return NULL;
  res = myrealpath(file, dest, 31);
  fchdir(fd);
  close(fd);
  return res;
}


x86 assembly (clang, -O0):
<realpath>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0x2e79(%rip),%rax
mov    (%rax),%esi
lea    0xe90(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    119a <realpath+0x4a>
mov    0x2e66(%rip),%rax
mov    (%rax),%rdi
add    $0x1,%rdi
call   1050 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11b2 <realpath+0x62>
movq   $0x0,-0x8(%rbp)
jmp    11e0 <realpath+0x90>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    $0x1f,%edx
call   1040 <myrealpath@plt>
mov    %rax,-0x28(%rbp)
mov    -0x1c(%rbp),%edi
call   1060 <fchdir@plt>
mov    -0x1c(%rbp),%edi
call   1030 <close@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<realpath>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%rbx
mov    %rdi,%r14
mov    0x2e7f(%rip),%rax
mov    (%rax),%esi
lea    0xe96(%rip),%rdi
call   1070 <open@plt>
mov    %eax,%ebp
test   %rbx,%rbx
jne    1191 <realpath+0x41>
mov    0x2e73(%rip),%rax
mov    (%rax),%rdi
add    $0x1,%rdi
call   1050 <malloc@plt>
mov    %rax,%rbx
test   %rax,%rax
je     11ba <realpath+0x6a>
mov    %r14,%rdi
mov    %rbx,%rsi
mov    $0x1f,%edx
call   1040 <myrealpath@plt>
mov    %rax,%rbx
mov    %ebp,%edi
call   1060 <fchdir@plt>
mov    %ebp,%edi
call   1030 <close@plt>
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret
xor    %eax,%eax
jmp    11b5 <realpath+0x65>

LLVM IR (-O0):
@.str = [2 x i8] c".\00"
@O_RDONLY = global i32 0
@PATH_MAX = global i64 0
define i8* @realpath(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
%8 = load i32, i32* @O_RDONLY
%9 = call i32 @open(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32 %8)
store i32 %9, i32* %6
%10 = load i8*, i8** %5
%11 = icmp ne i8* %10, null
br i1 %11, label %16, label %12
12: ; preds = %2
%13 = load i64, i64* @PATH_MAX
%14 = add nsw i64 %13, 1
%15 = call i8* @malloc(i64 %14)
store i8* %15, i8** %5
br label %16
16: ; preds = %12, %2
%17 = load i8*, i8** %5
%18 = icmp ne i8* %17, null
br i1 %18, label %20, label %19
19: ; preds = %16
store i8* null, i8** %3
br label %29
20: ; preds = %16
%21 = load i8*, i8** %4
%22 = load i8*, i8** %5
%23 = call i8* @myrealpath(i8* %21, i8* %22, i32 31)
store i8* %23, i8** %7
%24 = load i32, i32* %6
%25 = call i32 @fchdir(i32 %24)
%26 = load i32, i32* %6
%27 = call i32 @close(i32 %26)
%28 = load i8*, i8** %7
store i8* %28, i8** %3
br label %29
29: ; preds = %20, %19
%30 = load i8*, i8** %3
ret i8* %30
}

LLVM IR (-O2):
@.str = [2 x i8] c".\00"
@O_RDONLY = global i32 0
@PATH_MAX = global i64 0
define i8* @realpath(i8* %0, i8* %1) {
%3 = load i32, i32* @O_RDONLY
%4 = tail call i32 @open(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = icmp eq i8* %1, null
br i1 %5, label %6, label %11
6: ; preds = %2
%7 = load i64, i64* @PATH_MAX
%8 = add nsw i64 %7, 1
%9 = tail call i8* @malloc(i64 %8)
%10 = icmp eq i8* %9, null
br i1 %10, label %16, label %11
11: ; preds = %2, %6
%12 = phi i8* [ %9, %6 ], [ %1, %2 ]
%13 = tail call i8* @myrealpath(i8* %0, i8* %12, i32 31)
%14 = tail call i32 @fchdir(i32 %4)
%15 = tail call i32 @close(i32 %4)
br label %16
16: ; preds = %6, %11
%17 = phi i8* [ %13, %11 ], [ null, %6 ]
ret i8* %17
}