#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*/ *);
scalar_t__ ferror(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fputc(unsigned char, int /*<<< orphan*/ *);
int /*<<< orphan*/ fputword(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fwrite(unsigned char *, int, int, int /*<<< orphan*/ *);
int tga_save(const char *name, unsigned char *buffer, unsigned char *colorMap,
             int width, int height, int bits) {
  FILE *f;
  int colorMapType = 0;
  int imageType;
  int colorMapLen = 0;
  int colorMapBits = 0;
  int bitsPerPix;
  int i;
  int bb;
  unsigned char *pp;
  if ((f = fopen(name, "wb+")) == NULL)
    return 0;
  switch (bits) {
  case 8:
    imageType = 1;
    if (colorMap != NULL) {
      colorMapType = 1;
      colorMapLen = 256;
      colorMapBits = 24;
    }
    bitsPerPix = 8;
    bb = 1;
    break;
  case 16:
    imageType = 2;
    bitsPerPix = 16;
    bb = 2;
    break;
  case 24:
    imageType = 2;
    bitsPerPix = 24;
    bb = 3;
    break;
  case 32:
    imageType = 2;
    bitsPerPix = 32;
    bb = 4;
    break;
  default:
    fclose(f);
    return 0;
  }
  fputc(0, f);
  fputc(colorMapType, f);
  fputc(imageType, f);
  fputword(0, f);
  fputword(colorMapLen, f);
  fputc(colorMapBits, f);
  fputword(0, f);
  fputword(0, f);
  fputword(width, f);
  fputword(height, f);
  fputc(bitsPerPix, f);
  fputc(0x02 << 4, f);
  if (colorMapType == 1) {
    for (i = 0, pp = colorMap; i < 256; i++, pp += 3) {
      fputc(pp[0], f);
      fputc(pp[1], f);
      fputc(pp[2], f);
    }
  }
  fwrite(buffer, width * bb, height, f);
  if (ferror(f)) {
    fclose(f);
    return 0;
  }
  fclose(f);
  return 1;
}


x86 assembly (clang, -O0):
<tga_save>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
mov    %r8d,-0x28(%rbp)
mov    %r9d,-0x2c(%rbp)
movl   $0x0,-0x3c(%rbp)
movl   $0x0,-0x44(%rbp)
movl   $0x0,-0x48(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xec5(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x38(%rbp)
cmp    $0x0,%rax
jne    11be <tga_save+0x5e>
movl   $0x0,-0x4(%rbp)
jmp    13ea <tga_save+0x28a>
mov    -0x2c(%rbp),%eax
add    $0xfffffff8,%eax
mov    %eax,%ecx
mov    %rcx,-0x68(%rbp)
sub    $0x18,%eax
ja     126f <tga_save+0x10f>
mov    -0x68(%rbp),%rax
lea    0xe22(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
movl   $0x1,-0x40(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     120e <tga_save+0xae>
movl   $0x1,-0x3c(%rbp)
movl   $0x100,-0x44(%rbp)
movl   $0x18,-0x48(%rbp)
movl   $0x8,-0x4c(%rbp)
movl   $0x1,-0x54(%rbp)
jmp    1284 <tga_save+0x124>
movl   $0x2,-0x40(%rbp)
movl   $0x10,-0x4c(%rbp)
movl   $0x2,-0x54(%rbp)
jmp    1284 <tga_save+0x124>
movl   $0x2,-0x40(%rbp)
movl   $0x18,-0x4c(%rbp)
movl   $0x3,-0x54(%rbp)
jmp    1284 <tga_save+0x124>
movl   $0x2,-0x40(%rbp)
movl   $0x20,-0x4c(%rbp)
movl   $0x4,-0x54(%rbp)
jmp    1284 <tga_save+0x124>
mov    -0x38(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x0,-0x4(%rbp)
jmp    13ea <tga_save+0x28a>
mov    -0x38(%rbp),%rsi
xor    %edi,%edi
call   1050 <fputc@plt>
mov    -0x3c(%rbp),%eax
mov    -0x38(%rbp),%rsi
movzbl %al,%edi
call   1050 <fputc@plt>
mov    -0x40(%rbp),%eax
mov    -0x38(%rbp),%rsi
movzbl %al,%edi
call   1050 <fputc@plt>
mov    -0x38(%rbp),%rsi
xor    %edi,%edi
call   1060 <fputword@plt>
mov    -0x44(%rbp),%edi
mov    -0x38(%rbp),%rsi
call   1060 <fputword@plt>
mov    -0x48(%rbp),%eax
mov    -0x38(%rbp),%rsi
movzbl %al,%edi
call   1050 <fputc@plt>
mov    -0x38(%rbp),%rsi
xor    %edi,%edi
call   1060 <fputword@plt>
mov    -0x38(%rbp),%rsi
xor    %edi,%edi
call   1060 <fputword@plt>
mov    -0x24(%rbp),%edi
mov    -0x38(%rbp),%rsi
call   1060 <fputword@plt>
mov    -0x28(%rbp),%edi
mov    -0x38(%rbp),%rsi
call   1060 <fputword@plt>
mov    -0x4c(%rbp),%eax
mov    -0x38(%rbp),%rsi
movzbl %al,%edi
call   1050 <fputc@plt>
mov    -0x38(%rbp),%rsi
mov    $0x20,%edi
call   1050 <fputc@plt>
cmpl   $0x1,-0x3c(%rbp)
jne    139b <tga_save+0x23b>
movl   $0x0,-0x50(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x60(%rbp)
cmpl   $0x100,-0x50(%rbp)
jge    1396 <tga_save+0x236>
mov    -0x60(%rbp),%rax
mov    (%rax),%al
mov    -0x38(%rbp),%rsi
movzbl %al,%edi
call   1050 <fputc@plt>
mov    -0x60(%rbp),%rax
mov    0x1(%rax),%al
mov    -0x38(%rbp),%rsi
movzbl %al,%edi
call   1050 <fputc@plt>
mov    -0x60(%rbp),%rax
mov    0x2(%rax),%al
mov    -0x38(%rbp),%rsi
movzbl %al,%edi
call   1050 <fputc@plt>
mov    -0x50(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x50(%rbp)
mov    -0x60(%rbp),%rax
add    $0x3,%rax
mov    %rax,-0x60(%rbp)
jmp    1337 <tga_save+0x1d7>
jmp    139b <tga_save+0x23b>
mov    -0x18(%rbp),%rdi
mov    -0x24(%rbp),%esi
imul   -0x54(%rbp),%esi
mov    -0x28(%rbp),%edx
mov    -0x38(%rbp),%rcx
call   1080 <fwrite@plt>
mov    -0x38(%rbp),%rdi
call   1030 <ferror@plt>
cmp    $0x0,%rax
je     13da <tga_save+0x27a>
mov    -0x38(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x0,-0x4(%rbp)
jmp    13ea <tga_save+0x28a>
mov    -0x38(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tga_save>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %r9d,%r14d
mov    %r8d,%r12d
mov    %ecx,%r15d
mov    %rdx,%r13
mov    %rsi,%rbp
lea    0xe8c(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     11f5 <tga_save+0x95>
mov    %rax,%rbx
add    $0xfffffff8,%r14d
rol    $0x1d,%r14d
cmp    $0x3,%r14d
ja     11fc <tga_save+0x9c>
mov    %r12d,0x14(%rsp)
mov    %r15d,0x1c(%rsp)
mov    %rbp,0x20(%rsp)
mov    $0x2,%edx
mov    $0x10,%dil
xor    %esi,%esi
mov    $0x2,%bpl
lea    0xe3e(%rip),%rax
movslq (%rax,%r14,4),%rcx
add    %rax,%rcx
xor    %r15d,%r15d
xor    %r14d,%r14d
xor    %r12d,%r12d
jmp    *%rcx
mov    $0x1,%edx
mov    $0x8,%dil
mov    $0x1,%bpl
test   %r13,%r13
je     121d <tga_save+0xbd>
mov    $0x18,%r12b
mov    $0x100,%r14d
mov    $0x1,%sil
mov    $0x1,%r15b
jmp    1228 <tga_save+0xc8>
xor    %ebp,%ebp
jmp    1337 <tga_save+0x1d7>
mov    %rbx,%rdi
call   1040 <fclose@plt>
xor    %ebp,%ebp
jmp    1337 <tga_save+0x1d7>
mov    $0x3,%edx
mov    $0x18,%dil
jmp    121d <tga_save+0xbd>
mov    $0x4,%edx
mov    $0x20,%dil
xor    %esi,%esi
xor    %r15d,%r15d
xor    %r14d,%r14d
xor    %r12d,%r12d
mov    %dil,0xf(%rsp)
mov    %esi,0x10(%rsp)
mov    %edx,0x18(%rsp)
xor    %edi,%edi
mov    %rbx,%rsi
call   1050 <fputc@plt>
movzbl %r15b,%edi
mov    %rbx,%rsi
call   1050 <fputc@plt>
movzbl %bpl,%edi
mov    %rbx,%rsi
call   1050 <fputc@plt>
xor    %edi,%edi
mov    %rbx,%rsi
call   1060 <fputword@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
call   1060 <fputword@plt>
movzbl %r12b,%edi
mov    %rbx,%rsi
call   1050 <fputc@plt>
xor    %edi,%edi
mov    %rbx,%rsi
call   1060 <fputword@plt>
xor    %edi,%edi
mov    %rbx,%rsi
call   1060 <fputword@plt>
mov    0x1c(%rsp),%r15d
mov    %r15d,%edi
mov    %rbx,%rsi
call   1060 <fputword@plt>
mov    0x14(%rsp),%edi
mov    %edi,%r14d
mov    %rbx,%rsi
call   1060 <fputword@plt>
movzbl 0xf(%rsp),%edi
mov    %rbx,%rsi
call   1050 <fputc@plt>
mov    $0x20,%edi
mov    %rbx,%rsi
call   1050 <fputc@plt>
cmpb   $0x0,0x10(%rsp)
je     1306 <tga_save+0x1a6>
xor    %ebp,%ebp
xchg   %ax,%ax
movzbl 0x0(%r13,%rbp,1),%edi
mov    %rbx,%rsi
call   1050 <fputc@plt>
movzbl 0x1(%r13,%rbp,1),%edi
mov    %rbx,%rsi
call   1050 <fputc@plt>
movzbl 0x2(%r13,%rbp,1),%edi
mov    %rbx,%rsi
call   1050 <fputc@plt>
add    $0x3,%rbp
cmp    $0x300,%ebp
jne    12d0 <tga_save+0x170>
mov    0x18(%rsp),%esi
imul   %r15d,%esi
mov    0x20(%rsp),%rdi
mov    %r14d,%edx
mov    %rbx,%rcx
call   1080 <fwrite@plt>
mov    %rbx,%rdi
call   1030 <ferror@plt>
xor    %ebp,%ebp
test   %rax,%rax
sete   %bpl
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    %ebp,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [4 x i8] c"wb+\00"
define i32 @tga_save(i8* %0, i8* %1, i8* %2, i32 %3, i32 %4, i32 %5) {
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32*
%15 = alloca i32
%16 = alloca i32
%17 = alloca i32
%18 = alloca i32
%19 = alloca i32
%20 = alloca i32
%21 = alloca i32
%22 = alloca i8*
store i8* %0, i8** %8
store i8* %1, i8** %9
store i8* %2, i8** %10
store i32 %3, i32* %11
store i32 %4, i32* %12
store i32 %5, i32* %13
store i32 0, i32* %15
store i32 0, i32* %17
store i32 0, i32* %18
%23 = load i8*, i8** %8
%24 = call i32* @fopen(i8* %23, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
store i32* %24, i32** %14
%25 = icmp eq i32* %24, null
br i1 %25, label %26, label %27
26: ; preds = %6
store i32 0, i32* %7
br label %122
27: ; preds = %6
%28 = load i32, i32* %13
switch i32 %28, label %37 [
i32 8, label %29
i32 16, label %34
i32 24, label %35
i32 32, label %36
]
29: ; preds = %27
store i32 1, i32* %16
%30 = load i8*, i8** %10
%31 = icmp ne i8* %30, null
br i1 %31, label %32, label %33
32: ; preds = %29
store i32 1, i32* %15
store i32 256, i32* %17
store i32 24, i32* %18
br label %33
33: ; preds = %32, %29
store i32 8, i32* %19
store i32 1, i32* %21
br label %40
34: ; preds = %27
store i32 2, i32* %16
store i32 16, i32* %19
store i32 2, i32* %21
br label %40
35: ; preds = %27
store i32 2, i32* %16
store i32 24, i32* %19
store i32 3, i32* %21
br label %40
36: ; preds = %27
store i32 2, i32* %16
store i32 32, i32* %19
store i32 4, i32* %21
br label %40
37: ; preds = %27
%38 = load i32*, i32** %14
%39 = call i32 @fclose(i32* %38)
store i32 0, i32* %7
br label %122
40: ; preds = %36, %35, %34, %33
%41 = load i32*, i32** %14
%42 = call i32 @fputc(i8 zeroext 0, i32* %41)
%43 = load i32, i32* %15
%44 = trunc i32 %43 to i8
%45 = load i32*, i32** %14
%46 = call i32 @fputc(i8 zeroext %44, i32* %45)
%47 = load i32, i32* %16
%48 = trunc i32 %47 to i8
%49 = load i32*, i32** %14
%50 = call i32 @fputc(i8 zeroext %48, i32* %49)
%51 = load i32*, i32** %14
%52 = call i32 @fputword(i32 0, i32* %51)
%53 = load i32, i32* %17
%54 = load i32*, i32** %14
%55 = call i32 @fputword(i32 %53, i32* %54)
%56 = load i32, i32* %18
%57 = trunc i32 %56 to i8
%58 = load i32*, i32** %14
%59 = call i32 @fputc(i8 zeroext %57, i32* %58)
%60 = load i32*, i32** %14
%61 = call i32 @fputword(i32 0, i32* %60)
%62 = load i32*, i32** %14
%63 = call i32 @fputword(i32 0, i32* %62)
%64 = load i32, i32* %11
%65 = load i32*, i32** %14
%66 = call i32 @fputword(i32 %64, i32* %65)
%67 = load i32, i32* %12
%68 = load i32*, i32** %14
%69 = call i32 @fputword(i32 %67, i32* %68)
%70 = load i32, i32* %19
%71 = trunc i32 %70 to i8
%72 = load i32*, i32** %14
%73 = call i32 @fputc(i8 zeroext %71, i32* %72)
%74 = load i32*, i32** %14
%75 = call i32 @fputc(i8 zeroext 32, i32* %74)
%76 = load i32, i32* %15
%77 = icmp eq i32 %76, 1
br i1 %77, label %78, label %105
78: ; preds = %40
store i32 0, i32* %20
%79 = load i8*, i8** %10
store i8* %79, i8** %22
br label %80
80: ; preds = %99, %78
%81 = load i32, i32* %20
%82 = icmp slt i32 %81, 256
br i1 %82, label %83, label %104
83: ; preds = %80
%84 = load i8*, i8** %22
%85 = getelementptr inbounds i8, i8* %84, i64 0
%86 = load i8, i8* %85
%87 = load i32*, i32** %14
%88 = call i32 @fputc(i8 zeroext %86, i32* %87)
%89 = load i8*, i8** %22
%90 = getelementptr inbounds i8, i8* %89, i64 1
%91 = load i8, i8* %90
%92 = load i32*, i32** %14
%93 = call i32 @fputc(i8 zeroext %91, i32* %92)
%94 = load i8*, i8** %22
%95 = getelementptr inbounds i8, i8* %94, i64 2
%96 = load i8, i8* %95
%97 = load i32*, i32** %14
%98 = call i32 @fputc(i8 zeroext %96, i32* %97)
br label %99
99: ; preds = %83
%100 = load i32, i32* %20
%101 = add nsw i32 %100, 1
store i32 %101, i32* %20
%102 = load i8*, i8** %22
%103 = getelementptr inbounds i8, i8* %102, i64 3
store i8* %103, i8** %22
br label %80
104: ; preds = %80
br label %105
105: ; preds = %104, %40
%106 = load i8*, i8** %9
%107 = load i32, i32* %11
%108 = load i32, i32* %21
%109 = mul nsw i32 %107, %108
%110 = load i32, i32* %12
%111 = load i32*, i32** %14
%112 = call i32 @fwrite(i8* %106, i32 %109, i32 %110, i32* %111)
%113 = load i32*, i32** %14
%114 = call i64 @ferror(i32* %113)
%115 = icmp ne i64 %114, 0
br i1 %115, label %116, label %119
116: ; preds = %105
%117 = load i32*, i32** %14
%118 = call i32 @fclose(i32* %117)
store i32 0, i32* %7
br label %122
119: ; preds = %105
%120 = load i32*, i32** %14
%121 = call i32 @fclose(i32* %120)
store i32 1, i32* %7
br label %122
122: ; preds = %119, %116, %37, %26
%123 = load i32, i32* %7
ret i32 %123
}

LLVM IR (-O2):
@.str = [4 x i8] c"wb+\00"
define i32 @tga_save(i8* nocapture %0, i8* nocapture %1, i8* %2, i32 %3, i32 %4, i32 %5) {
%7 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%8 = icmp eq i32* %7, null
br i1 %8, label %60, label %9
9: ; preds = %6
%10 = add i32 %5, -8
%11 = tail call i32 @llvm.fshl.i32(i32 %10, i32 %10, i32 29)
switch i32 %11, label %16 [
i32 0, label %12
i32 1, label %19
i32 2, label %14
i32 3, label %15
]
12: ; preds = %9
%13 = icmp eq i8* %2, null
br i1 %13, label %18, label %19
14: ; preds = %9
br label %19
15: ; preds = %9
br label %19
16: ; preds = %9
%17 = tail call i32 @fclose(i32* %7)
br label %60
18: ; preds = %12
br label %19
19: ; preds = %12, %9, %15, %14, %18
%20 = phi i1 [ true, %12 ], [ false, %9 ], [ false, %15 ], [ false, %14 ], [ false, %18 ]
%21 = phi i8 [ 1, %12 ], [ 0, %9 ], [ 0, %15 ], [ 0, %14 ], [ 0, %18 ]
%22 = phi i8 [ 1, %12 ], [ 2, %9 ], [ 2, %15 ], [ 2, %14 ], [ 1, %18 ]
%23 = phi i32 [ 256, %12 ], [ 0, %9 ], [ 0, %15 ], [ 0, %14 ], [ 0, %18 ]
%24 = phi i8 [ 24, %12 ], [ 0, %9 ], [ 0, %15 ], [ 0, %14 ], [ 0, %18 ]
%25 = phi i8 [ 8, %12 ], [ 16, %9 ], [ 32, %15 ], [ 24, %14 ], [ 8, %18 ]
%26 = phi i32 [ 1, %12 ], [ 2, %9 ], [ 4, %15 ], [ 3, %14 ], [ 1, %18 ]
%27 = tail call i32 @fputc(i8 zeroext 0, i32* %7)
%28 = tail call i32 @fputc(i8 zeroext %21, i32* %7)
%29 = tail call i32 @fputc(i8 zeroext %22, i32* %7)
%30 = tail call i32 @fputword(i32 0, i32* %7)
%31 = tail call i32 @fputword(i32 %23, i32* %7)
%32 = tail call i32 @fputc(i8 zeroext %24, i32* %7)
%33 = tail call i32 @fputword(i32 0, i32* %7)
%34 = tail call i32 @fputword(i32 0, i32* %7)
%35 = tail call i32 @fputword(i32 %3, i32* %7)
%36 = tail call i32 @fputword(i32 %4, i32* %7)
%37 = tail call i32 @fputc(i8 zeroext %25, i32* %7)
%38 = tail call i32 @fputc(i8 zeroext 32, i32* %7)
br i1 %20, label %39, label %53
39: ; preds = %19, %39
%40 = phi i8* [ %51, %39 ], [ %2, %19 ]
%41 = phi i32 [ %50, %39 ], [ 0, %19 ]
%42 = load i8, i8* %40
%43 = tail call i32 @fputc(i8 zeroext %42, i32* %7)
%44 = getelementptr inbounds i8, i8* %40, i64 1
%45 = load i8, i8* %44
%46 = tail call i32 @fputc(i8 zeroext %45, i32* %7)
%47 = getelementptr inbounds i8, i8* %40, i64 2
%48 = load i8, i8* %47
%49 = tail call i32 @fputc(i8 zeroext %48, i32* %7)
%50 = add nuw nsw i32 %41, 1
%51 = getelementptr inbounds i8, i8* %40, i64 3
%52 = icmp eq i32 %50, 256
br i1 %52, label %53, label %39
53: ; preds = %39, %19
%54 = mul nsw i32 %26, %3
%55 = tail call i32 @fwrite(i8* %1, i32 %54, i32 %4, i32* %7)
%56 = tail call i64 @ferror(i32* %7)
%57 = icmp eq i64 %56, 0
%58 = tail call i32 @fclose(i32* %7)
%59 = zext i1 %57 to i32
br label %60
60: ; preds = %53, %6, %16
%61 = phi i32 [ 0, %16 ], [ 0, %6 ], [ %59, %53 ]
ret i32 %61
}