#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 int ssize_t;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ free(char *);
int lstat(char *, struct stat *);
char *malloc(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *, char *);
int 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 == -1) {
    perror("readlink");
    exit(EXIT_FAILURE);
  }
  if (r > sb.st_size) {
    fprintf(stderr, "symlink increased in size "
                    "between lstat() and readlink()\n");
    exit(EXIT_FAILURE);
  }
  linkname[r] = '\0';
  printf("'%s' points to '%s'\n", argv[1], linkname);
  free(linkname);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x4(%rbp)
je     11c5 <main+0x45>
mov    0x2e40(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe50(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    0x2e1a(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    -0x18(%rbp),%rsi
call   1060 <lstat@plt>
cmp    $0xffffffff,%eax
jne    11f9 <main+0x79>
lea    0xe30(%rip),%rdi
call   1090 <perror@plt>
mov    0x2de6(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    -0x18(%rbp),%edi
add    $0x1,%edi
call   1080 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1238 <main+0xb8>
mov    0x2dc6(%rip),%rax
mov    (%rax),%edi
lea    0xdf9(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    0x2da7(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%edx
add    $0x1,%edx
call   1040 <readlink@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    1276 <main+0xf6>
lea    0xdce(%rip),%rdi
call   1090 <perror@plt>
mov    0x2d69(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    -0x24(%rbp),%eax
cmp    -0x18(%rbp),%eax
jle    12a7 <main+0x127>
mov    0x2d57(%rip),%rax
mov    (%rax),%edi
lea    0xda8(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    0x2d38(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    -0x20(%rbp),%rax
movslq -0x24(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
mov    -0x20(%rbp),%rdx
lea    0xdae(%rip),%rdi
call   1050 <printf@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
cmp    $0x2,%edi
jne    11fd <main+0x7d>
mov    0x8(%r14),%rdi
mov    %rsp,%rsi
call   1060 <lstat@plt>
cmp    $0xffffffff,%eax
je     1219 <main+0x99>
mov    (%rsp),%r15d
lea    0x1(%r15),%ebp
mov    %ebp,%edi
call   1080 <malloc@plt>
test   %rax,%rax
je     1222 <main+0xa2>
mov    %rax,%rbx
mov    0x8(%r14),%rdi
mov    %rax,%rsi
mov    %ebp,%edx
call   1040 <readlink@plt>
cmp    $0xffffffff,%eax
je     1234 <main+0xb4>
cmp    %r15d,%eax
jg     1242 <main+0xc2>
cltq
movb   $0x0,(%rbx,%rax,1)
mov    0x8(%r14),%rsi
lea    0xe94(%rip),%rdi
mov    %rbx,%rdx
call   1050 <printf@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2ddc(%rip),%rax
mov    (%rax),%edi
mov    (%r14),%rdx
lea    0xdf0(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
jmp    1259 <main+0xd9>
lea    0xdf6(%rip),%rdi
jmp    123b <main+0xbb>
mov    0x2db7(%rip),%rax
mov    (%rax),%edi
lea    0xdea(%rip),%rsi
jmp    1252 <main+0xd2>
lea    0xdf6(%rip),%rdi
call   1090 <perror@plt>
jmp    1259 <main+0xd9>
mov    0x2d97(%rip),%rax
mov    (%rax),%edi
lea    0xde8(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    0x2d78(%rip),%rax
mov    (%rax),%edi
call   10a0 <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 = [9 x i8] c"readlink\00"
@.str.4 = [58 x i8] c"symlink increased in size between lstat() and readlink()\0A\00"
@.str.5 = [21 x i8] c"'%s' points to '%s'\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i8**
%5 = alloca %struct.stat
%6 = alloca i8*
%7 = alloca i32
store i32 %0, i32* %3
store i8** %1, i8*** %4
%8 = load i32, i32* %3
%9 = icmp ne i32 %8, 2
br i1 %9, label %10, label %18
10: ; preds = %2
%11 = load i32, i32* @stderr
%12 = load i8**, i8*** %4
%13 = getelementptr inbounds i8*, i8** %12, i64 0
%14 = load i8*, i8** %13
%15 = call i32 (i32, i8*, ...) @fprintf(i32 %11, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %14)
%16 = load i32, i32* @EXIT_FAILURE
%17 = call i32 @exit(i32 %16)
unreachable
18: ; preds = %2
%19 = load i8**, i8*** %4
%20 = getelementptr inbounds i8*, i8** %19, i64 1
%21 = load i8*, i8** %20
%22 = call i32 @lstat(i8* %21, %struct.stat* %5)
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %28
24: ; preds = %18
%25 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%26 = load i32, i32* @EXIT_FAILURE
%27 = call i32 @exit(i32 %26)
unreachable
28: ; preds = %18
%29 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 0
%30 = load i32, i32* %29
%31 = add nsw i32 %30, 1
%32 = call i8* @malloc(i32 %31)
store i8* %32, i8** %6
%33 = load i8*, i8** %6
%34 = icmp eq i8* %33, null
br i1 %34, label %35, label %40
35: ; preds = %28
%36 = load i32, i32* @stderr
%37 = call i32 (i32, i8*, ...) @fprintf(i32 %36, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
%38 = load i32, i32* @EXIT_FAILURE
%39 = call i32 @exit(i32 %38)
unreachable
40: ; preds = %28
%41 = load i8**, i8*** %4
%42 = getelementptr inbounds i8*, i8** %41, i64 1
%43 = load i8*, i8** %42
%44 = load i8*, i8** %6
%45 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 0
%46 = load i32, i32* %45
%47 = add nsw i32 %46, 1
%48 = call i32 @readlink(i8* %43, i8* %44, i32 %47)
store i32 %48, i32* %7
%49 = load i32, i32* %7
%50 = icmp eq i32 %49, -1
br i1 %50, label %51, label %55
51: ; preds = %40
%52 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
%53 = load i32, i32* @EXIT_FAILURE
%54 = call i32 @exit(i32 %53)
unreachable
55: ; preds = %40
%56 = load i32, i32* %7
%57 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 0
%58 = load i32, i32* %57
%59 = icmp sgt i32 %56, %58
br i1 %59, label %60, label %65
60: ; preds = %55
%61 = load i32, i32* @stderr
%62 = call i32 (i32, i8*, ...) @fprintf(i32 %61, i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str.4, i64 0, i64 0))
%63 = load i32, i32* @EXIT_FAILURE
%64 = call i32 @exit(i32 %63)
unreachable
65: ; preds = %55
%66 = load i8*, i8** %6
%67 = load i32, i32* %7
%68 = sext i32 %67 to i64
%69 = getelementptr inbounds i8, i8* %66, i64 %68
store i8 0, i8* %69
%70 = load i8**, i8*** %4
%71 = getelementptr inbounds i8*, i8** %70, i64 1
%72 = load i8*, i8** %71
%73 = load i8*, i8** %6
%74 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0), i8* %72, i8* %73)
%75 = load i8*, i8** %6
%76 = call i32 @free(i8* %75)
ret i32 0
}

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 = [9 x i8] c"readlink\00"
@.str.4 = [58 x i8] c"symlink increased in size between lstat() and readlink()\0A\00"
@.str.5 = [21 x i8] c"'%s' points to '%s'\0A\00"
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 i32 @readlink(i8* %33, i8* %25, i32 %24)
%35 = icmp eq i32 %34, -1
br i1 %35, label %36, label %40
36: ; preds = %32
%37 = tail call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
%38 = load i32, i32* @EXIT_FAILURE
%39 = tail call i32 @exit(i32 %38)
unreachable
40: ; preds = %32
%41 = icmp sgt i32 %34, %23
br i1 %41, label %42, label %47
42: ; preds = %40
%43 = load i32, i32* @stderr
%44 = tail call i32 (i32, i8*, ...) @fprintf(i32 %43, i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str.4, i64 0, i64 0))
%45 = load i32, i32* @EXIT_FAILURE
%46 = tail call i32 @exit(i32 %45)
unreachable
47: ; preds = %40
%48 = sext i32 %34 to i64
%49 = getelementptr inbounds i8, i8* %25, i64 %48
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.5, i64 0, i64 0), i8* %50, i8* %25)
%52 = tail call i32 @free(i8* %25)
ret i32 0
}