#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 */
unsigned long LoadBLP(char *, char *, long *, long *, int /*<<< orphan*/ *,
                      int /*<<< orphan*/ *);
unsigned long MakeJPG(char *, char *, long, long, int);
unsigned long MakeTGA(char *, char *, long, long, int);
int O_CREAT;
int O_RDWR;
int O_TRUNC;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ free(char *);
scalar_t__ isFileOfType(char const *, char *);
char *malloc(unsigned long);
int open(char const *, int, int);
long pwrite(int, char *, unsigned long, int /*<<< orphan*/);
int BLP2Everything(char *srcbuf, unsigned long srcbuflen,
                   const char *szFileDest) {
  int hFile;
  long r, w, h;
  char *rawImageData = NULL;
  unsigned long rawImageSize = 0;
  char *processedImageData = NULL;
  unsigned long processedImageSize = 0;
  rawImageSize = LoadBLP(NULL, srcbuf, &w, &h, NULL, NULL);
  rawImageData = (char *)malloc(rawImageSize);
  LoadBLP(rawImageData, srcbuf, &w, &h, NULL, NULL);
  int quality = 100;
  if (isFileOfType(szFileDest, "jpg")) {
    processedImageSize = MakeJPG(NULL, rawImageData, w, h, quality);
    processedImageData = malloc(processedImageSize);
    MakeJPG(processedImageData, rawImageData, w, h, quality);
  } else if (isFileOfType(szFileDest, "tga")) {
    processedImageSize = MakeTGA(NULL, rawImageData, w, h, 32);
    processedImageData = malloc(processedImageSize);
    MakeTGA(processedImageData, rawImageData, w, h, 32);
  } else {
    free(rawImageData);
    return 0;
  }
  free(rawImageData);
  hFile = open(szFileDest, O_RDWR | O_CREAT | O_TRUNC, 0644);
  if (hFile == -1)
    return 0;
  r = pwrite(hFile, processedImageData, processedImageSize, 0);
  close(hFile);
  free(processedImageData);
  return 1;
}


