#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 */
struct stat {
  scalar_t__ st_dev;
  scalar_t__ st_ino;
};
typedef size_t ssize_t;

/* Variables and functions */
int /*<<< orphan*/ EINVAL;
int /*<<< orphan*/ ELOOP;
int O_NONBLOCK;
int O_RDONLY;
scalar_t__ PATH_MAX;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ fstat(int, struct stat *);
char *malloc(scalar_t__);
int open(char const *, int);
size_t readlink(char *, char *, scalar_t__);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
size_t stat(char *, struct stat *);
char *realpath(const char *filename, char *resolved) {
  int fd;
  ssize_t r;
  struct stat st1, st2;
  char buf[15 + 3 * sizeof(int)];
  int alloc = 0;
  if (!filename) {
    errno = EINVAL;
    return 0;
  }
  if (!resolved) {
    alloc = 1;
    resolved = malloc(PATH_MAX);
    if (!resolved)
      return 0;
  }
  fd = open(filename, O_RDONLY | O_NONBLOCK);
  if (fd < 0)
    return 0;
  snprintf(buf, sizeof buf, "/proc/self/fd/%d", fd);
  r = readlink(buf, resolved, PATH_MAX - 1);
  if (r < 0)
    goto err;
  resolved[r] = 0;
  fstat(fd, &st1);
  r = stat(resolved, &st2);
  if (r < 0 || st1.st_dev != st2.st_dev || st1.st_ino != st2.st_ino) {
    if (!r)
      errno = ELOOP;
    goto err;
  }
  close(fd);
  return resolved;
err:
  if (alloc)
    free(resolved);
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<realpath>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x0,-0x74(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11c4 <realpath+0x44>
mov    0x2e0c(%rip),%rax
mov    (%rax),%ecx
mov    0x2e0b(%rip),%rax
mov    %ecx,(%rax)
movq   $0x0,-0x8(%rbp)
jmp    132c <realpath+0x1ac>
cmpq   $0x0,-0x18(%rbp)
jne    1206 <realpath+0x86>
movl   $0x1,-0x74(%rbp)
mov    0x2e13(%rip),%rax
mov    (%rax),%rdi
call   1080 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1201 <realpath+0x81>
movq   $0x0,-0x8(%rbp)
jmp    132c <realpath+0x1ac>
jmp    1206 <realpath+0x86>
mov    -0x10(%rbp),%rdi
mov    0x2dc7(%rip),%rax
mov    (%rax),%esi
mov    0x2dc6(%rip),%rax
or     (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    123b <realpath+0xbb>
movq   $0x0,-0x8(%rbp)
jmp    132c <realpath+0x1ac>
lea    -0x70(%rbp),%rdi
mov    -0x1c(%rbp),%ecx
mov    $0x1b,%esi
lea    0xdb2(%rip),%rdx
call   1050 <snprintf@plt>
lea    -0x70(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    0x2d8e(%rip),%rax
mov    (%rax),%rdx
sub    $0x1,%rdx
call   1040 <readlink@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jae    1282 <realpath+0x102>
jmp    1309 <realpath+0x189>
mov    -0x18(%rbp),%rax
mov    -0x28(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x1c(%rbp),%edi
lea    -0x38(%rbp),%rsi
call   10a0 <fstat@plt>
mov    -0x18(%rbp),%rdi
lea    -0x48(%rbp),%rsi
call   1070 <stat@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jb     12d2 <realpath+0x152>
mov    -0x38(%rbp),%rax
cmp    -0x48(%rbp),%rax
jne    12d2 <realpath+0x152>
mov    -0x30(%rbp),%rax
cmp    -0x40(%rbp),%rax
je     12f4 <realpath+0x174>
cmpq   $0x0,-0x28(%rbp)
jne    12ef <realpath+0x16f>
mov    0x2cec(%rip),%rax
mov    (%rax),%ecx
mov    0x2cd3(%rip),%rax
mov    %ecx,(%rax)
jmp    1309 <realpath+0x189>
mov    -0x1c(%rbp),%edi
call   1060 <close@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    132c <realpath+0x1ac>
cmpl   $0x0,-0x74(%rbp)
je     131c <realpath+0x19c>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x1c(%rbp),%edi
call   1060 <close@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<realpath>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x48,%rsp
test   %rdi,%rdi
je     124f <realpath+0xcf>
mov    %rsi,%r14
mov    %rdi,%rbp
mov    %rsi,%rbx
test   %rsi,%rsi
jne    11bc <realpath+0x3c>
mov    0x2e48(%rip),%rax
mov    (%rax),%rdi
call   1080 <malloc@plt>
mov    %rax,%rbx
test   %rax,%rax
je     1274 <realpath+0xf4>
mov    0x2e15(%rip),%rax
mov    0x2e16(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %rbp,%rdi
call   1090 <open@plt>
test   %eax,%eax
js     1274 <realpath+0xf4>
mov    %eax,%ebp
lea    0xe19(%rip),%rdx
lea    0x20(%rsp),%r15
mov    %r15,%rdi
mov    $0x1b,%esi
mov    %eax,%ecx
call   1050 <snprintf@plt>
mov    0x2dee(%rip),%rax
mov    (%rax),%rdx
add    $0xffffffffffffffff,%rdx
mov    %r15,%rdi
mov    %rbx,%rsi
call   1040 <readlink@plt>
movb   $0x0,(%rbx,%rax,1)
lea    0x10(%rsp),%rsi
mov    %ebp,%edi
call   10a0 <fstat@plt>
mov    %rsp,%rsi
mov    %rbx,%rdi
call   1070 <stat@plt>
mov    0x10(%rsp),%rcx
cmp    (%rsp),%rcx
jne    1263 <realpath+0xe3>
mov    0x18(%rsp),%rcx
cmp    0x8(%rsp),%rcx
jne    1263 <realpath+0xe3>
mov    %ebp,%edi
call   1060 <close@plt>
jmp    1276 <realpath+0xf6>
mov    0x2d62(%rip),%rax
mov    (%rax),%eax
mov    0x2d61(%rip),%rcx
mov    %eax,(%rcx)
jmp    1274 <realpath+0xf4>
test   %rax,%rax
je     1284 <realpath+0x104>
test   %r14,%r14
je     129b <realpath+0x11b>
mov    %ebp,%edi
call   1060 <close@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x48,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d45(%rip),%rax
mov    (%rax),%eax
mov    0x2d2c(%rip),%rcx
mov    %eax,(%rcx)
test   %r14,%r14
jne    126d <realpath+0xed>
mov    %rbx,%rdi
call   1030 <free@plt>
jmp    126d <realpath+0xed>

LLVM IR (-O0):
%struct.stat = type { i64, i64 }
@EINVAL = global i32 0
@errno = global i32 0
@PATH_MAX = global i64 0
@O_RDONLY = global i32 0
@O_NONBLOCK = global i32 0
@.str = [17 x i8] c"/proc/self/fd/%d\00"
@ELOOP = global i32 0
define i8* @realpath(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i64
%8 = alloca %struct.stat
%9 = alloca %struct.stat
%10 = alloca [27 x i8]
%11 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 0, i32* %11
%12 = load i8*, i8** %4
%13 = icmp ne i8* %12, null
br i1 %13, label %16, label %14
14: ; preds = %2
%15 = load i32, i32* @EINVAL
store i32 %15, i32* @errno
store i8* null, i8** %3
br label %88
16: ; preds = %2
%17 = load i8*, i8** %5
%18 = icmp ne i8* %17, null
br i1 %18, label %26, label %19
19: ; preds = %16
store i32 1, i32* %11
%20 = load i64, i64* @PATH_MAX
%21 = call i8* @malloc(i64 %20)
store i8* %21, i8** %5
%22 = load i8*, i8** %5
%23 = icmp ne i8* %22, null
br i1 %23, label %25, label %24
24: ; preds = %19
store i8* null, i8** %3
br label %88
25: ; preds = %19
br label %26
26: ; preds = %25, %16
%27 = load i8*, i8** %4
%28 = load i32, i32* @O_RDONLY
%29 = load i32, i32* @O_NONBLOCK
%30 = or i32 %28, %29
%31 = call i32 @open(i8* %27, i32 %30)
store i32 %31, i32* %6
%32 = load i32, i32* %6
%33 = icmp slt i32 %32, 0
br i1 %33, label %34, label %35
34: ; preds = %26
store i8* null, i8** %3
br label %88
35: ; preds = %26
%36 = getelementptr inbounds [27 x i8], [27 x i8]* %10, i64 0, i64 0
%37 = load i32, i32* %6
%38 = call i32 @snprintf(i8* %36, i32 27, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %37)
%39 = getelementptr inbounds [27 x i8], [27 x i8]* %10, i64 0, i64 0
%40 = load i8*, i8** %5
%41 = load i64, i64* @PATH_MAX
%42 = sub nsw i64 %41, 1
%43 = call i64 @readlink(i8* %39, i8* %40, i64 %42)
store i64 %43, i64* %7
%44 = load i64, i64* %7
%45 = icmp ult i64 %44, 0
br i1 %45, label %46, label %47
46: ; preds = %35
br label %79
47: ; preds = %35
%48 = load i8*, i8** %5
%49 = load i64, i64* %7
%50 = getelementptr inbounds i8, i8* %48, i64 %49
store i8 0, i8* %50
%51 = load i32, i32* %6
%52 = call i32 @fstat(i32 %51, %struct.stat* %8)
%53 = load i8*, i8** %5
%54 = call i64 @stat(i8* %53, %struct.stat* %9)
store i64 %54, i64* %7
%55 = load i64, i64* %7
%56 = icmp ult i64 %55, 0
br i1 %56, label %69, label %57
57: ; preds = %47
%58 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%59 = load i64, i64* %58
%60 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 0
%61 = load i64, i64* %60
%62 = icmp ne i64 %59, %61
br i1 %62, label %69, label %63
63: ; preds = %57
%64 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 1
%65 = load i64, i64* %64
%66 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 1
%67 = load i64, i64* %66
%68 = icmp ne i64 %65, %67
br i1 %68, label %69, label %75
69: ; preds = %63, %57, %47
%70 = load i64, i64* %7
%71 = icmp ne i64 %70, 0
br i1 %71, label %74, label %72
72: ; preds = %69
%73 = load i32, i32* @ELOOP
store i32 %73, i32* @errno
br label %74
74: ; preds = %72, %69
br label %79
75: ; preds = %63
%76 = load i32, i32* %6
%77 = call i32 @close(i32 %76)
%78 = load i8*, i8** %5
store i8* %78, i8** %3
br label %88
79: ; preds = %74, %46
%80 = load i32, i32* %11
%81 = icmp ne i32 %80, 0
br i1 %81, label %82, label %85
82: ; preds = %79
%83 = load i8*, i8** %5
%84 = call i32 @free(i8* %83)
br label %85
85: ; preds = %82, %79
%86 = load i32, i32* %6
%87 = call i32 @close(i32 %86)
store i8* null, i8** %3
br label %88
88: ; preds = %85, %75, %34, %24, %14
%89 = load i8*, i8** %3
ret i8* %89
}

LLVM IR (-O2):
%struct.stat = type { i64, i64 }
@EINVAL = global i32 0
@errno = global i32 0
@PATH_MAX = global i64 0
@O_RDONLY = global i32 0
@O_NONBLOCK = global i32 0
@.str = [17 x i8] c"/proc/self/fd/%d\00"
@ELOOP = global i32 0
define i8* @realpath(i8* %0, i8* %1) {
%3 = alloca %struct.stat
%4 = alloca %struct.stat
%5 = alloca [27 x i8]
%6 = bitcast %struct.stat* %3 to i8*
%7 = bitcast %struct.stat* %4 to i8*
%8 = getelementptr inbounds [27 x i8], [27 x i8]* %5, i64 0, i64 0
%9 = icmp eq i8* %0, null
br i1 %9, label %10, label %12
10: ; preds = %2
%11 = load i32, i32* @EINVAL
store i32 %11, i32* @errno
br label %55
12: ; preds = %2
%13 = icmp eq i8* %1, null
br i1 %13, label %14, label %18
14: ; preds = %12
%15 = load i64, i64* @PATH_MAX
%16 = tail call i8* @malloc(i64 %15)
%17 = icmp eq i8* %16, null
br i1 %17, label %55, label %18
18: ; preds = %14, %12
%19 = phi i8* [ %1, %12 ], [ %16, %14 ]
%20 = load i32, i32* @O_RDONLY
%21 = load i32, i32* @O_NONBLOCK
%22 = or i32 %21, %20
%23 = tail call i32 @open(i8* %0, i32 %22)
%24 = icmp slt i32 %23, 0
br i1 %24, label %55, label %25
25: ; preds = %18
%26 = call i32 @snprintf(i8* %8, i32 27, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i32 %23)
%27 = load i64, i64* @PATH_MAX
%28 = add nsw i64 %27, -1
%29 = call i64 @readlink(i8* %8, i8* %19, i64 %28)
%30 = getelementptr inbounds i8, i8* %19, i64 %29
store i8 0, i8* %30
%31 = call i32 @fstat(i32 %23, %struct.stat* %3)
%32 = call i64 @stat(i8* %19, %struct.stat* %4)
%33 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%34 = load i64, i64* %33
%35 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%36 = load i64, i64* %35
%37 = icmp eq i64 %34, %36
br i1 %37, label %38, label %44
38: ; preds = %25
%39 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 1
%40 = load i64, i64* %39
%41 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 1
%42 = load i64, i64* %41
%43 = icmp eq i64 %40, %42
br i1 %43, label %48, label %44
44: ; preds = %38, %25
%45 = icmp eq i64 %32, 0
br i1 %45, label %46, label %50
46: ; preds = %44
%47 = load i32, i32* @ELOOP
store i32 %47, i32* @errno
br label %50
48: ; preds = %38
%49 = call i32 @close(i32 %23)
br label %55
50: ; preds = %44, %46
br i1 %13, label %51, label %53
51: ; preds = %50
%52 = call i32 @free(i8* %19)
br label %53
53: ; preds = %51, %50
%54 = call i32 @close(i32 %23)
br label %55
55: ; preds = %18, %14, %53, %48, %10
%56 = phi i8* [ null, %53 ], [ %19, %48 ], [ null, %10 ], [ null, %14 ], [ null, %18 ]
ret i8* %56
}