#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 void FILE;

/* Variables and functions */
int fgetc(void *);
void *fopen(char *, char *);
int fread(unsigned char *, int, int, void *);
int /*<<< orphan*/ printf(char *, ...);
int main(int argc, char **argv) {
  FILE *fp;
  unsigned char lenb[4];
  int len;
  int c;
  int x;
  if (argc <= 1) {
    printf("usage: folks <fnm>\n");
    return (1);
  }
  fp = fopen(*(argv + 1), "rb");
  if (fp == ((void *)0)) {
    printf("failed to open %s\n", *(argv + 1));
    return (1);
  }
  while (fread(lenb, sizeof lenb, 1, fp) == 1) {
    len = lenb[0] << 8 | lenb[1];
    printf("len is %d\n", len);
    if (lenb[2] != 0 || lenb[3] != 0) {
      printf("file is corrupt - reserved\n");
      return (1);
    }
    if (len == 0) {
      break;
    }
    if (len < 4) {
      printf("file is corrupt - length\n");
      return (1);
    }
    c = fgetc(fp);
    printf("first data byte is %x\n", c);
    for (x = 5; x < len; x++) {
      fgetc(fp);
    }
  }
  return (0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x1,-0x8(%rbp)
jg     117a <main+0x3a>
lea    0xe99(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12bf <main+0x17f>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe8b(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11bf <main+0x7f>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xe6b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12bf <main+0x17f>
jmp    11c4 <main+0x84>
lea    -0x1c(%rbp),%rdi
mov    -0x18(%rbp),%rcx
mov    $0x4,%esi
mov    $0x1,%edx
call   1030 <fread@plt>
cmp    $0x1,%eax
jne    12b8 <main+0x178>
movzbl -0x1c(%rbp),%eax
shl    $0x8,%eax
movzbl -0x1b(%rbp),%ecx
or     %ecx,%eax
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%esi
lea    0xe2c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movzbl -0x1a(%rbp),%eax
cmp    $0x0,%eax
jne    121f <main+0xdf>
movzbl -0x19(%rbp),%eax
cmp    $0x0,%eax
je     1239 <main+0xf9>
lea    0xe0f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12bf <main+0x17f>
cmpl   $0x0,-0x20(%rbp)
jne    1248 <main+0x108>
jmp    12b8 <main+0x178>
cmpl   $0x4,-0x20(%rbp)
jge    126c <main+0x12c>
lea    0xdf8(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12bf <main+0x17f>
mov    -0x18(%rbp),%rdi
call   1050 <fgetc@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%esi
lea    0xde9(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x5,-0x28(%rbp)
mov    -0x28(%rbp),%eax
cmp    -0x20(%rbp),%eax
jge    12b3 <main+0x173>
mov    -0x18(%rbp),%rdi
call   1050 <fgetc@plt>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    1290 <main+0x150>
jmp    11c4 <main+0x84>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
cmp    $0x1,%edi
jg     1171 <main+0x21>
lea    0xf05(%rip),%rdi
call   1030 <puts@plt>
jmp    1251 <main+0x101>
mov    %rsi,%rbp
mov    0x8(%rsi),%rdi
lea    0xe81(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     123f <main+0xef>
mov    %rax,%rbx
lea    0x4(%rsp),%rdi
mov    $0x4,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   1040 <fread@plt>
xor    %r13d,%r13d
cmp    $0x1,%eax
jne    1257 <main+0x107>
lea    0xe5c(%rip),%r14
lea    0xe60(%rip),%r15
lea    0x4(%rsp),%r12
xor    %r13d,%r13d
jmp    11ea <main+0x9a>
nopl   0x0(%rax,%rax,1)
mov    %r12,%rdi
mov    $0x4,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1040 <fread@plt>
cmp    $0x1,%eax
jne    1257 <main+0x107>
movzwl 0x4(%rsp),%ebp
shl    $0x10,%ebp
bswap  %ebp
mov    %r14,%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x6(%rsp),%al
or     0x7(%rsp),%al
jne    1269 <main+0x119>
test   %ebp,%ebp
je     1257 <main+0x107>
cmp    $0x3,%ebp
jbe    1277 <main+0x127>
mov    %rbx,%rdi
call   1060 <fgetc@plt>
mov    %r15,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
cmp    $0x6,%ebp
jb     11d0 <main+0x80>
add    $0xfffffffb,%ebp
nop
mov    %rbx,%rdi
call   1060 <fgetc@plt>
add    $0xffffffff,%ebp
jne    1230 <main+0xe0>
jmp    11d0 <main+0x80>
mov    0x8(%rbp),%rsi
lea    0xdb9(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%r13d
mov    %r13d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xde1(%rip),%rdi
call   1030 <puts@plt>
jmp    1251 <main+0x101>
lea    0xdba(%rip),%rdi
call   1030 <puts@plt>
jmp    1251 <main+0x101>

LLVM IR (-O0):
@.str = [20 x i8] c"usage: folks <fnm>\0A\00"
@.str.1 = [3 x i8] c"rb\00"
@.str.2 = [19 x i8] c"failed to open %s\0A\00"
@.str.3 = [11 x i8] c"len is %d\0A\00"
@.str.4 = [28 x i8] c"file is corrupt - reserved\0A\00"
@.str.5 = [26 x i8] c"file is corrupt - length\0A\00"
@.str.6 = [23 x i8] c"first data byte is %x\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca [4 x i8]
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* %4
%12 = icmp sle i32 %11, 1
br i1 %12, label %13, label %15
13: ; preds = %2
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %81
15: ; preds = %2
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 1
%18 = load i8*, i8** %17
%19 = call i8* @fopen(i8* %18, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i8* %19, i8** %6
%20 = load i8*, i8** %6
%21 = icmp eq i8* %20, null
br i1 %21, label %22, label %27
22: ; preds = %15
%23 = load i8**, i8*** %5
%24 = getelementptr inbounds i8*, i8** %23, i64 1
%25 = load i8*, i8** %24
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i8* %25)
store i32 1, i32* %3
br label %81
27: ; preds = %15
br label %28
28: ; preds = %79, %27
%29 = getelementptr inbounds [4 x i8], [4 x i8]* %7, i64 0, i64 0
%30 = load i8*, i8** %6
%31 = call i32 @fread(i8* %29, i32 4, i32 1, i8* %30)
%32 = icmp eq i32 %31, 1
br i1 %32, label %33, label %80
33: ; preds = %28
%34 = getelementptr inbounds [4 x i8], [4 x i8]* %7, i64 0, i64 0
%35 = load i8, i8* %34
%36 = zext i8 %35 to i32
%37 = shl i32 %36, 8
%38 = getelementptr inbounds [4 x i8], [4 x i8]* %7, i64 0, i64 1
%39 = load i8, i8* %38
%40 = zext i8 %39 to i32
%41 = or i32 %37, %40
store i32 %41, i32* %8
%42 = load i32, i32* %8
%43 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i32 %42)
%44 = getelementptr inbounds [4 x i8], [4 x i8]* %7, i64 0, i64 2
%45 = load i8, i8* %44
%46 = zext i8 %45 to i32
%47 = icmp ne i32 %46, 0
br i1 %47, label %53, label %48
48: ; preds = %33
%49 = getelementptr inbounds [4 x i8], [4 x i8]* %7, i64 0, i64 3
%50 = load i8, i8* %49
%51 = zext i8 %50 to i32
%52 = icmp ne i32 %51, 0
br i1 %52, label %53, label %55
53: ; preds = %48, %33
%54 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0))
store i32 1, i32* %3
br label %81
55: ; preds = %48
%56 = load i32, i32* %8
%57 = icmp eq i32 %56, 0
br i1 %57, label %58, label %59
58: ; preds = %55
br label %80
59: ; preds = %55
%60 = load i32, i32* %8
%61 = icmp slt i32 %60, 4
br i1 %61, label %62, label %64
62: ; preds = %59
%63 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.5, i64 0, i64 0))
store i32 1, i32* %3
br label %81
64: ; preds = %59
%65 = load i8*, i8** %6
%66 = call i32 @fgetc(i8* %65)
store i32 %66, i32* %9
%67 = load i32, i32* %9
%68 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.6, i64 0, i64 0), i32 %67)
store i32 5, i32* %10
br label %69
69: ; preds = %76, %64
%70 = load i32, i32* %10
%71 = load i32, i32* %8
%72 = icmp slt i32 %70, %71
br i1 %72, label %73, label %79
73: ; preds = %69
%74 = load i8*, i8** %6
%75 = call i32 @fgetc(i8* %74)
br label %76
76: ; preds = %73
%77 = load i32, i32* %10
%78 = add nsw i32 %77, 1
store i32 %78, i32* %10
br label %69
79: ; preds = %69
br label %28
80: ; preds = %58, %28
store i32 0, i32* %3
br label %81
81: ; preds = %80, %62, %53, %22, %13
%82 = load i32, i32* %3
ret i32 %82
}

