#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*/ SEEK_END;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ compress_image(char *, long, char **, long *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fread(char *, long, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
long ftell(int /*<<< orphan*/ *);
int /*<<< orphan*/ fwrite(char *, long, int, int /*<<< orphan*/ *);
char getopt(int, char **, char *);
int /*<<< orphan*/ image_init();
char *malloc(long);
char *optarg;
int /*<<< orphan*/ printf(char *, long, long);
int main(int argc, char **argv) {
  char c = 0;
  char *filename = NULL;
  char *dst = NULL;
  while ((c = getopt(argc, argv, "f:o:")) != -1) {
    switch (c) {
    case 'f':
      filename = optarg;
      break;
    case 'o':
      dst = optarg;
      break;
    }
  }
  if (!filename)
    return -1;
  if (!dst)
    dst = "test.jpg";
  image_init();
  FILE *fp_in = fopen(filename, "r");
  if (!fp_in)
    return -1;
  fseek(fp_in, 0, SEEK_END);
  long int file_len = ftell(fp_in);
  fseek(fp_in, 0, SEEK_SET);
  char *buf = malloc(file_len);
  if (!buf)
    return -1;
  fread(buf, file_len, 1, fp_in);
  char *out_buf = NULL;
  long int out_len = 0;
  compress_image(buf, file_len, &out_buf, &out_len);
  printf("out_len: %ld   src_len: %ld\n", out_len, file_len);
  FILE *fp_out = fopen(dst, "w");
  fwrite(out_buf, out_len, 1, fp_out);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movb   $0x0,-0x11(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
lea    0xe28(%rip),%rdx
call   10b0 <getopt@plt>
mov    %al,-0x11(%rbp)
movsbl %al,%eax
cmp    $0xffffffff,%eax
je     1238 <main+0x98>
movsbl -0x11(%rbp),%eax
mov    %eax,-0x5c(%rbp)
sub    $0x66,%eax
je     1212 <main+0x72>
jmp    1201 <main+0x61>
mov    -0x5c(%rbp),%eax
sub    $0x6f,%eax
je     1225 <main+0x85>
jmp    1233 <main+0x93>
mov    0x2ddf(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x20(%rbp)
jmp    1233 <main+0x93>
mov    0x2dcc(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
jmp    11ca <main+0x2a>
cmpq   $0x0,-0x20(%rbp)
jne    124f <main+0xaf>
movl   $0xffffffff,-0x4(%rbp)
jmp    1370 <main+0x1d0>
cmpq   $0x0,-0x28(%rbp)
jne    1265 <main+0xc5>
lea    0xda4(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    $0x0,%al
call   1050 <image_init@plt>
mov    -0x20(%rbp),%rdi
lea    0xd97(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1297 <main+0xf7>
movl   $0xffffffff,-0x4(%rbp)
jmp    1370 <main+0x1d0>
mov    -0x30(%rbp),%rdi
mov    0x2d3e(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1090 <fseek@plt>
mov    -0x30(%rbp),%rdi
call   1070 <ftell@plt>
mov    %rax,-0x38(%rbp)
mov    -0x30(%rbp),%rdi
mov    0x2d0d(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1090 <fseek@plt>
mov    -0x38(%rbp),%rdi
call   1080 <malloc@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    12f0 <main+0x150>
movl   $0xffffffff,-0x4(%rbp)
jmp    1370 <main+0x1d0>
mov    -0x40(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    -0x30(%rbp),%rcx
mov    $0x1,%edx
call   1030 <fread@plt>
movq   $0x0,-0x48(%rbp)
movq   $0x0,-0x50(%rbp)
mov    -0x40(%rbp),%rdi
mov    -0x38(%rbp),%rsi
lea    -0x48(%rbp),%rdx
lea    -0x50(%rbp),%rcx
call   1060 <compress_image@plt>
mov    -0x50(%rbp),%rsi
mov    -0x38(%rbp),%rdx
lea    0xcd6(%rip),%rdi
call   1040 <printf@plt>
mov    -0x28(%rbp),%rdi
lea    0xce3(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x58(%rbp)
mov    -0x48(%rbp),%rdi
mov    -0x50(%rbp),%rsi
mov    -0x58(%rbp),%rcx
mov    $0x1,%edx
call   10c0 <fwrite@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%rbx
mov    %edi,%ebp
lea    0xe46(%rip),%rdx
call   10b0 <getopt@plt>
mov    $0xffffffff,%ecx
cmp    $0xff,%al
je     1316 <main+0x176>
xor    %r12d,%r12d
mov    0x2e22(%rip),%r15
lea    0xe23(%rip),%r14
xor    %r13d,%r13d
jmp    1204 <main+0x64>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    (%r15),%r13
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r14,%rdx
call   10b0 <getopt@plt>
cmp    $0xff,%al
je     1216 <main+0x76>
movsbl %al,%eax
cmp    $0x6f,%eax
je     11f0 <main+0x50>
cmp    $0x66,%eax
jne    11f3 <main+0x53>
mov    (%r15),%r12
jmp    11f3 <main+0x53>
test   %r12,%r12
je     1311 <main+0x171>
test   %r13,%r13
lea    0xddc(%rip),%r14
cmovne %r13,%r14
xor    %eax,%eax
call   1050 <image_init@plt>
lea    0xdd3(%rip),%rsi
mov    %r12,%rdi
call   10a0 <fopen@plt>
test   %rax,%rax
je     1311 <main+0x171>
mov    %rax,%rbp
mov    0x2d8a(%rip),%rax
mov    (%rax),%edx
mov    %rbp,%rdi
xor    %esi,%esi
call   1090 <fseek@plt>
mov    %rbp,%rdi
call   1070 <ftell@plt>
mov    %rax,%r12
mov    0x2d5c(%rip),%rax
mov    (%rax),%edx
mov    %rbp,%rdi
xor    %esi,%esi
call   1090 <fseek@plt>
mov    %r12,%rdi
call   1080 <malloc@plt>
test   %rax,%rax
mov    $0xffffffff,%ecx
je     1316 <main+0x176>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r12,%rsi
mov    $0x1,%edx
mov    %rbp,%rcx
call   1030 <fread@plt>
movq   $0x0,0x10(%rsp)
movq   $0x0,0x8(%rsp)
lea    0x10(%rsp),%rdx
lea    0x8(%rsp),%rcx
mov    %rbx,%rdi
mov    %r12,%rsi
call   1060 <compress_image@plt>
mov    0x8(%rsp),%rsi
lea    0xd31(%rip),%rdi
mov    %r12,%rdx
call   1040 <printf@plt>
lea    0xd3f(%rip),%rsi
mov    %r14,%rdi
call   10a0 <fopen@plt>
mov    0x10(%rsp),%rdi
mov    0x8(%rsp),%rsi
mov    $0x1,%edx
mov    %rax,%rcx
call   10c0 <fwrite@plt>
xor    %ecx,%ecx
jmp    1316 <main+0x176>
mov    $0xffffffff,%ecx
mov    %ecx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [5 x i8] c"f:o:\00"
@optarg = global i8* null
@.str.1 = [9 x i8] c"test.jpg\00"
@.str.2 = [2 x i8] c"r\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@.str.3 = [29 x i8] c"out_len: %ld src_len: %ld\0A\00"
@.str.4 = [2 x i8] c"w\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32*
%10 = alloca i64
%11 = alloca i8*
%12 = alloca i8*
%13 = alloca i64
%14 = alloca i32*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i8 0, i8* %6
store i8* null, i8** %7
store i8* null, i8** %8
br label %15
15: ; preds = %28, %2
%16 = load i32, i32* %4
%17 = load i8**, i8*** %5
%18 = call signext i8 @getopt(i32 %16, i8** %17, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i8 %18, i8* %6
%19 = sext i8 %18 to i32
%20 = icmp ne i32 %19, -1
br i1 %20, label %21, label %29
21: ; preds = %15
%22 = load i8, i8* %6
%23 = sext i8 %22 to i32
switch i32 %23, label %28 [
i32 102, label %24
i32 111, label %26
]
24: ; preds = %21
%25 = load i8*, i8** @optarg
store i8* %25, i8** %7
br label %28
26: ; preds = %21
%27 = load i8*, i8** @optarg
store i8* %27, i8** %8
br label %28
28: ; preds = %21, %26, %24
br label %15
29: ; preds = %15
%30 = load i8*, i8** %7
%31 = icmp ne i8* %30, null
br i1 %31, label %33, label %32
32: ; preds = %29
store i32 -1, i32* %3
br label %75
33: ; preds = %29
%34 = load i8*, i8** %8
%35 = icmp ne i8* %34, null
br i1 %35, label %37, label %36
36: ; preds = %33
store i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i8** %8
br label %37
37: ; preds = %36, %33
%38 = call i32 (...) @image_init()
%39 = load i8*, i8** %7
%40 = call i32* @fopen(i8* %39, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %40, i32** %9
%41 = load i32*, i32** %9
%42 = icmp ne i32* %41, null
br i1 %42, label %44, label %43
43: ; preds = %37
store i32 -1, i32* %3
br label %75
44: ; preds = %37
%45 = load i32*, i32** %9
%46 = load i32, i32* @SEEK_END
%47 = call i32 @fseek(i32* %45, i32 0, i32 %46)
%48 = load i32*, i32** %9
%49 = call i64 @ftell(i32* %48)
store i64 %49, i64* %10
%50 = load i32*, i32** %9
%51 = load i32, i32* @SEEK_SET
%52 = call i32 @fseek(i32* %50, i32 0, i32 %51)
%53 = load i64, i64* %10
%54 = call i8* @malloc(i64 %53)
store i8* %54, i8** %11
%55 = load i8*, i8** %11
%56 = icmp ne i8* %55, null
br i1 %56, label %58, label %57
57: ; preds = %44
store i32 -1, i32* %3
br label %75
58: ; preds = %44
%59 = load i8*, i8** %11
%60 = load i64, i64* %10
%61 = load i32*, i32** %9
%62 = call i32 @fread(i8* %59, i64 %60, i32 1, i32* %61)
store i8* null, i8** %12
store i64 0, i64* %13
%63 = load i8*, i8** %11
%64 = load i64, i64* %10
%65 = call i32 @compress_image(i8* %63, i64 %64, i8** %12, i64* %13)
%66 = load i64, i64* %13
%67 = load i64, i64* %10
%68 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0), i64 %66, i64 %67)
%69 = load i8*, i8** %8
%70 = call i32* @fopen(i8* %69, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
store i32* %70, i32** %14
%71 = load i8*, i8** %12
%72 = load i64, i64* %13
%73 = load i32*, i32** %14
%74 = call i32 @fwrite(i8* %71, i64 %72, i32 1, i32* %73)
store i32 0, i32* %3
br label %75
75: ; preds = %58, %57, %43, %32
%76 = load i32, i32* %3
ret i32 %76
}

LLVM IR (-O2):
@.str = [5 x i8] c"f:o:\00"
@optarg = global i8* null
@.str.1 = [9 x i8] c"test.jpg\00"
@.str.2 = [2 x i8] c"r\00"
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
@.str.3 = [29 x i8] c"out_len: %ld src_len: %ld\0A\00"
@.str.4 = [2 x i8] c"w\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i8*
%4 = alloca i64
%5 = tail call signext i8 @getopt(i32 %0, i8** %1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i8 %5, -1
br i1 %6, label %48, label %7
7: ; preds = %2, %16
%8 = phi i8 [ %19, %16 ], [ %5, %2 ]
%9 = phi i8* [ %18, %16 ], [ null, %2 ]
%10 = phi i8* [ %17, %16 ], [ null, %2 ]
%11 = sext i8 %8 to i32
switch i32 %11, label %16 [
i32 102, label %12
i32 111, label %14
]
12: ; preds = %7
%13 = load i8*, i8** @optarg
br label %16
14: ; preds = %7
%15 = load i8*, i8** @optarg
br label %16
16: ; preds = %7, %14, %12
%17 = phi i8* [ %10, %7 ], [ %15, %14 ], [ %10, %12 ]
%18 = phi i8* [ %9, %7 ], [ %9, %14 ], [ %13, %12 ]
%19 = tail call signext i8 @getopt(i32 %0, i8** %1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%20 = icmp eq i8 %19, -1
br i1 %20, label %21, label %7
21: ; preds = %16
%22 = icmp eq i8* %18, null
br i1 %22, label %48, label %23
23: ; preds = %21
%24 = icmp eq i8* %17, null
%25 = select i1 %24, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i8* %17
%26 = tail call i32 (...) @image_init()
%27 = tail call i32* @fopen(i8* %18, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%28 = icmp eq i32* %27, null
br i1 %28, label %48, label %29
29: ; preds = %23
%30 = load i32, i32* @SEEK_END
%31 = tail call i32 @fseek(i32* %27, i32 0, i32 %30)
%32 = tail call i64 @ftell(i32* %27)
%33 = load i32, i32* @SEEK_SET
%34 = tail call i32 @fseek(i32* %27, i32 0, i32 %33)
%35 = tail call i8* @malloc(i64 %32)
%36 = icmp eq i8* %35, null
br i1 %36, label %48, label %37
37: ; preds = %29
%38 = tail call i32 @fread(i8* %35, i64 %32, i32 1, i32* %27)
%39 = bitcast i8** %3 to i8*
store i8* null, i8** %3
%40 = bitcast i64* %4 to i8*
store i64 0, i64* %4
%41 = call i32 @compress_image(i8* %35, i64 %32, i8** %3, i64* %4)
%42 = load i64, i64* %4
%43 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0), i64 %42, i64 %32)
%44 = call i32* @fopen(i8* %25, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%45 = load i8*, i8** %3
%46 = load i64, i64* %4
%47 = call i32 @fwrite(i8* %45, i64 %46, i32 1, i32* %44)
br label %48
48: ; preds = %2, %23, %29, %37, %21
%49 = phi i32 [ -1, %21 ], [ -1, %23 ], [ 0, %37 ], [ -1, %29 ], [ -1, %2 ]
ret i32 %49
}