#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 /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fork();
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, int, char *);
int read(int, char *, int);
int /*<<< orphan*/ wait(int *);
int main(int argc, char *argv[]) {
  char c;
  int read_count;
  char searched = argv[1][0];
  for (int i = 2; i < argc; ++i) {
    if (!fork()) {
      char *filename = argv[i];
      int fd = open(filename, O_RDONLY);
      int count = 0;
      while ((read_count = read(fd, &c, 1))) {
        if (c == searched) {
          count++;
        }
      }
      printf("%d %s\n", count, filename);
      close(fd);
      return 0;
    }
  }
  for (int i = 2; i < argc; ++i) {
    int status;
    wait(&status);
  }
  return 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)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    (%rax),%al
mov    %al,-0x19(%rbp)
movl   $0x2,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    1247 <main+0xe7>
mov    $0x0,%al
call   1080 <fork@plt>
cmp    $0x0,%eax
jne    1234 <main+0xd4>
mov    -0x10(%rbp),%rax
movslq -0x20(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    0x2e27(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x2c(%rbp)
movl   $0x0,-0x30(%rbp)
mov    -0x2c(%rbp),%edi
lea    -0x11(%rbp),%rsi
mov    $0x1,%edx
call   1050 <read@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
je     120d <main+0xad>
movsbl -0x11(%rbp),%eax
movsbl -0x19(%rbp),%ecx
cmp    %ecx,%eax
jne    1208 <main+0xa8>
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
jmp    11d2 <main+0x72>
mov    -0x30(%rbp),%esi
mov    -0x28(%rbp),%rdx
lea    0xde5(%rip),%rdi
call   1030 <printf@plt>
mov    -0x2c(%rbp),%edi
call   1040 <close@plt>
movl   $0x0,-0x4(%rbp)
jmp    1278 <main+0x118>
jmp    1239 <main+0xd9>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    118a <main+0x2a>
movl   $0x2,-0x34(%rbp)
mov    -0x34(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    1271 <main+0x111>
lea    -0x38(%rbp),%rdi
call   1070 <wait@plt>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    124e <main+0xee>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
cmp    $0x3,%edi
jl     1247 <main+0xe7>
mov    %rsi,%r14
mov    %edi,%ebx
mov    0x8(%rsi),%rax
mov    (%rax),%r12b
mov    %edi,%r15d
mov    $0x2,%ebp
nopl   0x0(%rax)
xor    %eax,%eax
call   1080 <fork@plt>
test   %eax,%eax
je     11cf <main+0x6f>
add    $0x1,%rbp
cmp    %ebp,%r15d
jne    1190 <main+0x30>
cmp    $0x3,%ebx
jl     1247 <main+0xe7>
add    $0xfffffffe,%ebx
lea    0xc(%rsp),%rbp
cs nopw 0x0(%rax,%rax,1)
nop
mov    %rbp,%rdi
call   1070 <wait@plt>
add    $0xffffffff,%ebx
jne    11c0 <main+0x60>
jmp    1247 <main+0xe7>
mov    (%r14,%rbp,8),%r14
mov    0x2e0e(%rip),%rax
mov    (%rax),%esi
mov    %r14,%rdi
call   1060 <open@plt>
mov    %eax,%ebp
lea    0xb(%rsp),%rsi
mov    %eax,%edi
mov    $0x1,%edx
call   1050 <read@plt>
xor    %ebx,%ebx
test   %eax,%eax
je     122f <main+0xcf>
lea    0xb(%rsp),%r15
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
xor    %eax,%eax
cmp    %r12b,0xb(%rsp)
sete   %al
add    %eax,%ebx
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0x1,%edx
call   1050 <read@plt>
test   %eax,%eax
jne    1210 <main+0xb0>
lea    0xdca(%rip),%rdi
mov    %ebx,%esi
mov    %r14,%rdx
call   1030 <printf@plt>
mov    %ebp,%edi
call   1040 <close@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_RDONLY = global i32 0
@.str = [7 x i8] c"%d %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 i8
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 1
%17 = load i8*, i8** %16
%18 = getelementptr inbounds i8, i8* %17, i64 0
%19 = load i8, i8* %18
store i8 %19, i8* %8
store i32 2, i32* %9
br label %20
20: ; preds = %57, %2
%21 = load i32, i32* %9
%22 = load i32, i32* %4
%23 = icmp slt i32 %21, %22
br i1 %23, label %24, label %60
24: ; preds = %20
%25 = call i32 (...) @fork()
%26 = icmp ne i32 %25, 0
br i1 %26, label %56, label %27
27: ; preds = %24
%28 = load i8**, i8*** %5
%29 = load i32, i32* %9
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds i8*, i8** %28, i64 %30
%32 = load i8*, i8** %31
store i8* %32, i8** %10
%33 = load i8*, i8** %10
%34 = load i32, i32* @O_RDONLY
%35 = call i32 @open(i8* %33, i32 %34)
store i32 %35, i32* %11
store i32 0, i32* %12
br label %36
36: ; preds = %49, %27
%37 = load i32, i32* %11
%38 = call i32 @read(i32 %37, i8* %6, i32 1)
store i32 %38, i32* %7
%39 = icmp ne i32 %38, 0
br i1 %39, label %40, label %50
40: ; preds = %36
%41 = load i8, i8* %6
%42 = sext i8 %41 to i32
%43 = load i8, i8* %8
%44 = sext i8 %43 to i32
%45 = icmp eq i32 %42, %44
br i1 %45, label %46, label %49
46: ; preds = %40
%47 = load i32, i32* %12
%48 = add nsw i32 %47, 1
store i32 %48, i32* %12
br label %49
49: ; preds = %46, %40
br label %36
50: ; preds = %36
%51 = load i32, i32* %12
%52 = load i8*, i8** %10
%53 = call i32 @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 %51, i8* %52)
%54 = load i32, i32* %11
%55 = call i32 @close(i32 %54)
store i32 0, i32* %3
br label %71
56: ; preds = %24
br label %57
57: ; preds = %56
%58 = load i32, i32* %9
%59 = add nsw i32 %58, 1
store i32 %59, i32* %9
br label %20
60: ; preds = %20
store i32 2, i32* %13
br label %61
61: ; preds = %67, %60
%62 = load i32, i32* %13
%63 = load i32, i32* %4
%64 = icmp slt i32 %62, %63
br i1 %64, label %65, label %70
65: ; preds = %61
%66 = call i32 @wait(i32* %14)
br label %67
67: ; preds = %65
%68 = load i32, i32* %13
%69 = add nsw i32 %68, 1
store i32 %69, i32* %13
br label %61
70: ; preds = %61
store i32 0, i32* %3
br label %71
71: ; preds = %70, %50
%72 = load i32, i32* %3
ret i32 %72
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@.str = [7 x i8] c"%d %s\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i8
%4 = alloca i32
%5 = getelementptr inbounds i8*, i8** %1, i64 1
%6 = load i8*, i8** %5
%7 = load i8, i8* %6
%8 = icmp sgt i32 %0, 2
br i1 %8, label %12, label %44
9: ; preds = %36
br i1 %8, label %10, label %44
10: ; preds = %9
%11 = bitcast i32* %4 to i8*
br label %39
12: ; preds = %2, %36
%13 = phi i32 [ %37, %36 ], [ 2, %2 ]
%14 = tail call i32 (...) @fork()
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %36
16: ; preds = %12
%17 = zext i32 %13 to i64
%18 = getelementptr inbounds i8*, i8** %1, i64 %17
%19 = load i8*, i8** %18
%20 = load i32, i32* @O_RDONLY
%21 = tail call i32 @open(i8* %19, i32 %20)
%22 = call i32 @read(i32 %21, i8* %3, i32 1)
%23 = icmp eq i32 %22, 0
br i1 %23, label %32, label %24
24: ; preds = %16, %24
%25 = phi i32 [ %29, %24 ], [ 0, %16 ]
%26 = load i8, i8* %3
%27 = icmp eq i8 %26, %7
%28 = zext i1 %27 to i32
%29 = add nuw nsw i32 %25, %28
%30 = call i32 @read(i32 %21, i8* %3, i32 1)
%31 = icmp eq i32 %30, 0
br i1 %31, label %32, label %24
32: ; preds = %24, %16
%33 = phi i32 [ 0, %16 ], [ %29, %24 ]
%34 = tail call i32 @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 %33, i8* %19)
%35 = tail call i32 @close(i32 %21)
br label %44
36: ; preds = %12
%37 = add nuw nsw i32 %13, 1
%38 = icmp eq i32 %37, %0
br i1 %38, label %9, label %12
39: ; preds = %10, %39
%40 = phi i32 [ 2, %10 ], [ %42, %39 ]
%41 = call i32 @wait(i32* %4)
%42 = add nuw nsw i32 %40, 1
%43 = icmp eq i32 %42, %0
br i1 %43, label %44, label %39
44: ; preds = %39, %2, %9, %32
ret i32 0
}