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

/* Variables and functions */
int EXIT_FAILURE;
int EXIT_SUCCESS;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ ferror(int /*<<< orphan*/ *);
char fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
scalar_t__ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int getpid();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int, char);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcmp(char *, char *);
int main(int argc, char *argv[]) {
  if (argc != 2 || !strcmp(argv[1], "-h")) {
    fprintf(stderr, "usage: %s FILE\n", argv[0]);
    return EXIT_FAILURE;
  }
  FILE *fd;
  if ((fd = fopen(argv[1], "r")) == NULL) {
    perror("unable to open input file");
    return EXIT_FAILURE;
  }
  if (fork())
    fork();
  char c;
  while ((c = fgetc(fd)) > 0)
    printf("[%d] '%c'\n", getpid(), c);
  if (ferror(fd)) {
    perror("error while reading");
    fclose(fd);
    return EXIT_FAILURE;
  }
  fclose(fd);
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jne    11dd <main+0x3d>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe31(%rip),%rsi
call   1080 <strcmp@plt>
cmp    $0x0,%eax
jne    120a <main+0x6a>
mov    0x2dfc(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe0f(%rip),%rsi
call   1090 <fprintf@plt>
mov    0x2dd8(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    12ea <main+0x14a>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xdfa(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    1249 <main+0xa9>
lea    0xde2(%rip),%rdi
call   10b0 <perror@plt>
mov    0x2d99(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    12ea <main+0x14a>
mov    $0x0,%al
call   10c0 <fork@plt>
cmp    $0x0,%rax
je     1261 <main+0xc1>
mov    $0x0,%al
call   10c0 <fork@plt>
jmp    1266 <main+0xc6>
mov    -0x18(%rbp),%rdi
call   1070 <fgetc@plt>
mov    %al,-0x19(%rbp)
movsbl %al,%eax
cmp    $0x0,%eax
jle    129c <main+0xfc>
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%esi
lea    0xda1(%rip),%rdi
movsbl -0x19(%rbp),%edx
call   1060 <printf@plt>
jmp    1266 <main+0xc6>
mov    -0x18(%rbp),%rdi
call   1030 <ferror@plt>
cmp    $0x0,%rax
je     12d5 <main+0x135>
lea    0xd84(%rip),%rdi
call   10b0 <perror@plt>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
mov    0x2d0d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    12ea <main+0x14a>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
mov    0x2ceb(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%rbx
cmp    $0x2,%edi
jne    1250 <main+0xb0>
mov    0x8(%rbx),%r14
lea    0xe44(%rip),%rsi
mov    %r14,%rdi
call   1080 <strcmp@plt>
test   %eax,%eax
je     1250 <main+0xb0>
lea    0xe40(%rip),%rsi
mov    %r14,%rdi
call   10a0 <fopen@plt>
test   %rax,%rax
je     1277 <main+0xd7>
mov    %rax,%r15
xor    %eax,%eax
call   10c0 <fork@plt>
test   %rax,%rax
je     11fa <main+0x5a>
xor    %eax,%eax
call   10c0 <fork@plt>
mov    %r15,%rdi
call   1070 <fgetc@plt>
test   %al,%al
jle    1232 <main+0x92>
mov    %eax,%ebx
lea    0xe20(%rip),%r14
nop
xor    %eax,%eax
call   1040 <getpid@plt>
movsbl %bl,%edx
mov    %r14,%rdi
mov    %eax,%esi
call   1060 <printf@plt>
mov    %r15,%rdi
call   1070 <fgetc@plt>
mov    %eax,%ebx
test   %al,%al
jg     1210 <main+0x70>
mov    %r15,%rdi
call   1030 <ferror@plt>
test   %rax,%rax
jne    1285 <main+0xe5>
mov    %r15,%rdi
call   1050 <fclose@plt>
mov    0x2d82(%rip),%rax
jmp    126f <main+0xcf>
mov    0x2d89(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%rdx
lea    0xda0(%rip),%rsi
call   1090 <fprintf@plt>
mov    0x2d69(%rip),%rax
mov    (%rax),%eax
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xd97(%rip),%rdi
call   10b0 <perror@plt>
jmp    1268 <main+0xc8>
lea    0xdae(%rip),%rdi
call   10b0 <perror@plt>
mov    %r15,%rdi
call   1050 <fclose@plt>
jmp    1268 <main+0xc8>

LLVM IR (-O0):
@.str = [3 x i8] c"-h\00"
@stderr = global i32 0
@.str.1 = [16 x i8] c"usage: %s FILE\0A\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [26 x i8] c"unable to open input file\00"
@.str.4 = [11 x i8] c"[%d] '%c'\0A\00"
@.str.5 = [20 x i8] c"error while reading\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 i8
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = load i32, i32* %4
%9 = icmp ne i32 %8, 2
br i1 %9, label %16, label %10
10: ; preds = %2
%11 = load i8**, i8*** %5
%12 = getelementptr inbounds i8*, i8** %11, i64 1
%13 = load i8*, i8** %12
%14 = call i32 @strcmp(i8* %13, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%15 = icmp ne i32 %14, 0
br i1 %15, label %23, label %16
16: ; preds = %10, %2
%17 = load i32, i32* @stderr
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 0
%20 = load i8*, i8** %19
%21 = call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* %20)
%22 = load i32, i32* @EXIT_FAILURE
store i32 %22, i32* %3
br label %60
23: ; preds = %10
%24 = load i8**, i8*** %5
%25 = getelementptr inbounds i8*, i8** %24, i64 1
%26 = load i8*, i8** %25
%27 = call i32* @fopen(i8* %26, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %27, i32** %6
%28 = icmp eq i32* %27, null
br i1 %28, label %29, label %32
29: ; preds = %23
%30 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0))
%31 = load i32, i32* @EXIT_FAILURE
store i32 %31, i32* %3
br label %60
32: ; preds = %23
%33 = call i64 (...) @fork()
%34 = icmp ne i64 %33, 0
br i1 %34, label %35, label %37
35: ; preds = %32
%36 = call i64 (...) @fork()
br label %37
37: ; preds = %35, %32
br label %38
38: ; preds = %43, %37
%39 = load i32*, i32** %6
%40 = call signext i8 @fgetc(i32* %39)
store i8 %40, i8* %7
%41 = sext i8 %40 to i32
%42 = icmp sgt i32 %41, 0
br i1 %42, label %43, label %47
43: ; preds = %38
%44 = call i32 (...) @getpid()
%45 = load i8, i8* %7
%46 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0), i32 %44, i8 signext %45)
br label %38
47: ; preds = %38
%48 = load i32*, i32** %6
%49 = call i64 @ferror(i32* %48)
%50 = icmp ne i64 %49, 0
br i1 %50, label %51, label %56
51: ; preds = %47
%52 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0))
%53 = load i32*, i32** %6
%54 = call i32 @fclose(i32* %53)
%55 = load i32, i32* @EXIT_FAILURE
store i32 %55, i32* %3
br label %60
56: ; preds = %47
%57 = load i32*, i32** %6
%58 = call i32 @fclose(i32* %57)
%59 = load i32, i32* @EXIT_SUCCESS
store i32 %59, i32* %3
br label %60
60: ; preds = %56, %51, %29, %16
%61 = load i32, i32* %3
ret i32 %61
}

LLVM IR (-O2):
@.str = [3 x i8] c"-h\00"
@stderr = global i32 0
@.str.1 = [16 x i8] c"usage: %s FILE\0A\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [26 x i8] c"unable to open input file\00"
@.str.4 = [11 x i8] c"[%d] '%c'\0A\00"
@.str.5 = [20 x i8] c"error while reading\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 2
br i1 %3, label %4, label %9
4: ; preds = %2
%5 = getelementptr inbounds i8*, i8** %1, i64 1
%6 = load i8*, i8** %5
%7 = tail call i32 @strcmp(i8* dereferenceable(1) %6, i8* dereferenceable(3) getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%8 = icmp eq i32 %7, 0
br i1 %8, label %9, label %13
9: ; preds = %4, %2
%10 = load i32, i32* @stderr
%11 = load i8*, i8** %1
%12 = tail call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* %11)
br label %40
13: ; preds = %4
%14 = tail call i32* @fopen(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%15 = icmp eq i32* %14, null
br i1 %15, label %16, label %18
16: ; preds = %13
%17 = tail call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0))
br label %40
18: ; preds = %13
%19 = tail call i64 (...) @fork()
%20 = icmp eq i64 %19, 0
br i1 %20, label %23, label %21
21: ; preds = %18
%22 = tail call i64 (...) @fork()
br label %23
23: ; preds = %21, %18
%24 = tail call signext i8 @fgetc(i32* %14)
%25 = icmp sgt i8 %24, 0
br i1 %25, label %26, label %32
26: ; preds = %23, %26
%27 = phi i8 [ %30, %26 ], [ %24, %23 ]
%28 = tail call i32 (...) @getpid()
%29 = tail call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0), i32 %28, i8 signext %27)
%30 = tail call signext i8 @fgetc(i32* %14)
%31 = icmp sgt i8 %30, 0
br i1 %31, label %26, label %32
32: ; preds = %26, %23
%33 = tail call i64 @ferror(i32* %14)
%34 = icmp eq i64 %33, 0
br i1 %34, label %38, label %35
35: ; preds = %32
%36 = tail call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0))
%37 = tail call i32 @fclose(i32* %14)
br label %40
38: ; preds = %32
%39 = tail call i32 @fclose(i32* %14)
br label %40
40: ; preds = %16, %38, %35, %9
%41 = phi i32* [ @EXIT_FAILURE, %9 ], [ @EXIT_FAILURE, %16 ], [ @EXIT_FAILURE, %35 ], [ @EXIT_SUCCESS, %38 ]
%42 = load i32, i32* %41
ret i32 %42
}