#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*/ SIGN;
typedef int /*<<< orphan*/ HEADER;

/* Variables and functions */
int /*<<< orphan*/ gm_memcpy(unsigned char *, unsigned char *, int);
int SaveTGA(unsigned char *src, unsigned char *dst, unsigned char pal[768]) {
  unsigned char SIGN[] = "Generated by fMSX32!";
  unsigned char HEADER[] = {sizeof(SIGN), 1, 1, 0, 0, 0xFF, 0,
                            24,           0, 0, 0, 0, 0x40, 0x1,
                            0xF0,         0, 8, 0};
  int total = 0, x, y;
  gm_memcpy(&dst[total], HEADER, sizeof(HEADER));
  total += sizeof(HEADER);
  ;
  gm_memcpy(&dst[total], SIGN, sizeof(SIGN));
  total += sizeof(SIGN);
  ;
  gm_memcpy(&dst[total], pal, 768);
  total += 768;
  for (y = 0; y < 240; y++)
    for (x = 0; x < 320; x++)
      dst[total++] = src[(240 - y) + x * 240];
  return total;
}


x86 assembly (clang, -O0):
<SaveTGA>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    0xed5(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    0xed2(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    0xed0(%rip),%eax
mov    %eax,-0x20(%rbp)
mov    0xecb(%rip),%al
mov    %al,-0x1c(%rbp)
mov    0xecd(%rip),%rax
mov    %rax,-0x50(%rbp)
mov    0xeca(%rip),%rax
mov    %rax,-0x48(%rbp)
mov    0xec7(%rip),%ax
mov    %ax,-0x40(%rbp)
movl   $0x0,-0x54(%rbp)
mov    -0x10(%rbp),%rdi
movslq -0x54(%rbp),%rax
add    %rax,%rdi
lea    -0x50(%rbp),%rsi
mov    $0x12,%edx
call   1030 <gm_memcpy@plt>
movslq -0x54(%rbp),%rax
add    $0x12,%rax
mov    %eax,-0x54(%rbp)
mov    -0x10(%rbp),%rdi
movslq -0x54(%rbp),%rax
add    %rax,%rdi
lea    -0x30(%rbp),%rsi
mov    $0x15,%edx
call   1030 <gm_memcpy@plt>
movslq -0x54(%rbp),%rax
add    $0x15,%rax
mov    %eax,-0x54(%rbp)
mov    -0x10(%rbp),%rdi
movslq -0x54(%rbp),%rax
add    %rax,%rdi
mov    -0x18(%rbp),%rsi
mov    $0x300,%edx
call   1030 <gm_memcpy@plt>
mov    -0x54(%rbp),%eax
add    $0x300,%eax
mov    %eax,-0x54(%rbp)
movl   $0x0,-0x5c(%rbp)
cmpl   $0xf0,-0x5c(%rbp)
jge    1259 <SaveTGA+0x149>
movl   $0x0,-0x58(%rbp)
cmpl   $0x140,-0x58(%rbp)
jge    1246 <SaveTGA+0x136>
mov    -0x8(%rbp),%rax
mov    $0xf0,%ecx
sub    -0x5c(%rbp),%ecx
imul   $0xf0,-0x58(%rbp),%edx
add    %edx,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,1),%dl
mov    -0x10(%rbp),%rax
mov    -0x54(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x54(%rbp)
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x58(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x58(%rbp)
jmp    11fb <SaveTGA+0xeb>
jmp    124b <SaveTGA+0x13b>
mov    -0x5c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x5c(%rbp)
jmp    11e7 <SaveTGA+0xd7>
mov    -0x54(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<SaveTGA>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %rdx,%r15
mov    %rsi,%r14
mov    %rdi,%r12
movaps 0xed5(%rip),%xmm0
movaps %xmm0,0x20(%rsp)
movabs $0x21323358534d66,%rax
mov    %rax,0x2d(%rsp)
movaps 0xeda(%rip),%xmm0
movaps %xmm0,(%rsp)
movw   $0x8,0x10(%rsp)
mov    %rsp,%rsi
mov    %r14,%rdi
mov    $0x12,%edx
call   1030 <gm_memcpy@plt>
lea    0x12(%r14),%rdi
lea    0x20(%rsp),%rsi
mov    $0x15,%edx
call   1030 <gm_memcpy@plt>
lea    0x27(%r14),%rdi
mov    %r15,%rsi
mov    $0x300,%edx
call   1030 <gm_memcpy@plt>
add    $0x1,%r14
add    $0x1e0,%r12
mov    $0x327,%edx
xor    %r8d,%r8d
nopl   0x0(%rax,%rax,1)
mov    %rdx,%rax
movslq %eax,%rdx
mov    %r12,%rdi
mov    %r14,%rbx
xor    %esi,%esi
xchg   %ax,%ax
movzbl -0xf0(%rdi),%ecx
mov    %cl,-0x1(%rbx,%rdx,1)
movzbl (%rdi),%ecx
mov    %cl,(%rbx,%rdx,1)
add    $0xfffffffffffffffe,%rsi
add    $0x2,%rbx
add    $0x1e0,%rdi
cmp    $0xfffffffffffffec0,%rsi
jne    11b0 <SaveTGA+0xa0>
add    $0x1,%r8
sub    %rsi,%rdx
add    $0xffffffffffffffff,%r12
cmp    $0xf0,%r8
jne    11a0 <SaveTGA+0x90>
sub    %esi,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@__const.SaveTGA.SIGN = [21 x i8] c"Generated by fMSX32!\00"
@__const.SaveTGA.HEADER = [18 x i8] c"\15\01\01\00\00\FF\00\18\00\00\00\00@\01\F0\00\08\00"
define i32 @SaveTGA(i8* %0, i8* %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca [21 x i8]
%8 = alloca [18 x i8]
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i8* %2, i8** %6
%12 = bitcast [21 x i8]* %7 to i8*
%13 = bitcast [18 x i8]* %8 to i8*
store i32 0, i32* %9
%14 = load i8*, i8** %5
%15 = load i32, i32* %9
%16 = sext i32 %15 to i64
%17 = getelementptr inbounds i8, i8* %14, i64 %16
%18 = getelementptr inbounds [18 x i8], [18 x i8]* %8, i64 0, i64 0
%19 = call i32 @gm_memcpy(i8* %17, i8* %18, i32 18)
%20 = load i32, i32* %9
%21 = sext i32 %20 to i64
%22 = add i64 %21, 18
%23 = trunc i64 %22 to i32
store i32 %23, i32* %9
%24 = load i8*, i8** %5
%25 = load i32, i32* %9
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds i8, i8* %24, i64 %26
%28 = getelementptr inbounds [21 x i8], [21 x i8]* %7, i64 0, i64 0
%29 = call i32 @gm_memcpy(i8* %27, i8* %28, i32 21)
%30 = load i32, i32* %9
%31 = sext i32 %30 to i64
%32 = add i64 %31, 21
%33 = trunc i64 %32 to i32
store i32 %33, i32* %9
%34 = load i8*, i8** %5
%35 = load i32, i32* %9
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds i8, i8* %34, i64 %36
%38 = load i8*, i8** %6
%39 = call i32 @gm_memcpy(i8* %37, i8* %38, i32 768)
%40 = load i32, i32* %9
%41 = add nsw i32 %40, 768
store i32 %41, i32* %9
store i32 0, i32* %11
br label %42
42: ; preds = %68, %3
%43 = load i32, i32* %11
%44 = icmp slt i32 %43, 240
br i1 %44, label %45, label %71
45: ; preds = %42
store i32 0, i32* %10
br label %46
46: ; preds = %64, %45
%47 = load i32, i32* %10
%48 = icmp slt i32 %47, 320
br i1 %48, label %49, label %67
49: ; preds = %46
%50 = load i8*, i8** %4
%51 = load i32, i32* %11
%52 = sub nsw i32 240, %51
%53 = load i32, i32* %10
%54 = mul nsw i32 %53, 240
%55 = add nsw i32 %52, %54
%56 = sext i32 %55 to i64
%57 = getelementptr inbounds i8, i8* %50, i64 %56
%58 = load i8, i8* %57
%59 = load i8*, i8** %5
%60 = load i32, i32* %9
%61 = add nsw i32 %60, 1
store i32 %61, i32* %9
%62 = sext i32 %60 to i64
%63 = getelementptr inbounds i8, i8* %59, i64 %62
store i8 %58, i8* %63
br label %64
64: ; preds = %49
%65 = load i32, i32* %10
%66 = add nsw i32 %65, 1
store i32 %66, i32* %10
br label %46
67: ; preds = %46
br label %68
68: ; preds = %67
%69 = load i32, i32* %11
%70 = add nsw i32 %69, 1
store i32 %70, i32* %11
br label %42
71: ; preds = %42
%72 = load i32, i32* %9
ret i32 %72
}

LLVM IR (-O2):
@__const.SaveTGA.SIGN = [21 x i8] c"Generated by fMSX32!\00"
@__const.SaveTGA.HEADER = [18 x i8] c"\15\01\01\00\00\FF\00\18\00\00\00\00@\01\F0\00\08\00"
define i32 @SaveTGA(i8* nocapture %0, i8* %1, i8* %2) {
%4 = alloca [21 x i8]
%5 = alloca [18 x i8]
%6 = getelementptr inbounds [21 x i8], [21 x i8]* %4, i64 0, i64 0
%7 = getelementptr inbounds [18 x i8], [18 x i8]* %5, i64 0, i64 0
%8 = call i32 @gm_memcpy(i8* %1, i8* %7, i32 18)
%9 = getelementptr inbounds i8, i8* %1, i64 18
%10 = call i32 @gm_memcpy(i8* %9, i8* %6, i32 21)
%11 = getelementptr inbounds i8, i8* %1, i64 39
%12 = call i32 @gm_memcpy(i8* %11, i8* %2, i32 768)
br label %13
13: ; preds = %3, %37
%14 = phi i64 [ 0, %3 ], [ %38, %37 ]
%15 = phi i64 [ 807, %3 ], [ %33, %37 ]
%16 = sub nuw nsw i64 240, %14
%17 = shl i64 %15, 32
%18 = ashr exact i64 %17, 32
br label %19
19: ; preds = %19, %13
%20 = phi i64 [ %18, %13 ], [ %33, %19 ]
%21 = phi i64 [ 0, %13 ], [ %35, %19 ]
%22 = mul nuw nsw i64 %21, 240
%23 = add nuw nsw i64 %16, %22
%24 = getelementptr inbounds i8, i8* %0, i64 %23
%25 = load i8, i8* %24
%26 = add nsw i64 %20, 1
%27 = getelementptr inbounds i8, i8* %1, i64 %20
store i8 %25, i8* %27
%28 = or i64 %21, 1
%29 = mul nuw nsw i64 %28, 240
%30 = add nuw nsw i64 %16, %29
%31 = getelementptr inbounds i8, i8* %0, i64 %30
%32 = load i8, i8* %31
%33 = add nsw i64 %20, 2
%34 = getelementptr inbounds i8, i8* %1, i64 %26
store i8 %32, i8* %34
%35 = add nuw nsw i64 %21, 2
%36 = icmp eq i64 %35, 320
br i1 %36, label %37, label %19
37: ; preds = %19
%38 = add nuw nsw i64 %14, 1
%39 = icmp eq i64 %38, 240
br i1 %39, label %40, label %13
40: ; preds = %37
%41 = trunc i64 %33 to i32
ret i32 %41
}