#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 */

/* Variables and functions */
int /*<<< orphan*/ closetgafile(void *);
int createtgafile(char *, unsigned short, unsigned short);
void *opentgafile(char *);
int /*<<< orphan*/ writetgaregion(void *, int, int, int, int, unsigned char *);
int writetga(char *name, int xres, int yres, unsigned char *imgdata) {
  void *outfile;
  int rc = 0;
  rc = createtgafile(name, (unsigned short)xres, (unsigned short)yres);
  if (rc == 0) {
    outfile = opentgafile(name);
    if (outfile == NULL)
      return 5;
    writetgaregion(outfile, 1, 1, xres, yres, imgdata);
    closetgafile(outfile);
  }
  return rc;
}


x86 assembly (clang, -O0):
<writetga>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%eax
mov    %ax,%cx
mov    -0x18(%rbp),%eax
movzwl %cx,%esi
movzwl %ax,%edx
call   1060 <createtgafile@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jne    11ca <writetga+0x8a>
mov    -0x10(%rbp),%rdi
call   1050 <opentgafile@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11a6 <writetga+0x66>
movl   $0x5,-0x4(%rbp)
jmp    11d0 <writetga+0x90>
mov    -0x28(%rbp),%rdi
mov    -0x14(%rbp),%ecx
mov    -0x18(%rbp),%r8d
mov    -0x20(%rbp),%r9
mov    $0x1,%edx
mov    %edx,%esi
call   1030 <writetgaregion@plt>
mov    -0x28(%rbp),%rdi
call   1040 <closetgafile@plt>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<writetga>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rcx,%r14
mov    %edx,%r15d
mov    %esi,%ebx
mov    %rdi,%rbp
movzwl %bx,%esi
movzwl %r15w,%edx
call   1060 <createtgafile@plt>
test   %eax,%eax
jne    119d <writetga+0x5d>
mov    %rbp,%rdi
call   1050 <opentgafile@plt>
test   %rax,%rax
je     1198 <writetga+0x58>
mov    %rax,%rbp
mov    %rax,%rdi
mov    $0x1,%esi
mov    $0x1,%edx
mov    %ebx,%ecx
mov    %r15d,%r8d
mov    %r14,%r9
call   1030 <writetgaregion@plt>
mov    %rbp,%rdi
call   1040 <closetgafile@plt>
xor    %eax,%eax
jmp    119d <writetga+0x5d>
mov    $0x5,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @writetga(i8* %0, i32 %1, i32 %2, i8* %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i32
store i8* %0, i8** %6
store i32 %1, i32* %7
store i32 %2, i32* %8
store i8* %3, i8** %9
store i32 0, i32* %11
%12 = load i8*, i8** %6
%13 = load i32, i32* %7
%14 = trunc i32 %13 to i16
%15 = load i32, i32* %8
%16 = trunc i32 %15 to i16
%17 = call i32 @createtgafile(i8* %12, i16 zeroext %14, i16 zeroext %16)
store i32 %17, i32* %11
%18 = load i32, i32* %11
%19 = icmp eq i32 %18, 0
br i1 %19, label %20, label %34
20: ; preds = %4
%21 = load i8*, i8** %6
%22 = call i8* @opentgafile(i8* %21)
store i8* %22, i8** %10
%23 = load i8*, i8** %10
%24 = icmp eq i8* %23, null
br i1 %24, label %25, label %26
25: ; preds = %20
store i32 5, i32* %5
br label %36
26: ; preds = %20
%27 = load i8*, i8** %10
%28 = load i32, i32* %7
%29 = load i32, i32* %8
%30 = load i8*, i8** %9
%31 = call i32 @writetgaregion(i8* %27, i32 1, i32 1, i32 %28, i32 %29, i8* %30)
%32 = load i8*, i8** %10
%33 = call i32 @closetgafile(i8* %32)
br label %34
34: ; preds = %26, %4
%35 = load i32, i32* %11
store i32 %35, i32* %5
br label %36
36: ; preds = %34, %25
%37 = load i32, i32* %5
ret i32 %37
}

LLVM IR (-O2):
define i32 @writetga(i8* %0, i32 %1, i32 %2, i8* %3) {
%5 = trunc i32 %1 to i16
%6 = trunc i32 %2 to i16
%7 = tail call i32 @createtgafile(i8* %0, i16 zeroext %5, i16 zeroext %6)
%8 = icmp eq i32 %7, 0
br i1 %8, label %9, label %15
9: ; preds = %4
%10 = tail call i8* @opentgafile(i8* %0)
%11 = icmp eq i8* %10, null
br i1 %11, label %15, label %12
12: ; preds = %9
%13 = tail call i32 @writetgaregion(i8* %10, i32 1, i32 1, i32 %1, i32 %2, i8* %3)
%14 = tail call i32 @closetgafile(i8* %10)
br label %15
15: ; preds = %4, %12, %9
%16 = phi i32 [ 5, %9 ], [ 0, %12 ], [ %7, %4 ]
ret i32 %16
}