#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*/ buf;

/* Variables and functions */
int EXIT_FAILURE;
int EXIT_SUCCESS;
int /*<<< orphan*/ O_RDONLY;
int atoi(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int open(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int, char *);
int read(int, char *, int);
scalar_t__ read_form(int, char *, int);
int /*<<< orphan*/ stderr;
int main(int argc, char *argv[]) {
  int fd;
  char buf[8192];
  int expr_count;
  int i;
  int form_id;
  const char *filename;
  if (argc < 2 || argc > 3) {
    fprintf(stderr, "Usage: %s <file> [<form-id>]\n", argv[0]);
    return EXIT_FAILURE;
  }
  filename = argv[1];
  form_id = argc == 3 ? atoi(argv[2]) : -1;
  fd = open(filename, O_RDONLY);
  if (fd < 0) {
    perror("open");
    return EXIT_FAILURE;
  }
  if (read(fd, buf, 1) != 1) {
    fprintf(stderr, "Failed to read the expression count!\n");
    return EXIT_FAILURE;
  }
  expr_count = (int)((unsigned)buf[0] & 0xff);
  if (form_id + 1 > expr_count) {
    fprintf(stderr, "Form ID %d is over the maximum %d!\n", form_id,
            expr_count - 1);
    return EXIT_FAILURE;
  }
  for (i = 0; i < expr_count; i++) {
    if (read_form(fd, buf, sizeof(buf))) {
      if (form_id == -1 || form_id == i) {
        printf("Form %d: %s\n", i, buf);
        if (form_id == i) {
          break;
        }
      }
    } else {
      fprintf(stderr, "Failed to read form %d!\n", i);
      break;
    }
  }
  close(fd);
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2040,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jl     11ad <main+0x2d>
cmpl   $0x3,-0x8(%rbp)
jle    11dc <main+0x5c>
mov    0x2e24(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe3c(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    0x2dfe(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    13ed <main+0x26d>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x2038(%rbp)
cmpl   $0x3,-0x8(%rbp)
jne    120d <main+0x8d>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   10a0 <atoi@plt>
mov    %eax,-0x203c(%rbp)
jmp    121d <main+0x9d>
mov    $0xffffffff,%eax
mov    %eax,-0x203c(%rbp)
jmp    121d <main+0x9d>
mov    -0x203c(%rbp),%eax
mov    %eax,-0x202c(%rbp)
mov    -0x2038(%rbp),%rdi
mov    0x2db1(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1268 <main+0xe8>
lea    0xdcc(%rip),%rdi
call   1090 <perror@plt>
mov    0x2d72(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    13ed <main+0x26d>
mov    -0x14(%rbp),%edi
lea    -0x2020(%rbp),%rsi
mov    $0x1,%edx
call   1060 <read@plt>
cmp    $0x1,%eax
je     12ad <main+0x12d>
mov    0x2d4c(%rip),%rax
mov    (%rax),%edi
lea    0xd8e(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    0x2d2d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    13ed <main+0x26d>
movsbl -0x2020(%rbp),%eax
and    $0xff,%eax
mov    %eax,-0x2024(%rbp)
mov    -0x202c(%rbp),%eax
add    $0x1,%eax
cmp    -0x2024(%rbp),%eax
jle    130b <main+0x18b>
mov    0x2cfd(%rip),%rax
mov    (%rax),%edi
mov    -0x202c(%rbp),%edx
mov    -0x2024(%rbp),%ecx
sub    $0x1,%ecx
lea    0xd56(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    0x2ccf(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    13ed <main+0x26d>
movl   $0x0,-0x2028(%rbp)
mov    -0x2028(%rbp),%eax
cmp    -0x2024(%rbp),%eax
jge    13d9 <main+0x259>
mov    -0x14(%rbp),%edi
lea    -0x2020(%rbp),%rsi
mov    $0x2000,%edx
call   1030 <read_form@plt>
cmp    $0x0,%rax
je     139e <main+0x21e>
cmpl   $0xffffffff,-0x202c(%rbp)
je     1364 <main+0x1e4>
mov    -0x202c(%rbp),%eax
cmp    -0x2028(%rbp),%eax
jne    1399 <main+0x219>
mov    -0x2028(%rbp),%esi
lea    -0x2020(%rbp),%rdx
lea    0xcf5(%rip),%rdi
call   1040 <printf@plt>
mov    -0x202c(%rbp),%eax
cmp    -0x2028(%rbp),%eax
jne    1394 <main+0x214>
jmp    13d9 <main+0x259>
jmp    1399 <main+0x219>
jmp    13c0 <main+0x240>
mov    0x2c33(%rip),%rax
mov    (%rax),%edi
mov    -0x2028(%rbp),%edx
lea    0xcc6(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
jmp    13d9 <main+0x259>
jmp    13c5 <main+0x245>
mov    -0x2028(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2028(%rbp)
jmp    1315 <main+0x195>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
mov    0x2be0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x2040,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x2008,%rsp
lea    -0x4(%rdi),%eax
cmp    $0xfffffffd,%eax
ja     11b8 <main+0x38>
mov    0x2e38(%rip),%rax
mov    (%rax),%edi
mov    (%rsi),%rdx
lea    0xe54(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
jmp    12e8 <main+0x168>
mov    0x8(%rsi),%rbp
mov    $0xffffffff,%ebx
cmp    $0x3,%edi
jne    11d1 <main+0x51>
mov    0x10(%rsi),%rdi
call   10a0 <atoi@plt>
mov    %eax,%ebx
mov    0x2e10(%rip),%rax
mov    (%rax),%esi
mov    %rbp,%rdi
call   1080 <open@plt>
test   %eax,%eax
js     12dc <main+0x15c>
mov    %eax,%r15d
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0x1,%edx
call   1060 <read@plt>
cmp    $0x1,%eax
jne    12a1 <main+0x121>
movzbl (%rsp),%r14d
cmp    %r14d,%ebx
jge    12ba <main+0x13a>
test   %r14d,%r14d
je     1290 <main+0x110>
mov    %rsp,%rsi
mov    %r15d,%edi
mov    $0x2000,%edx
call   1030 <read_form@plt>
xor    %ebp,%ebp
test   %rax,%rax
je     1277 <main+0xf7>
lea    0xe37(%rip),%r12
mov    %rsp,%r13
nopl   0x0(%rax)
cmp    $0xffffffff,%ebx
je     1249 <main+0xc9>
cmp    %ebp,%ebx
jne    125a <main+0xda>
mov    %r12,%rdi
mov    %ebp,%esi
mov    %r13,%rdx
call   1040 <printf@plt>
cmp    %ebp,%ebx
je     1290 <main+0x110>
add    $0x1,%ebp
cmp    %r14d,%ebp
je     1290 <main+0x110>
mov    %r15d,%edi
mov    %r13,%rsi
mov    $0x2000,%edx
call   1030 <read_form@plt>
test   %rax,%rax
jne    1240 <main+0xc0>
mov    0x2d5a(%rip),%rax
mov    (%rax),%edi
lea    0xdf3(%rip),%rsi
mov    %ebp,%edx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    %r15d,%edi
call   1050 <close@plt>
mov    0x2d29(%rip),%rax
jmp    12ef <main+0x16f>
mov    0x2d30(%rip),%rax
mov    (%rax),%edi
lea    0xd72(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
jmp    12e8 <main+0x168>
mov    0x2d17(%rip),%rax
mov    (%rax),%edi
add    $0xffffffff,%r14d
lea    0xd7b(%rip),%rsi
mov    %ebx,%edx
mov    %r14d,%ecx
xor    %eax,%eax
call   1070 <fprintf@plt>
jmp    12e8 <main+0x168>
lea    0xd3b(%rip),%rdi
call   1090 <perror@plt>
mov    0x2ce1(%rip),%rax
mov    (%rax),%eax
add    $0x2008,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [30 x i8] c"Usage: %s <file> [<form-id>]\0A\00"
@EXIT_FAILURE = global i32 0
@O_RDONLY = global i32 0
@.str.1 = [5 x i8] c"open\00"
@.str.2 = [38 x i8] c"Failed to read the expression count!\0A\00"
@.str.3 = [36 x i8] c"Form ID %d is over the maximum %d!\0A\00"
@.str.4 = [13 x i8] c"Form %d: %s\0A\00"
@.str.5 = [25 x i8] c"Failed to read form %d!\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca [8192 x i8]
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%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 slt i32 %12, 2
br i1 %13, label %17, label %14
14: ; preds = %2
%15 = load i32, i32* %4
%16 = icmp sgt i32 %15, 3
br i1 %16, label %17, label %24
17: ; preds = %14, %2
%18 = load i32, i32* @stderr
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 0
%21 = load i8*, i8** %20
%22 = call i32 (i32, i8*, ...) @fprintf(i32 %18, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* %21)
%23 = load i32, i32* @EXIT_FAILURE
store i32 %23, i32* %3
br label %110
24: ; preds = %14
%25 = load i8**, i8*** %5
%26 = getelementptr inbounds i8*, i8** %25, i64 1
%27 = load i8*, i8** %26
store i8* %27, i8** %11
%28 = load i32, i32* %4
%29 = icmp eq i32 %28, 3
br i1 %29, label %30, label %35
30: ; preds = %24
%31 = load i8**, i8*** %5
%32 = getelementptr inbounds i8*, i8** %31, i64 2
%33 = load i8*, i8** %32
%34 = call i32 @atoi(i8* %33)
br label %36
35: ; preds = %24
br label %36
36: ; preds = %35, %30
%37 = phi i32 [ %34, %30 ], [ -1, %35 ]
store i32 %37, i32* %10
%38 = load i8*, i8** %11
%39 = load i32, i32* @O_RDONLY
%40 = call i32 @open(i8* %38, i32 %39)
store i32 %40, i32* %6
%41 = load i32, i32* %6
%42 = icmp slt i32 %41, 0
br i1 %42, label %43, label %46
43: ; preds = %36
%44 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%45 = load i32, i32* @EXIT_FAILURE
store i32 %45, i32* %3
br label %110
46: ; preds = %36
%47 = load i32, i32* %6
%48 = getelementptr inbounds [8192 x i8], [8192 x i8]* %7, i64 0, i64 0
%49 = call i32 @read(i32 %47, i8* %48, i32 1)
%50 = icmp ne i32 %49, 1
br i1 %50, label %51, label %55
51: ; preds = %46
%52 = load i32, i32* @stderr
%53 = call i32 (i32, i8*, ...) @fprintf(i32 %52, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0))
%54 = load i32, i32* @EXIT_FAILURE
store i32 %54, i32* %3
br label %110
55: ; preds = %46
%56 = getelementptr inbounds [8192 x i8], [8192 x i8]* %7, i64 0, i64 0
%57 = load i8, i8* %56
%58 = sext i8 %57 to i32
%59 = and i32 %58, 255
store i32 %59, i32* %8
%60 = load i32, i32* %10
%61 = add nsw i32 %60, 1
%62 = load i32, i32* %8
%63 = icmp sgt i32 %61, %62
br i1 %63, label %64, label %71
64: ; preds = %55
%65 = load i32, i32* @stderr
%66 = load i32, i32* %10
%67 = load i32, i32* %8
%68 = sub nsw i32 %67, 1
%69 = call i32 (i32, i8*, ...) @fprintf(i32 %65, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.3, i64 0, i64 0), i32 %66, i32 %68)
%70 = load i32, i32* @EXIT_FAILURE
store i32 %70, i32* %3
br label %110
71: ; preds = %55
store i32 0, i32* %9
br label %72
72: ; preds = %103, %71
%73 = load i32, i32* %9
%74 = load i32, i32* %8
%75 = icmp slt i32 %73, %74
br i1 %75, label %76, label %106
76: ; preds = %72
%77 = load i32, i32* %6
%78 = getelementptr inbounds [8192 x i8], [8192 x i8]* %7, i64 0, i64 0
%79 = call i64 @read_form(i32 %77, i8* %78, i32 8192)
%80 = icmp ne i64 %79, 0
br i1 %80, label %81, label %98
81: ; preds = %76
%82 = load i32, i32* %10
%83 = icmp eq i32 %82, -1
br i1 %83, label %88, label %84
84: ; preds = %81
%85 = load i32, i32* %10
%86 = load i32, i32* %9
%87 = icmp eq i32 %85, %86
br i1 %87, label %88, label %97
88: ; preds = %84, %81
%89 = load i32, i32* %9
%90 = getelementptr inbounds [8192 x i8], [8192 x i8]* %7, i64 0, i64 0
%91 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0), i32 %89, i8* %90)
%92 = load i32, i32* %10
%93 = load i32, i32* %9
%94 = icmp eq i32 %92, %93
br i1 %94, label %95, label %96
95: ; preds = %88
br label %106
96: ; preds = %88
br label %97
97: ; preds = %96, %84
br label %102
98: ; preds = %76
%99 = load i32, i32* @stderr
%100 = load i32, i32* %9
%101 = call i32 (i32, i8*, ...) @fprintf(i32 %99, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0), i32 %100)
br label %106
102: ; preds = %97
br label %103
103: ; preds = %102
%104 = load i32, i32* %9
%105 = add nsw i32 %104, 1
store i32 %105, i32* %9
br label %72
106: ; preds = %98, %95, %72
%107 = load i32, i32* %6
%108 = call i32 @close(i32 %107)
%109 = load i32, i32* @EXIT_SUCCESS
store i32 %109, i32* %3
br label %110
110: ; preds = %106, %64, %51, %43, %17
%111 = load i32, i32* %3
ret i32 %111
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [30 x i8] c"Usage: %s <file> [<form-id>]\0A\00"
@EXIT_FAILURE = global i32 0
@O_RDONLY = global i32 0
@.str.1 = [5 x i8] c"open\00"
@.str.2 = [38 x i8] c"Failed to read the expression count!\0A\00"
@.str.3 = [36 x i8] c"Form ID %d is over the maximum %d!\0A\00"
@.str.4 = [13 x i8] c"Form %d: %s\0A\00"
@.str.5 = [25 x i8] c"Failed to read form %d!\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [8192 x i8]
%4 = getelementptr inbounds [8192 x i8], [8192 x i8]* %3, i64 0, i64 0
%5 = add i32 %0, -4
%6 = icmp ult i32 %5, -2
br i1 %6, label %7, label %11
7: ; preds = %2
%8 = load i32, i32* @stderr
%9 = load i8*, i8** %1
%10 = tail call i32 (i32, i8*, ...) @fprintf(i32 %8, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* %9)
br label %68
11: ; preds = %2
%12 = getelementptr inbounds i8*, i8** %1, i64 1
%13 = load i8*, i8** %12
%14 = icmp eq i32 %0, 3
br i1 %14, label %15, label %19
15: ; preds = %11
%16 = getelementptr inbounds i8*, i8** %1, i64 2
%17 = load i8*, i8** %16
%18 = tail call i32 @atoi(i8* %17)
br label %19
19: ; preds = %11, %15
%20 = phi i32 [ %18, %15 ], [ -1, %11 ]
%21 = load i32, i32* @O_RDONLY
%22 = tail call i32 @open(i8* %13, i32 %21)
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %19
%25 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %68
26: ; preds = %19
%27 = call i32 @read(i32 %22, i8* %4, i32 1)
%28 = icmp eq i32 %27, 1
br i1 %28, label %32, label %29
29: ; preds = %26
%30 = load i32, i32* @stderr
%31 = tail call i32 (i32, i8*, ...) @fprintf(i32 %30, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0))
br label %68
32: ; preds = %26
%33 = load i8, i8* %4
%34 = zext i8 %33 to i32
%35 = icmp slt i32 %20, %34
br i1 %35, label %36, label %42
36: ; preds = %32
%37 = icmp eq i8 %33, 0
br i1 %37, label %66, label %38
38: ; preds = %36
%39 = icmp eq i32 %20, -1
%40 = call i64 @read_form(i32 %22, i8* %4, i32 8192)
%41 = icmp eq i64 %40, 0
br i1 %41, label %55, label %46
42: ; preds = %32
%43 = load i32, i32* @stderr
%44 = add nsw i32 %34, -1
%45 = tail call i32 (i32, i8*, ...) @fprintf(i32 %43, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.3, i64 0, i64 0), i32 %20, i32 %44)
br label %68
46: ; preds = %38, %62
%47 = phi i32 [ %63, %62 ], [ 0, %38 ]
%48 = icmp eq i32 %20, %47
%49 = select i1 %39, i1 true, i1 %48
br i1 %49, label %50, label %59
50: ; preds = %46
%51 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0), i32 %47, i8* %4)
%52 = add nuw nsw i32 %47, 1
%53 = icmp eq i32 %52, %34
%54 = select i1 %48, i1 true, i1 %53
br i1 %54, label %66, label %62
55: ; preds = %62, %38
%56 = phi i32 [ 0, %38 ], [ %63, %62 ]
%57 = load i32, i32* @stderr
%58 = call i32 (i32, i8*, ...) @fprintf(i32 %57, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0), i32 %56)
br label %66
59: ; preds = %46
%60 = add nuw nsw i32 %47, 1
%61 = icmp eq i32 %60, %34
br i1 %61, label %66, label %62
62: ; preds = %59, %50
%63 = phi i32 [ %60, %59 ], [ %52, %50 ]
%64 = call i64 @read_form(i32 %22, i8* %4, i32 8192)
%65 = icmp eq i64 %64, 0
br i1 %65, label %55, label %46
66: ; preds = %59, %50, %36, %55
%67 = call i32 @close(i32 %22)
br label %68
68: ; preds = %66, %42, %29, %24, %7
%69 = phi i32* [ @EXIT_FAILURE, %7 ], [ @EXIT_FAILURE, %24 ], [ @EXIT_FAILURE, %29 ], [ @EXIT_FAILURE, %42 ], [ @EXIT_SUCCESS, %66 ]
%70 = load i32, i32* %69
ret i32 %70
}