#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 */
typedef scalar_t__ ssize_t;

/* Variables and functions */
int /*<<< orphan*/ _PC_PATH_MAX;
int errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, int, ...);
int /*<<< orphan*/ free(char *);
scalar_t__ malloc(size_t);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, size_t);
long pathconf(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, char *);
scalar_t__ readlink(char *, char *, size_t);
int /*<<< orphan*/ stderr;
scalar_t__ strcmp(char *, char *);
char *strerror(int);
int main(int argc, char **argv) {
  if ((argc != 2) || (0 == strcmp(argv[1], "-h"))) {
    printf("usage: %s path\n", argv[0]);
    return 1;
  }
  int retval = 0;
  errno = 0;
  long PATH_MAX = pathconf(argv[1], _PC_PATH_MAX);
  if (PATH_MAX < 0) {
    if (errno != 0) {
      int error = 0;
      fprintf(stderr,
              "pathconf(%s, _PC_PATH_MAX) returned %d with error %d (%s)\n",
              argv[1], (int)PATH_MAX, error, strerror(error));
    } else {
      fprintf(stderr,
              "pathconf(%s, _PC_PATH_MAX) returned %d without changing errno; "
              "PATH_MAX is unlimited.\n",
              argv[1], (int)PATH_MAX);
    }
    PATH_MAX = 10000;
  }
  char *buf = (char *)malloc((size_t)(PATH_MAX + 1));
  memset(buf, 0, (size_t)(PATH_MAX + 1));
  ssize_t status = readlink(argv[1], buf, (size_t)(PATH_MAX + 1));
  if (0 > status) {
    int error = errno;
    fprintf(stderr, "readlink(%s) failed: %d (%s)\n", argv[1], error,
            strerror(error));
    retval = 1;
  } else {
    printf("%s\n", buf);
  }
  free(buf);
  return retval;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jne    11d2 <main+0x42>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe41(%rip),%rsi
call   1080 <strcmp@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
jne    11f1 <main+0x61>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe23(%rip),%rdi
call   1060 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1384 <main+0x1f4>
movl   $0x0,-0x14(%rbp)
mov    0x2dd1(%rip),%rax
movl   $0x0,(%rax)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2dcc(%rip),%rax
mov    (%rax),%esi
call   1050 <pathconf@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jge    12bf <main+0x12f>
mov    0x2d9f(%rip),%rax
cmpl   $0x0,(%rax)
je     1292 <main+0x102>
movl   $0x0,-0x24(%rbp)
mov    0x2d90(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x54(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x50(%rbp)
mov    -0x20(%rbp),%rax
mov    %eax,-0x44(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0x40(%rbp)
mov    -0x24(%rbp),%edi
call   10b0 <strerror@plt>
mov    -0x54(%rbp),%edi
mov    -0x50(%rbp),%rdx
mov    -0x44(%rbp),%ecx
mov    -0x40(%rbp),%r8d
mov    %rax,%r9
lea    0xd8d(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
jmp    12b7 <main+0x127>
mov    0x2d3f(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
mov    -0x20(%rbp),%rax
mov    %eax,%ecx
lea    0xd9e(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
movq   $0x2710,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
add    $0x1,%rdi
call   10a0 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x20(%rbp),%rdx
add    $0x1,%rdx
xor    %esi,%esi
call   1070 <memset@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x20(%rbp),%rdx
add    $0x1,%rdx
call   1040 <readlink@plt>
mov    %rax,-0x38(%rbp)
xor    %eax,%eax
cmp    -0x38(%rbp),%rax
jle    1365 <main+0x1d5>
mov    0x2cbd(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    0x2cb9(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x64(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x60(%rbp)
mov    -0x3c(%rbp),%eax
mov    %eax,-0x58(%rbp)
mov    -0x3c(%rbp),%edi
call   10b0 <strerror@plt>
mov    -0x64(%rbp),%edi
mov    -0x60(%rbp),%rdx
mov    -0x58(%rbp),%ecx
mov    %rax,%r8
lea    0xd53(%rip),%rsi
mov    $0x0,%al
call   1090 <fprintf@plt>
movl   $0x1,-0x14(%rbp)
jmp    1375 <main+0x1e5>
mov    -0x30(%rbp),%rsi
lea    0xd53(%rip),%rdi
call   1060 <printf@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%r14
cmp    $0x2,%edi
jne    1216 <main+0x86>
mov    0x8(%r14),%rdi
lea    0xe55(%rip),%rsi
call   1080 <strcmp@plt>
test   %rax,%rax
je     1216 <main+0x86>
mov    0x2e14(%rip),%r12
movl   $0x0,(%r12)
mov    0x8(%r14),%rdi
mov    0x2e11(%rip),%rax
mov    (%rax),%esi
call   1050 <pathconf@plt>
mov    %rax,%rbx
test   %rax,%rax
jns    1249 <main+0xb9>
cmpl   $0x0,(%r12)
mov    0x2dee(%rip),%rax
mov    (%rax),%ebp
mov    0x8(%r14),%r15
je     122f <main+0x9f>
xor    %edi,%edi
call   10b0 <strerror@plt>
lea    0xe13(%rip),%rsi
mov    %ebp,%edi
mov    %r15,%rdx
mov    %ebx,%ecx
xor    %r8d,%r8d
mov    %rax,%r9
xor    %eax,%eax
call   1090 <fprintf@plt>
jmp    1244 <main+0xb4>
mov    (%r14),%rsi
lea    0xde3(%rip),%rdi
call   1060 <printf@plt>
mov    $0x1,%ebp
jmp    12c9 <main+0x139>
lea    0xe18(%rip),%rsi
mov    %ebp,%edi
mov    %r15,%rdx
mov    %ebx,%ecx
xor    %eax,%eax
call   1090 <fprintf@plt>
mov    $0x2710,%ebx
add    $0x1,%rbx
mov    %rbx,%rdi
call   10a0 <malloc@plt>
mov    %rax,%r15
xor    %ebp,%ebp
mov    %rax,%rdi
xor    %esi,%esi
mov    %rbx,%rdx
call   1070 <memset@plt>
mov    0x8(%r14),%rdi
mov    %r15,%rsi
mov    %rbx,%rdx
call   1040 <readlink@plt>
test   %rax,%rax
js     128c <main+0xfc>
lea    0xe41(%rip),%rdi
mov    %r15,%rsi
call   1060 <printf@plt>
jmp    12c1 <main+0x131>
mov    (%r12),%ebx
mov    0x2d41(%rip),%rax
mov    (%rax),%ebp
mov    0x8(%r14),%r14
mov    %ebx,%edi
call   10b0 <strerror@plt>
lea    0xdfa(%rip),%rsi
mov    %ebp,%edi
mov    %r14,%rdx
mov    %ebx,%ecx
mov    %rax,%r8
xor    %eax,%eax
call   1090 <fprintf@plt>
mov    $0x1,%ebp
mov    %r15,%rdi
call   1030 <free@plt>
mov    %ebp,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"-h\00"
@.str.1 = [16 x i8] c"usage: %s path\0A\00"
@errno = global i32 0
@_PC_PATH_MAX = global i32 0
@stderr = global i32 0
@.str.2 = [59 x i8] c"pathconf(%s, _PC_PATH_MAX) returned %d with error %d (%s)\0A\00"
@.str.3 = [87 x i8] c"pathconf(%s, _PC_PATH_MAX) returned %d without changing errno; PATH_MAX is unlimited.\0A\00"
@.str.4 = [30 x i8] c"readlink(%s) failed: %d (%s)\0A\00"
@.str.5 = [4 x i8] c"%s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i64
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i64
%11 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = load i32, i32* %4
%13 = icmp ne i32 %12, 2
br i1 %13, label %20, label %14
14: ; preds = %2
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 1
%17 = load i8*, i8** %16
%18 = call i64 @strcmp(i8* %17, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%19 = icmp eq i64 0, %18
br i1 %19, label %20, label %25
20: ; preds = %14, %2
%21 = load i8**, i8*** %5
%22 = getelementptr inbounds i8*, i8** %21, i64 0
%23 = load i8*, i8** %22
%24 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* %23)
store i32 1, i32* %3
br label %91
25: ; preds = %14
store i32 0, i32* %6
store i32 0, i32* @errno
%26 = load i8**, i8*** %5
%27 = getelementptr inbounds i8*, i8** %26, i64 1
%28 = load i8*, i8** %27
%29 = load i32, i32* @_PC_PATH_MAX
%30 = call i64 @pathconf(i8* %28, i32 %29)
store i64 %30, i64* %7
%31 = load i64, i64* %7
%32 = icmp slt i64 %31, 0
br i1 %32, label %33, label %56
33: ; preds = %25
%34 = load i32, i32* @errno
%35 = icmp ne i32 %34, 0
br i1 %35, label %36, label %47
36: ; preds = %33
store i32 0, i32* %8
%37 = load i32, i32* @stderr
%38 = load i8**, i8*** %5
%39 = getelementptr inbounds i8*, i8** %38, i64 1
%40 = load i8*, i8** %39
%41 = load i64, i64* %7
%42 = trunc i64 %41 to i32
%43 = load i32, i32* %8
%44 = load i32, i32* %8
%45 = call i8* @strerror(i32 %44)
%46 = call i32 (i32, i8*, i8*, i32, ...) @fprintf(i32 %37, i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str.2, i64 0, i64 0), i8* %40, i32 %42, i32 %43, i8* %45)
br label %55
47: ; preds = %33
%48 = load i32, i32* @stderr
%49 = load i8**, i8*** %5
%50 = getelementptr inbounds i8*, i8** %49, i64 1
%51 = load i8*, i8** %50
%52 = load i64, i64* %7
%53 = trunc i64 %52 to i32
%54 = call i32 (i32, i8*, i8*, i32, ...) @fprintf(i32 %48, i8* getelementptr inbounds ([87 x i8], [87 x i8]* @.str.3, i64 0, i64 0), i8* %51, i32 %53)
br label %55
55: ; preds = %47, %36
store i64 10000, i64* %7
br label %56
56: ; preds = %55, %25
%57 = load i64, i64* %7
%58 = add nsw i64 %57, 1
%59 = call i64 @malloc(i64 %58)
%60 = inttoptr i64 %59 to i8*
store i8* %60, i8** %9
%61 = load i8*, i8** %9
%62 = load i64, i64* %7
%63 = add nsw i64 %62, 1
%64 = call i32 @memset(i8* %61, i32 0, i64 %63)
%65 = load i8**, i8*** %5
%66 = getelementptr inbounds i8*, i8** %65, i64 1
%67 = load i8*, i8** %66
%68 = load i8*, i8** %9
%69 = load i64, i64* %7
%70 = add nsw i64 %69, 1
%71 = call i64 @readlink(i8* %67, i8* %68, i64 %70)
store i64 %71, i64* %10
%72 = load i64, i64* %10
%73 = icmp sgt i64 0, %72
br i1 %73, label %74, label %84
74: ; preds = %56
%75 = load i32, i32* @errno
store i32 %75, i32* %11
%76 = load i32, i32* @stderr
%77 = load i8**, i8*** %5
%78 = getelementptr inbounds i8*, i8** %77, i64 1
%79 = load i8*, i8** %78
%80 = load i32, i32* %11
%81 = load i32, i32* %11
%82 = call i8* @strerror(i32 %81)
%83 = call i32 (i32, i8*, i8*, i32, ...) @fprintf(i32 %76, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.4, i64 0, i64 0), i8* %79, i32 %80, i8* %82)
store i32 1, i32* %6
br label %87
84: ; preds = %56
%85 = load i8*, i8** %9
%86 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i8* %85)
br label %87
87: ; preds = %84, %74
%88 = load i8*, i8** %9
%89 = call i32 @free(i8* %88)
%90 = load i32, i32* %6
store i32 %90, i32* %3
br label %91
91: ; preds = %87, %20
%92 = load i32, i32* %3
ret i32 %92
}

LLVM IR (-O2):
@.str = [3 x i8] c"-h\00"
@.str.1 = [16 x i8] c"usage: %s path\0A\00"
@errno = global i32 0
@_PC_PATH_MAX = global i32 0
@stderr = global i32 0
@.str.2 = [59 x i8] c"pathconf(%s, _PC_PATH_MAX) returned %d with error %d (%s)\0A\00"
@.str.3 = [87 x i8] c"pathconf(%s, _PC_PATH_MAX) returned %d without changing errno; PATH_MAX is unlimited.\0A\00"
@.str.4 = [30 x i8] c"readlink(%s) failed: %d (%s)\0A\00"
@.str.5 = [4 x i8] c"%s\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 2
br i1 %3, label %4, label %9
4: ; preds = %2
%5 = getelementptr inbounds i8*, i8** %1, i64 1
%6 = load i8*, i8** %5
%7 = tail call i64 @strcmp(i8* %6, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%8 = icmp eq i64 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %4, %2
%10 = load i8*, i8** %1
%11 = tail call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* %10)
br label %48
12: ; preds = %4
store i32 0, i32* @errno
%13 = load i8*, i8** %5
%14 = load i32, i32* @_PC_PATH_MAX
%15 = tail call i64 @pathconf(i8* %13, i32 %14)
%16 = icmp slt i64 %15, 0
br i1 %16, label %17, label %28
17: ; preds = %12
%18 = load i32, i32* @errno
%19 = icmp eq i32 %18, 0
%20 = load i32, i32* @stderr
%21 = load i8*, i8** %5
%22 = trunc i64 %15 to i32
br i1 %19, label %26, label %23
23: ; preds = %17
%24 = tail call i8* @strerror(i32 0)
%25 = tail call i32 (i32, i8*, i8*, i32, ...) @fprintf(i32 %20, i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str.2, i64 0, i64 0), i8* %21, i32 %22, i32 0, i8* %24)
br label %28
26: ; preds = %17
%27 = tail call i32 (i32, i8*, i8*, i32, ...) @fprintf(i32 %20, i8* getelementptr inbounds ([87 x i8], [87 x i8]* @.str.3, i64 0, i64 0), i8* %21, i32 %22)
br label %28
28: ; preds = %23, %26, %12
%29 = phi i64 [ %15, %12 ], [ 10000, %26 ], [ 10000, %23 ]
%30 = add nuw nsw i64 %29, 1
%31 = tail call i64 @malloc(i64 %30)
%32 = inttoptr i64 %31 to i8*
%33 = tail call i32 @memset(i8* %32, i32 0, i64 %30)
%34 = load i8*, i8** %5
%35 = tail call i64 @readlink(i8* %34, i8* %32, i64 %30)
%36 = icmp slt i64 %35, 0
br i1 %36, label %37, label %43
37: ; preds = %28
%38 = load i32, i32* @errno
%39 = load i32, i32* @stderr
%40 = load i8*, i8** %5
%41 = tail call i8* @strerror(i32 %38)
%42 = tail call i32 (i32, i8*, i8*, i32, ...) @fprintf(i32 %39, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.4, i64 0, i64 0), i8* %40, i32 %38, i8* %41)
br label %45
43: ; preds = %28
%44 = tail call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i8* %32)
br label %45
45: ; preds = %43, %37
%46 = phi i32 [ 1, %37 ], [ 0, %43 ]
%47 = tail call i32 @free(i8* %32)
br label %48
48: ; preds = %45, %9
%49 = phi i32 [ 1, %9 ], [ %46, %45 ]
ret i32 %49
}