#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 int /*<<< orphan*/ target;
struct stat {
  int /*<<< orphan*/ st_mode;
};

/* Variables and functions */
int /*<<< orphan*/ S_ISLNK(int /*<<< orphan*/);
scalar_t__ lstat(char *, struct stat *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ pst(struct stat);
int /*<<< orphan*/ puts(char *);
int readlink(char *, char *, int);
scalar_t__ stat(char *, struct stat *);
int main(int argc, char *argv[]) {
  if (argc != 2)
    return 1;
  struct stat st;
  if (lstat(argv[1], &st) < 0) {
    perror("lstat(argv[1],&st)");
    return 1;
  };
  puts("=== lstat()");
  pst(st);
  if (!S_ISLNK(st.st_mode))
    return 0;
  char target[1024];
  int len = readlink(argv[1], target, sizeof(target));
  if (len < 0) {
    perror("readlink");
    return 1;
  }
  target[len] = '\0';
  printf("symlink to %s\n\n", target);
  if (stat(argv[1], &st) < 0) {
    perror("stat(argv[1],&st)");
    return 1;
  };
  puts("=== stat()");
  pst(st);
}


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     11af <main+0x2f>
movl   $0x1,-0x4(%rbp)
jmp    12c0 <main+0x140>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    -0x18(%rbp),%rsi
call   1070 <lstat@plt>
cmp    $0x0,%rax
jge    11e2 <main+0x62>
lea    0xe2f(%rip),%rdi
call   1090 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12c0 <main+0x140>
lea    0xe2a(%rip),%rdi
call   1040 <puts@plt>
mov    -0x18(%rbp),%edi
call   10a0 <pst@plt>
mov    -0x18(%rbp),%edi
call   1030 <S_ISLNK@plt>
cmp    $0x0,%eax
jne    1213 <main+0x93>
movl   $0x0,-0x4(%rbp)
jmp    12c0 <main+0x140>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    -0x420(%rbp),%rsi
mov    $0x400,%edx
call   1050 <readlink@plt>
mov    %eax,-0x424(%rbp)
cmpl   $0x0,-0x424(%rbp)
jge    1257 <main+0xd7>
lea    0xdd9(%rip),%rdi
call   1090 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12c0 <main+0x140>
movslq -0x424(%rbp),%rax
movb   $0x0,-0x420(%rbp,%rax,1)
lea    -0x420(%rbp),%rsi
lea    0xdb4(%rip),%rdi
call   1060 <printf@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    -0x18(%rbp),%rsi
call   1080 <stat@plt>
cmp    $0x0,%rax
jge    12ac <main+0x12c>
lea    0xd66(%rip),%rdi
call   1090 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12c0 <main+0x140>
lea    0xd85(%rip),%rdi
call   1040 <puts@plt>
mov    -0x18(%rbp),%edi
call   10a0 <pst@plt>
mov    -0x4(%rbp),%eax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
mov    $0x1,%eax
cmp    $0x2,%edi
jne    1235 <main+0xb5>
push   %rbp
push   %rbx
sub    $0x418,%rsp
mov    %rsi,%rbx
mov    0x8(%rsi),%rdi
lea    0x8(%rsp),%rsi
call   1070 <lstat@plt>
test   %rax,%rax
js     1236 <main+0xb6>
lea    0xe5b(%rip),%rdi
call   1040 <puts@plt>
mov    0x8(%rsp),%ebp
mov    %ebp,%edi
call   10a0 <pst@plt>
mov    %ebp,%edi
call   1030 <S_ISLNK@plt>
test   %eax,%eax
je     122a <main+0xaa>
mov    0x8(%rbx),%rdi
lea    0x10(%rsp),%rsi
mov    $0x400,%edx
call   1050 <readlink@plt>
test   %eax,%eax
js     123f <main+0xbf>
mov    %eax,%eax
movb   $0x0,0x10(%rsp,%rax,1)
lea    0xe30(%rip),%rdi
lea    0x10(%rsp),%rsi
call   1060 <printf@plt>
mov    0x8(%rbx),%rdi
lea    0x8(%rsp),%rsi
call   1080 <stat@plt>
test   %rax,%rax
js     1248 <main+0xc8>
lea    0xe1c(%rip),%rdi
call   1040 <puts@plt>
mov    0x8(%rsp),%edi
call   10a0 <pst@plt>
xor    %eax,%eax
add    $0x418,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xdc3(%rip),%rdi
jmp    124f <main+0xcf>
lea    0xdd9(%rip),%rdi
jmp    124f <main+0xcf>
lea    0xdb2(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%eax
jmp    122c <main+0xac>

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [19 x i8] c"lstat(argv[1],&st)\00"
@.str.1 = [12 x i8] c"=== lstat()\00"
@.str.2 = [9 x i8] c"readlink\00"
@.str.3 = [16 x i8] c"symlink to %s\0A\0A\00"
@.str.4 = [18 x i8] c"stat(argv[1],&st)\00"
@.str.5 = [11 x i8] c"=== stat()\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 i32
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 %12
11: ; preds = %2
store i32 1, i32* %3
br label %58
12: ; preds = %2
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 1
%15 = load i8*, i8** %14
%16 = call i64 @lstat(i8* %15, %struct.stat* %6)
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %12
%19 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %58
20: ; preds = %12
%21 = call i32 @puts(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%22 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%23 = load i32, i32* %22
%24 = call i32 @pst(i32 %23)
%25 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%26 = load i32, i32* %25
%27 = call i32 @S_ISLNK(i32 %26)
%28 = icmp ne i32 %27, 0
br i1 %28, label %30, label %29
29: ; preds = %20
store i32 0, i32* %3
br label %58
30: ; preds = %20
%31 = load i8**, i8*** %5
%32 = getelementptr inbounds i8*, i8** %31, i64 1
%33 = load i8*, i8** %32
%34 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%35 = call i32 @readlink(i8* %33, i8* %34, i32 1024)
store i32 %35, i32* %8
%36 = load i32, i32* %8
%37 = icmp slt i32 %36, 0
br i1 %37, label %38, label %40
38: ; preds = %30
%39 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %3
br label %58
40: ; preds = %30
%41 = load i32, i32* %8
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 %42
store i8 0, i8* %43
%44 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%45 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i8* %44)
%46 = load i8**, i8*** %5
%47 = getelementptr inbounds i8*, i8** %46, i64 1
%48 = load i8*, i8** %47
%49 = call i64 @stat(i8* %48, %struct.stat* %6)
%50 = icmp slt i64 %49, 0
br i1 %50, label %51, label %53
51: ; preds = %40
%52 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0))
store i32 1, i32* %3
br label %58
53: ; preds = %40
%54 = call i32 @puts(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.5, i64 0, i64 0))
%55 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%56 = load i32, i32* %55
%57 = call i32 @pst(i32 %56)
br label %58
58: ; preds = %53, %51, %38, %29, %18, %11
%59 = load i32, i32* %3
ret i32 %59
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [19 x i8] c"lstat(argv[1],&st)\00"
@.str.1 = [12 x i8] c"=== lstat()\00"
@.str.2 = [9 x i8] c"readlink\00"
@.str.3 = [16 x i8] c"symlink to %s\0A\0A\00"
@.str.4 = [18 x i8] c"stat(argv[1],&st)\00"
@.str.5 = [11 x i8] c"=== stat()\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.stat
%4 = alloca [1024 x i8]
%5 = icmp eq i32 %0, 2
br i1 %5, label %6, label %45
6: ; preds = %2
%7 = bitcast %struct.stat* %3 to i8*
%8 = getelementptr inbounds i8*, i8** %1, i64 1
%9 = load i8*, i8** %8
%10 = call i64 @lstat(i8* %9, %struct.stat* %3)
%11 = icmp slt i64 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %6
%13 = tail call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
br label %43
14: ; preds = %6
%15 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
%16 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%17 = load i32, i32* %16
%18 = tail call i32 @pst(i32 %17)
%19 = tail call i32 @S_ISLNK(i32 %17)
%20 = icmp eq i32 %19, 0
br i1 %20, label %43, label %21
21: ; preds = %14
%22 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%23 = load i8*, i8** %8
%24 = call i32 @readlink(i8* %23, i8* %22, i32 1024)
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %21
%27 = tail call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
br label %41
28: ; preds = %21
%29 = zext i32 %24 to i64
%30 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 %29
store i8 0, i8* %30
%31 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i8* %22)
%32 = load i8*, i8** %8
%33 = call i64 @stat(i8* %32, %struct.stat* %3)
%34 = icmp slt i64 %33, 0
br i1 %34, label %35, label %37
35: ; preds = %28
%36 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0))
br label %41
37: ; preds = %28
%38 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.5, i64 0, i64 0))
%39 = load i32, i32* %16
%40 = call i32 @pst(i32 %39)
br label %41
41: ; preds = %37, %35, %26
%42 = phi i32 [ 1, %26 ], [ 1, %35 ], [ 0, %37 ]
br label %43
43: ; preds = %14, %41, %12
%44 = phi i32 [ 1, %12 ], [ %42, %41 ], [ 0, %14 ]
br label %45
45: ; preds = %2, %43
%46 = phi i32 [ %44, %43 ], [ 1, %2 ]
ret i32 %46
}