#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 *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ fwrite(int *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ sprintf(char *, char *, char *, char *);
int /*<<< orphan*/ stderr;
void save_bin_cryst_map(char *crystfile, char *targetcrystfile, int *map,
                        int N) {
  FILE *fp;
  int i;
  char fname[256];
  sprintf(fname, "%s_%s.mab", targetcrystfile, crystfile);
  fp = fopen(fname, "wb");
  if (!fp) {
    fprintf(stderr, "save_bin_cryst_map: unable to create file '%s'\n", fname);
    exit(1);
  }
  for (i = 0; i < N; i++) {
    fwrite(&map[i], sizeof(int), 1, fp);
  }
  fclose(fp);
}


x86 assembly (clang, -O0):
<save_bin_cryst_map>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
lea    -0x130(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    -0x8(%rbp),%rcx
lea    0xe70(%rip),%rsi
call   1060 <sprintf@plt>
lea    -0x130(%rbp),%rdi
lea    0xe67(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11dd <save_bin_cryst_map+0x7d>
mov    0x2e22(%rip),%rax
mov    (%rax),%edi
lea    -0x130(%rbp),%rdx
lea    0xe3f(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    1220 <save_bin_cryst_map+0xc0>
mov    -0x18(%rbp),%rdi
movslq -0x2c(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rdi
mov    -0x28(%rbp),%rcx
mov    $0x4,%esi
mov    $0x1,%edx
call   1080 <fwrite@plt>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    11e4 <save_bin_cryst_map+0x84>
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<save_bin_cryst_map>:
push   %rbp
push   %r14
push   %rbx
sub    $0x100,%rsp
mov    %ecx,%ebp
mov    %rdx,%rbx
mov    %rsi,%rdx
mov    %rdi,%rcx
lea    0xe83(%rip),%rsi
mov    %rsp,%r14
mov    %r14,%rdi
call   1060 <sprintf@plt>
lea    0xe7b(%rip),%rsi
mov    %r14,%rdi
call   1050 <fopen@plt>
test   %rax,%rax
je     11e2 <save_bin_cryst_map+0x82>
mov    %rax,%r14
test   %ebp,%ebp
jle    11cf <save_bin_cryst_map+0x6f>
mov    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nop
mov    %rbx,%rdi
mov    $0x4,%esi
mov    $0x1,%edx
mov    %r14,%rcx
call   1080 <fwrite@plt>
add    $0x4,%rbx
add    $0xffffffffffffffff,%rbp
jne    11b0 <save_bin_cryst_map+0x50>
mov    %r14,%rdi
add    $0x100,%rsp
pop    %rbx
pop    %r14
pop    %rbp
jmp    1030 <fclose@plt>
mov    0x2df7(%rip),%rax
mov    (%rax),%edi
lea    0xe1b(%rip),%rsi
mov    %rsp,%rdx
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [10 x i8] c"%s_%s.mab\00"
@.str.1 = [3 x i8] c"wb\00"
@stderr = global i32 0
@.str.2 = [48 x i8] c"save_bin_cryst_map: unable to create file '%s'\0A\00"
define void @save_bin_cryst_map(i8* %0, i8* %1, i32* %2, i32 %3) {
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32*
%8 = alloca i32
%9 = alloca i32*
%10 = alloca i32
%11 = alloca [256 x i8]
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32* %2, i32** %7
store i32 %3, i32* %8
%12 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 0
%13 = load i8*, i8** %6
%14 = load i8*, i8** %5
%15 = call i32 @sprintf(i8* %12, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* %13, i8* %14)
%16 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 0
%17 = call i32* @fopen(i8* %16, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %17, i32** %9
%18 = load i32*, i32** %9
%19 = icmp ne i32* %18, null
br i1 %19, label %25, label %20
20: ; preds = %4
%21 = load i32, i32* @stderr
%22 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 0
%23 = call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.2, i64 0, i64 0), i8* %22)
%24 = call i32 @exit(i32 1)
unreachable
25: ; preds = %4
store i32 0, i32* %10
br label %26
26: ; preds = %37, %25
%27 = load i32, i32* %10
%28 = load i32, i32* %8
%29 = icmp slt i32 %27, %28
br i1 %29, label %30, label %40
30: ; preds = %26
%31 = load i32*, i32** %7
%32 = load i32, i32* %10
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds i32, i32* %31, i64 %33
%35 = load i32*, i32** %9
%36 = call i32 @fwrite(i32* %34, i32 4, i32 1, i32* %35)
br label %37
37: ; preds = %30
%38 = load i32, i32* %10
%39 = add nsw i32 %38, 1
store i32 %39, i32* %10
br label %26
40: ; preds = %26
%41 = load i32*, i32** %9
%42 = call i32 @fclose(i32* %41)
ret void
}

LLVM IR (-O2):
@.str = [10 x i8] c"%s_%s.mab\00"
@.str.1 = [3 x i8] c"wb\00"
@stderr = global i32 0
@.str.2 = [48 x i8] c"save_bin_cryst_map: unable to create file '%s'\0A\00"
define void @save_bin_cryst_map(i8* %0, i8* %1, i32* nocapture %2, i32 %3) {
%5 = alloca [256 x i8]
%6 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%7 = call i32 @sprintf(i8* %6, i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* %1, i8* %0)
%8 = call i32* @fopen(i8* %6, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%9 = icmp eq i32* %8, null
br i1 %9, label %14, label %10
10: ; preds = %4
%11 = icmp sgt i32 %3, 0
br i1 %11, label %12, label %24
12: ; preds = %10
%13 = zext i32 %3 to i64
br label %18
14: ; preds = %4
%15 = load i32, i32* @stderr
%16 = call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.2, i64 0, i64 0), i8* %6)
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %12, %18
%19 = phi i64 [ 0, %12 ], [ %22, %18 ]
%20 = getelementptr inbounds i32, i32* %2, i64 %19
%21 = tail call i32 @fwrite(i32* %20, i32 4, i32 1, i32* %8)
%22 = add nuw nsw i64 %19, 1
%23 = icmp eq i64 %22, %13
br i1 %23, label %24, label %18
24: ; preds = %18, %10
%25 = tail call i32 @fclose(i32* %8)
ret void
}