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

/* Variables and functions */
int EXIT_FAILURE;
int EXIT_SUCCESS;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
scalar_t__ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int getpid();
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int, char);
int read(int, void *, int);
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;
  }
  int fd;
  if ((fd = open(argv[1], O_RDONLY)) == -1) {
    perror("unable to open input file");
    return EXIT_FAILURE;
  }
  if (fork())
    fork();
  char c;
  int n;
  while ((n = read(fd, (void *)&c, sizeof(char))) > 0)
    printf("[%d] '%c'\n", getpid(), c);
  if (n == -1) {
    perror("error while reading");
    close(fd);
    return EXIT_FAILURE;
  }
  close(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    11cd <main+0x3d>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe41(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%eax
jne    11fa <main+0x6a>
mov    0x2e04(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe1f(%rip),%rsi
call   1080 <fprintf@plt>
mov    0x2de0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    12d4 <main+0x144>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2ddf(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    1239 <main+0xa9>
lea    0xdf0(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2da1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    12d4 <main+0x144>
mov    $0x0,%al
call   10b0 <fork@plt>
cmp    $0x0,%rax
je     1251 <main+0xc1>
mov    $0x0,%al
call   10b0 <fork@plt>
jmp    1256 <main+0xc6>
mov    -0x14(%rbp),%edi
lea    -0x15(%rbp),%rsi
mov    $0x1,%edx
call   1060 <read@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jle    1291 <main+0x101>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
lea    0xdaa(%rip),%rdi
movsbl -0x15(%rbp),%edx
call   1040 <printf@plt>
jmp    1256 <main+0xc6>
cmpl   $0xffffffff,-0x1c(%rbp)
jne    12c0 <main+0x130>
lea    0xd96(%rip),%rdi
call   10a0 <perror@plt>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
mov    0x2d1a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    12d4 <main+0x144>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
mov    0x2cf9(%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
sub    $0x10,%rsp
mov    %rsi,%rbx
cmp    $0x2,%edi
jne    1251 <main+0xc1>
mov    0x8(%rbx),%r14
lea    0xe50(%rip),%rsi
mov    %r14,%rdi
call   1070 <strcmp@plt>
test   %eax,%eax
je     1251 <main+0xc1>
mov    0x2e21(%rip),%rax
mov    (%rax),%esi
mov    %r14,%rdi
call   1090 <open@plt>
cmp    $0xffffffff,%eax
je     127c <main+0xec>
mov    %eax,%ebx
xor    %eax,%eax
call   10b0 <fork@plt>
test   %rax,%rax
je     11ef <main+0x5f>
xor    %eax,%eax
call   10b0 <fork@plt>
lea    0xf(%rsp),%rsi
mov    %ebx,%edi
mov    $0x1,%edx
call   1060 <read@plt>
test   %eax,%eax
jle    1239 <main+0xa9>
lea    0xe22(%rip),%r14
lea    0xf(%rsp),%r15
xor    %eax,%eax
call   1030 <getpid@plt>
movsbl 0xf(%rsp),%edx
mov    %r14,%rdi
mov    %eax,%esi
call   1040 <printf@plt>
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0x1,%edx
call   1060 <read@plt>
test   %eax,%eax
jg     1210 <main+0x80>
cmp    $0xffffffff,%eax
je     128a <main+0xfa>
mov    0x2d83(%rip),%r14
mov    %ebx,%edi
call   1050 <close@plt>
mov    (%r14),%eax
jmp    1272 <main+0xe2>
mov    0x2d80(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%rdx
lea    0xd9f(%rip),%rsi
call   1080 <fprintf@plt>
mov    0x2d60(%rip),%rax
mov    (%rax),%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xd90(%rip),%rdi
call   10a0 <perror@plt>
jmp    1269 <main+0xd9>
lea    0xda7(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2d33(%rip),%r14
jmp    1245 <main+0xb5>

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