#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_mode;
  scalar_t__ st_size;
};
typedef size_t ssize_t;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int S_IFLNK;
int S_IFMT;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
scalar_t__ lstat(char *, struct stat *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
size_t readlink(char *, char *, int);
int /*<<< orphan*/ stderr;
int main(int argc, char *argv[]) {
  struct stat stab;
  char buf[1024];
  ssize_t sz;
  if (argc != 2) {
    fprintf(stderr, "Usage: %s /path/to/symlink\n", argv[0]);
    exit(2);
  }
  if (lstat(argv[1], &stab)) {
    perror("lstat");
    exit(1);
  }
  if ((stab.st_mode & S_IFMT) != S_IFLNK) {
    fprintf(stderr, "not a symlink\n");
    exit(1);
  }
  if ((sz = readlink(argv[1], buf, sizeof(buf))) < 0) {
    perror("readlink");
    exit(1);
  }
  buf[sz] = '\0';
  printf("%s -> %s\n", argv[1], buf);
  printf("readlink size: %d\n", (int)sz);
  printf("stat size: %d\n", (int)stab.st_size);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11ab <main+0x4b>
mov    0x2e46(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe66(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    $0x2,%edi
call   1080 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    -0x20(%rbp),%rsi
call   1050 <lstat@plt>
cmp    $0x0,%rax
je     11dc <main+0x7c>
lea    0xe4f(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x20(%rbp),%eax
mov    0x2df2(%rip),%rcx
and    (%rcx),%eax
mov    0x2df9(%rip),%rcx
cmp    (%rcx),%eax
je     1218 <main+0xb8>
mov    0x2dd2(%rip),%rax
mov    (%rax),%edi
lea    0xe1b(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    -0x420(%rbp),%rsi
mov    $0x400,%edx
call   1030 <readlink@plt>
mov    %rax,-0x428(%rbp)
cmp    $0x0,%rax
jae    1258 <main+0xf8>
lea    0xde8(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x428(%rbp),%rax
movb   $0x0,-0x420(%rbp,%rax,1)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    -0x420(%rbp),%rdx
lea    0xdbd(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x428(%rbp),%rax
mov    %eax,%esi
lea    0xdb0(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%rax
mov    %eax,%esi
lea    0xdaf(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x410,%rsp
mov    %rsi,%r15
cmp    $0x2,%edi
jne    120c <main+0xac>
mov    0x8(%r15),%rdi
mov    %rsp,%rsi
call   1050 <lstat@plt>
test   %rax,%rax
jne    1230 <main+0xd0>
mov    0x2e44(%rip),%rax
mov    (%rax),%eax
and    (%rsp),%eax
mov    0x2e48(%rip),%rcx
cmp    (%rcx),%eax
jne    1246 <main+0xe6>
mov    0x8(%r15),%rdi
lea    0x10(%rsp),%r14
mov    %r14,%rsi
mov    $0x400,%edx
call   1030 <readlink@plt>
mov    %rax,%rbx
movb   $0x0,0x10(%rsp,%rax,1)
mov    0x8(%r15),%rsi
lea    0xe60(%rip),%rdi
mov    %r14,%rdx
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xe59(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x8(%rsp),%esi
lea    0xe58(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x410,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
mov    0x2dbd(%rip),%rax
mov    (%rax),%edi
mov    (%r15),%rdx
lea    0xde1(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0x2,%edi
call   1080 <exit@plt>
lea    0xde5(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    0x2d83(%rip),%rax
mov    (%rax),%edi
lea    0xdcc(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
%struct.stat = type { i32, i64 }
@stderr = global i32 0
@.str = [28 x i8] c"Usage: %s /path/to/symlink\0A\00"
@.str.1 = [6 x i8] c"lstat\00"
@S_IFMT = global i32 0
@S_IFLNK = global i32 0
@.str.2 = [15 x i8] c"not a symlink\0A\00"
@.str.3 = [9 x i8] c"readlink\00"
@.str.4 = [10 x i8] c"%s -> %s\0A\00"
@.str.5 = [19 x i8] c"readlink size: %d\0A\00"
@.str.6 = [15 x i8] c"stat size: %d\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.stat
%7 = alloca [1024 x 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 %18
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 ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i8* %15)
%17 = call i32 @exit(i32 2)
unreachable
18: ; preds = %2
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 1
%21 = load i8*, i8** %20
%22 = call i64 @lstat(i8* %21, %struct.stat* %6)
%23 = icmp ne i64 %22, 0
br i1 %23, label %24, label %27
24: ; preds = %18
%25 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%26 = call i32 @exit(i32 1)
unreachable
27: ; preds = %18
%28 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%29 = load i32, i32* %28
%30 = load i32, i32* @S_IFMT
%31 = and i32 %29, %30
%32 = load i32, i32* @S_IFLNK
%33 = icmp ne i32 %31, %32
br i1 %33, label %34, label %38
34: ; preds = %27
%35 = load i32, i32* @stderr
%36 = call i32 (i32, i8*, ...) @fprintf(i32 %35, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
%37 = call i32 @exit(i32 1)
unreachable
38: ; preds = %27
%39 = load i8**, i8*** %5
%40 = getelementptr inbounds i8*, i8** %39, i64 1
%41 = load i8*, i8** %40
%42 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%43 = call i64 @readlink(i8* %41, i8* %42, i32 1024)
store i64 %43, i64* %8
%44 = icmp ult i64 %43, 0
br i1 %44, label %45, label %48
45: ; preds = %38
%46 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
%47 = call i32 @exit(i32 1)
unreachable
48: ; preds = %38
%49 = load i64, i64* %8
%50 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 %49
store i8 0, i8* %50
%51 = load i8**, i8*** %5
%52 = getelementptr inbounds i8*, i8** %51, i64 1
%53 = load i8*, i8** %52
%54 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%55 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i8* %53, i8* %54)
%56 = load i64, i64* %8
%57 = trunc i64 %56 to i32
%58 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0), i32 %57)
%59 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 1
%60 = load i64, i64* %59
%61 = trunc i64 %60 to i32
%62 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.6, i64 0, i64 0), i32 %61)
ret i32 0
}

LLVM IR (-O2):
%struct.stat = type { i32, i64 }
@stderr = global i32 0
@.str = [28 x i8] c"Usage: %s /path/to/symlink\0A\00"
@.str.1 = [6 x i8] c"lstat\00"
@S_IFMT = global i32 0
@S_IFLNK = global i32 0
@.str.2 = [15 x i8] c"not a symlink\0A\00"
@.str.4 = [10 x i8] c"%s -> %s\0A\00"
@.str.5 = [19 x i8] c"readlink size: %d\0A\00"
@.str.6 = [15 x i8] c"stat size: %d\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.stat
%4 = alloca [1024 x i8]
%5 = bitcast %struct.stat* %3 to i8*
%6 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%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*, ...) @fprintf(i32 %9, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i8* %10)
%12 = tail call i32 @exit(i32 2)
unreachable
13: ; preds = %2
%14 = getelementptr inbounds i8*, i8** %1, i64 1
%15 = load i8*, i8** %14
%16 = call i64 @lstat(i8* %15, %struct.stat* %3)
%17 = icmp eq i64 %16, 0
br i1 %17, label %21, label %18
18: ; preds = %13
%19 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%20 = tail call i32 @exit(i32 1)
unreachable
21: ; preds = %13
%22 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%23 = load i32, i32* %22
%24 = load i32, i32* @S_IFMT
%25 = and i32 %24, %23
%26 = load i32, i32* @S_IFLNK
%27 = icmp eq i32 %25, %26
br i1 %27, label %32, label %28
28: ; preds = %21
%29 = load i32, i32* @stderr
%30 = tail call i32 (i32, i8*, ...) @fprintf(i32 %29, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
%31 = tail call i32 @exit(i32 1)
unreachable
32: ; preds = %21
%33 = load i8*, i8** %14
%34 = call i64 @readlink(i8* %33, i8* %6, i32 1024)
%35 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 %34
store i8 0, i8* %35
%36 = load i8*, i8** %14
%37 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i8* %36, i8* %6)
%38 = trunc i64 %34 to i32
%39 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0), i32 %38)
%40 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 1
%41 = load i64, i64* %40
%42 = trunc i64 %41 to i32
%43 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.6, i64 0, i64 0), i32 %42)
ret i32 0
}