#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;
  }
  fd = open(filename, O_RDONLY | O_NONBLOCK);
  if (fd < 0)
    return 0;
  snprintf(buf, sizeof buf, "/proc/self/fd/%d", fd);
  if (!resolved) {
    alloc = 1;
    resolved = malloc(PATH_MAX);
    if (!resolved)
      return 0;
  }
  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>
mov    -0x10(%rbp),%rdi
mov    0x2e09(%rip),%rax
mov    (%rax),%esi
mov    0x2e08(%rip),%rax
or     (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11f9 <realpath+0x79>
movq   $0x0,-0x8(%rbp)
jmp    132c <realpath+0x1ac>
lea    -0x70(%rbp),%rdi
mov    -0x1c(%rbp),%ecx
mov    $0x1b,%esi
lea    0xdf4(%rip),%rdx
call   1050 <snprintf@plt>
cmpq   $0x0,-0x18(%rbp)
jne    1253 <realpath+0xd3>
movl   $0x1,-0x74(%rbp)
mov    0x2dc6(%rip),%rax
mov    (%rax),%rdi
call   1080 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    124e <realpath+0xce>
movq   $0x0,-0x8(%rbp)
jmp    132c <realpath+0x1ac>
jmp    1253 <realpath+0xd3>
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     1243 <realpath+0xc3>
mov    %rsi,%r14
mov    0x2e3b(%rip),%rax
mov    0x2e3c(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
call   1090 <open@plt>
test   %eax,%eax
js     1269 <realpath+0xe9>
mov    %eax,%r15d
lea    0xe41(%rip),%rdx
lea    0x20(%rsp),%rdi
mov    $0x1b,%esi
mov    %eax,%ecx
call   1050 <snprintf@plt>
mov    0x2e19(%rip),%rax
mov    (%rax),%rbp
mov    %r14,%rbx
test   %r14,%r14
jne    11f2 <realpath+0x72>
mov    %rbp,%rdi
call   1080 <malloc@plt>
mov    %rax,%rbx
test   %rax,%rax
je     1269 <realpath+0xe9>
add    $0xffffffffffffffff,%rbp
lea    0x20(%rsp),%rdi
mov    %rbx,%rsi
mov    %rbp,%rdx
call   1040 <readlink@plt>
movb   $0x0,(%rbx,%rax,1)
lea    0x10(%rsp),%rsi
mov    %r15d,%edi
call   10a0 <fstat@plt>
mov    %rsp,%rsi
mov    %rbx,%rdi
call   1070 <stat@plt>
mov    0x10(%rsp),%rcx
cmp    (%rsp),%rcx
jne    1257 <realpath+0xd7>
mov    0x18(%rsp),%rcx
cmp    0x8(%rsp),%rcx
jne    1257 <realpath+0xd7>
mov    %r15d,%edi
call   1060 <close@plt>
jmp    126b <realpath+0xeb>
mov    0x2d6e(%rip),%rax
mov    (%rax),%eax
mov    0x2d6d(%rip),%rcx
mov    %eax,(%rcx)
jmp    1269 <realpath+0xe9>
test   %rax,%rax
je     1279 <realpath+0xf9>
test   %r14,%r14
je     1290 <realpath+0x110>
mov    %r15d,%edi
call   1060 <close@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x48,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d50(%rip),%rax
mov    (%rax),%eax
mov    0x2d37(%rip),%rcx
mov    %eax,(%rcx)
test   %r14,%r14
jne    1261 <realpath+0xe1>
mov    %rbx,%rdi
call   1030 <free@plt>
jmp    1261 <realpath+0xe1>

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