#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*/ putc(char, int /*<<< orphan*/ *);
int tga_save(char *filename, char *data, unsigned int data_size, short width,
             short height) {
  FILE *fp = fopen(filename, "wb");
  unsigned int i = 0;
  if (fp == NULL)
    return -1;
  putc(0, fp);
  putc(0, fp);
  putc(3, fp);
  putc(0, fp);
  putc(0, fp);
  putc(0, fp);
  putc(0, fp);
  putc(0, fp);
  putc(0, fp);
  putc(0, fp);
  putc(0, fp);
  putc(0, fp);
  putc((width & 0x00FF), fp);
  putc((width & 0xFF00) / 256, fp);
  putc((height & 0x00FF), fp);
  putc((height & 0xFF00) / 256, fp);
  putc(8, fp);
  putc(0, fp);
  for (i = 0; i < data_size; i++, putc(data[i], fp))
    ;
  fclose(fp);
}


x86 assembly (clang, -O0):
<tga_save>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %r8w,%ax
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %cx,-0x1e(%rbp)
mov    %ax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xea6(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1181 <tga_save+0x51>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ca <tga_save+0x19a>
mov    -0x28(%rbp),%rsi
xor    %edi,%edi
call   1040 <putc@plt>
mov    -0x28(%rbp),%rsi
xor    %edi,%edi
call   1040 <putc@plt>
mov    -0x28(%rbp),%rsi
mov    $0x3,%edi
call   1040 <putc@plt>
mov    -0x28(%rbp),%rsi
xor    %edi,%edi
call   1040 <putc@plt>
mov    -0x28(%rbp),%rsi
xor    %edi,%edi
call   1040 <putc@plt>
mov    -0x28(%rbp),%rsi
xor    %edi,%edi
call   1040 <putc@plt>
mov    -0x28(%rbp),%rsi
xor    %edi,%edi
call   1040 <putc@plt>
mov    -0x28(%rbp),%rsi
xor    %edi,%edi
call   1040 <putc@plt>
mov    -0x28(%rbp),%rsi
xor    %edi,%edi
call   1040 <putc@plt>
mov    -0x28(%rbp),%rsi
xor    %edi,%edi
call   1040 <putc@plt>
mov    -0x28(%rbp),%rsi
xor    %edi,%edi
call   1040 <putc@plt>
mov    -0x28(%rbp),%rsi
xor    %edi,%edi
call   1040 <putc@plt>
movswl -0x1e(%rbp),%eax
and    $0xff,%eax
mov    -0x28(%rbp),%rsi
movsbl %al,%edi
call   1040 <putc@plt>
movswl -0x1e(%rbp),%eax
and    $0xff00,%eax
mov    $0x100,%ecx
cltd
idiv   %ecx
mov    -0x28(%rbp),%rsi
movsbl %al,%edi
call   1040 <putc@plt>
movswl -0x20(%rbp),%eax
and    $0xff,%eax
mov    -0x28(%rbp),%rsi
movsbl %al,%edi
call   1040 <putc@plt>
movswl -0x20(%rbp),%eax
and    $0xff00,%eax
mov    $0x100,%ecx
cltd
idiv   %ecx
mov    -0x28(%rbp),%rsi
movsbl %al,%edi
call   1040 <putc@plt>
mov    -0x28(%rbp),%rsi
mov    $0x8,%edi
call   1040 <putc@plt>
mov    -0x28(%rbp),%rsi
xor    %edi,%edi
call   1040 <putc@plt>
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jae    12c1 <tga_save+0x191>
jmp    129d <tga_save+0x16d>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x18(%rbp),%rax
mov    -0x2c(%rbp),%ecx
mov    (%rax,%rcx,1),%al
mov    -0x28(%rbp),%rsi
movsbl %al,%edi
call   1040 <putc@plt>
jmp    128c <tga_save+0x15c>
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tga_save>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r8d,%r12d
mov    %ecx,%ebp
mov    %edx,%r15d
mov    %rsi,%r14
lea    0xeb3(%rip),%rsi
call   1050 <fopen@plt>
test   %rax,%rax
je     125f <tga_save+0x12f>
mov    %rax,%r13
xor    %edi,%edi
mov    %rax,%rsi
call   1040 <putc@plt>
xor    %edi,%edi
mov    %r13,%rsi
call   1040 <putc@plt>
mov    $0x3,%edi
mov    %r13,%rsi
call   1040 <putc@plt>
xor    %edi,%edi
mov    %r13,%rsi
call   1040 <putc@plt>
xor    %edi,%edi
mov    %r13,%rsi
call   1040 <putc@plt>
xor    %edi,%edi
mov    %r13,%rsi
call   1040 <putc@plt>
xor    %edi,%edi
mov    %r13,%rsi
call   1040 <putc@plt>
xor    %edi,%edi
mov    %r13,%rsi
call   1040 <putc@plt>
xor    %edi,%edi
mov    %r13,%rsi
call   1040 <putc@plt>
xor    %edi,%edi
mov    %r13,%rsi
call   1040 <putc@plt>
xor    %edi,%edi
mov    %r13,%rsi
call   1040 <putc@plt>
xor    %edi,%edi
mov    %r13,%rsi
call   1040 <putc@plt>
movsbl %bpl,%edi
mov    %r13,%rsi
call   1040 <putc@plt>
shr    $0x8,%ebp
movsbl %bpl,%edi
mov    %r13,%rsi
call   1040 <putc@plt>
movsbl %r12b,%edi
mov    %r13,%rsi
call   1040 <putc@plt>
shr    $0x8,%r12d
movsbl %r12b,%edi
mov    %r13,%rsi
call   1040 <putc@plt>
mov    $0x8,%edi
mov    %r13,%rsi
call   1040 <putc@plt>
xor    %edi,%edi
mov    %r13,%rsi
call   1040 <putc@plt>
test   %r15d,%r15d
je     1257 <tga_save+0x127>
mov    %r15d,%ebp
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
movsbl 0x1(%r14,%rbx,1),%edi
add    $0x1,%rbx
mov    %r13,%rsi
call   1040 <putc@plt>
cmp    %rbx,%rbp
jne    1240 <tga_save+0x110>
mov    %r13,%rdi
call   1030 <fclose@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"wb\00"
define i32 @tga_save(i8* %0, i8* %1, i32 %2, i16 signext %3, i16 signext %4) {
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i16
%11 = alloca i16
%12 = alloca i32*
%13 = alloca i32
store i8* %0, i8** %7
store i8* %1, i8** %8
store i32 %2, i32* %9
store i16 %3, i16* %10
store i16 %4, i16* %11
%14 = load i8*, i8** %7
%15 = call i32* @fopen(i8* %14, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %15, i32** %12
store i32 0, i32* %13
%16 = load i32*, i32** %12
%17 = icmp eq i32* %16, null
br i1 %17, label %18, label %19
18: ; preds = %5
store i32 -1, i32* %6
br label %92
19: ; preds = %5
%20 = load i32*, i32** %12
%21 = call i32 @putc(i8 signext 0, i32* %20)
%22 = load i32*, i32** %12
%23 = call i32 @putc(i8 signext 0, i32* %22)
%24 = load i32*, i32** %12
%25 = call i32 @putc(i8 signext 3, i32* %24)
%26 = load i32*, i32** %12
%27 = call i32 @putc(i8 signext 0, i32* %26)
%28 = load i32*, i32** %12
%29 = call i32 @putc(i8 signext 0, i32* %28)
%30 = load i32*, i32** %12
%31 = call i32 @putc(i8 signext 0, i32* %30)
%32 = load i32*, i32** %12
%33 = call i32 @putc(i8 signext 0, i32* %32)
%34 = load i32*, i32** %12
%35 = call i32 @putc(i8 signext 0, i32* %34)
%36 = load i32*, i32** %12
%37 = call i32 @putc(i8 signext 0, i32* %36)
%38 = load i32*, i32** %12
%39 = call i32 @putc(i8 signext 0, i32* %38)
%40 = load i32*, i32** %12
%41 = call i32 @putc(i8 signext 0, i32* %40)
%42 = load i32*, i32** %12
%43 = call i32 @putc(i8 signext 0, i32* %42)
%44 = load i16, i16* %10
%45 = sext i16 %44 to i32
%46 = and i32 %45, 255
%47 = trunc i32 %46 to i8
%48 = load i32*, i32** %12
%49 = call i32 @putc(i8 signext %47, i32* %48)
%50 = load i16, i16* %10
%51 = sext i16 %50 to i32
%52 = and i32 %51, 65280
%53 = sdiv i32 %52, 256
%54 = trunc i32 %53 to i8
%55 = load i32*, i32** %12
%56 = call i32 @putc(i8 signext %54, i32* %55)
%57 = load i16, i16* %11
%58 = sext i16 %57 to i32
%59 = and i32 %58, 255
%60 = trunc i32 %59 to i8
%61 = load i32*, i32** %12
%62 = call i32 @putc(i8 signext %60, i32* %61)
%63 = load i16, i16* %11
%64 = sext i16 %63 to i32
%65 = and i32 %64, 65280
%66 = sdiv i32 %65, 256
%67 = trunc i32 %66 to i8
%68 = load i32*, i32** %12
%69 = call i32 @putc(i8 signext %67, i32* %68)
%70 = load i32*, i32** %12
%71 = call i32 @putc(i8 signext 8, i32* %70)
%72 = load i32*, i32** %12
%73 = call i32 @putc(i8 signext 0, i32* %72)
store i32 0, i32* %13
br label %74
74: ; preds = %79, %19
%75 = load i32, i32* %13
%76 = load i32, i32* %9
%77 = icmp ult i32 %75, %76
br i1 %77, label %78, label %89
78: ; preds = %74
br label %79
79: ; preds = %78
%80 = load i32, i32* %13
%81 = add i32 %80, 1
store i32 %81, i32* %13
%82 = load i8*, i8** %8
%83 = load i32, i32* %13
%84 = zext i32 %83 to i64
%85 = getelementptr inbounds i8, i8* %82, i64 %84
%86 = load i8, i8* %85
%87 = load i32*, i32** %12
%88 = call i32 @putc(i8 signext %86, i32* %87)
br label %74
89: ; preds = %74
%90 = load i32*, i32** %12
%91 = call i32 @fclose(i32* %90)
br label %92
92: ; preds = %89, %18
%93 = load i32, i32* %6
ret i32 %93
}

LLVM IR (-O2):
@.str = [3 x i8] c"wb\00"
define i32 @tga_save(i8* nocapture %0, i8* nocapture %1, i32 %2, i16 signext %3, i16 signext %4) {
%6 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%7 = icmp eq i32* %6, null
br i1 %7, label %45, label %8
8: ; preds = %5
%9 = tail call i32 @putc(i8 signext 0, i32* %6)
%10 = tail call i32 @putc(i8 signext 0, i32* %6)
%11 = tail call i32 @putc(i8 signext 3, i32* %6)
%12 = tail call i32 @putc(i8 signext 0, i32* %6)
%13 = tail call i32 @putc(i8 signext 0, i32* %6)
%14 = tail call i32 @putc(i8 signext 0, i32* %6)
%15 = tail call i32 @putc(i8 signext 0, i32* %6)
%16 = tail call i32 @putc(i8 signext 0, i32* %6)
%17 = tail call i32 @putc(i8 signext 0, i32* %6)
%18 = tail call i32 @putc(i8 signext 0, i32* %6)
%19 = tail call i32 @putc(i8 signext 0, i32* %6)
%20 = tail call i32 @putc(i8 signext 0, i32* %6)
%21 = trunc i16 %3 to i8
%22 = tail call i32 @putc(i8 signext %21, i32* %6)
%23 = lshr i16 %3, 8
%24 = trunc i16 %23 to i8
%25 = tail call i32 @putc(i8 signext %24, i32* %6)
%26 = trunc i16 %4 to i8
%27 = tail call i32 @putc(i8 signext %26, i32* %6)
%28 = lshr i16 %4, 8
%29 = trunc i16 %28 to i8
%30 = tail call i32 @putc(i8 signext %29, i32* %6)
%31 = tail call i32 @putc(i8 signext 8, i32* %6)
%32 = tail call i32 @putc(i8 signext 0, i32* %6)
%33 = icmp eq i32 %2, 0
br i1 %33, label %43, label %34
34: ; preds = %8
%35 = zext i32 %2 to i64
br label %36
36: ; preds = %34, %36
%37 = phi i64 [ 0, %34 ], [ %38, %36 ]
%38 = add nuw nsw i64 %37, 1
%39 = getelementptr inbounds i8, i8* %1, i64 %38
%40 = load i8, i8* %39
%41 = tail call i32 @putc(i8 signext %40, i32* %6)
%42 = icmp eq i64 %38, %35
br i1 %42, label %43, label %36
43: ; preds = %36, %8
%44 = tail call i32 @fclose(i32* %6)
br label %45
45: ; preds = %5, %43
ret i32 -1
}