#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 const *, char *);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ sprintf(char *, char *, ...);
void data_to_file(const unsigned char *data, const int data_size,
                  const char *filename, const char *array_name) {
  FILE *data_file = fopen(filename, "w");
  fclose(data_file);
  data_file = fopen(filename, "a");
  char array_descriptor[100];
  sprintf(array_descriptor, "unsigned char %s[] = \"", array_name);
  fputs("\n", data_file);
  fputs(array_descriptor, data_file);
  char current_hex[4];
  for (int i = 0; i < data_size; i++) {
    sprintf(current_hex, "\\x%02x", data[i]);
    fputs(current_hex, data_file);
  }
  fputs("\";", data_file);
  fputs("\n", data_file);
  fclose(data_file);
}


x86 assembly (clang, -O0):
<data_to_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe9b(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x18(%rbp),%rdi
lea    0xe80(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x28(%rbp)
lea    -0x90(%rbp),%rdi
mov    -0x20(%rbp),%rdx
lea    0xe67(%rip),%rsi
mov    $0x0,%al
call   1060 <sprintf@plt>
mov    -0x28(%rbp),%rsi
lea    0xe6c(%rip),%rdi
call   1040 <fputs@plt>
lea    -0x90(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1040 <fputs@plt>
movl   $0x0,-0x98(%rbp)
mov    -0x98(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    1225 <data_to_file+0xe5>
lea    -0x94(%rbp),%rdi
mov    -0x8(%rbp),%rax
movslq -0x98(%rbp),%rcx
movzbl (%rax,%rcx,1),%edx
lea    0xe23(%rip),%rsi
mov    $0x0,%al
call   1060 <sprintf@plt>
lea    -0x94(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1040 <fputs@plt>
mov    -0x98(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x98(%rbp)
jmp    11ce <data_to_file+0x8e>
mov    -0x28(%rbp),%rsi
lea    0xdf4(%rip),%rdi
call   1040 <fputs@plt>
mov    -0x28(%rbp),%rsi
lea    0xddb(%rip),%rdi
call   1040 <fputs@plt>
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
add    $0xa0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<data_to_file>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x78,%rsp
mov    %rcx,%r15
mov    %rdx,%rbx
mov    %esi,%r12d
mov    %rdi,%r14
lea    0xe7f(%rip),%rsi
mov    %rdx,%rdi
call   1060 <fopen@plt>
mov    %rax,%rdi
call   1030 <fclose@plt>
lea    0xe6a(%rip),%rsi
mov    %rbx,%rdi
call   1060 <fopen@plt>
mov    %rax,%r13
lea    0xe5a(%rip),%rsi
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
mov    %r15,%rdx
xor    %eax,%eax
call   1070 <sprintf@plt>
mov    $0xa,%edi
mov    %r13,%rsi
call   1050 <fputc@plt>
mov    %rbp,%rdi
mov    %r13,%rsi
call   1040 <fputs@plt>
test   %r12d,%r12d
jle    1216 <data_to_file+0xb6>
mov    %r12d,%r12d
lea    0xe38(%rip),%r15
lea    0xc(%rsp),%rbp
xor    %ebx,%ebx
nopw   0x0(%rax,%rax,1)
movzbl (%r14,%rbx,1),%edx
mov    %rbp,%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   1070 <sprintf@plt>
mov    %rbp,%rdi
mov    %r13,%rsi
call   1040 <fputs@plt>
add    $0x1,%rbx
cmp    %rbx,%r12
jne    11f0 <data_to_file+0x90>
lea    0xe05(%rip),%rdi
mov    $0x2,%esi
mov    $0x1,%edx
mov    %r13,%rcx
call   1080 <fwrite@plt>
mov    $0xa,%edi
mov    %r13,%rsi
call   1050 <fputc@plt>
mov    %r13,%rdi
add    $0x78,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <fclose@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"w\00"
@.str.1 = [2 x i8] c"a\00"
@.str.2 = [23 x i8] c"unsigned char %s[] = \22\00"
@.str.3 = [2 x i8] c"\0A\00"
@.str.4 = [7 x i8] c"\\x%02x\00"
@.str.5 = [3 x i8] c"\22;\00"
define void @data_to_file(i8* %0, i32 %1, i8* %2, i8* %3) {
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32*
%10 = alloca [100 x i8]
%11 = alloca [4 x i8]
%12 = alloca i32
store i8* %0, i8** %5
store i32 %1, i32* %6
store i8* %2, i8** %7
store i8* %3, i8** %8
%13 = load i8*, i8** %7
%14 = call i32* @fopen(i8* %13, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %14, i32** %9
%15 = load i32*, i32** %9
%16 = call i32 @fclose(i32* %15)
%17 = load i8*, i8** %7
%18 = call i32* @fopen(i8* %17, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %18, i32** %9
%19 = getelementptr inbounds [100 x i8], [100 x i8]* %10, i64 0, i64 0
%20 = load i8*, i8** %8
%21 = call i32 (i8*, i8*, ...) @sprintf(i8* %19, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i8* %20)
%22 = load i32*, i32** %9
%23 = call i32 @fputs(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i32* %22)
%24 = getelementptr inbounds [100 x i8], [100 x i8]* %10, i64 0, i64 0
%25 = load i32*, i32** %9
%26 = call i32 @fputs(i8* %24, i32* %25)
store i32 0, i32* %12
br label %27
27: ; preds = %43, %4
%28 = load i32, i32* %12
%29 = load i32, i32* %6
%30 = icmp slt i32 %28, %29
br i1 %30, label %31, label %46
31: ; preds = %27
%32 = getelementptr inbounds [4 x i8], [4 x i8]* %11, i64 0, i64 0
%33 = load i8*, i8** %5
%34 = load i32, i32* %12
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds i8, i8* %33, i64 %35
%37 = load i8, i8* %36
%38 = zext i8 %37 to i32
%39 = call i32 (i8*, i8*, ...) @sprintf(i8* %32, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0), i32 %38)
%40 = getelementptr inbounds [4 x i8], [4 x i8]* %11, i64 0, i64 0
%41 = load i32*, i32** %9
%42 = call i32 @fputs(i8* %40, i32* %41)
br label %43
43: ; preds = %31
%44 = load i32, i32* %12
%45 = add nsw i32 %44, 1
store i32 %45, i32* %12
br label %27
46: ; preds = %27
%47 = load i32*, i32** %9
%48 = call i32 @fputs(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i32* %47)
%49 = load i32*, i32** %9
%50 = call i32 @fputs(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i32* %49)
%51 = load i32*, i32** %9
%52 = call i32 @fclose(i32* %51)
ret void
}

LLVM IR (-O2):
@.str = [2 x i8] c"w\00"
@.str.1 = [2 x i8] c"a\00"
@.str.2 = [23 x i8] c"unsigned char %s[] = \22\00"
@.str.4 = [7 x i8] c"\\x%02x\00"
@.str.5 = [3 x i8] c"\22;\00"
define void @data_to_file(i8* nocapture %0, i32 %1, i8* nocapture %2, i8* %3) {
%5 = alloca [100 x i8]
%6 = alloca [4 x i8]
%7 = tail call i32* @fopen(i8* %2, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%8 = tail call i32 @fclose(i32* %7)
%9 = tail call i32* @fopen(i8* %2, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%10 = getelementptr inbounds [100 x i8], [100 x i8]* %5, i64 0, i64 0
%11 = call i32 (i8*, i8*, ...) @sprintf(i8* %10, i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i8* %3)
%12 = tail call i32 @fputc(i32 10, i32* %9)
%13 = call i32 @fputs(i8* %10, i32* %9)
%14 = getelementptr inbounds [4 x i8], [4 x i8]* %6, i64 0, i64 0
%15 = icmp sgt i32 %1, 0
br i1 %15, label %16, label %18
16: ; preds = %4
%17 = zext i32 %1 to i64
br label %22
18: ; preds = %22, %4
%19 = tail call i64 @fwrite(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i64 2, i64 1, i32* %9)
%20 = tail call i32 @fputc(i32 10, i32* %9)
%21 = tail call i32 @fclose(i32* %9)
ret void
22: ; preds = %16, %22
%23 = phi i64 [ 0, %16 ], [ %29, %22 ]
%24 = getelementptr inbounds i8, i8* %0, i64 %23
%25 = load i8, i8* %24
%26 = zext i8 %25 to i32
%27 = call i32 (i8*, i8*, ...) @sprintf(i8* %14, i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0), i32 %26)
%28 = call i32 @fputs(i8* %14, i32* %9)
%29 = add nuw nsw i64 %23, 1
%30 = icmp eq i64 %29, %17
br i1 %30, label %18, label %22
}