#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 MAP_NORESERVE;
int MAP_PRIVATE;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ PROT_READ;
char *bisect_end(char const *, size_t, char const *);
char *bisect_start(char *, size_t, char const *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
size_t get_file_size(char const *);
char *mmap(int /*<<< orphan*/ *, size_t, int /*<<< orphan*/, int, int,
           int /*<<< orphan*/);
int /*<<< orphan*/ munmap(char *, size_t);
int open(char const *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ write(int, char const *, int);
int main(int argc, char **argv) {
  if (argc < 2) {
    fprintf(stderr, "Usage: bisect <file> <pattern>\n");
    return 1;
  }
  const char *filename = argv[1];
  const char *pattern = argv[2];
  size_t file_size = get_file_size(filename);
  int fd = open(filename, O_RDONLY, 0);
  if (fd == -1) {
    fprintf(stderr, "Can't open file\n");
    return 1;
  }
  char *data =
      mmap(NULL, file_size, PROT_READ, MAP_PRIVATE | MAP_NORESERVE, fd, 0);
  if (data == NULL) {
    fprintf(stderr, "Can't mmap file into memory\n");
    return 1;
  }
  const char *start = bisect_start(data, file_size, pattern);
  if (start) {
    const char *end = bisect_end(start, file_size - (start - data), pattern);
    if (end) {
      write(1, start, end - start);
    } else {
      write(1, "\n", 1);
    }
  } else {
    write(1, "\n", 1);
  }
  munmap(data, file_size);
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11d1 <main+0x41>
mov    0x2e11(%rip),%rax
mov    (%rax),%edi
lea    0xe40(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    134c <main+0x1bc>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   10a0 <get_file_size@plt>
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
mov    0x2dd7(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <open@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0xffffffff,-0x2c(%rbp)
jne    1238 <main+0xa8>
mov    0x2daa(%rip),%rax
mov    (%rax),%edi
lea    0xdf9(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    134c <main+0x1bc>
mov    -0x28(%rbp),%rsi
mov    0x2db5(%rip),%rax
mov    (%rax),%edx
mov    0x2d74(%rip),%rax
mov    (%rax),%ecx
mov    0x2d8b(%rip),%rax
or     (%rax),%ecx
mov    -0x2c(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
xor    %r9d,%r9d
call   1050 <mmap@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    1297 <main+0x107>
mov    0x2d4b(%rip),%rax
mov    (%rax),%edi
lea    0xdab(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    134c <main+0x1bc>
mov    -0x38(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1080 <bisect_start@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
je     131d <main+0x18d>
mov    -0x40(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x40(%rbp),%rax
mov    -0x38(%rbp),%rcx
sub    %rcx,%rax
sub    %rax,%rsi
mov    -0x20(%rbp),%rdx
call   1030 <bisect_end@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
je     1305 <main+0x175>
mov    -0x40(%rbp),%rsi
mov    -0x48(%rbp),%rax
mov    -0x40(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,%edx
mov    $0x1,%edi
call   1040 <write@plt>
jmp    1318 <main+0x188>
mov    $0x1,%edx
lea    0xd0d(%rip),%rsi
mov    %edx,%edi
call   1040 <write@plt>
jmp    1330 <main+0x1a0>
mov    $0x1,%edx
lea    0xcf5(%rip),%rsi
mov    %edx,%edi
call   1040 <write@plt>
mov    -0x38(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1090 <munmap@plt>
mov    -0x2c(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
cmp    $0x1,%edi
jg     11b2 <main+0x22>
mov    0x2e24(%rip),%rax
mov    (%rax),%edi
lea    0xe53(%rip),%rsi
jmp    1271 <main+0xe1>
mov    0x8(%rsi),%rbx
mov    0x10(%rsi),%r15
mov    %rbx,%rdi
call   10a0 <get_file_size@plt>
mov    %rax,%r14
mov    0x2e0c(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
xor    %edx,%edx
call   10b0 <open@plt>
cmp    $0xffffffff,%eax
je     124f <main+0xbf>
mov    %eax,%ebp
mov    0x2e12(%rip),%rax
mov    (%rax),%edx
mov    0x2dd1(%rip),%rax
mov    0x2dea(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
xor    %edi,%edi
mov    %r14,%rsi
mov    %ebp,%r8d
xor    %r9d,%r9d
call   1050 <mmap@plt>
test   %rax,%rax
je     1261 <main+0xd1>
mov    %rax,%r12
mov    %rax,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
call   1080 <bisect_start@plt>
test   %rax,%rax
je     1284 <main+0xf4>
mov    %rax,%rbx
mov    %r14,%rsi
add    %r12,%rsi
sub    %rax,%rsi
mov    %rax,%rdi
mov    %r15,%rdx
call   1030 <bisect_end@plt>
test   %rax,%rax
je     1284 <main+0xf4>
sub    %ebx,%eax
mov    $0x1,%edi
mov    %rbx,%rsi
mov    %eax,%edx
jmp    1295 <main+0x105>
mov    0x2d72(%rip),%rax
mov    (%rax),%edi
lea    0xdc1(%rip),%rsi
jmp    1271 <main+0xe1>
mov    0x2d60(%rip),%rax
mov    (%rax),%edi
lea    0xdc0(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x1,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd93(%rip),%rsi
mov    $0x1,%edi
mov    $0x1,%edx
call   1040 <write@plt>
mov    %r12,%rdi
mov    %r14,%rsi
call   1090 <munmap@plt>
mov    %ebp,%edi
call   1060 <close@plt>
xor    %eax,%eax
jmp    127b <main+0xeb>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [32 x i8] c"Usage: bisect <file> <pattern>\0A\00"
@O_RDONLY = global i32 0
@.str.1 = [17 x i8] c"Can't open file\0A\00"
@PROT_READ = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_NORESERVE = global i32 0
@.str.2 = [29 x i8] c"Can't mmap file into memory\0A\00"
@.str.3 = [2 x i8] c"\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 i64
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%13 = load i32, i32* %4
%14 = icmp slt i32 %13, 2
br i1 %14, label %15, label %18
15: ; preds = %2
%16 = load i32, i32* @stderr
%17 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %88
18: ; preds = %2
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 1
%21 = load i8*, i8** %20
store i8* %21, i8** %6
%22 = load i8**, i8*** %5
%23 = getelementptr inbounds i8*, i8** %22, i64 2
%24 = load i8*, i8** %23
store i8* %24, i8** %7
%25 = load i8*, i8** %6
%26 = call i64 @get_file_size(i8* %25)
store i64 %26, i64* %8
%27 = load i8*, i8** %6
%28 = load i32, i32* @O_RDONLY
%29 = call i32 @open(i8* %27, i32 %28, i32 0)
store i32 %29, i32* %9
%30 = load i32, i32* %9
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %35
32: ; preds = %18
%33 = load i32, i32* @stderr
%34 = call i32 @fprintf(i32 %33, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %3
br label %88
35: ; preds = %18
%36 = load i64, i64* %8
%37 = load i32, i32* @PROT_READ
%38 = load i32, i32* @MAP_PRIVATE
%39 = load i32, i32* @MAP_NORESERVE
%40 = or i32 %38, %39
%41 = load i32, i32* %9
%42 = call i8* @mmap(i32* null, i64 %36, i32 %37, i32 %40, i32 %41, i32 0)
store i8* %42, i8** %10
%43 = load i8*, i8** %10
%44 = icmp eq i8* %43, null
br i1 %44, label %45, label %48
45: ; preds = %35
%46 = load i32, i32* @stderr
%47 = call i32 @fprintf(i32 %46, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %3
br label %88
48: ; preds = %35
%49 = load i8*, i8** %10
%50 = load i64, i64* %8
%51 = load i8*, i8** %7
%52 = call i8* @bisect_start(i8* %49, i64 %50, i8* %51)
store i8* %52, i8** %11
%53 = load i8*, i8** %11
%54 = icmp ne i8* %53, null
br i1 %54, label %55, label %80
55: ; preds = %48
%56 = load i8*, i8** %11
%57 = load i64, i64* %8
%58 = load i8*, i8** %11
%59 = load i8*, i8** %10
%60 = ptrtoint i8* %58 to i64
%61 = ptrtoint i8* %59 to i64
%62 = sub i64 %60, %61
%63 = sub i64 %57, %62
%64 = load i8*, i8** %7
%65 = call i8* @bisect_end(i8* %56, i64 %63, i8* %64)
store i8* %65, i8** %12
%66 = load i8*, i8** %12
%67 = icmp ne i8* %66, null
br i1 %67, label %68, label %77
68: ; preds = %55
%69 = load i8*, i8** %11
%70 = load i8*, i8** %12
%71 = load i8*, i8** %11
%72 = ptrtoint i8* %70 to i64
%73 = ptrtoint i8* %71 to i64
%74 = sub i64 %72, %73
%75 = trunc i64 %74 to i32
%76 = call i32 @write(i32 1, i8* %69, i32 %75)
br label %79
77: ; preds = %55
%78 = call i32 @write(i32 1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i32 1)
br label %79
79: ; preds = %77, %68
br label %82
80: ; preds = %48
%81 = call i32 @write(i32 1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i32 1)
br label %82
82: ; preds = %80, %79
%83 = load i8*, i8** %10
%84 = load i64, i64* %8
%85 = call i32 @munmap(i8* %83, i64 %84)
%86 = load i32, i32* %9
%87 = call i32 @close(i32 %86)
store i32 0, i32* %3
br label %88
88: ; preds = %82, %45, %32, %15
%89 = load i32, i32* %3
ret i32 %89
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [32 x i8] c"Usage: bisect <file> <pattern>\0A\00"
@O_RDONLY = global i32 0
@.str.1 = [17 x i8] c"Can't open file\0A\00"
@PROT_READ = global i32 0
@MAP_PRIVATE = global i32 0
@MAP_NORESERVE = global i32 0
@.str.2 = [29 x i8] c"Can't mmap file into memory\0A\00"
@.str.3 = [2 x i8] c"\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp slt i32 %0, 2
br i1 %3, label %4, label %7
4: ; preds = %2
%5 = load i32, i32* @stderr
%6 = tail call i32 @fprintf(i32 %5, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
br label %51
7: ; preds = %2
%8 = getelementptr inbounds i8*, i8** %1, i64 1
%9 = load i8*, i8** %8
%10 = getelementptr inbounds i8*, i8** %1, i64 2
%11 = load i8*, i8** %10
%12 = tail call i64 @get_file_size(i8* %9)
%13 = load i32, i32* @O_RDONLY
%14 = tail call i32 @open(i8* %9, i32 %13, i32 0)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %19
16: ; preds = %7
%17 = load i32, i32* @stderr
%18 = tail call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
br label %51
19: ; preds = %7
%20 = load i32, i32* @PROT_READ
%21 = load i32, i32* @MAP_PRIVATE
%22 = load i32, i32* @MAP_NORESERVE
%23 = or i32 %22, %21
%24 = tail call i8* @mmap(i32* null, i64 %12, i32 %20, i32 %23, i32 %14, i32 0)
%25 = icmp eq i8* %24, null
br i1 %25, label %26, label %29
26: ; preds = %19
%27 = load i32, i32* @stderr
%28 = tail call i32 @fprintf(i32 %27, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0))
br label %51
29: ; preds = %19
%30 = tail call i8* @bisect_start(i8* %24, i64 %12, i8* %11)
%31 = icmp eq i8* %30, null
br i1 %31, label %46, label %32
32: ; preds = %29
%33 = ptrtoint i8* %30 to i64
%34 = ptrtoint i8* %24 to i64
%35 = add i64 %12, %34
%36 = sub i64 %35, %33
%37 = tail call i8* @bisect_end(i8* %30, i64 %36, i8* %11)
%38 = icmp eq i8* %37, null
br i1 %38, label %44, label %39
39: ; preds = %32
%40 = ptrtoint i8* %37 to i64
%41 = sub i64 %40, %33
%42 = trunc i64 %41 to i32
%43 = tail call i32 @write(i32 1, i8* %30, i32 %42)
br label %48
44: ; preds = %32
%45 = tail call i32 @write(i32 1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i32 1)
br label %48
46: ; preds = %29
%47 = tail call i32 @write(i32 1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i32 1)
br label %48
48: ; preds = %39, %44, %46
%49 = tail call i32 @munmap(i8* %24, i64 %12)
%50 = tail call i32 @close(i32 %14)
br label %51
51: ; preds = %16, %48, %26, %4
%52 = phi i32 [ 1, %4 ], [ 1, %16 ], [ 1, %26 ], [ 0, %48 ]
ret i32 %52
}