#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 */
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ pi_clean(char **, char *);
scalar_t__ pi_read(int /*<<< orphan*/ **, char **, size_t *, char *);
int /*<<< orphan*/ pi_search(int *, int *, char *, int, char *, int);
int /*<<< orphan*/ pi_table(int **, char *, int);
int /*<<< orphan*/ printf(char *, int);
size_t strlen(char *);
int search(char *filename, char *input) {
  int rc = 0, index = -1;
  int *table = NULL;
  size_t filesize = 0, inputsize = 0;
  char *filedata = NULL, *input_clean = NULL;
  FILE *file = NULL;
  if (pi_read(&file, &filedata, &filesize, filename) != 0) {
    rc = 1;
    goto cleanup;
  }
  pi_clean(&input_clean, input);
  inputsize = strlen(input_clean);
  pi_table(&table, input, (int)inputsize);
  pi_search(&index, table, filedata, (int)filesize, input_clean,
            (int)inputsize);
  printf("%d\n", index);
cleanup:
  if (file) {
    (void)fclose(file);
  }
  if (filedata) {
    free(filedata);
  }
  if (input_clean) {
    free(input_clean);
  }
  return rc;
}


x86 assembly (clang, -O0):
<search>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0xffffffff,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
movq   $0x0,-0x40(%rbp)
movq   $0x0,-0x48(%rbp)
mov    -0x8(%rbp),%rcx
lea    -0x48(%rbp),%rdi
lea    -0x38(%rbp),%rsi
lea    -0x28(%rbp),%rdx
call   1090 <pi_read@plt>
cmp    $0x0,%rax
je     11f9 <search+0x79>
movl   $0x1,-0x14(%rbp)
jmp    1257 <search+0xd7>
mov    -0x10(%rbp),%rsi
lea    -0x40(%rbp),%rdi
call   1080 <pi_clean@plt>
mov    -0x40(%rbp),%rdi
call   1060 <strlen@plt>
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rsi
mov    -0x30(%rbp),%rax
mov    %eax,%edx
lea    -0x20(%rbp),%rdi
call   10a0 <pi_table@plt>
mov    -0x20(%rbp),%rsi
mov    -0x38(%rbp),%rdx
mov    -0x28(%rbp),%rax
mov    %eax,%ecx
mov    -0x40(%rbp),%r8
mov    -0x30(%rbp),%rax
mov    %eax,%r9d
lea    -0x18(%rbp),%rdi
call   1030 <pi_search@plt>
mov    -0x18(%rbp),%esi
lea    0xdae(%rip),%rdi
call   1070 <printf@plt>
cmpq   $0x0,-0x48(%rbp)
je     126b <search+0xeb>
mov    -0x48(%rbp),%rdi
call   1050 <fclose@plt>
cmpq   $0x0,-0x38(%rbp)
je     127f <search+0xff>
mov    -0x38(%rbp),%rdi
call   1040 <free@plt>
cmpq   $0x0,-0x40(%rbp)
je     1293 <search+0x113>
mov    -0x40(%rbp),%rdi
call   1040 <free@plt>
mov    -0x14(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<search>:
push   %rbp
push   %rbx
sub    $0x38,%rsp
mov    %rsi,%rbx
mov    %rdi,%rcx
movl   $0xffffffff,0xc(%rsp)
ff
movq   $0x0,0x30(%rsp)
movq   $0x0,0x28(%rsp)
movq   $0x0,0x18(%rsp)
movq   $0x0,0x10(%rsp)
movq   $0x0,0x20(%rsp)
lea    0x20(%rsp),%rdi
lea    0x18(%rsp),%rsi
lea    0x28(%rsp),%rdx
call   1090 <pi_read@plt>
mov    $0x1,%ebp
test   %rax,%rax
jne    123a <search+0xba>
lea    0x10(%rsp),%rdi
mov    %rbx,%rsi
call   1080 <pi_clean@plt>
mov    0x10(%rsp),%rdi
call   1060 <strlen@plt>
mov    %rax,%rbp
lea    0x30(%rsp),%rdi
mov    %rbx,%rsi
mov    %ebp,%edx
call   10a0 <pi_table@plt>
mov    0x30(%rsp),%rsi
mov    0x18(%rsp),%rdx
mov    0x28(%rsp),%ecx
mov    0x10(%rsp),%r8
lea    0xc(%rsp),%rdi
mov    %ebp,%r9d
call   1030 <pi_search@plt>
mov    0xc(%rsp),%esi
lea    0xdcd(%rip),%rdi
call   1070 <printf@plt>
xor    %ebp,%ebp
mov    0x20(%rsp),%rdi
test   %rdi,%rdi
je     1249 <search+0xc9>
call   1050 <fclose@plt>
mov    0x18(%rsp),%rdi
test   %rdi,%rdi
je     1258 <search+0xd8>
call   1040 <free@plt>
mov    0x10(%rsp),%rdi
test   %rdi,%rdi
je     1267 <search+0xe7>
call   1040 <free@plt>
mov    %ebp,%eax
add    $0x38,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [4 x i8] c"%d\0A\00"
define i32 @search(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i64
%9 = alloca i64
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca i32*
store i8* %0, i8** %3
store i8* %1, i8** %4
store i32 0, i32* %5
store i32 -1, i32* %6
store i32* null, i32** %7
store i64 0, i64* %8
store i64 0, i64* %9
store i8* null, i8** %10
store i8* null, i8** %11
store i32* null, i32** %12
%13 = load i8*, i8** %3
%14 = call i64 @pi_read(i32** %12, i8** %10, i64* %8, i8* %13)
%15 = icmp ne i64 %14, 0
br i1 %15, label %16, label %17
16: ; preds = %2
store i32 1, i32* %5
br label %36
17: ; preds = %2
%18 = load i8*, i8** %4
%19 = call i32 @pi_clean(i8** %11, i8* %18)
%20 = load i8*, i8** %11
%21 = call i64 @strlen(i8* %20)
store i64 %21, i64* %9
%22 = load i8*, i8** %4
%23 = load i64, i64* %9
%24 = trunc i64 %23 to i32
%25 = call i32 @pi_table(i32** %7, i8* %22, i32 %24)
%26 = load i32*, i32** %7
%27 = load i8*, i8** %10
%28 = load i64, i64* %8
%29 = trunc i64 %28 to i32
%30 = load i8*, i8** %11
%31 = load i64, i64* %9
%32 = trunc i64 %31 to i32
%33 = call i32 @pi_search(i32* %6, i32* %26, i8* %27, i32 %29, i8* %30, i32 %32)
%34 = load i32, i32* %6
%35 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %34)
br label %36
36: ; preds = %17, %16
%37 = load i32*, i32** %12
%38 = icmp ne i32* %37, null
br i1 %38, label %39, label %42
39: ; preds = %36
%40 = load i32*, i32** %12
%41 = call i32 @fclose(i32* %40)
br label %42
42: ; preds = %39, %36
%43 = load i8*, i8** %10
%44 = icmp ne i8* %43, null
br i1 %44, label %45, label %48
45: ; preds = %42
%46 = load i8*, i8** %10
%47 = call i32 @free(i8* %46)
br label %48
48: ; preds = %45, %42
%49 = load i8*, i8** %11
%50 = icmp ne i8* %49, null
br i1 %50, label %51, label %54
51: ; preds = %48
%52 = load i8*, i8** %11
%53 = call i32 @free(i8* %52)
br label %54
54: ; preds = %51, %48
%55 = load i32, i32* %5
ret i32 %55
}

