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

/* Variables and functions */
int /*<<< orphan*/ *calloc(int, int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *, char *);
int fwrite(int /*<<< orphan*/ *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ setup_freemap(int /*<<< orphan*/ *, int);
int /*<<< orphan*/ setup_root_dir(int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int adfs_format(const char *fn, FILE *fp, int sectors) {
  int status = 0;
  uint8_t *data = calloc((5 + 2), 256);
  if (data) {
    setup_freemap(data, sectors);
    setup_root_dir(data + 0x200);
    if (fwrite(data, 256, (5 + 2), fp) != (5 + 2)) {
      fprintf(stderr, "hdfmt: error writing to %s: %s\n", fn, strerror(errno));
      status = 2;
    }
  }
  return status;
}


x86 assembly (clang, -O0):
<adfs_format>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    $0x7,%edi
mov    $0x100,%esi
call   1040 <calloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1218 <adfs_format+0xb8>
mov    -0x20(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1030 <setup_freemap@plt>
mov    -0x20(%rbp),%rdi
add    $0x800,%rdi
call   1080 <setup_root_dir@plt>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x100,%esi
mov    $0x7,%edx
call   1060 <fwrite@plt>
cmp    $0x7,%eax
je     1213 <adfs_format+0xb3>
mov    0x2e05(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    0x2de9(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x2c(%rbp),%edi
mov    -0x28(%rbp),%rdx
mov    %rax,%rcx
lea    0xdf9(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0x2,-0x18(%rbp)
jmp    1218 <adfs_format+0xb8>
mov    -0x18(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<adfs_format>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edx,%ebp
mov    %rsi,%r15
mov    %rdi,%r14
mov    $0x7,%edi
mov    $0x100,%esi
call   1040 <calloc@plt>
xor    %r12d,%r12d
test   %rax,%rax
je     11ee <adfs_format+0x8e>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %ebp,%esi
call   1030 <setup_freemap@plt>
mov    %rbx,%rdi
add    $0x800,%rdi
call   1080 <setup_root_dir@plt>
mov    %rbx,%rdi
mov    $0x100,%esi
mov    $0x7,%edx
mov    %r15,%rcx
call   1060 <fwrite@plt>
cmp    $0x7,%eax
je     11ee <adfs_format+0x8e>
mov    0x2e1c(%rip),%rax
mov    (%rax),%ebx
mov    0x2e0b(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xe25(%rip),%rsi
mov    %ebx,%edi
mov    %r14,%rdx
mov    %rax,%rcx
call   1050 <fprintf@plt>
mov    $0x2,%r12d
mov    %r12d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [32 x i8] c"hdfmt: error writing to %s: %s\0A\00"
@errno = global i32 0
define i32 @adfs_format(i8* %0, i32* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32*
store i8* %0, i8** %4
store i32* %1, i32** %5
store i32 %2, i32* %6
store i32 0, i32* %7
%9 = call i32* @calloc(i32 7, i32 256)
store i32* %9, i32** %8
%10 = load i32*, i32** %8
%11 = icmp ne i32* %10, null
br i1 %11, label %12, label %30
12: ; preds = %3
%13 = load i32*, i32** %8
%14 = load i32, i32* %6
%15 = call i32 @setup_freemap(i32* %13, i32 %14)
%16 = load i32*, i32** %8
%17 = getelementptr inbounds i32, i32* %16, i64 512
%18 = call i32 @setup_root_dir(i32* %17)
%19 = load i32*, i32** %8
%20 = load i32*, i32** %5
%21 = call i32 @fwrite(i32* %19, i32 256, i32 7, i32* %20)
%22 = icmp ne i32 %21, 7
br i1 %22, label %23, label %29
23: ; preds = %12
%24 = load i32, i32* @stderr
%25 = load i8*, i8** %4
%26 = load i32, i32* @errno
%27 = call i8* @strerror(i32 %26)
%28 = call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i8* %25, i8* %27)
store i32 2, i32* %7
br label %29
29: ; preds = %23, %12
br label %30
30: ; preds = %29, %3
%31 = load i32, i32* %7
ret i32 %31
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [32 x i8] c"hdfmt: error writing to %s: %s\0A\00"
@errno = global i32 0
define i32 @adfs_format(i8* %0, i32* nocapture %1, i32 %2) {
%4 = tail call i32* @calloc(i32 7, i32 256)
%5 = icmp eq i32* %4, null
br i1 %5, label %17, label %6
6: ; preds = %3
%7 = tail call i32 @setup_freemap(i32* %4, i32 %2)
%8 = getelementptr inbounds i32, i32* %4, i64 512
%9 = tail call i32 @setup_root_dir(i32* %8)
%10 = tail call i32 @fwrite(i32* %4, i32 256, i32 7, i32* %1)
%11 = icmp eq i32 %10, 7
br i1 %11, label %17, label %12
12: ; preds = %6
%13 = load i32, i32* @stderr
%14 = load i32, i32* @errno
%15 = tail call i8* @strerror(i32 %14)
%16 = tail call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %15)
br label %17
17: ; preds = %6, %12, %3
%18 = phi i32 [ 2, %12 ], [ 0, %6 ], [ 0, %3 ]
ret i32 %18
}