#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;
};

/* Variables and functions */
int S_IRUSR;
scalar_t__ S_ISDIR(int);
scalar_t__ S_ISREG(int);
int atoi(char *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int fstat(int, struct stat *);
scalar_t__ isalpha(char);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *, char *);
int /*<<< orphan*/ stderr;
int main(int argc, char **argv) {
  if (argc != 2) {
    fprintf(stderr, "usage: %s fd\n", argv[0]);
    return -1;
  }
  char *c = argv[1];
  for (;;) {
    if (isalpha(*c)) {
      fprintf(stderr, "%s: not a number\n", argv[1]);
      return -1;
    }
    if (*c == '\0')
      break;
    c += 1;
  }
  int fd = atoi(argv[1]);
  struct stat stat;
  int ret = fstat(fd, &stat);
  if (ret) {
    perror("fstat");
    return -1;
  }
  char *type;
  if (S_ISREG(stat.st_mode))
    type = "regular";
  else if (S_ISDIR(stat.st_mode))
    type = "directory";
  else
    type = "other";
  char *readok;
  if ((stat.st_mode & S_IRUSR))
    readok = "yes";
  else
    readok = "no";
  printf("type: %s, read: %s\n", type, readok);
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11d8 <main+0x48>
mov    0x2e29(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe39(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1329 <main+0x199>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
movsbl (%rax),%edi
call   1070 <isalpha@plt>
cmp    $0x0,%rax
je     1223 <main+0x93>
mov    0x2ddf(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xdfc(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1329 <main+0x199>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    1238 <main+0xa8>
jmp    1249 <main+0xb9>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    11e4 <main+0x54>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1090 <atoi@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   10b0 <fstat@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     128a <main+0xfa>
lea    0xda7(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1329 <main+0x199>
mov    -0x20(%rbp),%edi
call   1060 <S_ISREG@plt>
cmp    $0x0,%rax
je     12ac <main+0x11c>
lea    0xd83(%rip),%rax
mov    %rax,-0x30(%rbp)
jmp    12de <main+0x14e>
mov    -0x20(%rbp),%edi
call   1040 <S_ISDIR@plt>
cmp    $0x0,%rax
je     12ce <main+0x13e>
lea    0xd69(%rip),%rax
mov    %rax,-0x30(%rbp)
jmp    12d9 <main+0x149>
lea    0xd63(%rip),%rax
mov    %rax,-0x30(%rbp)
jmp    12de <main+0x14e>
mov    -0x20(%rbp),%eax
mov    0x2cf0(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     1303 <main+0x173>
lea    0xd44(%rip),%rax
mov    %rax,-0x38(%rbp)
jmp    130e <main+0x17e>
lea    0xd38(%rip),%rax
mov    %rax,-0x38(%rbp)
mov    -0x30(%rbp),%rsi
mov    -0x38(%rbp),%rdx
lea    0xd28(%rip),%rdi
call   1030 <printf@plt>
xor    %edi,%edi
call   10a0 <exit@plt>
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
cmp    $0x2,%edi
jne    11ed <main+0x5d>
mov    0x8(%r14),%rbx
movsbl (%rbx),%edi
call   1070 <isalpha@plt>
test   %rax,%rax
jne    11d7 <main+0x47>
add    $0x1,%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
cmpb   $0x0,-0x1(%rbx)
je     1212 <main+0x82>
movsbl (%rbx),%edi
call   1070 <isalpha@plt>
add    $0x1,%rbx
test   %rax,%rax
je     11c0 <main+0x30>
mov    0x2e02(%rip),%rax
mov    (%rax),%edi
mov    0x8(%r14),%rdx
lea    0xe23(%rip),%rsi
jmp    1200 <main+0x70>
mov    0x2dec(%rip),%rax
mov    (%rax),%edi
mov    (%r14),%rdx
lea    0xe00(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
mov    0x8(%r14),%rdi
call   1090 <atoi@plt>
mov    %rsp,%rsi
mov    %eax,%edi
call   10b0 <fstat@plt>
test   %eax,%eax
je     1237 <main+0xa7>
lea    0xdf0(%rip),%rdi
call   1080 <perror@plt>
jmp    1205 <main+0x75>
mov    (%rsp),%ebx
mov    %ebx,%edi
call   1060 <S_ISREG@plt>
test   %rax,%rax
jne    1264 <main+0xd4>
mov    %ebx,%edi
call   1040 <S_ISDIR@plt>
test   %rax,%rax
lea    0xde1(%rip),%rax
lea    0xdd0(%rip),%rsi
cmove  %rax,%rsi
jmp    126b <main+0xdb>
lea    0xdbb(%rip),%rsi
mov    0x2d66(%rip),%rax
and    (%rax),%ebx
lea    0xdc7(%rip),%rax
lea    0xdbc(%rip),%rdx
cmove  %rax,%rdx
lea    0xdb8(%rip),%rdi
call   1030 <printf@plt>
xor    %edi,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
%struct.stat = type { i32 }
@stderr = global i32 0
@.str = [14 x i8] c"usage: %s fd\0A\00"
@.str.1 = [18 x i8] c"%s: not a number\0A\00"
@.str.2 = [6 x i8] c"fstat\00"
@.str.3 = [8 x i8] c"regular\00"
@.str.4 = [10 x i8] c"directory\00"
@.str.5 = [6 x i8] c"other\00"
@S_IRUSR = global i32 0
@.str.6 = [4 x i8] c"yes\00"
@.str.7 = [3 x i8] c"no\00"
@.str.8 = [20 x i8] c"type: %s, read: %s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i32
%8 = alloca %struct.stat
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = load i32, i32* %4
%13 = icmp ne i32 %12, 2
br i1 %13, label %14, label %20
14: ; preds = %2
%15 = load i32, i32* @stderr
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 0
%18 = load i8*, i8** %17
%19 = call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* %18)
store i32 -1, i32* %3
br label %82
20: ; preds = %2
%21 = load i8**, i8*** %5
%22 = getelementptr inbounds i8*, i8** %21, i64 1
%23 = load i8*, i8** %22
store i8* %23, i8** %6
br label %24
24: ; preds = %41, %20
%25 = load i8*, i8** %6
%26 = load i8, i8* %25
%27 = call i64 @isalpha(i8 signext %26)
%28 = icmp ne i64 %27, 0
br i1 %28, label %29, label %35
29: ; preds = %24
%30 = load i32, i32* @stderr
%31 = load i8**, i8*** %5
%32 = getelementptr inbounds i8*, i8** %31, i64 1
%33 = load i8*, i8** %32
%34 = call i32 @fprintf(i32 %30, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* %33)
store i32 -1, i32* %3
br label %82
35: ; preds = %24
%36 = load i8*, i8** %6
%37 = load i8, i8* %36
%38 = sext i8 %37 to i32
%39 = icmp eq i32 %38, 0
br i1 %39, label %40, label %41
40: ; preds = %35
br label %44
41: ; preds = %35
%42 = load i8*, i8** %6
%43 = getelementptr inbounds i8, i8* %42, i64 1
store i8* %43, i8** %6
br label %24
44: ; preds = %40
%45 = load i8**, i8*** %5
%46 = getelementptr inbounds i8*, i8** %45, i64 1
%47 = load i8*, i8** %46
%48 = call i32 @atoi(i8* %47)
store i32 %48, i32* %7
%49 = load i32, i32* %7
%50 = call i32 @fstat(i32 %49, %struct.stat* %8)
store i32 %50, i32* %9
%51 = load i32, i32* %9
%52 = icmp ne i32 %51, 0
br i1 %52, label %53, label %55
53: ; preds = %44
%54 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %3
br label %82
55: ; preds = %44
%56 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%57 = load i32, i32* %56
%58 = call i64 @S_ISREG(i32 %57)
%59 = icmp ne i64 %58, 0
br i1 %59, label %60, label %61
60: ; preds = %55
store i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i8** %10
br label %69
61: ; preds = %55
%62 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%63 = load i32, i32* %62
%64 = call i64 @S_ISDIR(i32 %63)
%65 = icmp ne i64 %64, 0
br i1 %65, label %66, label %67
66: ; preds = %61
store i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i8** %10
br label %68
67: ; preds = %61
store i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0), i8** %10
br label %68
68: ; preds = %67, %66
br label %69
69: ; preds = %68, %60
%70 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%71 = load i32, i32* %70
%72 = load i32, i32* @S_IRUSR
%73 = and i32 %71, %72
%74 = icmp ne i32 %73, 0
br i1 %74, label %75, label %76
75: ; preds = %69
store i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0), i8** %11
br label %77
76: ; preds = %69
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.7, i64 0, i64 0), i8** %11
br label %77
77: ; preds = %76, %75
%78 = load i8*, i8** %10
%79 = load i8*, i8** %11
%80 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.8, i64 0, i64 0), i8* %78, i8* %79)
%81 = call i32 @exit(i32 0)
unreachable
82: ; preds = %53, %29, %14
%83 = load i32, i32* %3
ret i32 %83
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@stderr = global i32 0
@.str = [14 x i8] c"usage: %s fd\0A\00"
@.str.1 = [18 x i8] c"%s: not a number\0A\00"
@.str.2 = [6 x i8] c"fstat\00"
@.str.3 = [8 x i8] c"regular\00"
@.str.4 = [10 x i8] c"directory\00"
@.str.5 = [6 x i8] c"other\00"
@S_IRUSR = global i32 0
@.str.6 = [4 x i8] c"yes\00"
@.str.7 = [3 x i8] c"no\00"
@.str.8 = [20 x i8] c"type: %s, read: %s\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.stat
%4 = icmp eq i32 %0, 2
br i1 %4, label %9, label %5
5: ; preds = %2
%6 = load i32, i32* @stderr
%7 = load i8*, i8** %1
%8 = tail call i32 @fprintf(i32 %6, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* %7)
br label %53
9: ; preds = %2
%10 = getelementptr inbounds i8*, i8** %1, i64 1
%11 = load i8*, i8** %10
%12 = load i8, i8* %11
%13 = tail call i64 @isalpha(i8 signext %12)
%14 = icmp eq i64 %13, 0
br i1 %14, label %23, label %19
15: ; preds = %23
%16 = load i8, i8* %27
%17 = tail call i64 @isalpha(i8 signext %16)
%18 = icmp eq i64 %17, 0
br i1 %18, label %23, label %19
19: ; preds = %15, %9
%20 = load i32, i32* @stderr
%21 = load i8*, i8** %10
%22 = tail call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* %21)
br label %53
23: ; preds = %9, %15
%24 = phi i8* [ %27, %15 ], [ %11, %9 ]
%25 = load i8, i8* %24
%26 = icmp eq i8 %25, 0
%27 = getelementptr inbounds i8, i8* %24, i64 1
br i1 %26, label %28, label %15
28: ; preds = %23
%29 = load i8*, i8** %10
%30 = tail call i32 @atoi(i8* %29)
%31 = bitcast %struct.stat* %3 to i8*
%32 = call i32 @fstat(i32 %30, %struct.stat* %3)
%33 = icmp eq i32 %32, 0
br i1 %33, label %36, label %34
34: ; preds = %28
%35 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
br label %53
36: ; preds = %28
%37 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%38 = load i32, i32* %37
%39 = tail call i64 @S_ISREG(i32 %38)
%40 = icmp eq i64 %39, 0
br i1 %40, label %41, label %45
41: ; preds = %36
%42 = tail call i64 @S_ISDIR(i32 %38)
%43 = icmp eq i64 %42, 0
%44 = select i1 %43, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0)
br label %45
45: ; preds = %41, %36
%46 = phi i8* [ getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), %36 ], [ %44, %41 ]
%47 = load i32, i32* @S_IRUSR
%48 = and i32 %47, %38
%49 = icmp eq i32 %48, 0
%50 = select i1 %49, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.7, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0)
%51 = tail call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.8, i64 0, i64 0), i8* %46, i8* %50)
%52 = tail call i32 @exit(i32 0)
unreachable
53: ; preds = %19, %34, %5
ret i32 -1
}