#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 O_CREAT;
int O_WRONLY;
int asprintf(char **, char *, char *);
int bisect(char *, char *, size_t, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ load(char *, char **, size_t *);
int open(char *, int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ print_help(char *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ strcmp(char *, char *);
int /*<<< orphan*/ strstr(char *, char *);
scalar_t__ test(char *, char *, int /*<<< orphan*/, size_t, int);
int main(int argc, char **argv) {
  char *cmdline_template;
  char *input_file;
  char *output_file;
  char *cmdline;
  char *input;
  size_t len;
  int output_fd;
  if (argc == 2 && ((!strcmp(argv[2], "--help")) || (!strcmp(argv[2], "-h"))))
    print_help(argv[0], stdout, 0);
  else if (argc != 4) {
    fprintf(stderr, "wrong number of arguments\n");
    exit(1);
  }
  cmdline_template = argv[1];
  input_file = argv[2];
  output_file = argv[3];
  if (!strstr(cmdline_template, "%s")) {
    fprintf(stderr, "no %%s is found in command line template\n");
    exit(1);
  }
  load(input_file, &input, &len);
  output_fd = open(output_file, O_WRONLY | O_CREAT, 0666);
  if (output_fd < 0) {
    perror("open(output)");
    exit(1);
  }
  if (asprintf(&cmdline, cmdline_template, output_file) == -1) {
    fprintf(stderr, "error in asprintf\n");
    exit(1);
  }
  if (test(cmdline, input, 0, len, output_fd) == 0) {
    fprintf(
        stderr,
        "the target command line exits normally against the original input\n");
    exit(1);
  }
  if (test(cmdline, input, 0, 0, output_fd) != 0) {
    fprintf(stderr,
            "the target command line exits normally against the empty input\n");
    exit(1);
  }
  return bisect(cmdline, input, len, output_fd);
}


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)
jne    1226 <main+0x76>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
lea    0xe21(%rip),%rsi
call   1030 <strcmp@plt>
cmp    $0x0,%eax
je     120a <main+0x5a>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
lea    0xe0b(%rip),%rsi
call   1030 <strcmp@plt>
cmp    $0x0,%eax
jne    1226 <main+0x76>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    0x2db8(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <print_help@plt>
jmp    1254 <main+0xa4>
cmpl   $0x4,-0x8(%rbp)
je     124f <main+0x9f>
mov    0x2da1(%rip),%rax
mov    (%rax),%edi
lea    0xdca(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
jmp    1254 <main+0xa4>
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    -0x10(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xda2(%rip),%rsi
call   10c0 <strstr@plt>
cmp    $0x0,%eax
jne    12b0 <main+0x100>
mov    0x2d40(%rip),%rax
mov    (%rax),%edi
lea    0xd87(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x20(%rbp),%rdi
lea    -0x38(%rbp),%rsi
lea    -0x40(%rbp),%rdx
call   1090 <load@plt>
mov    -0x28(%rbp),%rdi
mov    0x2cf4(%rip),%rax
mov    (%rax),%esi
mov    0x2d23(%rip),%rax
or     (%rax),%esi
mov    $0x1b6,%edx
call   1060 <open@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0x0,-0x44(%rbp)
jge    1304 <main+0x154>
lea    0xd5d(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x18(%rbp),%rsi
mov    -0x28(%rbp),%rdx
lea    -0x30(%rbp),%rdi
call   1050 <asprintf@plt>
cmp    $0xffffffff,%eax
jne    133d <main+0x18d>
mov    0x2cb3(%rip),%rax
mov    (%rax),%edi
lea    0xd31(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x30(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    -0x40(%rbp),%rcx
mov    -0x44(%rbp),%r8d
xor    %edx,%edx
call   10d0 <test@plt>
cmp    $0x0,%rax
jne    137d <main+0x1cd>
mov    0x2c73(%rip),%rax
mov    (%rax),%edi
lea    0xd04(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x30(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    -0x44(%rbp),%r8d
xor    %edx,%edx
xor    %eax,%eax
mov    %eax,%ecx
call   10d0 <test@plt>
cmp    $0x0,%rax
je     13bd <main+0x20d>
mov    0x2c33(%rip),%rax
mov    (%rax),%edi
lea    0xd07(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x30(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    -0x40(%rbp),%rdx
mov    -0x44(%rbp),%ecx
call   10b0 <bisect@plt>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%rbx
cmp    $0x4,%edi
je     120d <main+0x5d>
cmp    $0x2,%edi
jne    1338 <main+0x188>
mov    0x10(%rbx),%r14
lea    0xe29(%rip),%rsi
mov    %r14,%rdi
call   1030 <strcmp@plt>
test   %eax,%eax
je     11fa <main+0x4a>
lea    0xe1d(%rip),%rsi
mov    %r14,%rdi
call   1030 <strcmp@plt>
test   %eax,%eax
jne    1338 <main+0x188>
mov    (%rbx),%rdi
mov    0x2dcc(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1080 <print_help@plt>
mov    0x8(%rbx),%r14
mov    0x10(%rbx),%r12
mov    0x18(%rbx),%r15
lea    0xe05(%rip),%rsi
mov    %r14,%rdi
call   10c0 <strstr@plt>
test   %eax,%eax
je     12da <main+0x12a>
mov    %rsp,%rsi
lea    0x10(%rsp),%rdx
mov    %r12,%rdi
call   1090 <load@plt>
mov    0x2d79(%rip),%rax
mov    0x2daa(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %r15,%rdi
mov    $0x1b6,%edx
call   1060 <open@plt>
test   %eax,%eax
js     12ec <main+0x13c>
mov    %eax,%ebx
lea    0x8(%rsp),%rdi
mov    %r14,%rsi
mov    %r15,%rdx
call   1050 <asprintf@plt>
cmp    $0xffffffff,%eax
je     1302 <main+0x152>
mov    0x8(%rsp),%rdi
mov    (%rsp),%rsi
mov    0x10(%rsp),%rcx
xor    %edx,%edx
mov    %ebx,%r8d
call   10d0 <test@plt>
test   %rax,%rax
je     1314 <main+0x164>
mov    0x8(%rsp),%rdi
mov    (%rsp),%rsi
xor    %edx,%edx
xor    %ecx,%ecx
mov    %ebx,%r8d
call   10d0 <test@plt>
test   %rax,%rax
jne    1326 <main+0x176>
mov    0x8(%rsp),%rdi
mov    (%rsp),%rsi
mov    0x10(%rsp),%rdx
mov    %ebx,%ecx
call   10b0 <bisect@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
mov    0x2cf7(%rip),%rax
mov    (%rax),%edi
lea    0xd3e(%rip),%rsi
jmp    1348 <main+0x198>
lea    0xd5f(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    0x2ccf(%rip),%rax
mov    (%rax),%edi
lea    0xd4d(%rip),%rsi
jmp    1348 <main+0x198>
mov    0x2cbd(%rip),%rax
mov    (%rax),%edi
lea    0xd4e(%rip),%rsi
jmp    1348 <main+0x198>
mov    0x2cab(%rip),%rax
mov    (%rax),%edi
lea    0xd7f(%rip),%rsi
jmp    1348 <main+0x198>
mov    0x2c99(%rip),%rax
mov    (%rax),%edi
lea    0xcc2(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [7 x i8] c"--help\00"
@.str.1 = [3 x i8] c"-h\00"
@stdout = global i32 0
@stderr = global i32 0
@.str.2 = [27 x i8] c"wrong number of arguments\0A\00"
@.str.3 = [3 x i8] c"%s\00"
@.str.4 = [42 x i8] c"no %%s is found in command line template\0A\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@.str.5 = [13 x i8] c"open(output)\00"
@.str.6 = [19 x i8] c"error in asprintf\0A\00"
@.str.7 = [67 x i8] c"the target command line exits normally against the original input\0A\00"
@.str.8 = [64 x i8] c"the target command line exits normally against the empty input\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 i8*
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i64
%12 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%13 = load i32, i32* %4
%14 = icmp eq i32 %13, 2
br i1 %14, label %15, label %33
15: ; preds = %2
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 2
%18 = load i8*, i8** %17
%19 = call i32 @strcmp(i8* %18, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%20 = icmp ne i32 %19, 0
br i1 %20, label %21, label %27
21: ; preds = %15
%22 = load i8**, i8*** %5
%23 = getelementptr inbounds i8*, i8** %22, i64 2
%24 = load i8*, i8** %23
%25 = call i32 @strcmp(i8* %24, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%26 = icmp ne i32 %25, 0
br i1 %26, label %33, label %27
27: ; preds = %21, %15
%28 = load i8**, i8*** %5
%29 = getelementptr inbounds i8*, i8** %28, i64 0
%30 = load i8*, i8** %29
%31 = load i32, i32* @stdout
%32 = call i32 @print_help(i8* %30, i32 %31, i32 0)
br label %41
33: ; preds = %21, %2
%34 = load i32, i32* %4
%35 = icmp ne i32 %34, 4
br i1 %35, label %36, label %40
36: ; preds = %33
%37 = load i32, i32* @stderr
%38 = call i32 @fprintf(i32 %37, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0))
%39 = call i32 @exit(i32 1)
unreachable
40: ; preds = %33
br label %41
41: ; preds = %40, %27
%42 = load i8**, i8*** %5
%43 = getelementptr inbounds i8*, i8** %42, i64 1
%44 = load i8*, i8** %43
store i8* %44, i8** %6
%45 = load i8**, i8*** %5
%46 = getelementptr inbounds i8*, i8** %45, i64 2
%47 = load i8*, i8** %46
store i8* %47, i8** %7
%48 = load i8**, i8*** %5
%49 = getelementptr inbounds i8*, i8** %48, i64 3
%50 = load i8*, i8** %49
store i8* %50, i8** %8
%51 = load i8*, i8** %6
%52 = call i32 @strstr(i8* %51, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%53 = icmp ne i32 %52, 0
br i1 %53, label %58, label %54
54: ; preds = %41
%55 = load i32, i32* @stderr
%56 = call i32 @fprintf(i32 %55, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.4, i64 0, i64 0))
%57 = call i32 @exit(i32 1)
unreachable
58: ; preds = %41
%59 = load i8*, i8** %7
%60 = call i32 @load(i8* %59, i8** %10, i64* %11)
%61 = load i8*, i8** %8
%62 = load i32, i32* @O_WRONLY
%63 = load i32, i32* @O_CREAT
%64 = or i32 %62, %63
%65 = call i32 @open(i8* %61, i32 %64, i32 438)
store i32 %65, i32* %12
%66 = load i32, i32* %12
%67 = icmp slt i32 %66, 0
br i1 %67, label %68, label %71
68: ; preds = %58
%69 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0))
%70 = call i32 @exit(i32 1)
unreachable
71: ; preds = %58
%72 = load i8*, i8** %6
%73 = load i8*, i8** %8
%74 = call i32 @asprintf(i8** %9, i8* %72, i8* %73)
%75 = icmp eq i32 %74, -1
br i1 %75, label %76, label %80
76: ; preds = %71
%77 = load i32, i32* @stderr
%78 = call i32 @fprintf(i32 %77, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.6, i64 0, i64 0))
%79 = call i32 @exit(i32 1)
unreachable
80: ; preds = %71
%81 = load i8*, i8** %9
%82 = load i8*, i8** %10
%83 = load i64, i64* %11
%84 = load i32, i32* %12
%85 = call i64 @test(i8* %81, i8* %82, i32 0, i64 %83, i32 %84)
%86 = icmp eq i64 %85, 0
br i1 %86, label %87, label %91
87: ; preds = %80
%88 = load i32, i32* @stderr
%89 = call i32 @fprintf(i32 %88, i8* getelementptr inbounds ([67 x i8], [67 x i8]* @.str.7, i64 0, i64 0))
%90 = call i32 @exit(i32 1)
unreachable
91: ; preds = %80
%92 = load i8*, i8** %9
%93 = load i8*, i8** %10
%94 = load i32, i32* %12
%95 = call i64 @test(i8* %92, i8* %93, i32 0, i64 0, i32 %94)
%96 = icmp ne i64 %95, 0
br i1 %96, label %97, label %101
97: ; preds = %91
%98 = load i32, i32* @stderr
%99 = call i32 @fprintf(i32 %98, i8* getelementptr inbounds ([64 x i8], [64 x i8]* @.str.8, i64 0, i64 0))
%100 = call i32 @exit(i32 1)
unreachable
101: ; preds = %91
%102 = load i8*, i8** %9
%103 = load i8*, i8** %10
%104 = load i64, i64* %11
%105 = load i32, i32* %12
%106 = call i32 @bisect(i8* %102, i8* %103, i64 %104, i32 %105)
ret i32 %106
}

LLVM IR (-O2):
@.str = [7 x i8] c"--help\00"
@.str.1 = [3 x i8] c"-h\00"
@stdout = global i32 0
@stderr = global i32 0
@.str.2 = [27 x i8] c"wrong number of arguments\0A\00"
@.str.3 = [3 x i8] c"%s\00"
@.str.4 = [42 x i8] c"no %%s is found in command line template\0A\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@.str.5 = [13 x i8] c"open(output)\00"
@.str.6 = [19 x i8] c"error in asprintf\0A\00"
@.str.7 = [67 x i8] c"the target command line exits normally against the original input\0A\00"
@.str.8 = [64 x i8] c"the target command line exits normally against the empty input\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64
%6 = bitcast i8** %3 to i8*
%7 = bitcast i8** %4 to i8*
%8 = bitcast i64* %5 to i8*
switch i32 %0, label %21 [
i32 2, label %9
i32 4, label %25
]
9: ; preds = %2
%10 = getelementptr inbounds i8*, i8** %1, i64 2
%11 = load i8*, i8** %10
%12 = tail call i32 @strcmp(i8* dereferenceable(1) %11, i8* dereferenceable(7) getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%13 = icmp eq i32 %12, 0
br i1 %13, label %17, label %14
14: ; preds = %9
%15 = tail call i32 @strcmp(i8* dereferenceable(1) %11, i8* dereferenceable(3) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %21
17: ; preds = %14, %9
%18 = load i8*, i8** %1
%19 = load i32, i32* @stdout
%20 = tail call i32 @print_help(i8* %18, i32 %19, i32 0)
br label %25
21: ; preds = %14, %2
%22 = load i32, i32* @stderr
%23 = tail call i32 @fprintf(i32 %22, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0))
%24 = tail call i32 @exit(i32 1)
unreachable
25: ; preds = %2, %17
%26 = getelementptr inbounds i8*, i8** %1, i64 1
%27 = load i8*, i8** %26
%28 = getelementptr inbounds i8*, i8** %1, i64 2
%29 = load i8*, i8** %28
%30 = getelementptr inbounds i8*, i8** %1, i64 3
%31 = load i8*, i8** %30
%32 = tail call i32 @strstr(i8* %27, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%33 = icmp eq i32 %32, 0
br i1 %33, label %34, label %38
34: ; preds = %25
%35 = load i32, i32* @stderr
%36 = tail call i32 @fprintf(i32 %35, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.4, i64 0, i64 0))
%37 = tail call i32 @exit(i32 1)
unreachable
38: ; preds = %25
%39 = call i32 @load(i8* %29, i8** %4, i64* %5)
%40 = load i32, i32* @O_WRONLY
%41 = load i32, i32* @O_CREAT
%42 = or i32 %41, %40
%43 = call i32 @open(i8* %31, i32 %42, i32 438)
%44 = icmp slt i32 %43, 0
br i1 %44, label %45, label %48
45: ; preds = %38
%46 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0))
%47 = call i32 @exit(i32 1)
unreachable
48: ; preds = %38
%49 = call i32 @asprintf(i8** %3, i8* %27, i8* %31)
%50 = icmp eq i32 %49, -1
br i1 %50, label %51, label %55
51: ; preds = %48
%52 = load i32, i32* @stderr
%53 = call i32 @fprintf(i32 %52, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.6, i64 0, i64 0))
%54 = call i32 @exit(i32 1)
unreachable
55: ; preds = %48
%56 = load i8*, i8** %3
%57 = load i8*, i8** %4
%58 = load i64, i64* %5
%59 = call i64 @test(i8* %56, i8* %57, i32 0, i64 %58, i32 %43)
%60 = icmp eq i64 %59, 0
br i1 %60, label %61, label %65
61: ; preds = %55
%62 = load i32, i32* @stderr
%63 = call i32 @fprintf(i32 %62, i8* getelementptr inbounds ([67 x i8], [67 x i8]* @.str.7, i64 0, i64 0))
%64 = call i32 @exit(i32 1)
unreachable
65: ; preds = %55
%66 = load i8*, i8** %3
%67 = load i8*, i8** %4
%68 = call i64 @test(i8* %66, i8* %67, i32 0, i64 0, i32 %43)
%69 = icmp eq i64 %68, 0
br i1 %69, label %74, label %70
70: ; preds = %65
%71 = load i32, i32* @stderr
%72 = call i32 @fprintf(i32 %71, i8* getelementptr inbounds ([64 x i8], [64 x i8]* @.str.8, i64 0, i64 0))
%73 = call i32 @exit(i32 1)
unreachable
74: ; preds = %65
%75 = load i8*, i8** %3
%76 = load i8*, i8** %4
%77 = load i64, i64* %5
%78 = call i32 @bisect(i8* %75, i8* %76, i64 %77, i32 %43)
ret i32 %78
}