#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 {
  int st_size;
};
typedef scalar_t__ ssize_t;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ EXIT_SUCCESS;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int lstat(char *, struct stat *);
char *malloc(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *, char *);
scalar_t__ readlink(char *, char *, int);
int /*<<< orphan*/ stderr;
int main(int argc, char *argv[]) {
  struct stat sb;
  char *linkname;
  ssize_t r;
  if (argc != 2) {
    fprintf(stderr, "Usage: %s <pathname>\n", argv[0]);
    exit(EXIT_FAILURE);
  }
  if (lstat(argv[1], &sb) == -1) {
    perror("lstat");
    exit(EXIT_FAILURE);
  }
  linkname = malloc(sb.st_size + 1);
  if (linkname == NULL) {
    fprintf(stderr, "insufficient memory\n");
    exit(EXIT_FAILURE);
  }
  r = readlink(argv[1], linkname, sb.st_size + 1);
  if (r < 0) {
    perror("lstat");
    exit(EXIT_FAILURE);
  }
  if (r > sb.st_size) {
    fprintf(stderr, "symlink increased in size "
                    "between lstat() and readlink()\n");
    exit(EXIT_FAILURE);
  }
  linkname[sb.st_size] = '\0';
  printf("'%s' points to '%s'\n", argv[1], linkname);
  exit(EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11bc <main+0x4c>
mov    0x2e49(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe59(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    0x2e23(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    -0x18(%rbp),%rsi
call   1050 <lstat@plt>
cmp    $0xffffffff,%eax
jne    11f0 <main+0x80>
lea    0xe39(%rip),%rdi
call   1080 <perror@plt>
mov    0x2def(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    -0x18(%rbp),%edi
add    $0x1,%edi
call   1070 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    122f <main+0xbf>
mov    0x2dcf(%rip),%rax
mov    (%rax),%edi
lea    0xe02(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    0x2db0(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%edx
add    $0x1,%edx
call   1030 <readlink@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jge    126f <main+0xff>
lea    0xdba(%rip),%rdi
call   1080 <perror@plt>
mov    0x2d70(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    -0x28(%rbp),%rax
movslq -0x18(%rbp),%rcx
cmp    %rcx,%rax
jle    12a5 <main+0x135>
mov    0x2d59(%rip),%rax
mov    (%rax),%edi
lea    0xda1(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    0x2d3a(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
mov    -0x20(%rbp),%rax
movslq -0x18(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
mov    -0x20(%rbp),%rdx
lea    0xda7(%rip),%rdi
call   1040 <printf@plt>
mov    0x2d00(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
cmp    $0x2,%edi
jne    119e <main+0x2e>
mov    0x8(%rbx),%rdi
mov    %rsp,%rsi
call   1050 <lstat@plt>
cmp    $0xffffffff,%eax
jne    11ba <main+0x4a>
lea    0xe7f(%rip),%rdi
call   1080 <perror@plt>
jmp    1212 <main+0xa2>
mov    0x2e3b(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%rdx
lea    0xe4f(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
jmp    1212 <main+0xa2>
movslq (%rsp),%r15
lea    0x1(%r15),%ebp
mov    %ebp,%edi
call   1070 <malloc@plt>
test   %rax,%rax
jne    11e0 <main+0x70>
mov    0x2e0b(%rip),%rax
mov    (%rax),%edi
lea    0xe3e(%rip),%rsi
jmp    120b <main+0x9b>
mov    %rax,%r14
mov    0x8(%rbx),%rdi
mov    %rax,%rsi
mov    %ebp,%edx
call   1030 <readlink@plt>
test   %rax,%rax
js     1190 <main+0x20>
cmp    %r15,%rax
jle    1220 <main+0xb0>
mov    0x2dde(%rip),%rax
mov    (%rax),%edi
lea    0xe26(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    0x2dbf(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>
movb   $0x0,(%r14,%r15,1)
mov    0x8(%rbx),%rsi
lea    0xe3b(%rip),%rdi
mov    %r14,%rdx
call   1040 <printf@plt>
mov    0x2d91(%rip),%rax
mov    (%rax),%edi
call   1090 <exit@plt>

LLVM IR (-O0):
%struct.stat = type { i32 }
@stderr = global i32 0
@.str = [22 x i8] c"Usage: %s <pathname>\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [6 x i8] c"lstat\00"
@.str.2 = [21 x i8] c"insufficient memory\0A\00"
@.str.3 = [58 x i8] c"symlink increased in size between lstat() and readlink()\0A\00"
@.str.4 = [21 x i8] c"'%s' points to '%s'\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.stat
%7 = alloca i8*
%8 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* %4
%10 = icmp ne i32 %9, 2
br i1 %10, label %11, label %19
11: ; preds = %2
%12 = load i32, i32* @stderr
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 0
%15 = load i8*, i8** %14
%16 = call i32 (i32, i8*, ...) @fprintf(i32 %12, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %15)
%17 = load i32, i32* @EXIT_FAILURE
%18 = call i32 @exit(i32 %17)
unreachable
19: ; preds = %2
%20 = load i8**, i8*** %5
%21 = getelementptr inbounds i8*, i8** %20, i64 1
%22 = load i8*, i8** %21
%23 = call i32 @lstat(i8* %22, %struct.stat* %6)
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %29
25: ; preds = %19
%26 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%27 = load i32, i32* @EXIT_FAILURE
%28 = call i32 @exit(i32 %27)
unreachable
29: ; preds = %19
%30 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%31 = load i32, i32* %30
%32 = add nsw i32 %31, 1
%33 = call i8* @malloc(i32 %32)
store i8* %33, i8** %7
%34 = load i8*, i8** %7
%35 = icmp eq i8* %34, null
br i1 %35, label %36, label %41
36: ; preds = %29
%37 = load i32, i32* @stderr
%38 = call i32 (i32, i8*, ...) @fprintf(i32 %37, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
%39 = load i32, i32* @EXIT_FAILURE
%40 = call i32 @exit(i32 %39)
unreachable
41: ; preds = %29
%42 = load i8**, i8*** %5
%43 = getelementptr inbounds i8*, i8** %42, i64 1
%44 = load i8*, i8** %43
%45 = load i8*, i8** %7
%46 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%47 = load i32, i32* %46
%48 = add nsw i32 %47, 1
%49 = call i64 @readlink(i8* %44, i8* %45, i32 %48)
store i64 %49, i64* %8
%50 = load i64, i64* %8
%51 = icmp slt i64 %50, 0
br i1 %51, label %52, label %56
52: ; preds = %41
%53 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%54 = load i32, i32* @EXIT_FAILURE
%55 = call i32 @exit(i32 %54)
unreachable
56: ; preds = %41
%57 = load i64, i64* %8
%58 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%59 = load i32, i32* %58
%60 = sext i32 %59 to i64
%61 = icmp sgt i64 %57, %60
br i1 %61, label %62, label %67
62: ; preds = %56
%63 = load i32, i32* @stderr
%64 = call i32 (i32, i8*, ...) @fprintf(i32 %63, i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str.3, i64 0, i64 0))
%65 = load i32, i32* @EXIT_FAILURE
%66 = call i32 @exit(i32 %65)
unreachable
67: ; preds = %56
%68 = load i8*, i8** %7
%69 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%70 = load i32, i32* %69
%71 = sext i32 %70 to i64
%72 = getelementptr inbounds i8, i8* %68, i64 %71
store i8 0, i8* %72
%73 = load i8**, i8*** %5
%74 = getelementptr inbounds i8*, i8** %73, i64 1
%75 = load i8*, i8** %74
%76 = load i8*, i8** %7
%77 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0), i8* %75, i8* %76)
%78 = load i32, i32* @EXIT_SUCCESS
%79 = call i32 @exit(i32 %78)
unreachable
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@stderr = global i32 0
@.str = [22 x i8] c"Usage: %s <pathname>\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [6 x i8] c"lstat\00"
@.str.2 = [21 x i8] c"insufficient memory\0A\00"
@.str.3 = [58 x i8] c"symlink increased in size between lstat() and readlink()\0A\00"
@.str.4 = [21 x i8] c"'%s' points to '%s'\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = icmp eq i32 %0, 2
br i1 %5, label %12, label %6
6: ; preds = %2
%7 = load i32, i32* @stderr
%8 = load i8*, i8** %1
%9 = tail call i32 (i32, i8*, ...) @fprintf(i32 %7, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %8)
%10 = load i32, i32* @EXIT_FAILURE
%11 = tail call i32 @exit(i32 %10)
unreachable
12: ; preds = %2
%13 = getelementptr inbounds i8*, i8** %1, i64 1
%14 = load i8*, i8** %13
%15 = call i32 @lstat(i8* %14, %struct.stat* %3)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %21
17: ; preds = %12
%18 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%19 = load i32, i32* @EXIT_FAILURE
%20 = tail call i32 @exit(i32 %19)
unreachable
21: ; preds = %12
%22 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%23 = load i32, i32* %22
%24 = add nsw i32 %23, 1
%25 = tail call i8* @malloc(i32 %24)
%26 = icmp eq i8* %25, null
br i1 %26, label %27, label %32
27: ; preds = %21
%28 = load i32, i32* @stderr
%29 = tail call i32 (i32, i8*, ...) @fprintf(i32 %28, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
%30 = load i32, i32* @EXIT_FAILURE
%31 = tail call i32 @exit(i32 %30)
unreachable
32: ; preds = %21
%33 = load i8*, i8** %13
%34 = tail call i64 @readlink(i8* %33, i8* %25, i32 %24)
%35 = icmp slt i64 %34, 0
br i1 %35, label %36, label %40
36: ; preds = %32
%37 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%38 = load i32, i32* @EXIT_FAILURE
%39 = tail call i32 @exit(i32 %38)
unreachable
40: ; preds = %32
%41 = sext i32 %23 to i64
%42 = icmp sgt i64 %34, %41
br i1 %42, label %43, label %48
43: ; preds = %40
%44 = load i32, i32* @stderr
%45 = tail call i32 (i32, i8*, ...) @fprintf(i32 %44, i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str.3, i64 0, i64 0))
%46 = load i32, i32* @EXIT_FAILURE
%47 = tail call i32 @exit(i32 %46)
unreachable
48: ; preds = %40
%49 = getelementptr inbounds i8, i8* %25, i64 %41
store i8 0, i8* %49
%50 = load i8*, i8** %13
%51 = tail call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0), i8* %50, i8* %25)
%52 = load i32, i32* @EXIT_SUCCESS
%53 = tail call i32 @exit(i32 %52)
unreachable
}