#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;
scalar_t__ atoi(char *);
int /*<<< orphan*/ compress_image(char *, long, char **, long *, int);
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, ...);
int main(int argc, char **argv) {
  char c = 0;
  char *filename = NULL;
  char *dst = NULL;
  int qf = 75;
  while ((c = getopt(argc, argv, "f:o:q:")) != -1) {
    switch (c) {
    case 'f':
      filename = optarg;
      break;
    case 'o':
      dst = optarg;
      break;
    case 'q':
      qf = (int *)atoi(optarg);
      break;
    }
  }
  printf("qf: %ld\n", qf);
  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, qf);
  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    $0x70,%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)
movl   $0x4b,-0x2c(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
lea    0xe11(%rip),%rdx
call   10b0 <getopt@plt>
mov    %al,-0x11(%rbp)
movsbl %al,%eax
cmp    $0xffffffff,%eax
je     1277 <main+0xc7>
movsbl -0x11(%rbp),%eax
mov    %eax,-0x64(%rbp)
sub    $0x66,%eax
je     123a <main+0x8a>
jmp    1218 <main+0x68>
mov    -0x64(%rbp),%eax
sub    $0x6f,%eax
je     124d <main+0x9d>
jmp    1229 <main+0x79>
mov    -0x64(%rbp),%eax
sub    $0x71,%eax
je     1260 <main+0xb0>
jmp    1272 <main+0xc2>
mov    0x2db7(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x20(%rbp)
jmp    1272 <main+0xc2>
mov    0x2da4(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
jmp    1272 <main+0xc2>
mov    0x2d91(%rip),%rax
mov    (%rax),%rdi
call   10c0 <atoi@plt>
mov    %eax,-0x2c(%rbp)
jmp    11e1 <main+0x31>
movslq -0x2c(%rbp),%rsi
lea    0xd85(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
cmpq   $0x0,-0x20(%rbp)
jne    12a0 <main+0xf0>
movl   $0xffffffff,-0x4(%rbp)
jmp    13c7 <main+0x217>
cmpq   $0x0,-0x28(%rbp)
jne    12b6 <main+0x106>
lea    0xd5e(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    $0x0,%al
call   1050 <image_init@plt>
mov    -0x20(%rbp),%rdi
lea    0xd51(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    12e8 <main+0x138>
movl   $0xffffffff,-0x4(%rbp)
jmp    13c7 <main+0x217>
mov    -0x38(%rbp),%rdi
mov    0x2ced(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1090 <fseek@plt>
mov    -0x38(%rbp),%rdi
call   1070 <ftell@plt>
mov    %rax,-0x40(%rbp)
mov    -0x38(%rbp),%rdi
mov    0x2cbc(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1090 <fseek@plt>
mov    -0x40(%rbp),%rdi
call   1080 <malloc@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
jne    1341 <main+0x191>
movl   $0xffffffff,-0x4(%rbp)
jmp    13c7 <main+0x217>
mov    -0x48(%rbp),%rdi
mov    -0x40(%rbp),%rsi
mov    -0x38(%rbp),%rcx
mov    $0x1,%edx
call   1030 <fread@plt>
movq   $0x0,-0x50(%rbp)
movq   $0x0,-0x58(%rbp)
mov    -0x48(%rbp),%rdi
mov    -0x40(%rbp),%rsi
mov    -0x2c(%rbp),%r8d
lea    -0x50(%rbp),%rdx
lea    -0x58(%rbp),%rcx
call   1060 <compress_image@plt>
mov    -0x58(%rbp),%rsi
mov    -0x40(%rbp),%rdx
lea    0xc8c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x28(%rbp),%rdi
lea    0xc97(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x60(%rbp)
mov    -0x50(%rbp),%rdi
mov    -0x58(%rbp),%rsi
mov    -0x60(%rbp),%rcx
mov    $0x1,%edx
call   10d0 <fwrite@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%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    0xe36(%rip),%rdx
call   10b0 <getopt@plt>
cmp    $0xff,%al
je     1359 <main+0x1a9>
mov    $0x4b,%r15d
xor    %ecx,%ecx
mov    %rcx,0x8(%rsp)
mov    0x2e0d(%rip),%r14
lea    0xe0e(%rip),%r12
xor    %r13d,%r13d
jmp    121c <main+0x6c>
nopw   0x0(%rax,%rax,1)
mov    (%r14),%rdi
call   10c0 <atoi@plt>
mov    %rax,%r15
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r12,%rdx
call   10b0 <getopt@plt>
cmp    $0xff,%al
je     1245 <main+0x95>
movsbl %al,%eax
cmp    $0x71,%eax
je     1200 <main+0x50>
cmp    $0x6f,%eax
je     1240 <main+0x90>
cmp    $0x66,%eax
jne    120b <main+0x5b>
mov    (%r14),%rax
mov    %rax,0x8(%rsp)
jmp    120b <main+0x5b>
nopl   0x0(%rax,%rax,1)
mov    (%r14),%r13
jmp    120b <main+0x5b>
mov    %r15,%r14
movslq %r14d,%rsi
lea    0xdb5(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0xffffffff,%ebp
mov    0x8(%rsp),%rbx
test   %rbx,%rbx
je     1371 <main+0x1c1>
test   %r13,%r13
lea    0xd9a(%rip),%r12
cmovne %r13,%r12
xor    %eax,%eax
call   1050 <image_init@plt>
lea    0xd91(%rip),%rsi
mov    %rbx,%rdi
call   10a0 <fopen@plt>
test   %rax,%rax
je     1371 <main+0x1c1>
mov    %rax,%rbx
mov    0x2d3d(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1090 <fseek@plt>
mov    %rbx,%rdi
call   1070 <ftell@plt>
mov    %rax,%r15
mov    0x2d0f(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1090 <fseek@plt>
mov    %r15,%rdi
call   1080 <malloc@plt>
test   %rax,%rax
je     1371 <main+0x1c1>
mov    %rax,%r13
mov    %rax,%rdi
mov    %r15,%rsi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1030 <fread@plt>
movq   $0x0,0x10(%rsp)
movq   $0x0,(%rsp)
lea    0x10(%rsp),%rdx
mov    %rsp,%rcx
mov    %r13,%rdi
mov    %r15,%rsi
mov    %r14d,%r8d
call   1060 <compress_image@plt>
mov    (%rsp),%rsi
lea    0xcf5(%rip),%rdi
mov    %r15,%rdx
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xd01(%rip),%rsi
mov    %r12,%rdi
call   10a0 <fopen@plt>
mov    0x10(%rsp),%rdi
mov    (%rsp),%rsi
mov    $0x1,%edx
mov    %rax,%rcx
call   10d0 <fwrite@plt>
xor    %ebp,%ebp
jmp    1371 <main+0x1c1>
lea    0xca7(%rip),%rdi
mov    $0x4b,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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