#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*/ _PC_PATH_MAX;
int /*<<< orphan*/ __chk_fail();
long __pathconf(char const *, int /*<<< orphan*/);
char *__realpath(char const *, char *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ memcpy(char *, char *, size_t);
int strlen(char *);
char *__realpath_chk(const char *buf, char *resolved, size_t resolvedlen) {
  long int pathmax = __pathconf(buf, _PC_PATH_MAX);
  if (pathmax != -1) {
    if (resolvedlen < pathmax)
      __chk_fail();
    return __realpath(buf, resolved);
  }
  char *res = __realpath(buf, NULL);
  if (res != NULL) {
    size_t actlen = strlen(res) + 1;
    if (actlen > resolvedlen)
      __chk_fail();
    memcpy(resolved, res, actlen);
    free(res);
    res = resolved;
  }
  return res;
}


x86 assembly (clang, -O0):
<__realpath_chk>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e61(%rip),%rax
mov    (%rax),%esi
call   1040 <__pathconf@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0xffffffffffffffff,-0x28(%rbp)
je     11c0 <__realpath_chk+0x60>
mov    -0x20(%rbp),%rax
cmp    -0x28(%rbp),%rax
jae    11aa <__realpath_chk+0x4a>
mov    $0x0,%al
call   1060 <__chk_fail@plt>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1080 <__realpath@plt>
mov    %rax,-0x8(%rbp)
jmp    122d <__realpath_chk+0xcd>
mov    -0x10(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1080 <__realpath@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     1225 <__realpath_chk+0xc5>
mov    -0x30(%rbp),%rdi
call   1050 <strlen@plt>
add    $0x1,%eax
cltq
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
cmp    -0x20(%rbp),%rax
jbe    1203 <__realpath_chk+0xa3>
mov    $0x0,%al
call   1060 <__chk_fail@plt>
mov    -0x18(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x38(%rbp),%rdx
call   1070 <memcpy@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<__realpath_chk>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rdx,%r12
mov    %rsi,%r14
mov    %rdi,%rbx
mov    0x2e67(%rip),%rax
mov    (%rax),%esi
call   1040 <__pathconf@plt>
cmp    $0xffffffffffffffff,%rax
je     11a6 <__realpath_chk+0x46>
cmp    %r12,%rax
jbe    1192 <__realpath_chk+0x32>
xor    %eax,%eax
call   1060 <__chk_fail@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
jmp    1080 <__realpath@plt>
xor    %r13d,%r13d
mov    %rbx,%rdi
xor    %esi,%esi
call   1080 <__realpath@plt>
test   %rax,%rax
je     11ee <__realpath_chk+0x8e>
mov    %rax,%r15
mov    %rax,%rdi
call   1050 <strlen@plt>
add    $0x1,%eax
movslq %eax,%rbx
cmp    %r12,%rbx
jbe    11d5 <__realpath_chk+0x75>
xor    %eax,%eax
call   1060 <__chk_fail@plt>
mov    %r14,%rdi
mov    %r15,%rsi
mov    %rbx,%rdx
call   1070 <memcpy@plt>
mov    %r15,%rdi
call   1030 <free@plt>
mov    %r14,%r13
mov    %r13,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@_PC_PATH_MAX = global i32 0
define i8* @__realpath_chk(i8* %0, i8* %1, i64 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
%9 = alloca i8*
%10 = alloca i64
store i8* %0, i8** %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%11 = load i8*, i8** %5
%12 = load i32, i32* @_PC_PATH_MAX
%13 = call i64 @__pathconf(i8* %11, i32 %12)
store i64 %13, i64* %8
%14 = load i64, i64* %8
%15 = icmp ne i64 %14, -1
br i1 %15, label %16, label %26
16: ; preds = %3
%17 = load i64, i64* %7
%18 = load i64, i64* %8
%19 = icmp ult i64 %17, %18
br i1 %19, label %20, label %22
20: ; preds = %16
%21 = call i32 (...) @__chk_fail()
br label %22
22: ; preds = %20, %16
%23 = load i8*, i8** %5
%24 = load i8*, i8** %6
%25 = call i8* @__realpath(i8* %23, i8* %24)
store i8* %25, i8** %4
br label %51
26: ; preds = %3
%27 = load i8*, i8** %5
%28 = call i8* @__realpath(i8* %27, i8* null)
store i8* %28, i8** %9
%29 = load i8*, i8** %9
%30 = icmp ne i8* %29, null
br i1 %30, label %31, label %49
31: ; preds = %26
%32 = load i8*, i8** %9
%33 = call i32 @strlen(i8* %32)
%34 = add nsw i32 %33, 1
%35 = sext i32 %34 to i64
store i64 %35, i64* %10
%36 = load i64, i64* %10
%37 = load i64, i64* %7
%38 = icmp ugt i64 %36, %37
br i1 %38, label %39, label %41
39: ; preds = %31
%40 = call i32 (...) @__chk_fail()
br label %41
41: ; preds = %39, %31
%42 = load i8*, i8** %6
%43 = load i8*, i8** %9
%44 = load i64, i64* %10
%45 = call i32 @memcpy(i8* %42, i8* %43, i64 %44)
%46 = load i8*, i8** %9
%47 = call i32 @free(i8* %46)
%48 = load i8*, i8** %6
store i8* %48, i8** %9
br label %49
49: ; preds = %41, %26
%50 = load i8*, i8** %9
store i8* %50, i8** %4
br label %51
51: ; preds = %49, %22
%52 = load i8*, i8** %4
ret i8* %52
}

LLVM IR (-O2):
@_PC_PATH_MAX = global i32 0
define i8* @__realpath_chk(i8* %0, i8* %1, i64 %2) {
%4 = load i32, i32* @_PC_PATH_MAX
%5 = tail call i64 @__pathconf(i8* %0, i32 %4)
%6 = icmp eq i64 %5, -1
br i1 %6, label %13, label %7
7: ; preds = %3
%8 = icmp ugt i64 %5, %2
br i1 %8, label %9, label %11
9: ; preds = %7
%10 = tail call i32 (...) @__chk_fail()
br label %11
11: ; preds = %9, %7
%12 = tail call i8* @__realpath(i8* %0, i8* %1)
br label %26
13: ; preds = %3
%14 = tail call i8* @__realpath(i8* %0, i8* null)
%15 = icmp eq i8* %14, null
br i1 %15, label %26, label %16
16: ; preds = %13
%17 = tail call i32 @strlen(i8* %14)
%18 = add nsw i32 %17, 1
%19 = sext i32 %18 to i64
%20 = icmp ugt i64 %19, %2
br i1 %20, label %21, label %23
21: ; preds = %16
%22 = tail call i32 (...) @__chk_fail()
br label %23
23: ; preds = %21, %16
%24 = tail call i32 @memcpy(i8* %1, i8* %14, i64 %19)
%25 = tail call i32 @free(i8* %14)
br label %26
26: ; preds = %13, %23, %11
%27 = phi i8* [ %12, %11 ], [ %1, %23 ], [ null, %13 ]
ret i8* %27
}