#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*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ spng_dec_go_to_image_data(int /*<<< orphan*/ *);
int /*<<< orphan*/ spng_dec_literal_to_rgb(int /*<<< orphan*/ *, unsigned int,
                                           unsigned char *, unsigned char *,
                                           unsigned char *);
int /*<<< orphan*/ spng_dec_read_bits(int /*<<< orphan*/ *, int);
unsigned int spng_dec_read_length(int /*<<< orphan*/ *, unsigned int);
unsigned int spng_dec_read_literal(int /*<<< orphan*/ *);
int /*<<< orphan*/ spng_dec_read_palet(int /*<<< orphan*/ *);
int /*<<< orphan*/ spng_dec_read_size(int /*<<< orphan*/ *, unsigned int *,
                                      unsigned int *);
int /*<<< orphan*/ usage();
int /*<<< orphan*/ write_ppm_header(int /*<<< orphan*/ *, unsigned int,
                                    unsigned int);
int /*<<< orphan*/ write_ppm_pixel(int /*<<< orphan*/ *, unsigned char,
                                   unsigned char, unsigned char);
int main(int argc, char *argv[]) {
  unsigned char r, g, b;
  unsigned int i, x, y;
  unsigned int w, h;
  unsigned int lit;
  FILE *fp = NULL;
  FILE *fpppm = NULL;
  if (argc > 1) {
    fp = fopen(argv[1], "r");
  }
  if (!fp) {
    usage();
    return 2;
  }
  fpppm = fopen("out.ppm", "w");
  if (!fpppm) {
    usage();
    return 2;
  }
  spng_dec_read_size(fp, &w, &h);
  write_ppm_header(fpppm, w, h);
  spng_dec_read_palet(fp);
  spng_dec_go_to_image_data(fp);
  spng_dec_read_bits(fp, 3);
  y = 0;
  while (y < h) {
    spng_dec_read_literal(fp);
    x = 0;
    while (x < w) {
      lit = spng_dec_read_literal(fp);
      if (lit < 256) {
        spng_dec_literal_to_rgb(fp, lit, &r, &g, &b);
        x++;
        write_ppm_pixel(fpppm, r, g, b);
      } else {
        lit = spng_dec_read_length(fp, lit);
        x += lit;
        spng_dec_read_bits(fp, 5);
        for (i = 0; i < lit; i++) {
          write_ppm_pixel(fpppm, r, g, b);
        }
      }
    }
    y++;
  }
  fclose(fpppm);
  fclose(fp);
  return 1;
}


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)
movq   $0x0,-0x38(%rbp)
movq   $0x0,-0x40(%rbp)
cmpl   $0x1,-0x8(%rbp)
jle    1208 <main+0x48>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe01(%rip),%rsi
call   10d0 <fopen@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    1226 <main+0x66>
mov    $0x0,%al
call   10c0 <usage@plt>
movl   $0x2,-0x4(%rbp)
jmp    13b3 <main+0x1f3>
lea    0xdd5(%rip),%rdi
lea    0xdd6(%rip),%rsi
call   10d0 <fopen@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    125b <main+0x9b>
mov    $0x0,%al
call   10c0 <usage@plt>
movl   $0x2,-0x4(%rbp)
jmp    13b3 <main+0x1f3>
mov    -0x38(%rbp),%rdi
lea    -0x24(%rbp),%rsi
lea    -0x28(%rbp),%rdx
call   10a0 <spng_dec_read_size@plt>
mov    -0x40(%rbp),%rdi
mov    -0x24(%rbp),%esi
mov    -0x28(%rbp),%edx
call   1050 <write_ppm_header@plt>
mov    -0x38(%rbp),%rdi
call   1070 <spng_dec_read_palet@plt>
mov    -0x38(%rbp),%rdi
call   10b0 <spng_dec_go_to_image_data@plt>
mov    -0x38(%rbp),%rdi
mov    $0x3,%esi
call   1030 <spng_dec_read_bits@plt>
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x28(%rbp),%eax
jae    139a <main+0x1da>
mov    -0x38(%rbp),%rdi
call   10e0 <spng_dec_read_literal@plt>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x24(%rbp),%eax
jae    138c <main+0x1cc>
mov    -0x38(%rbp),%rdi
call   10e0 <spng_dec_read_literal@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x100,-0x2c(%rbp)
jae    1322 <main+0x162>
mov    -0x38(%rbp),%rdi
mov    -0x2c(%rbp),%esi
lea    -0x11(%rbp),%rdx
lea    -0x12(%rbp),%rcx
lea    -0x13(%rbp),%r8
call   1080 <spng_dec_literal_to_rgb@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x40(%rbp),%rdi
mov    -0x11(%rbp),%cl
mov    -0x12(%rbp),%al
movzbl %cl,%esi
movzbl %al,%edx
movzbl -0x13(%rbp),%ecx
call   1090 <write_ppm_pixel@plt>
jmp    1387 <main+0x1c7>
mov    -0x38(%rbp),%rdi
mov    -0x2c(%rbp),%esi
call   1060 <spng_dec_read_length@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
add    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x38(%rbp),%rdi
mov    $0x5,%esi
call   1030 <spng_dec_read_bits@plt>
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x2c(%rbp),%eax
jae    1382 <main+0x1c2>
mov    -0x40(%rbp),%rdi
mov    -0x11(%rbp),%cl
mov    -0x12(%rbp),%al
movzbl %cl,%esi
movzbl %al,%edx
movzbl -0x13(%rbp),%ecx
call   1090 <write_ppm_pixel@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    134f <main+0x18f>
jmp    1387 <main+0x1c7>
jmp    12be <main+0xfe>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    12a2 <main+0xe2>
mov    -0x40(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x38(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
cmp    $0x2,%edi
jl     132e <main+0x16e>
mov    0x8(%rsi),%rdi
lea    0xe1e(%rip),%rsi
call   10d0 <fopen@plt>
test   %rax,%rax
je     132e <main+0x16e>
mov    %rax,%r14
lea    0xe08(%rip),%rdi
lea    0xe09(%rip),%rsi
call   10d0 <fopen@plt>
test   %rax,%rax
je     132e <main+0x16e>
mov    %rax,%rbx
lea    0x14(%rsp),%rsi
lea    0x10(%rsp),%rdx
mov    %r14,%rdi
call   10a0 <spng_dec_read_size@plt>
mov    0x14(%rsp),%esi
mov    0x10(%rsp),%edx
mov    %rbx,%rdi
call   1050 <write_ppm_header@plt>
mov    %r14,%rdi
call   1070 <spng_dec_read_palet@plt>
mov    %r14,%rdi
call   10b0 <spng_dec_go_to_image_data@plt>
mov    %r14,%rdi
mov    $0x3,%esi
call   1030 <spng_dec_read_bits@plt>
cmpl   $0x0,0x10(%rsp)
je     1349 <main+0x189>
xor    %r15d,%r15d
lea    0xd(%rsp),%r13
jmp    127f <main+0xbf>
cs nopw 0x0(%rax,%rax,1)
add    $0x1,%r15d
cmp    0x10(%rsp),%r15d
jae    1349 <main+0x189>
mov    %r14,%rdi
call   10e0 <spng_dec_read_literal@plt>
cmpl   $0x0,0x14(%rsp)
je     1270 <main+0xb0>
xor    %r12d,%r12d
jmp    12d4 <main+0x114>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
lea    0xf(%rsp),%rdx
lea    0xe(%rsp),%rcx
mov    %r13,%r8
call   1080 <spng_dec_literal_to_rgb@plt>
add    $0x1,%r12d
movzbl 0xf(%rsp),%esi
movzbl 0xe(%rsp),%edx
movzbl 0xd(%rsp),%ecx
mov    %rbx,%rdi
call   1090 <write_ppm_pixel@plt>
cmp    0x14(%rsp),%r12d
jae    1270 <main+0xb0>
mov    %r14,%rdi
call   10e0 <spng_dec_read_literal@plt>
mov    %r14,%rdi
mov    %eax,%esi
cmp    $0xff,%eax
jbe    12a0 <main+0xe0>
call   1060 <spng_dec_read_length@plt>
mov    %eax,%ebp
add    %eax,%r12d
mov    %r14,%rdi
mov    $0x5,%esi
call   1030 <spng_dec_read_bits@plt>
test   %ebp,%ebp
je     12cd <main+0x10d>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
movzbl 0xf(%rsp),%esi
movzbl 0xe(%rsp),%edx
movzbl 0xd(%rsp),%ecx
mov    %rbx,%rdi
call   1090 <write_ppm_pixel@plt>
add    $0xffffffff,%ebp
jne    1310 <main+0x150>
jmp    12cd <main+0x10d>
xor    %eax,%eax
call   10c0 <usage@plt>
mov    $0x2,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    %r14,%rdi
call   1040 <fclose@plt>
mov    $0x1,%eax
jmp    133a <main+0x17a>

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [8 x i8] c"out.ppm\00"
@.str.2 = [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 i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32*
%16 = alloca i32*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32* null, i32** %15
store i32* null, i32** %16
%17 = load i32, i32* %4
%18 = icmp sgt i32 %17, 1
br i1 %18, label %19, label %24
19: ; preds = %2
%20 = load i8**, i8*** %5
%21 = getelementptr inbounds i8*, i8** %20, i64 1
%22 = load i8*, i8** %21
%23 = call i32* @fopen(i8* %22, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %23, i32** %15
br label %24
24: ; preds = %19, %2
%25 = load i32*, i32** %15
%26 = icmp ne i32* %25, null
br i1 %26, label %29, label %27
27: ; preds = %24
%28 = call i32 (...) @usage()
store i32 2, i32* %3
br label %107
29: ; preds = %24
%30 = call i32* @fopen(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %30, i32** %16
%31 = load i32*, i32** %16
%32 = icmp ne i32* %31, null
br i1 %32, label %35, label %33
33: ; preds = %29
%34 = call i32 (...) @usage()
store i32 2, i32* %3
br label %107
35: ; preds = %29
%36 = load i32*, i32** %15
%37 = call i32 @spng_dec_read_size(i32* %36, i32* %12, i32* %13)
%38 = load i32*, i32** %16
%39 = load i32, i32* %12
%40 = load i32, i32* %13
%41 = call i32 @write_ppm_header(i32* %38, i32 %39, i32 %40)
%42 = load i32*, i32** %15
%43 = call i32 @spng_dec_read_palet(i32* %42)
%44 = load i32*, i32** %15
%45 = call i32 @spng_dec_go_to_image_data(i32* %44)
%46 = load i32*, i32** %15
%47 = call i32 @spng_dec_read_bits(i32* %46, i32 3)
store i32 0, i32* %11
br label %48
48: ; preds = %99, %35
%49 = load i32, i32* %11
%50 = load i32, i32* %13
%51 = icmp ult i32 %49, %50
br i1 %51, label %52, label %102
52: ; preds = %48
%53 = load i32*, i32** %15
%54 = call i32 @spng_dec_read_literal(i32* %53)
store i32 0, i32* %10
br label %55
55: ; preds = %98, %52
%56 = load i32, i32* %10
%57 = load i32, i32* %12
%58 = icmp ult i32 %56, %57
br i1 %58, label %59, label %99
59: ; preds = %55
%60 = load i32*, i32** %15
%61 = call i32 @spng_dec_read_literal(i32* %60)
store i32 %61, i32* %14
%62 = load i32, i32* %14
%63 = icmp ult i32 %62, 256
br i1 %63, label %64, label %75
64: ; preds = %59
%65 = load i32*, i32** %15
%66 = load i32, i32* %14
%67 = call i32 @spng_dec_literal_to_rgb(i32* %65, i32 %66, i8* %6, i8* %7, i8* %8)
%68 = load i32, i32* %10
%69 = add i32 %68, 1
store i32 %69, i32* %10
%70 = load i32*, i32** %16
%71 = load i8, i8* %6
%72 = load i8, i8* %7
%73 = load i8, i8* %8
%74 = call i32 @write_ppm_pixel(i32* %70, i8 zeroext %71, i8 zeroext %72, i8 zeroext %73)
br label %98
75: ; preds = %59
%76 = load i32*, i32** %15
%77 = load i32, i32* %14
%78 = call i32 @spng_dec_read_length(i32* %76, i32 %77)
store i32 %78, i32* %14
%79 = load i32, i32* %14
%80 = load i32, i32* %10
%81 = add i32 %80, %79
store i32 %81, i32* %10
%82 = load i32*, i32** %15
%83 = call i32 @spng_dec_read_bits(i32* %82, i32 5)
store i32 0, i32* %9
br label %84
84: ; preds = %94, %75
%85 = load i32, i32* %9
%86 = load i32, i32* %14
%87 = icmp ult i32 %85, %86
br i1 %87, label %88, label %97
88: ; preds = %84
%89 = load i32*, i32** %16
%90 = load i8, i8* %6
%91 = load i8, i8* %7
%92 = load i8, i8* %8
%93 = call i32 @write_ppm_pixel(i32* %89, i8 zeroext %90, i8 zeroext %91, i8 zeroext %92)
br label %94
94: ; preds = %88
%95 = load i32, i32* %9
%96 = add i32 %95, 1
store i32 %96, i32* %9
br label %84
97: ; preds = %84
br label %98
98: ; preds = %97, %64
br label %55
99: ; preds = %55
%100 = load i32, i32* %11
%101 = add i32 %100, 1
store i32 %101, i32* %11
br label %48
102: ; preds = %48
%103 = load i32*, i32** %16
%104 = call i32 @fclose(i32* %103)
%105 = load i32*, i32** %15
%106 = call i32 @fclose(i32* %105)
store i32 1, i32* %3
br label %107
107: ; preds = %102, %33, %27
%108 = load i32, i32* %3
ret i32 %108
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [8 x i8] c"out.ppm\00"
@.str.2 = [2 x i8] c"w\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i8
%4 = alloca i8
%5 = alloca i8
%6 = alloca i32
%7 = alloca i32
%8 = bitcast i32* %6 to i8*
%9 = bitcast i32* %7 to i8*
%10 = icmp sgt i32 %0, 1
br i1 %10, label %11, label %16
11: ; preds = %2
%12 = getelementptr inbounds i8*, i8** %1, i64 1
%13 = load i8*, i8** %12
%14 = tail call i32* @fopen(i8* %13, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%15 = icmp eq i32* %14, null
br i1 %15, label %16, label %18
16: ; preds = %2, %11
%17 = tail call i32 (...) @usage()
br label %73
18: ; preds = %11
%19 = tail call i32* @fopen(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%20 = icmp eq i32* %19, null
br i1 %20, label %21, label %23
21: ; preds = %18
%22 = tail call i32 (...) @usage()
br label %73
23: ; preds = %18
%24 = call i32 @spng_dec_read_size(i32* %14, i32* %6, i32* %7)
%25 = load i32, i32* %6
%26 = load i32, i32* %7
%27 = call i32 @write_ppm_header(i32* %19, i32 %25, i32 %26)
%28 = call i32 @spng_dec_read_palet(i32* %14)
%29 = call i32 @spng_dec_go_to_image_data(i32* %14)
%30 = call i32 @spng_dec_read_bits(i32* %14, i32 3)
%31 = load i32, i32* %7
%32 = icmp eq i32 %31, 0
br i1 %32, label %70, label %33
33: ; preds = %23, %66
%34 = phi i32 [ %67, %66 ], [ 0, %23 ]
%35 = call i32 @spng_dec_read_literal(i32* %14)
%36 = load i32, i32* %6
%37 = icmp eq i32 %36, 0
br i1 %37, label %66, label %38
38: ; preds = %33, %62
%39 = phi i32 [ %63, %62 ], [ 0, %33 ]
%40 = call i32 @spng_dec_read_literal(i32* %14)
%41 = icmp ult i32 %40, 256
br i1 %41, label %42, label %49
42: ; preds = %38
%43 = call i32 @spng_dec_literal_to_rgb(i32* %14, i32 %40, i8* %3, i8* %4, i8* %5)
%44 = add nuw i32 %39, 1
%45 = load i8, i8* %3
%46 = load i8, i8* %4
%47 = load i8, i8* %5
%48 = call i32 @write_ppm_pixel(i32* %19, i8 zeroext %45, i8 zeroext %46, i8 zeroext %47)
br label %62
49: ; preds = %38
%50 = call i32 @spng_dec_read_length(i32* %14, i32 %40)
%51 = add i32 %50, %39
%52 = call i32 @spng_dec_read_bits(i32* %14, i32 5)
%53 = icmp eq i32 %50, 0
br i1 %53, label %62, label %54
54: ; preds = %49, %54
%55 = phi i32 [ %60, %54 ], [ 0, %49 ]
%56 = load i8, i8* %3
%57 = load i8, i8* %4
%58 = load i8, i8* %5
%59 = call i32 @write_ppm_pixel(i32* %19, i8 zeroext %56, i8 zeroext %57, i8 zeroext %58)
%60 = add nuw i32 %55, 1
%61 = icmp eq i32 %60, %50
br i1 %61, label %62, label %54
62: ; preds = %54, %49, %42
%63 = phi i32 [ %44, %42 ], [ %51, %49 ], [ %51, %54 ]
%64 = load i32, i32* %6
%65 = icmp ult i32 %63, %64
br i1 %65, label %38, label %66
66: ; preds = %62, %33
%67 = add nuw i32 %34, 1
%68 = load i32, i32* %7
%69 = icmp ult i32 %67, %68
br i1 %69, label %33, label %70
70: ; preds = %66, %23
%71 = call i32 @fclose(i32* %19)
%72 = call i32 @fclose(i32* %14)
br label %73
73: ; preds = %70, %21, %16
%74 = phi i32 [ 1, %70 ], [ 2, %21 ], [ 2, %16 ]
ret i32 %74
}