LLVM IR (-O2):
@.str.1 = [3 x i8] c"rb\00"
@.str.2 = [19 x i8] c"failed to open %s\0A\00"
@.str.3 = [11 x i8] c"len is %d\0A\00"
@.str.6 = [23 x i8] c"first data byte is %x\0A\00"
@str = [25 x i8] c"file is corrupt - length\00"
@str.7 = [27 x i8] c"file is corrupt - reserved\00"
@str.8 = [19 x i8] c"usage: folks <fnm>\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [4 x i8]
%4 = getelementptr inbounds [4 x i8], [4 x i8]* %3, i64 0, i64 0
%5 = icmp slt i32 %0, 2
br i1 %5, label %6, label %8
6: ; preds = %2
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str.8, i64 0, i64 0))
br label %56
8: ; preds = %2
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
%11 = tail call i8* @fopen(i8* %10, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%12 = icmp eq i8* %11, null
br i1 %12, label %20, label %13
13: ; preds = %8
%14 = call i32 @fread(i8* %4, i32 4, i32 1, i8* %11)
%15 = icmp eq i32 %14, 1
br i1 %15, label %16, label %56
16: ; preds = %13
%17 = getelementptr inbounds [4 x i8], [4 x i8]* %3, i64 0, i64 1
%18 = getelementptr inbounds [4 x i8], [4 x i8]* %3, i64 0, i64 2
%19 = getelementptr inbounds [4 x i8], [4 x i8]* %3, i64 0, i64 3
br label %26
20: ; preds = %8
%21 = load i8*, i8** %9
%22 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i8* %21)
br label %56
23: ; preds = %51, %47
%24 = call i32 @fread(i8* %4, i32 4, i32 1, i8* %11)
%25 = icmp eq i32 %24, 1
br i1 %25, label %26, label %56
26: ; preds = %16, %23
%27 = load i8, i8* %4
%28 = zext i8 %27 to i32
%29 = shl nuw nsw i32 %28, 8
%30 = load i8, i8* %17
%31 = zext i8 %30 to i32
%32 = or i32 %29, %31
%33 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i32 %32)
%34 = load i8, i8* %18
%35 = icmp ne i8 %34, 0
%36 = load i8, i8* %19
%37 = icmp ne i8 %36, 0
%38 = select i1 %35, i1 true, i1 %37
br i1 %38, label %39, label %41
39: ; preds = %26
%40 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str.7, i64 0, i64 0))
br label %56
41: ; preds = %26
%42 = icmp eq i32 %32, 0
br i1 %42, label %56, label %43
43: ; preds = %41
%44 = icmp ult i32 %32, 4
br i1 %44, label %45, label %47
45: ; preds = %43
%46 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str, i64 0, i64 0))
br label %56
47: ; preds = %43
%48 = tail call i32 @fgetc(i8* %11)
%49 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.6, i64 0, i64 0), i32 %48)
%50 = icmp ugt i32 %32, 5
br i1 %50, label %51, label %23
51: ; preds = %47, %51
%52 = phi i32 [ %54, %51 ], [ 5, %47 ]
%53 = tail call i32 @fgetc(i8* %11)
%54 = add nuw nsw i32 %52, 1
%55 = icmp eq i32 %54, %32
br i1 %55, label %23, label %51
56: ; preds = %41, %23, %13, %45, %39, %20, %6
%57 = phi i32 [ 1, %6 ], [ 1, %20 ], [ 1, %39 ], [ 1, %45 ], [ 0, %13 ], [ 0, %23 ], [ 0, %41 ]
ret i32 %57
}