#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 fwrite(char *, int, int, int /*<<< orphan*/ *);
scalar_t__ sprintf(char *, char *, ...);
int /*<<< orphan*/ terminate(char *);
void write_as_c(FILE *f, unsigned char *data, int len) {
  char buf[512];
  int i, n, ll;
  while (len > 0) {
    n = (len > 16) ? 16 : len;
    ll = 0;
    ll += sprintf(buf + ll, "\t\t");
    for (i = 0; i < n; i++)
      ll += sprintf(buf + ll, "0x%02x,", data[i]);
    ll += sprintf(buf + ll, "\n");
    if (fwrite(buf, 1, ll, f) != ll)
      terminate("error writing to file");
    data += n;
    len -= n;
  }
}


x86 assembly (clang, -O0):
<write_as_c>:
push   %rbp
mov    %rsp,%rbp
sub    $0x230,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jle    12b8 <write_as_c+0x188>
cmpl   $0x10,-0x14(%rbp)
jle    116a <write_as_c+0x3a>
mov    $0x10,%eax
mov    %eax,-0x230(%rbp)
jmp    1173 <write_as_c+0x43>
mov    -0x14(%rbp),%eax
mov    %eax,-0x230(%rbp)
mov    -0x230(%rbp),%eax
mov    %eax,-0x228(%rbp)
movl   $0x0,-0x22c(%rbp)
lea    -0x220(%rbp),%rdi
movslq -0x22c(%rbp),%rax
add    %rax,%rdi
lea    0xe5f(%rip),%rsi
mov    $0x0,%al
call   1040 <sprintf@plt>
mov    %rax,%rcx
movslq -0x22c(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x22c(%rbp)
movl   $0x0,-0x224(%rbp)
mov    -0x224(%rbp),%eax
cmp    -0x228(%rbp),%eax
jge    122c <write_as_c+0xfc>
lea    -0x220(%rbp),%rdi
movslq -0x22c(%rbp),%rax
add    %rax,%rdi
mov    -0x10(%rbp),%rax
movslq -0x224(%rbp),%rcx
movzbl (%rax,%rcx,1),%edx
lea    0xe05(%rip),%rsi
mov    $0x0,%al
call   1040 <sprintf@plt>
mov    %rax,%rcx
movslq -0x22c(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x22c(%rbp)
mov    -0x224(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x224(%rbp)
jmp    11c5 <write_as_c+0x95>
lea    -0x220(%rbp),%rdi
movslq -0x22c(%rbp),%rax
add    %rax,%rdi
lea    0xdc7(%rip),%rsi
mov    $0x0,%al
call   1040 <sprintf@plt>
mov    %rax,%rcx
movslq -0x22c(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x22c(%rbp)
lea    -0x220(%rbp),%rdi
mov    -0x22c(%rbp),%edx
mov    -0x8(%rbp),%rcx
mov    $0x1,%esi
call   1050 <fwrite@plt>
cmp    -0x22c(%rbp),%eax
je     1291 <write_as_c+0x161>
lea    0xd81(%rip),%rdi
call   1030 <terminate@plt>
mov    -0x228(%rbp),%ecx
mov    -0x10(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
mov    -0x228(%rbp),%ecx
mov    -0x14(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x14(%rbp)
jmp    1146 <write_as_c+0x16>
add    $0x230,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_as_c>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x218,%rsp
mov    %rdi,0x8(%rsp)
test   %edx,%edx
jle    1217 <write_as_c+0xe7>
mov    %rsi,%r13
lea    0x10(%rsp),%rbx
lea    0xea6(%rip),%r12
jmp    1170 <write_as_c+0x40>
nop
add    %r14,%r13
mov    0x4(%rsp),%edx
sub    %r14d,%edx
jle    1217 <write_as_c+0xe7>
cmp    $0x10,%edx
mov    $0x10,%r14d
mov    %edx,0x4(%rsp)
cmovb  %edx,%r14d
mov    %rbx,%rdi
lea    0xe75(%rip),%rsi
xor    %eax,%eax
call   1040 <sprintf@plt>
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
movslq %eax,%rbx
lea    (%rsp,%rbx,1),%rdi
add    $0x10,%rdi
movzbl 0x0(%r13,%rbp,1),%edx
mov    %r12,%rsi
xor    %eax,%eax
call   1040 <sprintf@plt>
add    %rbx,%rax
add    $0x1,%rbp
cmp    %rbp,%r14
jne    11a0 <write_as_c+0x70>
movslq %eax,%r15
lea    (%rsp,%r15,1),%rdi
add    $0x10,%rdi
lea    0xe32(%rip),%rsi
xor    %eax,%eax
call   1040 <sprintf@plt>
add    %eax,%r15d
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x1,%esi
mov    %r15d,%edx
mov    0x8(%rsp),%rcx
call   1050 <fwrite@plt>
cmp    %r15d,%eax
je     1160 <write_as_c+0x30>
lea    0xe00(%rip),%rdi
call   1030 <terminate@plt>
jmp    1160 <write_as_c+0x30>
add    $0x218,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"\09\09\00"
@.str.1 = [8 x i8] c"0x%02x,\00"
@.str.2 = [2 x i8] c"\0A\00"
@.str.3 = [22 x i8] c"error writing to file\00"
define void @write_as_c(i32* %0, i8* %1, i32 %2) {
%4 = alloca i32*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca [512 x i8]
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i32* %0, i32** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
br label %11
11: ; preds = %72, %3
%12 = load i32, i32* %6
%13 = icmp sgt i32 %12, 0
br i1 %13, label %14, label %80
14: ; preds = %11
%15 = load i32, i32* %6
%16 = icmp sgt i32 %15, 16
br i1 %16, label %17, label %18
17: ; preds = %14
br label %20
18: ; preds = %14
%19 = load i32, i32* %6
br label %20
20: ; preds = %18, %17
%21 = phi i32 [ 16, %17 ], [ %19, %18 ]
store i32 %21, i32* %9
store i32 0, i32* %10
%22 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 0
%23 = load i32, i32* %10
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds i8, i8* %22, i64 %24
%26 = call i64 (i8*, i8*, ...) @sprintf(i8* %25, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%27 = load i32, i32* %10
%28 = sext i32 %27 to i64
%29 = add nsw i64 %28, %26
%30 = trunc i64 %29 to i32
store i32 %30, i32* %10
store i32 0, i32* %8
br label %31
31: ; preds = %51, %20
%32 = load i32, i32* %8
%33 = load i32, i32* %9
%34 = icmp slt i32 %32, %33
br i1 %34, label %35, label %54
35: ; preds = %31
%36 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 0
%37 = load i32, i32* %10
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds i8, i8* %36, i64 %38
%40 = load i8*, i8** %5
%41 = load i32, i32* %8
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds i8, i8* %40, i64 %42
%44 = load i8, i8* %43
%45 = zext i8 %44 to i32
%46 = call i64 (i8*, i8*, ...) @sprintf(i8* %39, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i32 %45)
%47 = load i32, i32* %10
%48 = sext i32 %47 to i64
%49 = add nsw i64 %48, %46
%50 = trunc i64 %49 to i32
store i32 %50, i32* %10
br label %51
51: ; preds = %35
%52 = load i32, i32* %8
%53 = add nsw i32 %52, 1
store i32 %53, i32* %8
br label %31
54: ; preds = %31
%55 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 0
%56 = load i32, i32* %10
%57 = sext i32 %56 to i64
%58 = getelementptr inbounds i8, i8* %55, i64 %57
%59 = call i64 (i8*, i8*, ...) @sprintf(i8* %58, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%60 = load i32, i32* %10
%61 = sext i32 %60 to i64
%62 = add nsw i64 %61, %59
%63 = trunc i64 %62 to i32
store i32 %63, i32* %10
%64 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 0
%65 = load i32, i32* %10
%66 = load i32*, i32** %4
%67 = call i32 @fwrite(i8* %64, i32 1, i32 %65, i32* %66)
%68 = load i32, i32* %10
%69 = icmp ne i32 %67, %68
br i1 %69, label %70, label %72
70: ; preds = %54
%71 = call i32 @terminate(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
br label %72
72: ; preds = %70, %54
%73 = load i32, i32* %9
%74 = load i8*, i8** %5
%75 = sext i32 %73 to i64
%76 = getelementptr inbounds i8, i8* %74, i64 %75
store i8* %76, i8** %5
%77 = load i32, i32* %9
%78 = load i32, i32* %6
%79 = sub nsw i32 %78, %77
store i32 %79, i32* %6
br label %11
80: ; preds = %11
ret void
}

LLVM IR (-O2):
@.str = [3 x i8] c"\09\09\00"
@.str.1 = [8 x i8] c"0x%02x,\00"
@.str.2 = [2 x i8] c"\0A\00"
@.str.3 = [22 x i8] c"error writing to file\00"
define void @write_as_c(i32* nocapture %0, i8* nocapture %1, i32 %2) {
%4 = alloca [512 x i8]
%5 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%6 = icmp sgt i32 %2, 0
br i1 %6, label %7, label %43
7: ; preds = %3, %38
%8 = phi i8* [ %40, %38 ], [ %1, %3 ]
%9 = phi i32 [ %41, %38 ], [ %2, %3 ]
%10 = icmp ult i32 %9, 16
%11 = select i1 %10, i32 %9, i32 16
%12 = call i64 (i8*, i8*, ...) @sprintf(i8* %5, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%13 = zext i32 %11 to i64
br label %14
14: ; preds = %7, %14
%15 = phi i64 [ 0, %7 ], [ %25, %14 ]
%16 = phi i64 [ %12, %7 ], [ %24, %14 ]
%17 = shl i64 %16, 32
%18 = ashr exact i64 %17, 32
%19 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 %18
%20 = getelementptr inbounds i8, i8* %8, i64 %15
%21 = load i8, i8* %20
%22 = zext i8 %21 to i32
%23 = call i64 (i8*, i8*, ...) @sprintf(i8* %19, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i32 %22)
%24 = add nsw i64 %23, %18
%25 = add nuw nsw i64 %15, 1
%26 = icmp eq i64 %25, %13
br i1 %26, label %27, label %14
27: ; preds = %14
%28 = shl i64 %24, 32
%29 = ashr exact i64 %28, 32
%30 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 %29
%31 = call i64 (i8*, i8*, ...) @sprintf(i8* %30, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%32 = add i64 %31, %24
%33 = trunc i64 %32 to i32
%34 = call i32 @fwrite(i8* %5, i32 1, i32 %33, i32* %0)
%35 = icmp eq i32 %34, %33
br i1 %35, label %38, label %36
36: ; preds = %27
%37 = call i32 @terminate(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
br label %38
38: ; preds = %36, %27
%39 = zext i32 %11 to i64
%40 = getelementptr inbounds i8, i8* %8, i64 %39
%41 = sub nsw i32 %9, %11
%42 = icmp sgt i32 %41, 0
br i1 %42, label %7, label %43
43: ; preds = %38, %3
ret void
}