#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 */
size_t atoi(char *);
int /*<<< orphan*/ close_file(int);
size_t file_size(int);
char *mmap_file(int, size_t);
int open_file(char const *);
int /*<<< orphan*/ perror(char const *);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ scan_for_string(char const *, size_t, char const *, size_t);
int main(int argc, char **argv) {
  const char *search_string;
  const char *filename;
  int file_descriptor;
  const char *data;
  size_t fsize;
  size_t context = 64;
  if (argc < 3 || argc > 4) {
    printf("Usage: %s string filename [context-length]\n", argv[0]);
    return 1;
  }
  search_string = argv[1];
  filename = argv[2];
  if (argc == 4)
    context = atoi(argv[3]);
  file_descriptor = open_file(filename);
  if (file_descriptor == -1) {
    perror(filename);
    return 1;
  }
  fsize = file_size(file_descriptor);
  data = mmap_file(file_descriptor, fsize);
  if (data == NULL) {
    perror(filename);
    return 1;
  }
  scan_for_string(data, fsize, search_string, context);
  close_file(file_descriptor);
}


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)
movq   $0x40,-0x40(%rbp)
cmpl   $0x3,-0x8(%rbp)
jl     11b2 <main+0x32>
cmpl   $0x4,-0x8(%rbp)
jle    11d1 <main+0x51>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe40(%rip),%rdi
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1288 <main+0x108>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x20(%rbp)
cmpl   $0x4,-0x8(%rbp)
jne    1204 <main+0x84>
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
call   1090 <atoi@plt>
mov    %rax,-0x40(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <open_file@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    122f <main+0xaf>
mov    -0x20(%rbp),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1288 <main+0x108>
mov    -0x24(%rbp),%edi
call   1060 <file_size@plt>
mov    %rax,-0x38(%rbp)
mov    -0x24(%rbp),%edi
mov    -0x38(%rbp),%rsi
call   10a0 <mmap_file@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    126b <main+0xeb>
mov    -0x20(%rbp),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1288 <main+0x108>
mov    -0x30(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x40(%rbp),%rcx
call   1070 <scan_for_string@plt>
mov    -0x24(%rbp),%edi
call   1030 <close_file@plt>
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
lea    -0x5(%rdi),%eax
cmp    $0xfffffffd,%eax
ja     11a6 <main+0x26>
mov    (%rsi),%rsi
lea    0xe66(%rip),%rdi
call   1050 <printf@plt>
mov    $0x1,%eax
jmp    1207 <main+0x87>
mov    0x8(%rsi),%r14
mov    0x10(%rsi),%r15
mov    $0x40,%r12d
cmp    $0x4,%edi
jne    11c5 <main+0x45>
mov    0x18(%rsi),%rdi
call   1090 <atoi@plt>
mov    %rax,%r12
mov    %r15,%rdi
call   1040 <open_file@plt>
cmp    $0xffffffff,%eax
je     1210 <main+0x90>
mov    %eax,%ebp
mov    %eax,%edi
call   1060 <file_size@plt>
mov    %rax,%rbx
mov    %ebp,%edi
mov    %rax,%rsi
call   10a0 <mmap_file@plt>
test   %rax,%rax
je     1210 <main+0x90>
mov    %rax,%rdi
mov    %rbx,%rsi
mov    %r14,%rdx
mov    %r12,%rcx
call   1070 <scan_for_string@plt>
mov    %ebp,%edi
call   1030 <close_file@plt>
xor    %eax,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %r15,%rdi
call   1080 <perror@plt>
mov    $0x1,%eax
jmp    1207 <main+0x87>

LLVM IR (-O0):
@.str = [44 x i8] c"Usage: %s string filename [context-length]\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i64
%11 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i64 64, i64* %11
%12 = load i32, i32* %4
%13 = icmp slt i32 %12, 3
br i1 %13, label %17, label %14
14: ; preds = %2
%15 = load i32, i32* %4
%16 = icmp sgt i32 %15, 4
br i1 %16, label %17, label %22
17: ; preds = %14, %2
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 0
%20 = load i8*, i8** %19
%21 = call i32 @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str, i64 0, i64 0), i8* %20)
store i32 1, i32* %3
br label %63
22: ; preds = %14
%23 = load i8**, i8*** %5
%24 = getelementptr inbounds i8*, i8** %23, i64 1
%25 = load i8*, i8** %24
store i8* %25, i8** %6
%26 = load i8**, i8*** %5
%27 = getelementptr inbounds i8*, i8** %26, i64 2
%28 = load i8*, i8** %27
store i8* %28, i8** %7
%29 = load i32, i32* %4
%30 = icmp eq i32 %29, 4
br i1 %30, label %31, label %36
31: ; preds = %22
%32 = load i8**, i8*** %5
%33 = getelementptr inbounds i8*, i8** %32, i64 3
%34 = load i8*, i8** %33
%35 = call i64 @atoi(i8* %34)
store i64 %35, i64* %11
br label %36
36: ; preds = %31, %22
%37 = load i8*, i8** %7
%38 = call i32 @open_file(i8* %37)
store i32 %38, i32* %8
%39 = load i32, i32* %8
%40 = icmp eq i32 %39, -1
br i1 %40, label %41, label %44
41: ; preds = %36
%42 = load i8*, i8** %7
%43 = call i32 @perror(i8* %42)
store i32 1, i32* %3
br label %63
44: ; preds = %36
%45 = load i32, i32* %8
%46 = call i64 @file_size(i32 %45)
store i64 %46, i64* %10
%47 = load i32, i32* %8
%48 = load i64, i64* %10
%49 = call i8* @mmap_file(i32 %47, i64 %48)
store i8* %49, i8** %9
%50 = load i8*, i8** %9
%51 = icmp eq i8* %50, null
br i1 %51, label %52, label %55
52: ; preds = %44
%53 = load i8*, i8** %7
%54 = call i32 @perror(i8* %53)
store i32 1, i32* %3
br label %63
55: ; preds = %44
%56 = load i8*, i8** %9
%57 = load i64, i64* %10
%58 = load i8*, i8** %6
%59 = load i64, i64* %11
%60 = call i32 @scan_for_string(i8* %56, i64 %57, i8* %58, i64 %59)
%61 = load i32, i32* %8
%62 = call i32 @close_file(i32 %61)
br label %63
63: ; preds = %55, %52, %41, %17
%64 = load i32, i32* %3
ret i32 %64
}

