#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*/ PATH_MAX;
int /*<<< orphan*/ getcwd(char *, int /*<<< orphan*/);
scalar_t__ malloc(int /*<<< orphan*/);
int /*<<< orphan*/ strcat(char *, char const *);
int /*<<< orphan*/ strcpy(char *, char const *);
char *realpath(const char *path, char *resolved_path) {
  ;
  if (resolved_path == NULL)
    resolved_path = (char *)malloc(PATH_MAX);
  if (*path == '/')
    strcpy(resolved_path, path);
  else {
    getcwd(resolved_path, PATH_MAX);
    strcat(resolved_path, "/");
    strcat(resolved_path, path);
  }
  return resolved_path;
}


x86 assembly (clang, -O0):
<realpath>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    116d <realpath+0x2d>
mov    0x2e8e(%rip),%rax
mov    (%rax),%edi
call   1050 <malloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
jne    118f <realpath+0x4f>
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1030 <strcpy@plt>
jmp    11be <realpath+0x7e>
mov    -0x10(%rbp),%rdi
mov    0x2e56(%rip),%rax
mov    (%rax),%esi
call   1040 <getcwd@plt>
mov    -0x10(%rbp),%rdi
lea    0xe54(%rip),%rsi
call   1060 <strcat@plt>
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1060 <strcat@plt>
mov    -0x10(%rbp),%rax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<realpath>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %rdi,%r14
test   %rsi,%rsi
je     1162 <realpath+0x22>
cmpb   $0x2f,(%r14)
jne    1179 <realpath+0x39>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1030 <strcpy@plt>
jmp    11a4 <realpath+0x64>
mov    0x2e87(%rip),%rax
mov    (%rax),%edi
call   1050 <malloc@plt>
mov    %rax,%rbx
cmpb   $0x2f,(%r14)
je     1155 <realpath+0x15>
mov    0x2e70(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1040 <getcwd@plt>
lea    0xe6f(%rip),%rsi
mov    %rbx,%rdi
call   1060 <strcat@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1060 <strcat@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

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

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