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

/* Variables and functions */
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
unsigned long find_offset(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ format_buffer(char *, int);
size_t fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, unsigned long,
                         int /*<<< orphan*/);
int /*<<< orphan*/ fwrite(char *, int, size_t, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, ...);
int extract_text(char *input_file, char *output_file) {
  FILE *pFileIn = NULL;
  FILE *pFileOut = NULL;
  printf("Opening file: %s\n", input_file);
  unsigned long offset = 0;
  pFileIn = fopen(input_file, "rb");
  if (pFileIn == NULL) {
    printf("Error opening file, aborting.\n");
    return 1;
  }
  printf("Searching for offset...");
  if ((offset = find_offset(pFileIn)) == -1) {
    printf("%30s", "Failed.\n");
  } else {
    printf("%#30lx\n", offset);
  }
  fseek(pFileIn, offset, SEEK_SET);
  pFileOut = fopen(output_file, "w");
  if (pFileOut == NULL) {
    printf("Failed to open file for output, aborting.\n");
  }
  char buffer[4096];
  size_t readBytes;
  char utf8[3];
  utf8[0] = 0xEF;
  utf8[1] = 0xBB;
  utf8[2] = 0xBF;
  utf8[3] = 0x20;
  fwrite(utf8, sizeof(char), sizeof(utf8), pFileOut);
  while ((readBytes = fread(buffer, sizeof(char), sizeof(buffer), pFileIn)) !=
         0) {
    format_buffer(buffer, sizeof(buffer));
    fwrite(buffer, sizeof(char), readBytes, pFileOut);
  }
  fclose(pFileIn);
  fclose(pFileOut);
  printf("Successfully extracted text to %s\n", output_file);
  return 0;
}


