#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*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
scalar_t__ fwrite(unsigned char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, unsigned int, char *);
int /*<<< orphan*/ sprintf(char *, char *, unsigned int);
void saveblock_binary(unsigned int n, unsigned char *block) {
  FILE *fp;
  char filename[128];
  sprintf(filename, "blk-%d.bin", n);
  fp = fopen(filename, "w");
  if (fwrite(block, 512, 1, fp) <= 0) {
    printf("Error saving block %d to file: '%s'\n", n, filename);
    exit(1);
  }
  fclose(fp);
}


x86 assembly (clang, -O0):
<saveblock_binary>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0xa0(%rbp),%rdi
mov    -0x4(%rbp),%edx
lea    0xe7d(%rip),%rsi
call   1060 <sprintf@plt>
lea    -0xa0(%rbp),%rdi
lea    0xe75(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rcx
mov    $0x200,%esi
mov    $0x1,%edx
call   1080 <fwrite@plt>
cmp    $0x0,%rax
jg     11e0 <saveblock_binary+0x80>
mov    -0x4(%rbp),%esi
lea    -0xa0(%rbp),%rdx
lea    0xe3c(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
add    $0xa0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<saveblock_binary>:
push   %rbp
push   %r14
push   %rbx
sub    $0x80,%rsp
mov    %rsi,%rbp
mov    %edi,%r14d
lea    0xe88(%rip),%rsi
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    %r14d,%edx
call   1060 <sprintf@plt>
lea    0xe7e(%rip),%rsi
mov    %rbx,%rdi
call   1050 <fopen@plt>
mov    %rax,%rbx
mov    %rbp,%rdi
mov    $0x200,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   1080 <fwrite@plt>
test   %rax,%rax
jle    11c5 <saveblock_binary+0x65>
mov    %rbx,%rdi
add    $0x80,%rsp
pop    %rbx
pop    %r14
pop    %rbp
jmp    1030 <fclose@plt>
lea    0xe41(%rip),%rdi
mov    %rsp,%rdx
mov    %r14d,%esi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [11 x i8] c"blk-%d.bin\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [37 x i8] c"Error saving block %d to file: '%s'\0A\00"
define void @saveblock_binary(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca [128 x i8]
store i32 %0, i32* %3
store i8* %1, i8** %4
%7 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%8 = load i32, i32* %3
%9 = call i32 @sprintf(i8* %7, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %8)
%10 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%11 = call i32* @fopen(i8* %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %11, i32** %5
%12 = load i8*, i8** %4
%13 = load i32*, i32** %5
%14 = call i64 @fwrite(i8* %12, i32 512, i32 1, i32* %13)
%15 = icmp sle i64 %14, 0
br i1 %15, label %16, label %21
16: ; preds = %2
%17 = load i32, i32* %3
%18 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%19 = call i32 @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0), i32 %17, i8* %18)
%20 = call i32 @exit(i32 1)
unreachable
21: ; preds = %2
%22 = load i32*, i32** %5
%23 = call i32 @fclose(i32* %22)
ret void
}

LLVM IR (-O2):
@.str = [11 x i8] c"blk-%d.bin\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [37 x i8] c"Error saving block %d to file: '%s'\0A\00"
define void @saveblock_binary(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 = tail call i64 @fwrite(i8* %1, i32 512, i32 1, i32* %6)
%8 = icmp slt i64 %7, 1
br i1 %8, label %9, label %12
9: ; preds = %2
%10 = call i32 @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0), i32 %0, i8* %4)
%11 = call i32 @exit(i32 1)
unreachable
12: ; preds = %2
%13 = tail call i32 @fclose(i32* %6)
ret void
}