#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*/ fprintf(int /*<<< orphan*/ *, char *, ...);
int fread(char *, int, int, int /*<<< orphan*/ *);
void generate(const char *name, FILE *in, FILE *out) {
  char buf[12];
  char *glue = "";
  int len;
  int size = 0;
  fprintf(out, "static unsigned char %s_arr[] = {\n", name);
  while ((len = fread(buf, 1, 12, in)) > 0) {
    int col;
    size += len;
    fprintf(out, "    ");
    for (col = 0; col < len; ++col) {
      fprintf(out, "%s0x%02x", glue, (unsigned char)buf[col]);
      glue = ",";
    }
    fprintf(out, "\n");
  }
  fprintf(out, "};\n");
  fprintf(out, "struct membuf %s[1] = {{%s_arr, %d, %d}};\n", name, name, size,
          size);
}


x86 assembly (clang, -O0):
<generate>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
lea    0xf22(%rip),%rax
mov    %rax,-0x30(%rbp)
movl   $0x0,-0x38(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rdx
lea    0xeab(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
lea    -0x24(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
mov    $0xc,%edx
call   1030 <fread@plt>
mov    %eax,-0x34(%rbp)
cmp    $0x0,%eax
jle    11fc <generate+0xdc>
mov    -0x34(%rbp),%eax
add    -0x38(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe90(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movl   $0x0,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
cmp    -0x34(%rbp),%eax
jge    11e5 <generate+0xc5>
mov    -0x18(%rbp),%rdi
mov    -0x30(%rbp),%rdx
movslq -0x3c(%rbp),%rax
movzbl -0x24(%rbp,%rax,1),%ecx
lea    0xe63(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
lea    0xe5e(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
jmp    11a1 <generate+0x81>
mov    -0x18(%rbp),%rdi
lea    0xe6c(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
jmp    115c <generate+0x3c>
mov    -0x18(%rbp),%rdi
lea    0xe53(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rdx
mov    -0x8(%rbp),%rcx
mov    -0x38(%rbp),%r8d
mov    -0x38(%rbp),%r9d
lea    0xe0a(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<generate>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rdx,%r14
mov    %rsi,%rbp
mov    %rdi,%rdx
lea    0xea2(%rip),%rsi
xor    %ebx,%ebx
mov    %r14,%rdi
mov    %rdx,0x18(%rsp)
xor    %eax,%eax
call   1050 <fprintf@plt>
lea    0xc(%rsp),%rdi
mov    $0x1,%esi
mov    $0xc,%edx
mov    %rbp,0x20(%rsp)
mov    %rbp,%rcx
call   1030 <fread@plt>
test   %eax,%eax
jle    1253 <generate+0x113>
mov    %eax,%ebp
lea    0xec1(%rip),%r15
lea    0xe85(%rip),%r13
lea    0xe87(%rip),%r12
xor    %ebx,%ebx
jmp    11e6 <generate+0xa6>
xchg   %ax,%ax
mov    0x8(%rsp),%ebx
add    %r15d,%ebx
mov    $0xa,%edi
mov    %r14,%rsi
call   1040 <fputc@plt>
lea    0xc(%rsp),%rdi
mov    $0x1,%esi
mov    $0xc,%edx
mov    0x20(%rsp),%rcx
call   1030 <fread@plt>
mov    %eax,%ebp
mov    %r12,%r15
test   %eax,%eax
jle    1253 <generate+0x113>
mov    %ebx,0x8(%rsp)
mov    $0x4,%esi
mov    $0x1,%edx
lea    0xe28(%rip),%rdi
mov    %r14,%rcx
call   1060 <fwrite@plt>
movzbl 0xc(%rsp),%ecx
mov    %r14,%rdi
mov    %r13,%rsi
mov    %r15,%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
cmp    $0x1,%ebp
mov    %ebp,%r15d
je     11b0 <generate+0x70>
mov    %r15d,%ebp
mov    $0x1,%ebx
nopl   0x0(%rax,%rax,1)
movzbl 0xc(%rsp,%rbx,1),%ecx
mov    %r14,%rdi
mov    %r13,%rsi
mov    %r12,%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
add    $0x1,%rbx
cmp    %rbx,%rbp
jne    1230 <generate+0xf0>
jmp    11b0 <generate+0x70>
lea    0xe00(%rip),%rdi
mov    $0x3,%esi
mov    $0x1,%edx
mov    %r14,%rcx
call   1060 <fwrite@plt>
lea    0xdc0(%rip),%rsi
mov    %r14,%rdi
mov    0x18(%rsp),%rcx
mov    %rcx,%rdx
mov    %ebx,%r8d
mov    %ebx,%r9d
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <fprintf@plt>

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

LLVM IR (-O2):
@.str = [1 x i8] zeroinitializer
@.str.1 = [35 x i8] c"static unsigned char %s_arr[] = {\0A\00"
@.str.2 = [5 x i8] c" \00"
@.str.3 = [9 x i8] c"%s0x%02x\00"
@.str.4 = [2 x i8] c",\00"
@.str.6 = [4 x i8] c"};\0A\00"
@.str.7 = [43 x i8] c"struct membuf %s[1] = {{%s_arr, %d, %d}};\0A\00"
define void @generate(i8* %0, i32* nocapture %1, i32* nocapture %2) {
%4 = alloca [12 x i8]
%5 = getelementptr inbounds [12 x i8], [12 x i8]* %4, i64 0, i64 0
%6 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %2, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%7 = call i32 @fread(i8* %5, i32 1, i32 12, i32* %1)
%8 = icmp sgt i32 %7, 0
br i1 %8, label %9, label %32
9: ; preds = %3, %28
%10 = phi i32 [ %30, %28 ], [ %7, %3 ]
%11 = phi i32 [ %13, %28 ], [ 0, %3 ]
%12 = phi i8* [ getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0), %28 ], [ getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0), %3 ]
%13 = add nsw i32 %10, %11
%14 = tail call i64 @fwrite(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i64 4, i64 1, i32* %2)
%15 = zext i32 %10 to i64
%16 = load i8, i8* %5
%17 = zext i8 %16 to i32
%18 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %2, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i8* %12, i32 %17)
%19 = icmp eq i32 %10, 1
br i1 %19, label %28, label %20
20: ; preds = %9, %20
%21 = phi i64 [ %26, %20 ], [ 1, %9 ]
%22 = getelementptr inbounds [12 x i8], [12 x i8]* %4, i64 0, i64 %21
%23 = load i8, i8* %22
%24 = zext i8 %23 to i32
%25 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %2, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0), i32 %24)
%26 = add nuw nsw i64 %21, 1
%27 = icmp eq i64 %26, %15
br i1 %27, label %28, label %20
28: ; preds = %20, %9
%29 = tail call i32 @fputc(i32 10, i32* %2)
%30 = call i32 @fread(i8* %5, i32 1, i32 12, i32* %1)
%31 = icmp sgt i32 %30, 0
br i1 %31, label %9, label %32
32: ; preds = %28, %3
%33 = phi i32 [ 0, %3 ], [ %13, %28 ]
%34 = tail call i64 @fwrite(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0), i64 3, i64 1, i32* %2)
%35 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %2, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.7, i64 0, i64 0), i8* %0, i8* %0, i32 %33, i32 %33)
ret void
}