LLVM IR (-O2):
@.str = [4 x i8] c"%d\0A\00"
define i32 @search(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i64
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32*
%9 = bitcast i32* %3 to i8*
store i32 -1, i32* %3
%10 = bitcast i32** %4 to i8*
store i32* null, i32** %4
%11 = bitcast i64* %5 to i8*
store i64 0, i64* %5
%12 = bitcast i8** %6 to i8*
store i8* null, i8** %6
%13 = bitcast i8** %7 to i8*
store i8* null, i8** %7
%14 = bitcast i32** %8 to i8*
store i32* null, i32** %8
%15 = call i64 @pi_read(i32** %8, i8** %6, i64* %5, i8* %0)
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %31
17: ; preds = %2
%18 = call i32 @pi_clean(i8** %7, i8* %1)
%19 = load i8*, i8** %7
%20 = call i64 @strlen(i8* dereferenceable(1) %19)
%21 = trunc i64 %20 to i32
%22 = call i32 @pi_table(i32** %4, i8* %1, i32 %21)
%23 = load i32*, i32** %4
%24 = load i8*, i8** %6
%25 = load i64, i64* %5
%26 = trunc i64 %25 to i32
%27 = load i8*, i8** %7
%28 = call i32 @pi_search(i32* %3, i32* %23, i8* %24, i32 %26, i8* %27, i32 %21)
%29 = load i32, i32* %3
%30 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %29)
br label %31
31: ; preds = %2, %17
%32 = phi i32 [ 0, %17 ], [ 1, %2 ]
%33 = load i32*, i32** %8
%34 = icmp eq i32* %33, null
br i1 %34, label %37, label %35
35: ; preds = %31
%36 = call i32 @fclose(i32* %33)
br label %37
37: ; preds = %35, %31
%38 = load i8*, i8** %6
%39 = icmp eq i8* %38, null
br i1 %39, label %42, label %40
40: ; preds = %37
%41 = call i32 @free(i8* %38)
br label %42
42: ; preds = %40, %37
%43 = load i8*, i8** %7
%44 = icmp eq i8* %43, null
br i1 %44, label %47, label %45
45: ; preds = %42
%46 = call i32 @free(i8* %43)
br label %47
47: ; preds = %45, %42
ret i32 %32
}