#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 */
char *create_debug_filename();
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ fwrite(unsigned char const *, size_t, int,
                          int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
void write_to_disk(const unsigned char *data, size_t size) {
  FILE *f;
  char *filename = create_debug_filename();
  f = fopen(filename, "w");
  if (f) {
    fwrite(data, size, 1, f);
    fclose(f);
    fprintf(stderr, "DEBUG: Frame written to file '%s'\n", filename);
  }
  free(filename);
}


x86 assembly (clang, -O0):
<write_to_disk>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x0,%al
call   1050 <create_debug_filename@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
lea    0xe7a(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11d2 <write_to_disk+0x72>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rcx
mov    $0x1,%edx
call   1080 <fwrite@plt>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
mov    0x2e20(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rdx
lea    0xe35(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_to_disk>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r15
xor    %eax,%eax
call   1050 <create_debug_filename@plt>
mov    %rax,%r12
lea    0xe81(%rip),%rsi
mov    %rax,%rdi
call   1070 <fopen@plt>
test   %rax,%rax
je     11c2 <write_to_disk+0x62>
mov    %rax,%rbx
mov    %r15,%rdi
mov    %r14,%rsi
mov    $0x1,%edx
mov    %rax,%rcx
call   1080 <fwrite@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    0x2e2f(%rip),%rax
mov    (%rax),%edi
lea    0xe48(%rip),%rsi
mov    %r12,%rdx
call   1060 <fprintf@plt>
mov    %r12,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
jmp    1030 <free@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"w\00"
@stderr = global i32 0
@.str.1 = [35 x i8] c"DEBUG: Frame written to file '%s'\0A\00"
define void @write_to_disk(i8* %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i32*
%6 = alloca i8*
store i8* %0, i8** %3
store i64 %1, i64* %4
%7 = call i8* (...) @create_debug_filename()
store i8* %7, i8** %6
%8 = load i8*, i8** %6
%9 = call i32* @fopen(i8* %8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %9, i32** %5
%10 = load i32*, i32** %5
%11 = icmp ne i32* %10, null
br i1 %11, label %12, label %22
12: ; preds = %2
%13 = load i8*, i8** %3
%14 = load i64, i64* %4
%15 = load i32*, i32** %5
%16 = call i32 @fwrite(i8* %13, i64 %14, i32 1, i32* %15)
%17 = load i32*, i32** %5
%18 = call i32 @fclose(i32* %17)
%19 = load i32, i32* @stderr
%20 = load i8*, i8** %6
%21 = call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i8* %20)
br label %22
22: ; preds = %12, %2
%23 = load i8*, i8** %6
%24 = call i32 @free(i8* %23)
ret void
}

LLVM IR (-O2):
@.str = [2 x i8] c"w\00"
@stderr = global i32 0
@.str.1 = [35 x i8] c"DEBUG: Frame written to file '%s'\0A\00"
define void @write_to_disk(i8* nocapture %0, i64 %1) {
%3 = tail call i8* (...) @create_debug_filename()
%4 = tail call i32* @fopen(i8* %3, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %11, label %6
6: ; preds = %2
%7 = tail call i32 @fwrite(i8* %0, i64 %1, i32 1, i32* %4)
%8 = tail call i32 @fclose(i32* %4)
%9 = load i32, i32* @stderr
%10 = tail call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i8* %3)
br label %11
11: ; preds = %6, %2
%12 = tail call i32 @free(i8* %3)
ret void
}