#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 *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, unsigned char);
int /*<<< orphan*/ sprintf(char *, char *, unsigned int);
void saveblock_asm(unsigned int n, unsigned char *block) {
  FILE *fp;
  char filename[128];
  unsigned int i;
  sprintf(filename, "blk-%d.inc", n);
  fp = fopen(filename, "w");
  fprintf(fp, ";; Block no. %d\n", n);
  fprintf(fp, "block_%d:\n", n);
  for (i = 0; i < 512; i++) {
    fprintf(fp, "\tdb\t0x%02x\n", block[i]);
  }
  fclose(fp);
}


x86 assembly (clang, -O0):
<saveblock_asm>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0xa0(%rbp),%rdi
mov    -0x4(%rbp),%edx
lea    0xe9d(%rip),%rsi
call   1060 <sprintf@plt>
lea    -0xa0(%rbp),%rdi
lea    0xe95(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x4(%rbp),%eax
lea    0xe80(%rip),%rsi
movzbl %al,%edx
call   1040 <fprintf@plt>
mov    -0x18(%rbp),%rdi
mov    -0x4(%rbp),%eax
lea    0xe7b(%rip),%rsi
movzbl %al,%edx
call   1040 <fprintf@plt>
movl   $0x0,-0xa4(%rbp)
cmpl   $0x200,-0xa4(%rbp)
jae    11f7 <saveblock_asm+0xb7>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    -0xa4(%rbp),%ecx
lea    0xe4f(%rip),%rsi
movzbl (%rax,%rcx,1),%edx
call   1040 <fprintf@plt>
mov    -0xa4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xa4(%rbp)
jmp    11b5 <saveblock_asm+0x75>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
add    $0xb0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<saveblock_asm>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x88,%rsp
mov    %rsi,%r14
mov    %edi,%ebp
lea    0xea7(%rip),%rsi
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    %ebp,%edx
call   1060 <sprintf@plt>
lea    0xe9e(%rip),%rsi
mov    %rbx,%rdi
call   1050 <fopen@plt>
mov    %rax,%rbx
movzbl %bpl,%ebp
lea    0xe8a(%rip),%rsi
mov    %rax,%rdi
mov    %ebp,%edx
call   1040 <fprintf@plt>
lea    0xe8a(%rip),%rsi
mov    %rbx,%rdi
mov    %ebp,%edx
call   1040 <fprintf@plt>
xor    %ebp,%ebp
lea    0xe82(%rip),%r15
nopw   0x0(%rax,%rax,1)
movzbl (%r14,%rbp,1),%edx
mov    %rbx,%rdi
mov    %r15,%rsi
call   1040 <fprintf@plt>
add    $0x1,%rbp
cmp    $0x200,%rbp
jne    11b0 <saveblock_asm+0x70>
mov    %rbx,%rdi
add    $0x88,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <fclose@plt>

LLVM IR (-O0):
@.str = [11 x i8] c"blk-%d.inc\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [17 x i8] c";; Block no. %d\0A\00"
@.str.3 = [11 x i8] c"block_%d:\0A\00"
@.str.4 = [12 x i8] c"\09db\090x%02x\0A\00"
define void @saveblock_asm(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca [128 x i8]
%7 = alloca i32
store i32 %0, i32* %3
store i8* %1, i8** %4
%8 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%9 = load i32, i32* %3
%10 = call i32 @sprintf(i8* %8, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %9)
%11 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%12 = call i32* @fopen(i8* %11, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %12, i32** %5
%13 = load i32*, i32** %5
%14 = load i32, i32* %3
%15 = trunc i32 %14 to i8
%16 = call i32 @fprintf(i32* %13, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i8 zeroext %15)
%17 = load i32*, i32** %5
%18 = load i32, i32* %3
%19 = trunc i32 %18 to i8
%20 = call i32 @fprintf(i32* %17, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i8 zeroext %19)
store i32 0, i32* %7
br label %21
21: ; preds = %32, %2
%22 = load i32, i32* %7
%23 = icmp ult i32 %22, 512
br i1 %23, label %24, label %35
24: ; preds = %21
%25 = load i32*, i32** %5
%26 = load i8*, i8** %4
%27 = load i32, i32* %7
%28 = zext i32 %27 to i64
%29 = getelementptr inbounds i8, i8* %26, i64 %28
%30 = load i8, i8* %29
%31 = call i32 @fprintf(i32* %25, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0), i8 zeroext %30)
br label %32
32: ; preds = %24
%33 = load i32, i32* %7
%34 = add i32 %33, 1
store i32 %34, i32* %7
br label %21
35: ; preds = %21
%36 = load i32*, i32** %5
%37 = call i32 @fclose(i32* %36)
ret void
}

LLVM IR (-O2):
@.str = [11 x i8] c"blk-%d.inc\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [17 x i8] c";; Block no. %d\0A\00"
@.str.3 = [11 x i8] c"block_%d:\0A\00"
@.str.4 = [12 x i8] c"\09db\090x%02x\0A\00"
define void @saveblock_asm(i32 %0, i8* nocapture %1) {
%3 = alloca [128 x i8]
%4 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%5 = call i32 @sprintf(i8* %4, i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %0)
%6 = call i32* @fopen(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%7 = trunc i32 %0 to i8
%8 = tail call i32 @fprintf(i32* %6, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i8 zeroext %7)
%9 = tail call i32 @fprintf(i32* %6, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i8 zeroext %7)
br label %10
10: ; preds = %2, %10
%11 = phi i64 [ 0, %2 ], [ %15, %10 ]
%12 = getelementptr inbounds i8, i8* %1, i64 %11
%13 = load i8, i8* %12
%14 = tail call i32 @fprintf(i32* %6, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0), i8 zeroext %13)
%15 = add nuw nsw i64 %11, 1
%16 = icmp eq i64 %15, 512
br i1 %16, label %17, label %10
17: ; preds = %10
%18 = tail call i32 @fclose(i32* %6)
ret void
}