#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 */
scalar_t__ EINVAL;
int PATH_MAX;
scalar_t__ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, ...);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, char *);
int readlink(char *, char *, int);
char *rindex(char *, char);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcpy(char *, char *);
char *strerror(scalar_t__);
int /*<<< orphan*/ strncpy(char *, char *, int);
int main(int argc, char *argv[]) {
  char buf1[PATH_MAX], buf2[PATH_MAX];
  char *src, *dst, *tmp;
  int res;
  if (argc != 2) {
    fprintf(stderr, "Usage: %s <FileName>\n", argv[0]);
    exit(1);
  }
  src = buf1;
  dst = buf2;
  strncpy(src, argv[1], PATH_MAX);
  src[PATH_MAX - 1] = (char)0;
  while (1) {
    memset(dst, 0, PATH_MAX);
    res = readlink(src, dst, PATH_MAX - 1);
    if (res == -1) {
      if (errno == EINVAL) {
        printf("%s\n", src);
        exit(0);
      } else {
        fprintf(stderr, "%s: Can't readlink '%s' %s(%d)\n", argv[0], src,
                strerror(errno), errno);
        exit(1);
      }
    } else {
      if (dst[0] != (char)'/') {
        char *last_slash;
        last_slash = rindex(src, '/');
        strcpy(last_slash + 1, dst);
      } else {
        tmp = src;
        src = dst;
        dst = tmp;
      }
    }
  }
  return 1;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e40(%rip),%rax
mov    (%rax),%ecx
mov    %rsp,%rdx
mov    %rdx,-0x18(%rbp)
mov    %rcx,%rsi
add    $0xf,%rsi
and    $0xfffffffffffffff0,%rsi
mov    %rsp,%rdx
sub    %rsi,%rdx
mov    %rdx,-0x60(%rbp)
mov    %rdx,%rsp
mov    %rcx,-0x20(%rbp)
mov    (%rax),%eax
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x58(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x28(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     1225 <main+0x95>
mov    0x2dd4(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xdec(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x58(%rbp),%rax
mov    -0x60(%rbp),%rcx
mov    %rcx,-0x30(%rbp)
mov    %rax,-0x38(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
mov    0x2da8(%rip),%rax
mov    (%rax),%edx
call   1030 <strncpy@plt>
mov    -0x30(%rbp),%rax
mov    0x2d96(%rip),%rcx
mov    (%rcx),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    126b <main+0xdb>
mov    -0x38(%rbp),%rdi
mov    0x2d7a(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1070 <memset@plt>
mov    -0x30(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    0x2d62(%rip),%rax
mov    (%rax),%edx
sub    $0x1,%edx
call   1050 <readlink@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0xffffffff,-0x44(%rbp)
jne    1334 <main+0x1a4>
mov    0x2d24(%rip),%rax
mov    (%rax),%rax
mov    0x2d12(%rip),%rcx
cmp    (%rcx),%rax
jne    12d6 <main+0x146>
mov    -0x30(%rbp),%rsi
lea    0xd4c(%rip),%rdi
call   1060 <printf@plt>
xor    %edi,%edi
call   10a0 <exit@plt>
mov    0x2cfb(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x74(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x70(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x68(%rbp)
mov    0x2cd4(%rip),%rax
mov    (%rax),%rdi
call   10b0 <strerror@plt>
mov    -0x74(%rbp),%edi
mov    -0x70(%rbp),%rdx
mov    -0x68(%rbp),%rcx
mov    %rax,%r8
mov    0x2cb7(%rip),%rax
mov    (%rax),%r9
lea    0xcf7(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x38(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
je     136c <main+0x1dc>
mov    -0x30(%rbp),%rdi
mov    $0x2f,%esi
call   1090 <rindex@plt>
mov    %rax,-0x50(%rbp)
mov    -0x50(%rbp),%rdi
add    $0x1,%rdi
mov    -0x38(%rbp),%rsi
call   1040 <strcpy@plt>
jmp    1384 <main+0x1f4>
mov    -0x30(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x40(%rbp),%rax
mov    %rax,-0x38(%rbp)
jmp    1389 <main+0x1f9>
jmp    1266 <main+0xd6>

x86 assembly (clang, -O2):
<main>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    0x2e48(%rip),%r13
mov    0x0(%r13),%edx
mov    %rsp,%r15
lea    0xf(%rdx),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r15
mov    %r15,%rsp
mov    %rsp,%r12
sub    %rax,%r12
mov    %r12,%rsp
cmp    $0x2,%edi
jne    1253 <main+0xc3>
mov    0x8(%r14),%rsi
mov    %r15,%rdi
call   1030 <strncpy@plt>
movslq 0x0(%r13),%rdx
movb   $0x0,-0x1(%rdx,%r15,1)
mov    %r12,%rdi
xor    %esi,%esi
call   1070 <memset@plt>
mov    0x0(%r13),%edx
add    $0xffffffff,%edx
mov    %r15,%rdi
mov    %r12,%rsi
jmp    1237 <main+0xa7>
xchg   %ax,%ax
mov    %r15,%rdi
mov    $0x2f,%esi
call   1090 <rindex@plt>
lea    0x1(%rax),%rdi
mov    %r12,%rsi
call   1040 <strcpy@plt>
mov    %r12,%rbx
mov    0x0(%r13),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1070 <memset@plt>
mov    0x0(%r13),%edx
add    $0xffffffff,%edx
mov    %r15,%rdi
mov    %rbx,%rsi
call   1050 <readlink@plt>
cmp    $0xffffffff,%eax
je     1277 <main+0xe7>
cmpb   $0x2f,(%r12)
jne    1200 <main+0x70>
mov    %r15,%rbx
mov    %r12,%r15
mov    %rbx,%r12
jmp    121c <main+0x8c>
mov    0x2d7e(%rip),%rax
mov    (%rax),%edi
mov    (%r14),%rdx
lea    0xd9a(%rip),%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    0x2d52(%rip),%rbx
mov    (%rbx),%rdi
mov    0x2d40(%rip),%rax
cmp    (%rax),%rdi
jne    12a3 <main+0x113>
lea    0xd82(%rip),%rdi
mov    %r15,%rsi
call   1060 <printf@plt>
xor    %edi,%edi
call   10a0 <exit@plt>
mov    0x2d2e(%rip),%rax
mov    (%rax),%r12d
mov    (%r14),%r14
call   10b0 <strerror@plt>
mov    (%rbx),%r9
lea    0xd5b(%rip),%rsi
mov    %r12d,%edi
mov    %r14,%rdx
mov    %r15,%rcx
mov    %rax,%r8
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@PATH_MAX = global i32 0
@stderr = global i32 0
@.str = [22 x i8] c"Usage: %s <FileName>\0A\00"
@errno = global i64 0
@EINVAL = global i64 0
@.str.1 = [4 x i8] c"%s\0A\00"
@.str.2 = [32 x i8] c"%s: Can't readlink '%s' %s(%d)\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca i32
%13 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%14 = load i32, i32* @PATH_MAX
%15 = zext i32 %14 to i64
%16 = call i8* @llvm.stacksave()
store i8* %16, i8** %6
%17 = alloca i8, i64 %15
store i64 %15, i64* %7
%18 = load i32, i32* @PATH_MAX
%19 = zext i32 %18 to i64
%20 = alloca i8, i64 %19
store i64 %19, i64* %8
%21 = load i32, i32* %4
%22 = icmp ne i32 %21, 2
br i1 %22, label %23, label %30
23: ; preds = %2
%24 = load i32, i32* @stderr
%25 = load i8**, i8*** %5
%26 = getelementptr inbounds i8*, i8** %25, i64 0
%27 = load i8*, i8** %26
%28 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %24, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %27)
%29 = call i32 @exit(i32 1)
unreachable
30: ; preds = %2
store i8* %17, i8** %9
store i8* %20, i8** %10
%31 = load i8*, i8** %9
%32 = load i8**, i8*** %5
%33 = getelementptr inbounds i8*, i8** %32, i64 1
%34 = load i8*, i8** %33
%35 = load i32, i32* @PATH_MAX
%36 = call i32 @strncpy(i8* %31, i8* %34, i32 %35)
%37 = load i8*, i8** %9
%38 = load i32, i32* @PATH_MAX
%39 = sub nsw i32 %38, 1
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds i8, i8* %37, i64 %40
store i8 0, i8* %41
br label %42
42: ; preds = %91, %30
br label %43
43: ; preds = %42
%44 = load i8*, i8** %10
%45 = load i32, i32* @PATH_MAX
%46 = call i32 @memset(i8* %44, i32 0, i32 %45)
%47 = load i8*, i8** %9
%48 = load i8*, i8** %10
%49 = load i32, i32* @PATH_MAX
%50 = sub nsw i32 %49, 1
%51 = call i32 @readlink(i8* %47, i8* %48, i32 %50)
store i32 %51, i32* %12
%52 = load i32, i32* %12
%53 = icmp eq i32 %52, -1
br i1 %53, label %54, label %73
54: ; preds = %43
%55 = load i64, i64* @errno
%56 = load i64, i64* @EINVAL
%57 = icmp eq i64 %55, %56
br i1 %57, label %58, label %62
58: ; preds = %54
%59 = load i8*, i8** %9
%60 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %59)
%61 = call i32 @exit(i32 0)
unreachable
62: ; preds = %54
%63 = load i32, i32* @stderr
%64 = load i8**, i8*** %5
%65 = getelementptr inbounds i8*, i8** %64, i64 0
%66 = load i8*, i8** %65
%67 = load i8*, i8** %9
%68 = load i64, i64* @errno
%69 = call i8* @strerror(i64 %68)
%70 = load i64, i64* @errno
%71 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %63, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), i8* %66, i8* %67, i8* %69, i64 %70)
%72 = call i32 @exit(i32 1)
unreachable
73: ; preds = %43
%74 = load i8*, i8** %10
%75 = getelementptr inbounds i8, i8* %74, i64 0
%76 = load i8, i8* %75
%77 = sext i8 %76 to i32
%78 = icmp ne i32 %77, 47
br i1 %78, label %79, label %86
79: ; preds = %73
%80 = load i8*, i8** %9
%81 = call i8* @rindex(i8* %80, i8 signext 47)
store i8* %81, i8** %13
%82 = load i8*, i8** %13
%83 = getelementptr inbounds i8, i8* %82, i64 1
%84 = load i8*, i8** %10
%85 = call i32 @strcpy(i8* %83, i8* %84)
br label %90
86: ; preds = %73
%87 = load i8*, i8** %9
store i8* %87, i8** %11
%88 = load i8*, i8** %10
store i8* %88, i8** %9
%89 = load i8*, i8** %11
store i8* %89, i8** %10
br label %90
90: ; preds = %86, %79
br label %91
91: ; preds = %90
br label %42
}

LLVM IR (-O2):
@PATH_MAX = global i32 0
@stderr = global i32 0
@.str = [22 x i8] c"Usage: %s <FileName>\0A\00"
@errno = global i64 0
@EINVAL = global i64 0
@.str.1 = [4 x i8] c"%s\0A\00"
@.str.2 = [32 x i8] c"%s: Can't readlink '%s' %s(%d)\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = load i32, i32* @PATH_MAX
%4 = zext i32 %3 to i64
%5 = alloca i8, i64 %4
%6 = alloca i8, i64 %4
%7 = icmp eq i32 %0, 2
br i1 %7, label %13, label %8
8: ; preds = %2
%9 = load i32, i32* @stderr
%10 = load i8*, i8** %1
%11 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %9, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %10)
%12 = tail call i32 @exit(i32 1)
unreachable
13: ; preds = %2
%14 = getelementptr inbounds i8*, i8** %1, i64 1
%15 = load i8*, i8** %14
%16 = call i32 @strncpy(i8* %5, i8* %15, i32 %3)
%17 = load i32, i32* @PATH_MAX
%18 = add nsw i32 %17, -1
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds i8, i8* %5, i64 %19
store i8 0, i8* %20
%21 = call i32 @memset(i8* %6, i32 0, i32 %17)
%22 = load i32, i32* @PATH_MAX
%23 = add nsw i32 %22, -1
%24 = call i32 @readlink(i8* %5, i8* %6, i32 %23)
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %41
26: ; preds = %50, %13
%27 = phi i8* [ %5, %13 ], [ %52, %50 ]
%28 = load i64, i64* @errno
%29 = load i64, i64* @EINVAL
%30 = icmp eq i64 %28, %29
br i1 %30, label %31, label %34
31: ; preds = %26
%32 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %27)
%33 = call i32 @exit(i32 0)
unreachable
34: ; preds = %26
%35 = load i32, i32* @stderr
%36 = load i8*, i8** %1
%37 = call i8* @strerror(i64 %28)
%38 = load i64, i64* @errno
%39 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %35, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), i8* %36, i8* %27, i8* %37, i64 %38)
%40 = call i32 @exit(i32 1)
unreachable
41: ; preds = %13, %50
%42 = phi i8* [ %52, %50 ], [ %5, %13 ]
%43 = phi i8* [ %51, %50 ], [ %6, %13 ]
%44 = load i8, i8* %43
%45 = icmp eq i8 %44, 47
br i1 %45, label %50, label %46
46: ; preds = %41
%47 = call i8* @rindex(i8* %42, i8 signext 47)
%48 = getelementptr inbounds i8, i8* %47, i64 1
%49 = call i32 @strcpy(i8* %48, i8* %43)
br label %50
50: ; preds = %41, %46
%51 = phi i8* [ %43, %46 ], [ %42, %41 ]
%52 = phi i8* [ %42, %46 ], [ %43, %41 ]
%53 = load i32, i32* @PATH_MAX
%54 = call i32 @memset(i8* %51, i32 0, i32 %53)
%55 = load i32, i32* @PATH_MAX
%56 = add nsw i32 %55, -1
%57 = call i32 @readlink(i8* %52, i8* %51, i32 %56)
%58 = icmp eq i32 %57, -1
br i1 %58, label %26, label %41
}