LLVM IR (-O2):
@.str = [44 x i8] c"Usage: %s string filename [context-length]\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = add i32 %0, -5
%4 = icmp ult i32 %3, -2
br i1 %4, label %5, label %8
5: ; preds = %2
%6 = load i8*, i8** %1
%7 = tail call i32 @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str, i64 0, i64 0), i8* %6)
br label %33
8: ; preds = %2
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
%11 = getelementptr inbounds i8*, i8** %1, i64 2
%12 = load i8*, i8** %11
%13 = icmp eq i32 %0, 4
br i1 %13, label %14, label %18
14: ; preds = %8
%15 = getelementptr inbounds i8*, i8** %1, i64 3
%16 = load i8*, i8** %15
%17 = tail call i64 @atoi(i8* %16)
br label %18
18: ; preds = %14, %8
%19 = phi i64 [ %17, %14 ], [ 64, %8 ]
%20 = tail call i32 @open_file(i8* %12)
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %24
22: ; preds = %18
%23 = tail call i32 @perror(i8* %12)
br label %33
24: ; preds = %18
%25 = tail call i64 @file_size(i32 %20)
%26 = tail call i8* @mmap_file(i32 %20, i64 %25)
%27 = icmp eq i8* %26, null
br i1 %27, label %28, label %30
28: ; preds = %24
%29 = tail call i32 @perror(i8* %12)
br label %33
30: ; preds = %24
%31 = tail call i32 @scan_for_string(i8* %26, i64 %25, i8* %10, i64 %19)
%32 = tail call i32 @close_file(i32 %20)
br label %33
33: ; preds = %30, %28, %22, %5
%34 = phi i32 [ 1, %5 ], [ 1, %22 ], [ 1, %28 ], [ 0, %30 ]
ret i32 %34
}