#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 /*<<< orphan*/ FILE;

/* Variables and functions */
int atoi(char *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fread(void *, int /*<<< orphan*/, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fwrite(void *, int, int, int /*<<< orphan*/ *);
int jpg_transcode(void *, int /*<<< orphan*/, int);
void *malloc(int /*<<< orphan*/);
int /*<<< orphan*/ puts(char *);
scalar_t__ stat(char *, struct stat *);
scalar_t__ strcmp(char *, char *);
int /*<<< orphan*/ usage();
int main(int argc, char *argv[]) {
  int q, len;
  void *src;
  FILE *f, *out;
  struct stat st;
  if (argc < 3) {
    usage();
  }
  if (strcmp(argv[1], "-q") != 0) {
    usage();
  }
  q = atoi(argv[2]);
  if ((f = fopen("images/js-wa-900.jpg", "rb")) == NULL) {
    puts("Barf");
    exit(2);
  }
  if (stat("images/js-wa-900.jpg", &st) != 0) {
    exit(3);
  }
  src = malloc(st.st_size);
  if (src == NULL) {
    exit(4);
  }
  fread(src, st.st_size, 1, f);
  len = jpg_transcode(src, st.st_size, q);
  out = fopen("out.jpg", "wb");
  if (out) {
    fwrite(src, len, 1, out);
    fclose(out);
  }
  fclose(f);
  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    11e7 <main+0x27>
mov    $0x0,%al
call   1090 <usage@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe0a(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
je     120c <main+0x4c>
mov    $0x0,%al
call   1090 <usage@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   10c0 <atoi@plt>
mov    %eax,-0x14(%rbp)
lea    0xde0(%rip),%rdi
lea    0xdee(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    1253 <main+0x93>
lea    0xdd7(%rip),%rdi
call   1030 <puts@plt>
mov    $0x2,%edi
call   10d0 <exit@plt>
lea    0xda9(%rip),%rdi
lea    -0x38(%rbp),%rsi
call   1070 <stat@plt>
cmp    $0x0,%rax
je     1277 <main+0xb7>
mov    $0x3,%edi
call   10d0 <exit@plt>
mov    -0x38(%rbp),%edi
call   1080 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1298 <main+0xd8>
mov    $0x4,%edi
call   10d0 <exit@plt>
mov    -0x20(%rbp),%rdi
mov    -0x38(%rbp),%esi
mov    -0x28(%rbp),%rcx
mov    $0x1,%edx
call   1040 <fread@plt>
mov    -0x20(%rbp),%rdi
mov    -0x38(%rbp),%esi
mov    -0x14(%rbp),%edx
call   10a0 <jpg_transcode@plt>
mov    %eax,-0x18(%rbp)
lea    0xd5a(%rip),%rdi
lea    0xd5b(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     12ff <main+0x13f>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%esi
mov    -0x30(%rbp),%rcx
mov    $0x1,%edx
call   10e0 <fwrite@plt>
mov    -0x30(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x28(%rbp),%rdi
call   1050 <fclose@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
cmp    $0x2,%edi
jg     11d6 <main+0x16>
xor    %eax,%eax
call   1090 <usage@plt>
mov    0x8(%rbx),%rdi
lea    0xe1f(%rip),%rsi
call   1060 <strcmp@plt>
test   %rax,%rax
je     11f2 <main+0x32>
xor    %eax,%eax
call   1090 <usage@plt>
mov    0x10(%rbx),%rdi
call   10c0 <atoi@plt>
mov    %eax,%r14d
lea    0xdfe(%rip),%rdi
lea    0xe0c(%rip),%rsi
call   10b0 <fopen@plt>
test   %rax,%rax
je     12b9 <main+0xf9>
mov    %rax,%r15
lea    0xddf(%rip),%rdi
mov    %rsp,%rsi
call   1070 <stat@plt>
test   %rax,%rax
jne    12cf <main+0x10f>
mov    (%rsp),%ebx
mov    %ebx,%edi
call   1080 <malloc@plt>
test   %rax,%rax
je     12d9 <main+0x119>
mov    %rax,%rbp
mov    %rax,%rdi
mov    %ebx,%esi
mov    $0x1,%edx
mov    %r15,%rcx
call   1040 <fread@plt>
mov    (%rsp),%esi
mov    %rbp,%rdi
mov    %r14d,%edx
call   10a0 <jpg_transcode@plt>
mov    %eax,%r14d
lea    0xdab(%rip),%rdi
lea    0xdac(%rip),%rsi
call   10b0 <fopen@plt>
test   %rax,%rax
je     12a4 <main+0xe4>
mov    %rax,%rbx
mov    %rbp,%rdi
mov    %r14d,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   10e0 <fwrite@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
mov    %r15,%rdi
call   1050 <fclose@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd5b(%rip),%rdi
call   1030 <puts@plt>
mov    $0x2,%edi
call   10d0 <exit@plt>
mov    $0x3,%edi
call   10d0 <exit@plt>
mov    $0x4,%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [3 x i8] c"-q\00"
@.str.1 = [21 x i8] c"images/js-wa-900.jpg\00"
@.str.2 = [3 x i8] c"rb\00"
@.str.3 = [5 x i8] c"Barf\00"
@.str.4 = [8 x i8] c"out.jpg\00"
@.str.5 = [3 x i8] c"wb\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32*
%10 = alloca i32*
%11 = alloca %struct.stat
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 (...) @usage()
br label %16
16: ; preds = %14, %2
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 1
%19 = load i8*, i8** %18
%20 = call i64 @strcmp(i8* %19, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%21 = icmp ne i64 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %16
%23 = call i32 (...) @usage()
br label %24
24: ; preds = %22, %16
%25 = load i8**, i8*** %5
%26 = getelementptr inbounds i8*, i8** %25, i64 2
%27 = load i8*, i8** %26
%28 = call i32 @atoi(i8* %27)
store i32 %28, i32* %6
%29 = call i32* @fopen(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
store i32* %29, i32** %9
%30 = icmp eq i32* %29, null
br i1 %30, label %31, label %34
31: ; preds = %24
%32 = call i32 @puts(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%33 = call i32 @exit(i32 2)
unreachable
34: ; preds = %24
%35 = call i64 @stat(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), %struct.stat* %11)
%36 = icmp ne i64 %35, 0
br i1 %36, label %37, label %39
37: ; preds = %34
%38 = call i32 @exit(i32 3)
unreachable
39: ; preds = %34
%40 = getelementptr inbounds %struct.stat, %struct.stat* %11, i32 0, i32 0
%41 = load i32, i32* %40
%42 = call i8* @malloc(i32 %41)
store i8* %42, i8** %8
%43 = load i8*, i8** %8
%44 = icmp eq i8* %43, null
br i1 %44, label %45, label %47
45: ; preds = %39
%46 = call i32 @exit(i32 4)
unreachable
47: ; preds = %39
%48 = load i8*, i8** %8
%49 = getelementptr inbounds %struct.stat, %struct.stat* %11, i32 0, i32 0
%50 = load i32, i32* %49
%51 = load i32*, i32** %9
%52 = call i32 @fread(i8* %48, i32 %50, i32 1, i32* %51)
%53 = load i8*, i8** %8
%54 = getelementptr inbounds %struct.stat, %struct.stat* %11, i32 0, i32 0
%55 = load i32, i32* %54
%56 = load i32, i32* %6
%57 = call i32 @jpg_transcode(i8* %53, i32 %55, i32 %56)
store i32 %57, i32* %7
%58 = call i32* @fopen(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0))
store i32* %58, i32** %10
%59 = load i32*, i32** %10
%60 = icmp ne i32* %59, null
br i1 %60, label %61, label %68
61: ; preds = %47
%62 = load i8*, i8** %8
%63 = load i32, i32* %7
%64 = load i32*, i32** %10
%65 = call i32 @fwrite(i8* %62, i32 %63, i32 1, i32* %64)
%66 = load i32*, i32** %10
%67 = call i32 @fclose(i32* %66)
br label %68
68: ; preds = %61, %47
%69 = load i32*, i32** %9
%70 = call i32 @fclose(i32* %69)
ret i32 0
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [3 x i8] c"-q\00"
@.str.1 = [21 x i8] c"images/js-wa-900.jpg\00"
@.str.2 = [3 x i8] c"rb\00"
@.str.3 = [5 x i8] c"Barf\00"
@.str.4 = [8 x i8] c"out.jpg\00"
@.str.5 = [3 x i8] c"wb\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = icmp slt i32 %0, 3
br i1 %5, label %6, label %8
6: ; preds = %2
%7 = tail call i32 (...) @usage()
br label %8
8: ; preds = %6, %2
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
%11 = tail call i64 @strcmp(i8* %10, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%12 = icmp eq i64 %11, 0
br i1 %12, label %15, label %13
13: ; preds = %8
%14 = tail call i32 (...) @usage()
br label %15
15: ; preds = %13, %8
%16 = getelementptr inbounds i8*, i8** %1, i64 2
%17 = load i8*, i8** %16
%18 = tail call i32 @atoi(i8* %17)
%19 = tail call i32* @fopen(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%20 = icmp eq i32* %19, null
br i1 %20, label %21, label %24
21: ; preds = %15
%22 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%23 = tail call i32 @exit(i32 2)
unreachable
24: ; preds = %15
%25 = call i64 @stat(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), %struct.stat* %3)
%26 = icmp eq i64 %25, 0
br i1 %26, label %29, label %27
27: ; preds = %24
%28 = call i32 @exit(i32 3)
unreachable
29: ; preds = %24
%30 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%31 = load i32, i32* %30
%32 = call i8* @malloc(i32 %31)
%33 = icmp eq i8* %32, null
br i1 %33, label %34, label %36
34: ; preds = %29
%35 = call i32 @exit(i32 4)
unreachable
36: ; preds = %29
%37 = call i32 @fread(i8* %32, i32 %31, i32 1, i32* %19)
%38 = load i32, i32* %30
%39 = call i32 @jpg_transcode(i8* %32, i32 %38, i32 %18)
%40 = call i32* @fopen(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0))
%41 = icmp eq i32* %40, null
br i1 %41, label %45, label %42
42: ; preds = %36
%43 = call i32 @fwrite(i8* %32, i32 %39, i32 1, i32* %40)
%44 = call i32 @fclose(i32* %40)
br label %45
45: ; preds = %42, %36
%46 = call i32 @fclose(i32* %19)
ret i32 0
}