#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 /*<<< orphan*/ st_size;
};
typedef int ssize_t;

/* Variables and functions */
int BLP2Everything(void *, size_t, char const *);
int /*<<< orphan*/ O_RDONLY;
int fstat(int, struct stat *);
void *malloc(size_t);
int open(char const *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
int read(int, void *, size_t);
int main(int argc, char const *argv[]) {
  if (argc < 3) {
    printf(
        "usage: blpconvert <input file path> <output file path>\n\nAvailable "
        "output formats:\n    .tga (Targa)\n    .jpg (JPEG)\n");
    return 1;
  }
  int blpfd = open(argv[1], O_RDONLY);
  if (blpfd == -1) {
    printf("error: unable to open the input file\n");
    return 1;
  }
  struct stat sb;
  int result = fstat(blpfd, &sb);
  if (result == -1) {
    printf("error: unable to stat the input file\n");
    return 1;
  }
  size_t input_size = (size_t)(sb.st_size);
  void *input_data = malloc(input_size);
  if (input_data == NULL) {
    printf("error: unable to malloc the input file buffer\n");
    return 1;
  }
  ssize_t bytes_read = read(blpfd, input_data, input_size);
  if (bytes_read == -1) {
    printf("error: unable to read the input file\n");
    return 1;
  }
  result = BLP2Everything(input_data, input_size, argv[2]);
  if (result == 0) {
    printf("error: unable to convert the input file to the desired output "
           "format\n");
    return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
jge    1198 <main+0x38>
lea    0xe79(%rip),%rdi
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12b2 <main+0x152>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e41(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11d3 <main+0x73>
lea    0xeb2(%rip),%rdi
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12b2 <main+0x152>
mov    -0x14(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   1080 <fstat@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    1204 <main+0xa4>
lea    0xea7(%rip),%rdi
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12b2 <main+0x152>
movslq -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
call   1060 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    123c <main+0xdc>
lea    0xe95(%rip),%rdi
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12b2 <main+0x152>
mov    -0x14(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1050 <read@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0xffffffff,-0x34(%rbp)
jne    1271 <main+0x111>
lea    0xe8f(%rip),%rdi
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12b2 <main+0x152>
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdx
call   1030 <BLP2Everything@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jne    12ab <main+0x14b>
lea    0xe7b(%rip),%rdi
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12b2 <main+0x152>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
cmp    $0x2,%edi
jg     1178 <main+0x18>
lea    0xf6f(%rip),%rdi
jmp    120b <main+0xab>
mov    %rsi,%rbx
mov    0x8(%rsi),%rdi
mov    0x2e62(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     11e9 <main+0x89>
mov    %eax,%ebp
mov    %rsp,%rsi
mov    %eax,%edi
call   1080 <fstat@plt>
cmp    $0xffffffff,%eax
je     11f2 <main+0x92>
movslq (%rsp),%r14
mov    %r14,%rdi
call   1060 <malloc@plt>
test   %rax,%rax
je     11fb <main+0x9b>
mov    %rax,%r15
mov    %ebp,%edi
mov    %rax,%rsi
mov    %r14,%rdx
call   1050 <read@plt>
cmp    $0xffffffff,%eax
je     1204 <main+0xa4>
mov    0x10(%rbx),%rdx
mov    %r15,%rdi
mov    %r14,%rsi
call   1030 <BLP2Everything@plt>
mov    %eax,%ecx
xor    %eax,%eax
test   %ecx,%ecx
jne    1215 <main+0xb5>
lea    0xe19(%rip),%rdi
jmp    120b <main+0xab>
lea    0xecd(%rip),%rdi
jmp    120b <main+0xab>
lea    0xe9f(%rip),%rdi
jmp    120b <main+0xab>
lea    0xe68(%rip),%rdi
jmp    120b <main+0xab>
lea    0xe3a(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [116 x i8] c"usage: blpconvert <input file path> <output file path>\0A\0AAvailable output formats:\0A .tga (Targa)\0A .jpg (JPEG)\0A\00"
@O_RDONLY = global i32 0
@.str.1 = [38 x i8] c"error: unable to open the input file\0A\00"
@.str.2 = [38 x i8] c"error: unable to stat the input file\0A\00"
@.str.3 = [47 x i8] c"error: unable to malloc the input file buffer\0A\00"
@.str.4 = [38 x i8] c"error: unable to read the input file\0A\00"
@.str.5 = [70 x i8] c"error: unable to convert the input file to the desired output format\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca %struct.stat
%8 = alloca i32
%9 = alloca i64
%10 = alloca i8*
%11 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = load i32, i32* %4
%13 = icmp slt i32 %12, 3
br i1 %13, label %14, label %16
14: ; preds = %2
%15 = call i32 @printf(i8* getelementptr inbounds ([116 x i8], [116 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %64
16: ; preds = %2
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 1
%19 = load i8*, i8** %18
%20 = load i32, i32* @O_RDONLY
%21 = call i32 @open(i8* %19, i32 %20)
store i32 %21, i32* %6
%22 = load i32, i32* %6
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %26
24: ; preds = %16
%25 = call i32 @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %3
br label %64
26: ; preds = %16
%27 = load i32, i32* %6
%28 = call i32 @fstat(i32 %27, %struct.stat* %7)
store i32 %28, i32* %8
%29 = load i32, i32* %8
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %33
31: ; preds = %26
%32 = call i32 @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %3
br label %64
33: ; preds = %26
%34 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%35 = load i32, i32* %34
%36 = sext i32 %35 to i64
store i64 %36, i64* %9
%37 = load i64, i64* %9
%38 = call i8* @malloc(i64 %37)
store i8* %38, i8** %10
%39 = load i8*, i8** %10
%40 = icmp eq i8* %39, null
br i1 %40, label %41, label %43
41: ; preds = %33
%42 = call i32 @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %3
br label %64
43: ; preds = %33
%44 = load i32, i32* %6
%45 = load i8*, i8** %10
%46 = load i64, i64* %9
%47 = call i32 @read(i32 %44, i8* %45, i64 %46)
store i32 %47, i32* %11
%48 = load i32, i32* %11
%49 = icmp eq i32 %48, -1
br i1 %49, label %50, label %52
50: ; preds = %43
%51 = call i32 @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.4, i64 0, i64 0))
store i32 1, i32* %3
br label %64
52: ; preds = %43
%53 = load i8*, i8** %10
%54 = load i64, i64* %9
%55 = load i8**, i8*** %5
%56 = getelementptr inbounds i8*, i8** %55, i64 2
%57 = load i8*, i8** %56
%58 = call i32 @BLP2Everything(i8* %53, i64 %54, i8* %57)
store i32 %58, i32* %8
%59 = load i32, i32* %8
%60 = icmp eq i32 %59, 0
br i1 %60, label %61, label %63
61: ; preds = %52
%62 = call i32 @printf(i8* getelementptr inbounds ([70 x i8], [70 x i8]* @.str.5, i64 0, i64 0))
store i32 1, i32* %3
br label %64
63: ; preds = %52
store i32 0, i32* %3
br label %64
64: ; preds = %63, %61, %50, %41, %31, %24, %14
%65 = load i32, i32* %3
ret i32 %65
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@O_RDONLY = global i32 0
@str = [69 x i8] c"error: unable to convert the input file to the desired output format\00"
@str.6 = [37 x i8] c"error: unable to read the input file\00"
@str.7 = [46 x i8] c"error: unable to malloc the input file buffer\00"
@str.8 = [37 x i8] c"error: unable to stat the input file\00"
@str.9 = [37 x i8] c"error: unable to open the input file\00"
@str.10 = [115 x i8] c"usage: blpconvert <input file path> <output file path>\0A\0AAvailable output formats:\0A .tga (Targa)\0A .jpg (JPEG)\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.stat
%4 = icmp slt i32 %0, 3
br i1 %4, label %5, label %7
5: ; preds = %2
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([115 x i8], [115 x i8]* @str.10, i64 0, i64 0))
br label %38
7: ; preds = %2
%8 = getelementptr inbounds i8*, i8** %1, i64 1
%9 = load i8*, i8** %8
%10 = load i32, i32* @O_RDONLY
%11 = tail call i32 @open(i8* %9, i32 %10)
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %15
13: ; preds = %7
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @str.9, i64 0, i64 0))
br label %38
15: ; preds = %7
%16 = bitcast %struct.stat* %3 to i8*
%17 = call i32 @fstat(i32 %11, %struct.stat* %3)
%18 = icmp eq i32 %17, -1
br i1 %18, label %33, label %19
19: ; preds = %15
%20 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%21 = load i32, i32* %20
%22 = sext i32 %21 to i64
%23 = tail call i8* @malloc(i64 %22)
%24 = icmp eq i8* %23, null
br i1 %24, label %33, label %25
25: ; preds = %19
%26 = tail call i32 @read(i32 %11, i8* %23, i64 %22)
%27 = icmp eq i32 %26, -1
br i1 %27, label %33, label %28
28: ; preds = %25
%29 = getelementptr inbounds i8*, i8** %1, i64 2
%30 = load i8*, i8** %29
%31 = tail call i32 @BLP2Everything(i8* %23, i64 %22, i8* %30)
%32 = icmp eq i32 %31, 0
br i1 %32, label %33, label %36
33: ; preds = %28, %25, %19, %15
%34 = phi i8* [ getelementptr inbounds ([37 x i8], [37 x i8]* @str.8, i64 0, i64 0), %15 ], [ getelementptr inbounds ([46 x i8], [46 x i8]* @str.7, i64 0, i64 0), %19 ], [ getelementptr inbounds ([37 x i8], [37 x i8]* @str.6, i64 0, i64 0), %25 ], [ getelementptr inbounds ([69 x i8], [69 x i8]* @str, i64 0, i64 0), %28 ]
%35 = tail call i32 @puts(i8* dereferenceable(1) %34)
br label %36
36: ; preds = %33, %28
%37 = phi i32 [ 0, %28 ], [ 1, %33 ]
br label %38
38: ; preds = %13, %36, %5
%39 = phi i32 [ 1, %5 ], [ 1, %13 ], [ %37, %36 ]
ret i32 %39
}