#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;
  int st_size;
};
typedef int /*<<< orphan*/ buf2;

/* Variables and functions */
int ENOENT;
int S_ISFIFO(int);
int S_ISLNK(int);
int S_ISREG(int);
int errno;
int lstat(char *, struct stat *);
int /*<<< orphan*/ printf(char *, ...);
int readlink(char *, char *, int);
int stat(char *, struct stat *);
char *strerror(int);
int main(int argc, char **argv) {
  struct stat buf;
  struct stat buf1;
  struct stat buf3;
  char buf2[20];
  int ret = lstat(argv[1], &buf);
  printf(
      "lstat ret%d, mod %x, isreg %d, islink %d, isfifo %d, err %d size %d\n",
      ret, buf.st_mode, S_ISREG(buf.st_mode), S_ISLNK(buf.st_mode),
      S_ISFIFO(buf.st_mode), errno, buf.st_size);
  ret = stat(argv[1], &buf1);
  if (errno == ENOENT)
    printf("noent\n");
  printf("stat ret%d, mod %x, isreg %d, islink %d, err&noent %d, %s\n", ret,
         buf1.st_mode, S_ISREG(buf1.st_mode), S_ISLNK(buf1.st_mode),
         errno & -ENOENT, strerror(errno));
  ret = readlink(argv[1], buf2, sizeof(buf2));
  if (ret > 0)
    buf2[ret] = '\0';
  printf("iii %s\n", buf2);
  ret = stat(buf2, &buf3);
  if (errno == ENOENT)
    printf("noent\n");
  printf("stat ret%d, isreg %d, islink %d, err&noent %d, %s\n", ret,
         S_ISREG(buf3.st_mode), S_ISLNK(buf3.st_mode), errno & -ENOENT,
         strerror(errno));
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    -0x18(%rbp),%rsi
call   1070 <lstat@plt>
mov    %eax,-0x44(%rbp)
mov    -0x44(%rbp),%eax
mov    %eax,-0x54(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x50(%rbp)
mov    -0x18(%rbp),%edi
call   1090 <S_ISREG@plt>
mov    %eax,-0x4c(%rbp)
mov    -0x18(%rbp),%edi
call   1030 <S_ISLNK@plt>
mov    %eax,-0x48(%rbp)
mov    -0x18(%rbp),%edi
call   1050 <S_ISFIFO@plt>
mov    -0x54(%rbp),%esi
mov    -0x50(%rbp),%edx
mov    -0x4c(%rbp),%ecx
mov    -0x48(%rbp),%r8d
mov    %eax,%r9d
mov    0x2df2(%rip),%rax
mov    (%rax),%r10d
mov    -0x14(%rbp),%eax
lea    0xe05(%rip),%rdi
mov    %r10d,(%rsp)
mov    %eax,0x8(%rsp)
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    -0x20(%rbp),%rsi
call   1080 <stat@plt>
mov    %eax,-0x44(%rbp)
mov    0x2dbb(%rip),%rax
mov    (%rax),%eax
mov    0x2da2(%rip),%rcx
cmp    (%rcx),%eax
jne    1244 <main+0xc4>
lea    0xe08(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x44(%rbp),%eax
mov    %eax,-0x68(%rbp)
mov    -0x20(%rbp),%eax
mov    %eax,-0x64(%rbp)
mov    -0x20(%rbp),%edi
call   1090 <S_ISREG@plt>
mov    %eax,-0x60(%rbp)
mov    -0x20(%rbp),%edi
call   1030 <S_ISLNK@plt>
mov    %eax,-0x5c(%rbp)
mov    0x2d73(%rip),%rax
mov    (%rax),%eax
xor    %ecx,%ecx
mov    0x2d58(%rip),%rdx
sub    (%rdx),%ecx
and    %ecx,%eax
mov    %eax,-0x58(%rbp)
mov    0x2d5a(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x68(%rbp),%esi
mov    -0x64(%rbp),%edx
mov    -0x60(%rbp),%ecx
mov    -0x5c(%rbp),%r8d
mov    -0x58(%rbp),%r9d
lea    0xda7(%rip),%rdi
mov    %rax,(%rsp)
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    -0x40(%rbp),%rsi
mov    $0x14,%edx
call   1040 <readlink@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0x0,-0x44(%rbp)
jle    12dc <main+0x15c>
movslq -0x44(%rbp),%rax
movb   $0x0,-0x40(%rbp,%rax,1)
lea    -0x40(%rbp),%rsi
lea    0xda0(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    -0x40(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1080 <stat@plt>
mov    %eax,-0x44(%rbp)
mov    0x2cdb(%rip),%rax
mov    (%rax),%eax
mov    0x2cc2(%rip),%rcx
cmp    (%rcx),%eax
jne    1324 <main+0x1a4>
lea    0xd28(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x44(%rbp),%eax
mov    %eax,-0x78(%rbp)
mov    -0x28(%rbp),%edi
call   1090 <S_ISREG@plt>
mov    %eax,-0x74(%rbp)
mov    -0x28(%rbp),%edi
call   1030 <S_ISLNK@plt>
mov    %eax,-0x70(%rbp)
mov    0x2c99(%rip),%rax
mov    (%rax),%eax
xor    %ecx,%ecx
mov    0x2c7e(%rip),%rdx
sub    (%rdx),%ecx
and    %ecx,%eax
mov    %eax,-0x6c(%rbp)
mov    0x2c80(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x78(%rbp),%esi
mov    -0x74(%rbp),%edx
mov    -0x70(%rbp),%ecx
mov    -0x6c(%rbp),%r8d
mov    %rax,%r9
lea    0xd11(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
xor    %eax,%eax
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %rsi,%r14
mov    0x8(%rsi),%rdi
mov    %rsp,%rsi
call   1080 <lstat@plt>
mov    %eax,%r15d
mov    (%rsp),%ebx
mov    %ebx,%edi
call   10a0 <S_ISREG@plt>
mov    %eax,%r12d
mov    %ebx,%edi
call   1030 <S_ISLNK@plt>
mov    %eax,%r13d
mov    %ebx,%edi
call   1060 <S_ISFIFO@plt>
mov    0x2e0b(%rip),%rcx
mov    (%rcx),%r10d
mov    0x4(%rsp),%ebp
lea    0xe1d(%rip),%rdi
mov    %r15d,%esi
mov    %ebx,%edx
mov    %r12d,%ecx
mov    %r13d,%r8d
mov    %eax,%r9d
xor    %eax,%eax
push   %rbp
push   %r10
call   1070 <printf@plt>
add    $0x10,%rsp
mov    %r14,0x8(%rsp)
mov    0x8(%r14),%rdi
lea    0x18(%rsp),%rsi
call   1090 <stat@plt>
mov    %eax,%r15d
mov    0x2dc4(%rip),%rax
mov    (%rax),%eax
mov    0x2dab(%rip),%rcx
cmp    (%rcx),%eax
jne    1235 <main+0xa5>
lea    0xe8b(%rip),%rdi
call   1040 <puts@plt>
mov    0x18(%rsp),%ebx
mov    %ebx,%edi
call   10a0 <S_ISREG@plt>
mov    %eax,%r12d
mov    %ebx,%edi
call   1030 <S_ISLNK@plt>
mov    %eax,%r13d
mov    0x2d8c(%rip),%rax
mov    (%rax),%edi
xor    %ebp,%ebp
mov    0x2d71(%rip),%rax
sub    (%rax),%ebp
and    %edi,%ebp
call   10b0 <strerror@plt>
mov    %rax,%r10
sub    $0x8,%rsp
lea    0xdcf(%rip),%rdi
mov    %r15d,%esi
mov    %ebx,%edx
mov    %r12d,%ecx
mov    %r13d,%r8d
mov    %ebp,%r9d
xor    %eax,%eax
push   %r10
call   1070 <printf@plt>
add    $0x10,%rsp
mov    0x8(%rsp),%rax
mov    0x8(%rax),%rdi
lea    0x20(%rsp),%rsi
mov    $0x14,%edx
call   1050 <readlink@plt>
test   %eax,%eax
jle    12b4 <main+0x124>
mov    %eax,%eax
movb   $0x0,0x20(%rsp,%rax,1)
lea    0xdc5(%rip),%rdi
lea    0x20(%rsp),%rbx
mov    %rbx,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
lea    0x10(%rsp),%rsi
mov    %rbx,%rdi
call   1090 <stat@plt>
mov    %eax,%r14d
mov    0x2cff(%rip),%rax
mov    (%rax),%eax
mov    0x2ce6(%rip),%rcx
cmp    (%rcx),%eax
jne    12fa <main+0x16a>
lea    0xdc6(%rip),%rdi
call   1040 <puts@plt>
mov    0x10(%rsp),%ebp
mov    %ebp,%edi
call   10a0 <S_ISREG@plt>
mov    %eax,%r15d
mov    %ebp,%edi
call   1030 <S_ISLNK@plt>
mov    %eax,%ebp
mov    0x2cc8(%rip),%rax
mov    (%rax),%edi
xor    %ebx,%ebx
mov    0x2cad(%rip),%rax
sub    (%rax),%ebx
and    %edi,%ebx
call   10b0 <strerror@plt>
lea    0xd55(%rip),%rdi
mov    %r14d,%esi
mov    %r15d,%edx
mov    %ebp,%ecx
mov    %ebx,%r8d
mov    %rax,%r9
xor    %eax,%eax
call   1070 <printf@plt>
xor    %eax,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32, i32 }
@.str = [69 x i8] c"lstat ret%d, mod %x, isreg %d, islink %d, isfifo %d, err %d size %d\0A\00"
@errno = global i32 0
@ENOENT = global i32 0
@.str.1 = [7 x i8] c"noent\0A\00"
@.str.2 = [59 x i8] c"stat ret%d, mod %x, isreg %d, islink %d, err&noent %d, %s\0A\00"
@.str.3 = [8 x i8] c"iii %s\0A\00"
@.str.4 = [51 x i8] c"stat ret%d, isreg %d, islink %d, err&noent %d, %s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.stat
%7 = alloca %struct.stat
%8 = alloca %struct.stat
%9 = alloca [20 x i8]
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i8**, i8*** %5
%12 = getelementptr inbounds i8*, i8** %11, i64 1
%13 = load i8*, i8** %12
%14 = call i32 @lstat(i8* %13, %struct.stat* %6)
store i32 %14, i32* %10
%15 = load i32, i32* %10
%16 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%17 = load i32, i32* %16
%18 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%19 = load i32, i32* %18
%20 = call i32 @S_ISREG(i32 %19)
%21 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%22 = load i32, i32* %21
%23 = call i32 @S_ISLNK(i32 %22)
%24 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%25 = load i32, i32* %24
%26 = call i32 @S_ISFIFO(i32 %25)
%27 = load i32, i32* @errno
%28 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 1
%29 = load i32, i32* %28
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([69 x i8], [69 x i8]* @.str, i64 0, i64 0), i32 %15, i32 %17, i32 %20, i32 %23, i32 %26, i32 %27, i32 %29)
%31 = load i8**, i8*** %5
%32 = getelementptr inbounds i8*, i8** %31, i64 1
%33 = load i8*, i8** %32
%34 = call i32 @stat(i8* %33, %struct.stat* %7)
store i32 %34, i32* %10
%35 = load i32, i32* @errno
%36 = load i32, i32* @ENOENT
%37 = icmp eq i32 %35, %36
br i1 %37, label %38, label %40
38: ; preds = %2
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %40
40: ; preds = %38, %2
%41 = load i32, i32* %10
%42 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%43 = load i32, i32* %42
%44 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%45 = load i32, i32* %44
%46 = call i32 @S_ISREG(i32 %45)
%47 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%48 = load i32, i32* %47
%49 = call i32 @S_ISLNK(i32 %48)
%50 = load i32, i32* @errno
%51 = load i32, i32* @ENOENT
%52 = sub nsw i32 0, %51
%53 = and i32 %50, %52
%54 = load i32, i32* @errno
%55 = call i8* @strerror(i32 %54)
%56 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str.2, i64 0, i64 0), i32 %41, i32 %43, i32 %46, i32 %49, i32 %53, i8* %55)
%57 = load i8**, i8*** %5
%58 = getelementptr inbounds i8*, i8** %57, i64 1
%59 = load i8*, i8** %58
%60 = getelementptr inbounds [20 x i8], [20 x i8]* %9, i64 0, i64 0
%61 = call i32 @readlink(i8* %59, i8* %60, i32 20)
store i32 %61, i32* %10
%62 = load i32, i32* %10
%63 = icmp sgt i32 %62, 0
br i1 %63, label %64, label %68
64: ; preds = %40
%65 = load i32, i32* %10
%66 = sext i32 %65 to i64
%67 = getelementptr inbounds [20 x i8], [20 x i8]* %9, i64 0, i64 %66
store i8 0, i8* %67
br label %68
68: ; preds = %64, %40
%69 = getelementptr inbounds [20 x i8], [20 x i8]* %9, i64 0, i64 0
%70 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i8* %69)
%71 = getelementptr inbounds [20 x i8], [20 x i8]* %9, i64 0, i64 0
%72 = call i32 @stat(i8* %71, %struct.stat* %8)
store i32 %72, i32* %10
%73 = load i32, i32* @errno
%74 = load i32, i32* @ENOENT
%75 = icmp eq i32 %73, %74
br i1 %75, label %76, label %78
76: ; preds = %68
%77 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %78
78: ; preds = %76, %68
%79 = load i32, i32* %10
%80 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%81 = load i32, i32* %80
%82 = call i32 @S_ISREG(i32 %81)
%83 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%84 = load i32, i32* %83
%85 = call i32 @S_ISLNK(i32 %84)
%86 = load i32, i32* @errno
%87 = load i32, i32* @ENOENT
%88 = sub nsw i32 0, %87
%89 = and i32 %86, %88
%90 = load i32, i32* @errno
%91 = call i8* @strerror(i32 %90)
%92 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.4, i64 0, i64 0), i32 %79, i32 %82, i32 %85, i32 %89, i8* %91)
ret i32 0
}

LLVM IR (-O2):
%struct.stat = type { i32, i32 }
@.str = [69 x i8] c"lstat ret%d, mod %x, isreg %d, islink %d, isfifo %d, err %d size %d\0A\00"
@errno = global i32 0
@ENOENT = global i32 0
@.str.2 = [59 x i8] c"stat ret%d, mod %x, isreg %d, islink %d, err&noent %d, %s\0A\00"
@.str.3 = [8 x i8] c"iii %s\0A\00"
@.str.4 = [51 x i8] c"stat ret%d, isreg %d, islink %d, err&noent %d, %s\0A\00"
@str.5 = [6 x i8] c"noent\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.stat
%4 = alloca %struct.stat
%5 = alloca %struct.stat
%6 = alloca [20 x i8]
%7 = bitcast %struct.stat* %3 to i8*
%8 = bitcast %struct.stat* %4 to i8*
%9 = bitcast %struct.stat* %5 to i8*
%10 = getelementptr inbounds [20 x i8], [20 x i8]* %6, i64 0, i64 0
%11 = getelementptr inbounds i8*, i8** %1, i64 1
%12 = load i8*, i8** %11
%13 = call i32 @lstat(i8* %12, %struct.stat* %3)
%14 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%15 = load i32, i32* %14
%16 = tail call i32 @S_ISREG(i32 %15)
%17 = tail call i32 @S_ISLNK(i32 %15)
%18 = tail call i32 @S_ISFIFO(i32 %15)
%19 = load i32, i32* @errno
%20 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 1
%21 = load i32, i32* %20
%22 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([69 x i8], [69 x i8]* @.str, i64 0, i64 0), i32 %13, i32 %15, i32 %16, i32 %17, i32 %18, i32 %19, i32 %21)
%23 = load i8*, i8** %11
%24 = call i32 @stat(i8* %23, %struct.stat* %4)
%25 = load i32, i32* @errno
%26 = load i32, i32* @ENOENT
%27 = icmp eq i32 %25, %26
br i1 %27, label %28, label %30
28: ; preds = %2
%29 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @str.5, i64 0, i64 0))
br label %30
30: ; preds = %28, %2
%31 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%32 = load i32, i32* %31
%33 = tail call i32 @S_ISREG(i32 %32)
%34 = tail call i32 @S_ISLNK(i32 %32)
%35 = load i32, i32* @errno
%36 = load i32, i32* @ENOENT
%37 = sub nsw i32 0, %36
%38 = and i32 %35, %37
%39 = tail call i8* @strerror(i32 %35)
%40 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([59 x i8], [59 x i8]* @.str.2, i64 0, i64 0), i32 %24, i32 %32, i32 %33, i32 %34, i32 %38, i8* %39)
%41 = load i8*, i8** %11
%42 = call i32 @readlink(i8* %41, i8* %10, i32 20)
%43 = icmp sgt i32 %42, 0
br i1 %43, label %44, label %47
44: ; preds = %30
%45 = zext i32 %42 to i64
%46 = getelementptr inbounds [20 x i8], [20 x i8]* %6, i64 0, i64 %45
store i8 0, i8* %46
br label %47
47: ; preds = %44, %30
%48 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i8* %10)
%49 = call i32 @stat(i8* %10, %struct.stat* %5)
%50 = load i32, i32* @errno
%51 = load i32, i32* @ENOENT
%52 = icmp eq i32 %50, %51
br i1 %52, label %53, label %55
53: ; preds = %47
%54 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @str.5, i64 0, i64 0))
br label %55
55: ; preds = %53, %47
%56 = getelementptr inbounds %struct.stat, %struct.stat* %5, i64 0, i32 0
%57 = load i32, i32* %56
%58 = call i32 @S_ISREG(i32 %57)
%59 = call i32 @S_ISLNK(i32 %57)
%60 = load i32, i32* @errno
%61 = load i32, i32* @ENOENT
%62 = sub nsw i32 0, %61
%63 = and i32 %60, %62
%64 = call i8* @strerror(i32 %60)
%65 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([51 x i8], [51 x i8]* @.str.4, i64 0, i64 0), i32 %49, i32 %58, i32 %59, i32 %63, i8* %64)
ret i32 0
}