x86 assembly (clang, -O0):
<extract_text>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1040,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x10(%rbp),%rsi
lea    0xe52(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movq   $0x0,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe4a(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11f6 <extract_text+0x76>
lea    0xe32(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1348 <extract_text+0x1c8>
lea    0xe37(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x20(%rbp),%rdi
call   1080 <find_offset@plt>
mov    %rax,-0x30(%rbp)
cmp    $0xffffffffffffffff,%rax
jne    1235 <extract_text+0xb5>
lea    0xe2a(%rip),%rdi
lea    0xe28(%rip),%rsi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1247 <extract_text+0xc7>
mov    -0x30(%rbp),%rsi
lea    0xe1a(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x20(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    0x2d8a(%rip),%rax
mov    (%rax),%edx
call   1070 <fseek@plt>
mov    -0x18(%rbp),%rdi
lea    0xdfa(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    128a <extract_text+0x10a>
lea    0xde1(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movb   $0xef,-0x103b(%rbp)
movb   $0xbb,-0x103a(%rbp)
movb   $0xbf,-0x1039(%rbp)
movb   $0x20,-0x1038(%rbp)
lea    -0x103b(%rbp),%rdi
mov    -0x28(%rbp),%rcx
mov    $0x1,%esi
mov    $0x3,%edx
call   10a0 <fwrite@plt>
lea    -0x1030(%rbp),%rdi
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
mov    $0x1000,%edx
call   1030 <fread@plt>
mov    %rax,-0x1038(%rbp)
cmp    $0x0,%rax
je     131d <extract_text+0x19d>
lea    -0x1030(%rbp),%rdi
mov    $0x1000,%esi
call   1060 <format_buffer@plt>
lea    -0x1030(%rbp),%rdi
mov    -0x1038(%rbp),%rdx
mov    -0x28(%rbp),%rcx
mov    $0x1,%esi
call   10a0 <fwrite@plt>
jmp    12c0 <extract_text+0x140>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x18(%rbp),%rsi
lea    0xd55(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1040,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<extract_text>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1010,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
lea    0xe53(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xe54(%rip),%rsi
mov    %rbx,%rdi
call   10a0 <fopen@plt>
test   %rax,%rax
je     11f9 <extract_text+0x69>
mov    %rax,%r15
lea    0xe40(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %r15,%rdi
call   1090 <find_offset@plt>
mov    %rax,%rbx
cmp    $0xffffffffffffffff,%rax
je     120f <extract_text+0x7f>
lea    0xe47(%rip),%rdi
mov    %rbx,%rsi
jmp    121d <extract_text+0x8d>
lea    0xe92(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%ebx
jmp    1310 <extract_text+0x180>
lea    0xe17(%rip),%rdi
lea    0xe15(%rip),%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x2db5(%rip),%rax
mov    (%rax),%edx
mov    %r15,%rdi
mov    %rbx,%rsi
call   1080 <fseek@plt>
lea    0xe04(%rip),%rsi
mov    %r14,%rdi
call   10a0 <fopen@plt>
mov    %rax,%r12
test   %rax,%rax
jne    125b <extract_text+0xcb>
lea    0xe12(%rip),%rdi
call   1030 <puts@plt>
movw   $0xbbef,0xd(%rsp)
movb   $0xbf,0xf(%rsp)
movb   $0x20,0x10(%rsp)
lea    0xd(%rsp),%rdi
mov    $0x3,%edx
mov    $0x1,%esi
mov    %r12,%rcx
call   10b0 <fwrite@plt>
lea    0x10(%rsp),%rdi
mov    $0x1,%esi
mov    $0x1000,%edx
mov    %r15,%rcx
call   1040 <fread@plt>
test   %rax,%rax
je     12ed <extract_text+0x15d>
mov    %rax,%rbx
lea    0x10(%rsp),%r13
nopw   0x0(%rax,%rax,1)
mov    %r13,%rdi
mov    $0x1000,%esi
call   1070 <format_buffer@plt>
mov    %r13,%rdi
mov    $0x1,%esi
mov    %rbx,%rdx
mov    %r12,%rcx
call   10b0 <fwrite@plt>
mov    %r13,%rdi
mov    $0x1,%esi
mov    $0x1000,%edx
mov    %r15,%rcx
call   1040 <fread@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    12b0 <extract_text+0x120>
mov    %r15,%rdi
call   1050 <fclose@plt>
mov    %r12,%rdi
call   1050 <fclose@plt>
lea    0xd41(%rip),%rdi
xor    %ebx,%ebx
mov    %r14,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %ebx,%eax
add    $0x1010,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [18 x i8] c"Opening file: %s\0A\00"
@.str.1 = [3 x i8] c"rb\00"
@.str.2 = [31 x i8] c"Error opening file, aborting.\0A\00"
@.str.3 = [24 x i8] c"Searching for offset...\00"
@.str.4 = [5 x i8] c"%30s\00"
@.str.5 = [9 x i8] c"Failed.\0A\00"
@.str.6 = [8 x i8] c"%lx\0A\00"
@SEEK_SET = global i32 0
@.str.7 = [2 x i8] c"w\00"
@.str.8 = [43 x i8] c"Failed to open file for output, aborting.\0A\00"
@.str.9 = [35 x i8] c"Successfully extracted text to %s\0A\00"
define i32 @extract_text(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca i64
%9 = alloca [4096 x i8]
%10 = alloca i64
%11 = alloca [3 x i8]
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32* null, i32** %6
store i32* null, i32** %7
%12 = load i8*, i8** %4
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %12)
store i64 0, i64* %8
%14 = load i8*, i8** %4
%15 = call i32* @fopen(i8* %14, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %15, i32** %6
%16 = load i32*, i32** %6
%17 = icmp eq i32* %16, null
br i1 %17, label %18, label %20
18: ; preds = %2
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %3
br label %68
20: ; preds = %2
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0))
%22 = load i32*, i32** %6
%23 = call i64 @find_offset(i32* %22)
store i64 %23, i64* %8
%24 = icmp eq i64 %23, -1
br i1 %24, label %25, label %27
25: ; preds = %20
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0))
br label %30
27: ; preds = %20
%28 = load i64, i64* %8
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.6, i64 0, i64 0), i64 %28)
br label %30
30: ; preds = %27, %25
%31 = load i32*, i32** %6
%32 = load i64, i64* %8
%33 = load i32, i32* @SEEK_SET
%34 = call i32 @fseek(i32* %31, i64 %32, i32 %33)
%35 = load i8*, i8** %5
%36 = call i32* @fopen(i8* %35, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.7, i64 0, i64 0))
store i32* %36, i32** %7
%37 = load i32*, i32** %7
%38 = icmp eq i32* %37, null
br i1 %38, label %39, label %41
39: ; preds = %30
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.8, i64 0, i64 0))
br label %41
41: ; preds = %39, %30
%42 = getelementptr inbounds [3 x i8], [3 x i8]* %11, i64 0, i64 0
store i8 -17, i8* %42
%43 = getelementptr inbounds [3 x i8], [3 x i8]* %11, i64 0, i64 1
store i8 -69, i8* %43
%44 = getelementptr inbounds [3 x i8], [3 x i8]* %11, i64 0, i64 2
store i8 -65, i8* %44
%45 = getelementptr inbounds [3 x i8], [3 x i8]* %11, i64 0, i64 3
store i8 32, i8* %45
%46 = getelementptr inbounds [3 x i8], [3 x i8]* %11, i64 0, i64 0
%47 = load i32*, i32** %7
%48 = call i32 @fwrite(i8* %46, i32 1, i64 3, i32* %47)
br label %49
49: ; preds = %54, %41
%50 = getelementptr inbounds [4096 x i8], [4096 x i8]* %9, i64 0, i64 0
%51 = load i32*, i32** %6
%52 = call i64 @fread(i8* %50, i32 1, i32 4096, i32* %51)
store i64 %52, i64* %10
%53 = icmp ne i64 %52, 0
br i1 %53, label %54, label %61
54: ; preds = %49
%55 = getelementptr inbounds [4096 x i8], [4096 x i8]* %9, i64 0, i64 0
%56 = call i32 @format_buffer(i8* %55, i32 4096)
%57 = getelementptr inbounds [4096 x i8], [4096 x i8]* %9, i64 0, i64 0
%58 = load i64, i64* %10
%59 = load i32*, i32** %7
%60 = call i32 @fwrite(i8* %57, i32 1, i64 %58, i32* %59)
br label %49
61: ; preds = %49
%62 = load i32*, i32** %6
%63 = call i32 @fclose(i32* %62)
%64 = load i32*, i32** %7
%65 = call i32 @fclose(i32* %64)
%66 = load i8*, i8** %5
%67 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.9, i64 0, i64 0), i8* %66)
store i32 0, i32* %3
br label %68
68: ; preds = %61, %18
%69 = load i32, i32* %3
ret i32 %69
}

LLVM IR (-O2):
@.str = [18 x i8] c"Opening file: %s\0A\00"
@.str.1 = [3 x i8] c"rb\00"
@.str.3 = [24 x i8] c"Searching for offset...\00"
@.str.4 = [5 x i8] c"%30s\00"
@.str.5 = [9 x i8] c"Failed.\0A\00"
@.str.6 = [8 x i8] c"%lx\0A\00"
@SEEK_SET = global i32 0
@.str.7 = [2 x i8] c"w\00"
@.str.9 = [35 x i8] c"Successfully extracted text to %s\0A\00"
@str = [42 x i8] c"Failed to open file for output, aborting.\00"
@str.10 = [30 x i8] c"Error opening file, aborting.\00"
define i32 @extract_text(i8* %0, i8* %1) {
%3 = alloca [4096 x i8]
%4 = alloca [3 x i8]
%5 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %0)
%6 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%7 = icmp eq i32* %6, null
br i1 %7, label %8, label %10
8: ; preds = %2
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @str.10, i64 0, i64 0))
br label %44
10: ; preds = %2
%11 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0))
%12 = tail call i64 @find_offset(i32* %6)
%13 = icmp eq i64 %12, -1
br i1 %13, label %14, label %16
14: ; preds = %10
%15 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0))
br label %18
16: ; preds = %10
%17 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.6, i64 0, i64 0), i64 %12)
br label %18
18: ; preds = %16, %14
%19 = load i32, i32* @SEEK_SET
%20 = tail call i32 @fseek(i32* %6, i64 %12, i32 %19)
%21 = tail call i32* @fopen(i8* %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.7, i64 0, i64 0))
%22 = icmp eq i32* %21, null
br i1 %22, label %23, label %25
23: ; preds = %18
%24 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([42 x i8], [42 x i8]* @str, i64 0, i64 0))
br label %25
25: ; preds = %23, %18
%26 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%27 = getelementptr inbounds [3 x i8], [3 x i8]* %4, i64 0, i64 0
store i8 -17, i8* %27
%28 = getelementptr inbounds [3 x i8], [3 x i8]* %4, i64 0, i64 1
store i8 -69, i8* %28
%29 = getelementptr inbounds [3 x i8], [3 x i8]* %4, i64 0, i64 2
store i8 -65, i8* %29
%30 = getelementptr inbounds [3 x i8], [3 x i8]* %4, i64 0, i64 3
store i8 32, i8* %30
%31 = call i32 @fwrite(i8* %27, i32 1, i64 3, i32* %21)
%32 = call i64 @fread(i8* %26, i32 1, i32 4096, i32* %6)
%33 = icmp eq i64 %32, 0
br i1 %33, label %40, label %34
34: ; preds = %25, %34
%35 = phi i64 [ %38, %34 ], [ %32, %25 ]
%36 = call i32 @format_buffer(i8* %26, i32 4096)
%37 = call i32 @fwrite(i8* %26, i32 1, i64 %35, i32* %21)
%38 = call i64 @fread(i8* %26, i32 1, i32 4096, i32* %6)
%39 = icmp eq i64 %38, 0
br i1 %39, label %40, label %34
40: ; preds = %34, %25
%41 = call i32 @fclose(i32* %6)
%42 = call i32 @fclose(i32* %21)
%43 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @.str.9, i64 0, i64 0), i8* %1)
br label %44
44: ; preds = %40, %8
%45 = phi i32 [ 1, %8 ], [ 0, %40 ]
ret i32 %45
}