#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 */
struct stat {
  int st_size;
};
typedef int off_t;

/* Variables and functions */
int O_CREAT;
int /*<<< orphan*/ O_RDONLY;
int O_TRUNC;
int O_WRONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
scalar_t__ malloc(int);
int open(char *, int, ...);
int read(int, char *, int);
scalar_t__ stat(char *, struct stat *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ write(int, char *, int);
int main(int argc, char **argv) {
  struct stat statbuf;
  char *file_data;
  char *data;
  off_t filesize;
  int file_handle;
  if (argc < 2 || argc > 3) {
    fprintf(stderr, "Usage: prepare_financisto input [output]\n");
    exit(1);
  }
  if (stat(argv[1], &statbuf) != 0) {
    fprintf(stderr, "Could not stat %s\n", argv[1]);
    exit(1);
  }
  filesize = statbuf.st_size;
  file_data = (char *)malloc(filesize + 1);
  if (file_data == NULL) {
    fprintf(stderr, "Could not allocate %d bytes for file data\n", filesize);
    exit(1);
  }
  file_handle = open(argv[1], O_RDONLY);
  if (read(file_handle, file_data, filesize) != filesize) {
    fprintf(stderr, "Could not read all of file %s\n", argv[1]);
    exit(1);
  }
  close(file_handle);
  for (data = file_data; *data & 0x80; data++, filesize--) {
    if (filesize == 0) {
      fprintf(stderr, "No non-rubbish bytes found in %s\n", argv[1]);
      exit(1);
    }
  }
  file_handle = open(argv[argc - 1], O_WRONLY | O_TRUNC | O_CREAT, 0666);
  write(file_handle, data, filesize);
  close(file_handle);
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jl     11aa <main+0x2a>
cmpl   $0x3,-0x8(%rbp)
jle    11cb <main+0x4b>
mov    0x2e17(%rip),%rax
mov    (%rax),%edi
lea    0xe46(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    -0x18(%rbp),%rsi
call   1070 <stat@plt>
cmp    $0x0,%rax
je     120f <main+0x8f>
mov    0x2ddb(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xe2c(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%edi
add    $0x1,%edi
call   1080 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1253 <main+0xd3>
mov    0x2d92(%rip),%rax
mov    (%rax),%edi
mov    -0x2c(%rbp),%edx
lea    0xdfb(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2d76(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1090 <open@plt>
mov    %eax,-0x30(%rbp)
mov    -0x30(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x2c(%rbp),%edx
call   1050 <read@plt>
cmp    -0x2c(%rbp),%eax
je     12af <main+0x12f>
mov    0x2d3b(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xdca(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x30(%rbp),%edi
call   1040 <close@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
movsbl (%rax),%eax
and    $0x80,%eax
cmp    $0x0,%eax
je     1326 <main+0x1a6>
cmpl   $0x0,-0x2c(%rbp)
jne    1307 <main+0x187>
mov    0x2ce3(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xd91(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
jmp    130c <main+0x18c>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
mov    -0x2c(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x2c(%rbp)
jmp    12bf <main+0x13f>
mov    -0x10(%rbp),%rax
mov    -0x8(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,8),%rdi
mov    0x2c7a(%rip),%rax
mov    (%rax),%esi
mov    0x2ca9(%rip),%rax
or     (%rax),%esi
mov    0x2ca8(%rip),%rax
or     (%rax),%esi
mov    $0x1b6,%edx
mov    $0x0,%al
call   1090 <open@plt>
mov    %eax,-0x30(%rbp)
mov    -0x30(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    -0x2c(%rbp),%edx
call   1030 <write@plt>
mov    -0x30(%rbp),%edi
call   1040 <close@plt>
xor    %edi,%edi
call   10a0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %edi,%r15d
lea    -0x4(%r15),%eax
cmp    $0xfffffffd,%eax
ja     11b9 <main+0x39>
mov    0x2e29(%rip),%rax
mov    (%rax),%edi
lea    0xe58(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    %rsi,%r14
mov    0x8(%rsi),%rdi
lea    0x8(%rsp),%rsi
call   1070 <stat@plt>
test   %rax,%rax
jne    1203 <main+0x83>
mov    0x8(%rsp),%ebx
lea    0x1(%rbx),%edi
call   1080 <malloc@plt>
test   %rax,%rax
jne    1228 <main+0xa8>
mov    0x2de1(%rip),%rax
mov    (%rax),%edi
lea    0xe4d(%rip),%rsi
mov    %ebx,%edx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    0x2dbe(%rip),%rax
mov    (%rax),%edi
mov    0x8(%r14),%rdx
lea    0xe13(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    %rax,%r12
mov    0x8(%r14),%rdi
mov    0x2da2(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
call   1090 <open@plt>
mov    %eax,%ebp
mov    %eax,%edi
mov    %r12,%rsi
mov    %ebx,%edx
call   1050 <read@plt>
cmp    %ebx,%eax
jne    12cd <main+0x14d>
mov    %ebp,%edi
call   1040 <close@plt>
cmpb   $0x0,(%r12)
jns    1282 <main+0x102>
mov    %r12,%rax
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
sub    $0x1,%ebx
jb     12e6 <main+0x166>
lea    0x1(%rax),%r12
cmpb   $0x0,0x1(%rax)
mov    %r12,%rax
js     1270 <main+0xf0>
mov    0x2d2f(%rip),%rax
mov    0x2d60(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
add    $0xffffffff,%r15d
mov    (%r14,%r15,8),%rdi
mov    0x2d55(%rip),%rax
or     (%rax),%esi
mov    $0x1b6,%edx
xor    %eax,%eax
call   1090 <open@plt>
mov    %eax,%ebp
mov    %eax,%edi
mov    %r12,%rsi
mov    %ebx,%edx
call   1030 <write@plt>
mov    %ebp,%edi
call   1040 <close@plt>
xor    %edi,%edi
call   10a0 <exit@plt>
mov    0x2cf4(%rip),%rax
mov    (%rax),%edi
mov    0x8(%r14),%rdx
lea    0xd87(%rip),%rsi
jmp    1217 <main+0x97>
mov    0x2cdb(%rip),%rax
mov    (%rax),%edi
mov    0x8(%r14),%rdx
lea    0xd8d(%rip),%rsi
jmp    1217 <main+0x97>

LLVM IR (-O0):
%struct.stat = type { i32 }
@stderr = global i32 0
@.str = [42 x i8] c"Usage: prepare_financisto input [output]\0A\00"
@.str.1 = [19 x i8] c"Could not stat %s\0A\00"
@.str.2 = [43 x i8] c"Could not allocate %d bytes for file data\0A\00"
@O_RDONLY = global i32 0
@.str.3 = [31 x i8] c"Could not read all of file %s\0A\00"
@.str.4 = [34 x i8] c"No non-rubbish bytes found in %s\0A\00"
@O_WRONLY = global i32 0
@O_TRUNC = global i32 0
@O_CREAT = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.stat
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* %4
%12 = icmp slt i32 %11, 2
br i1 %12, label %16, label %13
13: ; preds = %2
%14 = load i32, i32* %4
%15 = icmp sgt i32 %14, 3
br i1 %15, label %16, label %20
16: ; preds = %13, %2
%17 = load i32, i32* @stderr
%18 = call i32 (i32, i8*, ...) @fprintf(i32 %17, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0))
%19 = call i32 @exit(i32 1)
unreachable
20: ; preds = %13
%21 = load i8**, i8*** %5
%22 = getelementptr inbounds i8*, i8** %21, i64 1
%23 = load i8*, i8** %22
%24 = call i64 @stat(i8* %23, %struct.stat* %6)
%25 = icmp ne i64 %24, 0
br i1 %25, label %26, label %33
26: ; preds = %20
%27 = load i32, i32* @stderr
%28 = load i8**, i8*** %5
%29 = getelementptr inbounds i8*, i8** %28, i64 1
%30 = load i8*, i8** %29
%31 = call i32 (i32, i8*, ...) @fprintf(i32 %27, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %30)
%32 = call i32 @exit(i32 1)
unreachable
33: ; preds = %20
%34 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%35 = load i32, i32* %34
store i32 %35, i32* %9
%36 = load i32, i32* %9
%37 = add nsw i32 %36, 1
%38 = call i64 @malloc(i32 %37)
%39 = inttoptr i64 %38 to i8*
store i8* %39, i8** %7
%40 = load i8*, i8** %7
%41 = icmp eq i8* %40, null
br i1 %41, label %42, label %47
42: ; preds = %33
%43 = load i32, i32* @stderr
%44 = load i32, i32* %9
%45 = call i32 (i32, i8*, ...) @fprintf(i32 %43, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.2, i64 0, i64 0), i32 %44)
%46 = call i32 @exit(i32 1)
unreachable
47: ; preds = %33
%48 = load i8**, i8*** %5
%49 = getelementptr inbounds i8*, i8** %48, i64 1
%50 = load i8*, i8** %49
%51 = load i32, i32* @O_RDONLY
%52 = call i32 (i8*, i32, ...) @open(i8* %50, i32 %51)
store i32 %52, i32* %10
%53 = load i32, i32* %10
%54 = load i8*, i8** %7
%55 = load i32, i32* %9
%56 = call i32 @read(i32 %53, i8* %54, i32 %55)
%57 = load i32, i32* %9
%58 = icmp ne i32 %56, %57
br i1 %58, label %59, label %66
59: ; preds = %47
%60 = load i32, i32* @stderr
%61 = load i8**, i8*** %5
%62 = getelementptr inbounds i8*, i8** %61, i64 1
%63 = load i8*, i8** %62
%64 = call i32 (i32, i8*, ...) @fprintf(i32 %60, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0), i8* %63)
%65 = call i32 @exit(i32 1)
unreachable
66: ; preds = %47
%67 = load i32, i32* %10
%68 = call i32 @close(i32 %67)
%69 = load i8*, i8** %7
store i8* %69, i8** %8
br label %70
70: ; preds = %87, %66
%71 = load i8*, i8** %8
%72 = load i8, i8* %71
%73 = sext i8 %72 to i32
%74 = and i32 %73, 128
%75 = icmp ne i32 %74, 0
br i1 %75, label %76, label %92
76: ; preds = %70
%77 = load i32, i32* %9
%78 = icmp eq i32 %77, 0
br i1 %78, label %79, label %86
79: ; preds = %76
%80 = load i32, i32* @stderr
%81 = load i8**, i8*** %5
%82 = getelementptr inbounds i8*, i8** %81, i64 1
%83 = load i8*, i8** %82
%84 = call i32 (i32, i8*, ...) @fprintf(i32 %80, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.4, i64 0, i64 0), i8* %83)
%85 = call i32 @exit(i32 1)
unreachable
86: ; preds = %76
br label %87
87: ; preds = %86
%88 = load i8*, i8** %8
%89 = getelementptr inbounds i8, i8* %88, i32 1
store i8* %89, i8** %8
%90 = load i32, i32* %9
%91 = add nsw i32 %90, -1
store i32 %91, i32* %9
br label %70
92: ; preds = %70
%93 = load i8**, i8*** %5
%94 = load i32, i32* %4
%95 = sub nsw i32 %94, 1
%96 = sext i32 %95 to i64
%97 = getelementptr inbounds i8*, i8** %93, i64 %96
%98 = load i8*, i8** %97
%99 = load i32, i32* @O_WRONLY
%100 = load i32, i32* @O_TRUNC
%101 = or i32 %99, %100
%102 = load i32, i32* @O_CREAT
%103 = or i32 %101, %102
%104 = call i32 (i8*, i32, ...) @open(i8* %98, i32 %103, i32 438)
store i32 %104, i32* %10
%105 = load i32, i32* %10
%106 = load i8*, i8** %8
%107 = load i32, i32* %9
%108 = call i32 @write(i32 %105, i8* %106, i32 %107)
%109 = load i32, i32* %10
%110 = call i32 @close(i32 %109)
%111 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@stderr = global i32 0
@.str = [42 x i8] c"Usage: prepare_financisto input [output]\0A\00"
@.str.1 = [19 x i8] c"Could not stat %s\0A\00"
@.str.2 = [43 x i8] c"Could not allocate %d bytes for file data\0A\00"
@O_RDONLY = global i32 0
@.str.3 = [31 x i8] c"Could not read all of file %s\0A\00"
@.str.4 = [34 x i8] c"No non-rubbish bytes found in %s\0A\00"
@O_WRONLY = global i32 0
@O_TRUNC = global i32 0
@O_CREAT = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = add i32 %0, -4
%6 = icmp ult i32 %5, -2
br i1 %6, label %7, label %11
7: ; preds = %2
%8 = load i32, i32* @stderr
%9 = tail call i32 (i32, i8*, ...) @fprintf(i32 %8, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0))
%10 = tail call i32 @exit(i32 1)
unreachable
11: ; preds = %2
%12 = getelementptr inbounds i8*, i8** %1, i64 1
%13 = load i8*, i8** %12
%14 = call i64 @stat(i8* %13, %struct.stat* %3)
%15 = icmp eq i64 %14, 0
br i1 %15, label %21, label %16
16: ; preds = %11
%17 = load i32, i32* @stderr
%18 = load i8*, i8** %12
%19 = call i32 (i32, i8*, ...) @fprintf(i32 %17, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %18)
%20 = call i32 @exit(i32 1)
unreachable
21: ; preds = %11
%22 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%23 = load i32, i32* %22
%24 = add nsw i32 %23, 1
%25 = call i64 @malloc(i32 %24)
%26 = inttoptr i64 %25 to i8*
%27 = icmp eq i64 %25, 0
br i1 %27, label %28, label %32
28: ; preds = %21
%29 = load i32, i32* @stderr
%30 = call i32 (i32, i8*, ...) @fprintf(i32 %29, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.2, i64 0, i64 0), i32 %23)
%31 = call i32 @exit(i32 1)
unreachable
32: ; preds = %21
%33 = load i8*, i8** %12
%34 = load i32, i32* @O_RDONLY
%35 = call i32 (i8*, i32, ...) @open(i8* %33, i32 %34)
%36 = call i32 @read(i32 %35, i8* %26, i32 %23)
%37 = icmp eq i32 %36, %23
br i1 %37, label %43, label %38
38: ; preds = %32
%39 = load i32, i32* @stderr
%40 = load i8*, i8** %12
%41 = call i32 (i32, i8*, ...) @fprintf(i32 %39, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0), i8* %40)
%42 = call i32 @exit(i32 1)
unreachable
43: ; preds = %32
%44 = call i32 @close(i32 %35)
%45 = load i8, i8* %26
%46 = icmp sgt i8 %45, -1
br i1 %46, label %61, label %47
47: ; preds = %43, %56
%48 = phi i32 [ %58, %56 ], [ %23, %43 ]
%49 = phi i8* [ %57, %56 ], [ %26, %43 ]
%50 = icmp eq i32 %48, 0
br i1 %50, label %51, label %56
51: ; preds = %47
%52 = load i32, i32* @stderr
%53 = load i8*, i8** %12
%54 = call i32 (i32, i8*, ...) @fprintf(i32 %52, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.4, i64 0, i64 0), i8* %53)
%55 = call i32 @exit(i32 1)
unreachable
56: ; preds = %47
%57 = getelementptr inbounds i8, i8* %49, i64 1
%58 = add nsw i32 %48, -1
%59 = load i8, i8* %57
%60 = icmp sgt i8 %59, -1
br i1 %60, label %61, label %47
61: ; preds = %56, %43
%62 = phi i8* [ %26, %43 ], [ %57, %56 ]
%63 = phi i32 [ %23, %43 ], [ %58, %56 ]
%64 = add nsw i32 %0, -1
%65 = zext i32 %64 to i64
%66 = getelementptr inbounds i8*, i8** %1, i64 %65
%67 = load i8*, i8** %66
%68 = load i32, i32* @O_WRONLY
%69 = load i32, i32* @O_TRUNC
%70 = or i32 %69, %68
%71 = load i32, i32* @O_CREAT
%72 = or i32 %70, %71
%73 = call i32 (i8*, i32, ...) @open(i8* %67, i32 %72, i32 438)
%74 = call i32 @write(i32 %73, i8* %62, i32 %63)
%75 = call i32 @close(i32 %73)
%76 = call i32 @exit(i32 0)
unreachable
}