x86 assembly (clang, -O0):
<BLP2Everything>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movq   $0x0,-0x48(%rbp)
movq   $0x0,-0x50(%rbp)
movq   $0x0,-0x58(%rbp)
movq   $0x0,-0x60(%rbp)
mov    -0x10(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%r9d
lea    -0x38(%rbp),%rdx
lea    -0x40(%rbp),%rcx
mov    %r9,%rdi
mov    %r9,%r8
call   1050 <LoadBLP@plt>
mov    %rax,-0x50(%rbp)
mov    -0x50(%rbp),%rdi
call   1060 <malloc@plt>
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rdi
mov    -0x10(%rbp),%rsi
lea    -0x38(%rbp),%rdx
lea    -0x40(%rbp),%rcx
xor    %eax,%eax
mov    %eax,%r9d
mov    %r9,%r8
call   1050 <LoadBLP@plt>
movl   $0x64,-0x64(%rbp)
mov    -0x20(%rbp),%rdi
lea    0xde0(%rip),%rsi
call   10b0 <isFileOfType@plt>
cmp    $0x0,%rax
je     1277 <BLP2Everything+0xe7>
mov    -0x48(%rbp),%rsi
mov    -0x38(%rbp),%rdx
mov    -0x40(%rbp),%rcx
mov    -0x64(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
call   1070 <MakeJPG@plt>
mov    %rax,-0x60(%rbp)
mov    -0x60(%rbp),%rdi
call   1060 <malloc@plt>
mov    %rax,-0x58(%rbp)
mov    -0x58(%rbp),%rdi
mov    -0x48(%rbp),%rsi
mov    -0x38(%rbp),%rdx
mov    -0x40(%rbp),%rcx
mov    -0x64(%rbp),%r8d
call   1070 <MakeJPG@plt>
jmp    12f7 <BLP2Everything+0x167>
mov    -0x20(%rbp),%rdi
lea    0xd82(%rip),%rsi
call   10b0 <isFileOfType@plt>
cmp    $0x0,%rax
je     12dd <BLP2Everything+0x14d>
mov    -0x48(%rbp),%rsi
mov    -0x38(%rbp),%rdx
mov    -0x40(%rbp),%rcx
xor    %eax,%eax
mov    %eax,%edi
mov    $0x20,%r8d
call   10a0 <MakeTGA@plt>
mov    %rax,-0x60(%rbp)
mov    -0x60(%rbp),%rdi
call   1060 <malloc@plt>
mov    %rax,-0x58(%rbp)
mov    -0x58(%rbp),%rdi
mov    -0x48(%rbp),%rsi
mov    -0x38(%rbp),%rdx
mov    -0x40(%rbp),%rcx
mov    $0x20,%r8d
call   10a0 <MakeTGA@plt>
jmp    12f2 <BLP2Everything+0x162>
mov    -0x48(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
jmp    1370 <BLP2Everything+0x1e0>
jmp    12f7 <BLP2Everything+0x167>
mov    -0x48(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rdi
mov    0x2ccd(%rip),%rax
mov    (%rax),%esi
mov    0x2ce4(%rip),%rax
or     (%rax),%esi
mov    0x2cd3(%rip),%rax
or     (%rax),%esi
mov    $0x1a4,%edx
call   1090 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    1342 <BLP2Everything+0x1b2>
movl   $0x0,-0x4(%rbp)
jmp    1370 <BLP2Everything+0x1e0>
mov    -0x24(%rbp),%edi
mov    -0x58(%rbp),%rsi
mov    -0x60(%rbp),%rdx
xor    %ecx,%ecx
call   1080 <pwrite@plt>
mov    %rax,-0x30(%rbp)
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
mov    -0x58(%rbp),%rdi
call   1030 <free@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<BLP2Everything>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%r14
mov    %rdi,%rbx
xor    %r12d,%r12d
lea    0x10(%rsp),%r15
lea    0x8(%rsp),%rbp
xor    %edi,%edi
mov    %rbx,%rsi
mov    %r15,%rdx
mov    %rbp,%rcx
xor    %r8d,%r8d
xor    %r9d,%r9d
call   1050 <LoadBLP@plt>
mov    %rax,%rdi
call   1060 <malloc@plt>
mov    %rax,%r13
mov    %rax,%rdi
mov    %rbx,%rsi
mov    %r15,%rdx
mov    %rbp,%rcx
xor    %r8d,%r8d
xor    %r9d,%r9d
call   1050 <LoadBLP@plt>
lea    0xe10(%rip),%rsi
mov    %r14,%rdi
call   10b0 <isFileOfType@plt>
test   %rax,%rax
je     1242 <BLP2Everything+0xb2>
mov    0x10(%rsp),%rdx
mov    0x8(%rsp),%rcx
xor    %edi,%edi
mov    %r13,%rsi
mov    $0x64,%r8d
call   1070 <MakeJPG@plt>
mov    %rax,%r12
mov    %rax,%rdi
call   1060 <malloc@plt>
mov    %rax,%r15
mov    0x10(%rsp),%rdx
mov    0x8(%rsp),%rcx
mov    %rax,%rdi
mov    %r13,%rsi
mov    $0x64,%r8d
call   1070 <MakeJPG@plt>
jmp    129d <BLP2Everything+0x10d>
lea    0xdbb(%rip),%rsi
mov    %r14,%rdi
call   10b0 <isFileOfType@plt>
test   %rax,%rax
je     12f7 <BLP2Everything+0x167>
mov    0x10(%rsp),%rdx
mov    0x8(%rsp),%rcx
xor    %edi,%edi
mov    %r13,%rsi
mov    $0x20,%r8d
call   10a0 <MakeTGA@plt>
mov    %rax,%r12
mov    %rax,%rdi
call   1060 <malloc@plt>
mov    %rax,%r15
mov    0x10(%rsp),%rdx
mov    0x8(%rsp),%rcx
mov    %rax,%rdi
mov    %r13,%rsi
mov    $0x20,%r8d
call   10a0 <MakeTGA@plt>
mov    %r13,%rdi
call   1030 <free@plt>
mov    0x2d2c(%rip),%rax
mov    0x2d45(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2d32(%rip),%rax
or     (%rax),%esi
mov    %r14,%rdi
mov    $0x1a4,%edx
call   1090 <open@plt>
cmp    $0xffffffff,%eax
je     12f2 <BLP2Everything+0x162>
mov    %eax,%ebx
mov    %eax,%edi
mov    %r15,%rsi
mov    %r12,%rdx
xor    %ecx,%ecx
call   1080 <pwrite@plt>
mov    %ebx,%edi
call   1040 <close@plt>
mov    $0x1,%r12d
jmp    12fa <BLP2Everything+0x16a>
xor    %r12d,%r12d
jmp    1302 <BLP2Everything+0x172>
mov    %r13,%r15
mov    %r15,%rdi
call   1030 <free@plt>
mov    %r12d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [4 x i8] c"jpg\00"
@.str.1 = [4 x i8] c"tga\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
define i32 @BLP2Everything(i8* %0, i64 %1, i8* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i64
%10 = alloca i64
%11 = alloca i64
%12 = alloca i8*
%13 = alloca i64
%14 = alloca i8*
%15 = alloca i64
%16 = alloca i32
store i8* %0, i8** %5
store i64 %1, i64* %6
store i8* %2, i8** %7
store i8* null, i8** %12
store i64 0, i64* %13
store i8* null, i8** %14
store i64 0, i64* %15
%17 = load i8*, i8** %5
%18 = call i64 @LoadBLP(i8* null, i8* %17, i64* %10, i64* %11, i32* null, i32* null)
store i64 %18, i64* %13
%19 = load i64, i64* %13
%20 = call i8* @malloc(i64 %19)
store i8* %20, i8** %12
%21 = load i8*, i8** %12
%22 = load i8*, i8** %5
%23 = call i64 @LoadBLP(i8* %21, i8* %22, i64* %10, i64* %11, i32* null, i32* null)
store i32 100, i32* %16
%24 = load i8*, i8** %7
%25 = call i64 @isFileOfType(i8* %24, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%26 = icmp ne i64 %25, 0
br i1 %26, label %27, label %41
27: ; preds = %3
%28 = load i8*, i8** %12
%29 = load i64, i64* %10
%30 = load i64, i64* %11
%31 = load i32, i32* %16
%32 = call i64 @MakeJPG(i8* null, i8* %28, i64 %29, i64 %30, i32 %31)
store i64 %32, i64* %15
%33 = load i64, i64* %15
%34 = call i8* @malloc(i64 %33)
store i8* %34, i8** %14
%35 = load i8*, i8** %14
%36 = load i8*, i8** %12
%37 = load i64, i64* %10
%38 = load i64, i64* %11
%39 = load i32, i32* %16
%40 = call i64 @MakeJPG(i8* %35, i8* %36, i64 %37, i64 %38, i32 %39)
br label %61
41: ; preds = %3
%42 = load i8*, i8** %7
%43 = call i64 @isFileOfType(i8* %42, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%44 = icmp ne i64 %43, 0
br i1 %44, label %45, label %57
45: ; preds = %41
%46 = load i8*, i8** %12
%47 = load i64, i64* %10
%48 = load i64, i64* %11
%49 = call i64 @MakeTGA(i8* null, i8* %46, i64 %47, i64 %48, i32 32)
store i64 %49, i64* %15
%50 = load i64, i64* %15
%51 = call i8* @malloc(i64 %50)
store i8* %51, i8** %14
%52 = load i8*, i8** %14
%53 = load i8*, i8** %12
%54 = load i64, i64* %10
%55 = load i64, i64* %11
%56 = call i64 @MakeTGA(i8* %52, i8* %53, i64 %54, i64 %55, i32 32)
br label %60
57: ; preds = %41
%58 = load i8*, i8** %12
%59 = call i32 @free(i8* %58)
store i32 0, i32* %4
br label %83
60: ; preds = %45
br label %61
61: ; preds = %60, %27
%62 = load i8*, i8** %12
%63 = call i32 @free(i8* %62)
%64 = load i8*, i8** %7
%65 = load i32, i32* @O_RDWR
%66 = load i32, i32* @O_CREAT
%67 = or i32 %65, %66
%68 = load i32, i32* @O_TRUNC
%69 = or i32 %67, %68
%70 = call i32 @open(i8* %64, i32 %69, i32 420)
store i32 %70, i32* %8
%71 = load i32, i32* %8
%72 = icmp eq i32 %71, -1
br i1 %72, label %73, label %74
73: ; preds = %61
store i32 0, i32* %4
br label %83
74: ; preds = %61
%75 = load i32, i32* %8
%76 = load i8*, i8** %14
%77 = load i64, i64* %15
%78 = call i64 @pwrite(i32 %75, i8* %76, i64 %77, i32 0)
store i64 %78, i64* %9
%79 = load i32, i32* %8
%80 = call i32 @close(i32 %79)
%81 = load i8*, i8** %14
%82 = call i32 @free(i8* %81)
store i32 1, i32* %4
br label %83
83: ; preds = %74, %73, %57
%84 = load i32, i32* %4
ret i32 %84
}

LLVM IR (-O2):
@.str = [4 x i8] c"jpg\00"
@.str.1 = [4 x i8] c"tga\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
define i32 @BLP2Everything(i8* %0, i64 %1, i8* %2) {
%4 = alloca i64
%5 = alloca i64
%6 = bitcast i64* %4 to i8*
%7 = bitcast i64* %5 to i8*
%8 = call i64 @LoadBLP(i8* null, i8* %0, i64* %4, i64* %5, i32* null, i32* null)
%9 = call i8* @malloc(i64 %8)
%10 = call i64 @LoadBLP(i8* %9, i8* %0, i64* %4, i64* %5, i32* null, i32* null)
%11 = call i64 @isFileOfType(i8* %2, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%12 = icmp eq i64 %11, 0
br i1 %12, label %21, label %13
13: ; preds = %3
%14 = load i64, i64* %4
%15 = load i64, i64* %5
%16 = call i64 @MakeJPG(i8* null, i8* %9, i64 %14, i64 %15, i32 100)
%17 = call i8* @malloc(i64 %16)
%18 = load i64, i64* %4
%19 = load i64, i64* %5
%20 = call i64 @MakeJPG(i8* %17, i8* %9, i64 %18, i64 %19, i32 100)
br label %32
21: ; preds = %3
%22 = call i64 @isFileOfType(i8* %2, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%23 = icmp eq i64 %22, 0
br i1 %23, label %46, label %24
24: ; preds = %21
%25 = load i64, i64* %4
%26 = load i64, i64* %5
%27 = call i64 @MakeTGA(i8* null, i8* %9, i64 %25, i64 %26, i32 32)
%28 = call i8* @malloc(i64 %27)
%29 = load i64, i64* %4
%30 = load i64, i64* %5
%31 = call i64 @MakeTGA(i8* %28, i8* %9, i64 %29, i64 %30, i32 32)
br label %32
32: ; preds = %24, %13
%33 = phi i8* [ %17, %13 ], [ %28, %24 ]
%34 = phi i64 [ %16, %13 ], [ %27, %24 ]
%35 = call i32 @free(i8* %9)
%36 = load i32, i32* @O_RDWR
%37 = load i32, i32* @O_CREAT
%38 = or i32 %37, %36
%39 = load i32, i32* @O_TRUNC
%40 = or i32 %38, %39
%41 = call i32 @open(i8* %2, i32 %40, i32 420)
%42 = icmp eq i32 %41, -1
br i1 %42, label %50, label %43
43: ; preds = %32
%44 = call i64 @pwrite(i32 %41, i8* %33, i64 %34, i32 0)
%45 = call i32 @close(i32 %41)
br label %46
46: ; preds = %21, %43
%47 = phi i8* [ %33, %43 ], [ %9, %21 ]
%48 = phi i32 [ 1, %43 ], [ 0, %21 ]
%49 = call i32 @free(i8* %47)
br label %50
50: ; preds = %46, %32
%51 = phi i32 [ 0, %32 ], [ %48, %46 ]
ret i32 %51
}