#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*/ Debug_ErrorMsgArg(char *);
char *FU_GetNewTimestampFilename(char *);
int /*<<< orphan*/ GBA_ConvertScreenBufferTo24RGB(unsigned char *);
int /*<<< orphan*/ Save_PNG(char const *, unsigned char *, int, int,
                            int /*<<< orphan*/);
int /*<<< orphan*/ free(unsigned char *);
unsigned char *malloc(int);
void GBA_Screenshot(const char *path) {
  unsigned char *buffer = malloc(240 * 160 * 3);
  if (buffer == NULL) {
    Debug_ErrorMsgArg("%s(): Not enough memory.");
    return;
  }
  GBA_ConvertScreenBufferTo24RGB(buffer);
  if (path) {
    Save_PNG(path, buffer, 240, 160, 0);
  } else {
    char *name = FU_GetNewTimestampFilename("gba_screenshot");
    Save_PNG(name, buffer, 240, 160, 0);
  }
  free(buffer);
}


x86 assembly (clang, -O0):
<GBA_Screenshot>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    $0x1c200,%edi
call   1060 <malloc@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1196 <GBA_Screenshot+0x36>
lea    0xe74(%rip),%rdi
call   1080 <Debug_ErrorMsgArg@plt>
jmp    11fc <GBA_Screenshot+0x9c>
mov    -0x10(%rbp),%rdi
call   1040 <GBA_ConvertScreenBufferTo24RGB@plt>
cmpq   $0x0,-0x8(%rbp)
je     11c9 <GBA_Screenshot+0x69>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    $0xf0,%edx
mov    $0xa0,%ecx
xor    %r8d,%r8d
call   1070 <Save_PNG@plt>
jmp    11f3 <GBA_Screenshot+0x93>
lea    0xe49(%rip),%rdi
call   1050 <FU_GetNewTimestampFilename@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    $0xf0,%edx
mov    $0xa0,%ecx
xor    %r8d,%r8d
call   1070 <Save_PNG@plt>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<GBA_Screenshot>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
mov    $0x1c200,%edi
call   1060 <malloc@plt>
test   %rax,%rax
je     118b <GBA_Screenshot+0x2b>
mov    %rax,%rbx
mov    %rax,%rdi
call   1040 <GBA_ConvertScreenBufferTo24RGB@plt>
test   %r14,%r14
je     119e <GBA_Screenshot+0x3e>
mov    %r14,%rdi
jmp    11ad <GBA_Screenshot+0x4d>
lea    0xe6e(%rip),%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1080 <Debug_ErrorMsgArg@plt>
lea    0xe74(%rip),%rdi
call   1050 <FU_GetNewTimestampFilename@plt>
mov    %rax,%rdi
mov    %rbx,%rsi
mov    $0xf0,%edx
mov    $0xa0,%ecx
xor    %r8d,%r8d
call   1070 <Save_PNG@plt>
mov    %rbx,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1030 <free@plt>

LLVM IR (-O0):
@.str = [25 x i8] c"%s(): Not enough memory.\00"
@.str.1 = [15 x i8] c"gba_screenshot\00"
define void @GBA_Screenshot(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
store i8* %0, i8** %2
%5 = call i8* @malloc(i32 115200)
store i8* %5, i8** %3
%6 = load i8*, i8** %3
%7 = icmp eq i8* %6, null
br i1 %7, label %8, label %10
8: ; preds = %1
%9 = call i32 @Debug_ErrorMsgArg(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
br label %27
10: ; preds = %1
%11 = load i8*, i8** %3
%12 = call i32 @GBA_ConvertScreenBufferTo24RGB(i8* %11)
%13 = load i8*, i8** %2
%14 = icmp ne i8* %13, null
br i1 %14, label %15, label %19
15: ; preds = %10
%16 = load i8*, i8** %2
%17 = load i8*, i8** %3
%18 = call i32 @Save_PNG(i8* %16, i8* %17, i32 240, i32 160, i32 0)
br label %24
19: ; preds = %10
%20 = call i8* @FU_GetNewTimestampFilename(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
store i8* %20, i8** %4
%21 = load i8*, i8** %4
%22 = load i8*, i8** %3
%23 = call i32 @Save_PNG(i8* %21, i8* %22, i32 240, i32 160, i32 0)
br label %24
24: ; preds = %19, %15
%25 = load i8*, i8** %3
%26 = call i32 @free(i8* %25)
br label %27
27: ; preds = %24, %8
ret void
}

LLVM IR (-O2):
@.str = [25 x i8] c"%s(): Not enough memory.\00"
@.str.1 = [15 x i8] c"gba_screenshot\00"
define void @GBA_Screenshot(i8* %0) {
%2 = tail call dereferenceable_or_null(115200) i8* @malloc(i32 115200)
%3 = icmp eq i8* %2, null
br i1 %3, label %4, label %6
4: ; preds = %1
%5 = tail call i32 @Debug_ErrorMsgArg(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
br label %16
6: ; preds = %1
%7 = tail call i32 @GBA_ConvertScreenBufferTo24RGB(i8* %2)
%8 = icmp eq i8* %0, null
br i1 %8, label %11, label %9
9: ; preds = %6
%10 = tail call i32 @Save_PNG(i8* %0, i8* %2, i32 240, i32 160, i32 0)
br label %14
11: ; preds = %6
%12 = tail call i8* @FU_GetNewTimestampFilename(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%13 = tail call i32 @Save_PNG(i8* %12, i8* %2, i32 240, i32 160, i32 0)
br label %14
14: ; preds = %11, %9
%15 = tail call i32 @free(i8* %2)
br label %16
16: ; preds = %14, %4
ret void
}