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

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *);
int /*<<< orphan*/ fwrite(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
int save_map_to_file(uint8_t *map, const char *path) {
  FILE *outfile = fopen(path, "w");
  if (!outfile) {
    fprintf(stderr, "Could not save file: %s\n", path);
    return 1;
  }
  fwrite("battle", 6, 1, outfile);
  uint8_t ver = 1;
  fwrite(&ver, 1, 1, outfile);
  fwrite(map, sizeof(*map), 78 * 19, outfile);
  fclose(outfile);
  return 0;
}


x86 assembly (clang, -O0):
<save_map_to_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xea5(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1194 <save_map_to_file+0x54>
mov    0x2e6a(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
lea    0xe7f(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    11ed <save_map_to_file+0xad>
mov    -0x20(%rbp),%rcx
lea    0xe7c(%rip),%rdi
mov    $0x6,%esi
mov    $0x1,%edx
call   1060 <fwrite@plt>
movb   $0x1,-0x21(%rbp)
mov    -0x20(%rbp),%rcx
lea    -0x21(%rbp),%rdi
mov    $0x1,%edx
mov    %edx,%esi
call   1060 <fwrite@plt>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
mov    $0x5ca,%edx
call   1060 <fwrite@plt>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<save_map_to_file>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r15
mov    %rdi,%r14
lea    0xe9e(%rip),%rsi
mov    %r15,%rdi
call   1060 <fopen@plt>
test   %rax,%rax
je     11b9 <save_map_to_file+0x69>
mov    %rax,%rbx
lea    0xea2(%rip),%rdi
mov    $0x6,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   1070 <fwrite@plt>
mov    $0x1,%edi
mov    %rbx,%rsi
call   1040 <fputc@plt>
mov    %r14,%rdi
mov    $0x1,%esi
mov    $0x5ca,%edx
mov    %rbx,%rcx
call   1070 <fwrite@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
jmp    11d6 <save_map_to_file+0x86>
mov    0x2e20(%rip),%rax
mov    (%rax),%edi
lea    0xe39(%rip),%rsi
mov    %r15,%rdx
call   1050 <fprintf@plt>
mov    $0x1,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [2 x i8] c"w\00"
@stderr = global i32 0
@.str.1 = [25 x i8] c"Could not save file: %s\0A\00"
@.str.2 = [7 x i8] c"battle\00"
define i32 @save_map_to_file(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca i8
store i8* %0, i8** %4
store i8* %1, i8** %5
%8 = load i8*, i8** %5
%9 = call i32* @fopen(i8* %8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %9, i32** %6
%10 = load i32*, i32** %6
%11 = icmp ne i32* %10, null
br i1 %11, label %16, label %12
12: ; preds = %2
%13 = load i32, i32* @stderr
%14 = load i8*, i8** %5
%15 = call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %14)
store i32 1, i32* %3
br label %26
16: ; preds = %2
%17 = load i32*, i32** %6
%18 = call i32 @fwrite(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32 6, i32 1, i32* %17)
store i8 1, i8* %7
%19 = load i32*, i32** %6
%20 = call i32 @fwrite(i8* %7, i32 1, i32 1, i32* %19)
%21 = load i8*, i8** %4
%22 = load i32*, i32** %6
%23 = call i32 @fwrite(i8* %21, i32 1, i32 1482, i32* %22)
%24 = load i32*, i32** %6
%25 = call i32 @fclose(i32* %24)
store i32 0, i32* %3
br label %26
26: ; preds = %16, %12
%27 = load i32, i32* %3
ret i32 %27
}

LLVM IR (-O2):
@.str = [2 x i8] c"w\00"
@stderr = global i32 0
@.str.1 = [25 x i8] c"Could not save file: %s\0A\00"
@.str.2 = [7 x i8] c"battle\00"
define i32 @save_map_to_file(i8* nocapture %0, i8* %1) {
%3 = tail call i32* @fopen(i8* %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %5, label %8
5: ; preds = %2
%6 = load i32, i32* @stderr
%7 = tail call i32 @fprintf(i32 %6, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %1)
br label %13
8: ; preds = %2
%9 = tail call i32 @fwrite(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32 6, i32 1, i32* %3)
%10 = tail call i32 @fputc(i32 1, i32* %3)
%11 = tail call i32 @fwrite(i8* %0, i32 1, i32 1482, i32* %3)
%12 = tail call i32 @fclose(i32* %3)
br label %13
13: ; preds = %8, %5
%14 = phi i32 [ 0, %8 ], [ 1, %5 ]
ret i32 %14
}