#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 uint8_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, ...);
int /*<<< orphan*/ perror(char const *);
void save_c_source(const char *filename, const uint8_t *buf, int buf_len,
                   const char *var_name) {
  FILE *f;
  int i;
  f = fopen(filename, "wb");
  if (!f) {
    perror(filename);
    exit(1);
  }
  fprintf(f, "/* This file is automatically generated - do not edit */\n\n");
  fprintf(f, "const uint8_t %s[] = {\n", var_name);
  for (i = 0; i < buf_len; i++) {
    fprintf(f, " 0x%02x,", buf[i]);
    if ((i % 8) == 7 || (i == buf_len - 1))
      fprintf(f, "\n");
  }
  fprintf(f, "};\n");
  fclose(f);
}


x86 assembly (clang, -O0):
<save_c_source>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe8e(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1199 <save_c_source+0x49>
mov    -0x8(%rbp),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x28(%rbp),%rdi
lea    0xe5f(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rdx
lea    0xe84(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    123b <save_c_source+0xeb>
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rax
movslq -0x2c(%rbp),%rcx
mov    (%rax,%rcx,4),%edx
lea    0xe6c(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    -0x2c(%rbp),%eax
mov    $0x8,%ecx
cltd
idiv   %ecx
cmp    $0x7,%edx
je     1216 <save_c_source+0xc6>
mov    -0x2c(%rbp),%eax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jne    1228 <save_c_source+0xd8>
mov    -0x28(%rbp),%rdi
lea    0xe1b(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
jmp    122d <save_c_source+0xdd>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    11c8 <save_c_source+0x78>
mov    -0x28(%rbp),%rdi
lea    0xe19(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<save_c_source>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r12
mov    %edx,%r15d
mov    %rsi,%r14
mov    %rdi,%r13
lea    0xe72(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     1255 <save_c_source+0xe5>
mov    %rax,%rbx
lea    0xe5d(%rip),%rdi
mov    $0x3a,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   1090 <fwrite@plt>
lea    0xe7f(%rip),%rsi
mov    %rbx,%rdi
mov    %r12,%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
test   %r15d,%r15d
jle    1226 <save_c_source+0xb6>
lea    -0x1(%r15),%r12d
mov    %r15d,%r13d
lea    0xe77(%rip),%r15
xor    %ebp,%ebp
jmp    1206 <save_c_source+0x96>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    $0xa,%edi
mov    %rbx,%rsi
call   1040 <fputc@plt>
add    $0x1,%rbp
cmp    %rbp,%r13
je     1226 <save_c_source+0xb6>
mov    (%r14,%rbp,4),%edx
mov    %rbx,%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    %ebp,%eax
not    %eax
test   $0x7,%al
je     11f0 <save_c_source+0x80>
cmp    %rbp,%r12
jne    11fd <save_c_source+0x8d>
jmp    11f0 <save_c_source+0x80>
lea    0xe32(%rip),%rdi
mov    $0x3,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1090 <fwrite@plt>
mov    %rbx,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <fclose@plt>
mov    %r13,%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [3 x i8] c"wb\00"
@.str.1 = [59 x i8] c"/* This file is automatically generated - do not edit */\0A\0A\00"
@.str.2 = [24 x i8] c"const uint8_t %s[] = {\0A\00"
@.str.3 = [9 x i8] c" 0x%02x,\00"
@.str.4 = [2 x i8] c"\0A\00"
@.str.5 = [4 x i8] c"};\0A\00"
define void @save_c_source(i8* %0, i32* %1, i32 %2, i8* %3) {
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32*
%10 = alloca i32
store i8* %0, i8** %5
store i32* %1, i32** %6
store i32 %2, i32* %7
store i8* %3, i8** %8
%11 = load i8*, i8** %5
%12 = call i32* @fopen(i8* %11, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %12, i32** %9
%13 = load i32*, i32** %9
%14 = icmp ne i32* %13, null
br i1 %14, label %19, label %15
15: ; preds = %4
%16 = load i8*, i8** %5
%17 = call i32 @perror(i8* %16)
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %4
%20 = load i32*, i32** %9
%21 = call i32 (i32*, i8*, ...) @fprintf(i32* %20, i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str.1, i64 0, i64 0))
%22 = load i32*, i32** %9
%23 = load i8*, i8** %8
%24 = call i32 (i32*, i8*, ...) @fprintf(i32* %22, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i8* %23)
store i32 0, i32* %10
br label %25
25: ; preds = %49, %19
%26 = load i32, i32* %10
%27 = load i32, i32* %7
%28 = icmp slt i32 %26, %27
br i1 %28, label %29, label %52
29: ; preds = %25
%30 = load i32*, i32** %9
%31 = load i32*, i32** %6
%32 = load i32, i32* %10
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds i32, i32* %31, i64 %33
%35 = load i32, i32* %34
%36 = call i32 (i32*, i8*, ...) @fprintf(i32* %30, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i32 %35)
%37 = load i32, i32* %10
%38 = srem i32 %37, 8
%39 = icmp eq i32 %38, 7
br i1 %39, label %45, label %40
40: ; preds = %29
%41 = load i32, i32* %10
%42 = load i32, i32* %7
%43 = sub nsw i32 %42, 1
%44 = icmp eq i32 %41, %43
br i1 %44, label %45, label %48
45: ; preds = %40, %29
%46 = load i32*, i32** %9
%47 = call i32 (i32*, i8*, ...) @fprintf(i32* %46, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
br label %48
48: ; preds = %45, %40
br label %49
49: ; preds = %48
%50 = load i32, i32* %10
%51 = add nsw i32 %50, 1
store i32 %51, i32* %10
br label %25
52: ; preds = %25
%53 = load i32*, i32** %9
%54 = call i32 (i32*, i8*, ...) @fprintf(i32* %53, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0))
%55 = load i32*, i32** %9
%56 = call i32 @fclose(i32* %55)
ret void
}

LLVM IR (-O2):
@.str = [3 x i8] c"wb\00"
@.str.1 = [59 x i8] c"/* This file is automatically generated - do not edit */\0A\0A\00"
@.str.2 = [24 x i8] c"const uint8_t %s[] = {\0A\00"
@.str.3 = [9 x i8] c" 0x%02x,\00"
@.str.5 = [4 x i8] c"};\0A\00"
define void @save_c_source(i8* nocapture %0, i32* nocapture %1, i32 %2, i8* %3) {
%5 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %10
7: ; preds = %4
%8 = tail call i32 @perror(i8* %0)
%9 = tail call i32 @exit(i32 1)
unreachable
10: ; preds = %4
%11 = tail call i64 @fwrite(i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str.1, i64 0, i64 0), i64 58, i64 1, i32* %5)
%12 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %5, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i8* %3)
%13 = icmp sgt i32 %2, 0
br i1 %13, label %14, label %32
14: ; preds = %10
%15 = add nsw i32 %2, -1
%16 = zext i32 %15 to i64
%17 = zext i32 %2 to i64
br label %18
18: ; preds = %14, %29
%19 = phi i64 [ 0, %14 ], [ %30, %29 ]
%20 = getelementptr inbounds i32, i32* %1, i64 %19
%21 = load i32, i32* %20
%22 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %5, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i32 %21)
%23 = and i64 %19, 7
%24 = icmp eq i64 %23, 7
%25 = icmp eq i64 %19, %16
%26 = select i1 %24, i1 true, i1 %25
br i1 %26, label %27, label %29
27: ; preds = %18
%28 = tail call i32 @fputc(i32 10, i32* %5)
br label %29
29: ; preds = %18, %27
%30 = add nuw nsw i64 %19, 1
%31 = icmp eq i64 %30, %17
br i1 %31, label %32, label %18
32: ; preds = %29, %10
%33 = tail call i64 @fwrite(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i64 3, i64 1, i32* %5)
%34 = tail call i32 @fclose(i32* %5)
ret void
}