#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*/ fprintf(int /*<<< orphan*/, char *, int, char *, int);
int fwrite(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *open_file_or_die(char *, char *);
int /*<<< orphan*/ stderr;
int write_region_to_file(char *region, int file_size, char *file_name) {
  FILE *fp;
  int bytes_written;
  int rc = 0;
  fp = open_file_or_die(file_name, "wb");
  bytes_written = fwrite(region, sizeof(char), file_size, fp);
  if (bytes_written != file_size) {
    fprintf(stderr,
            "Could not write %d bytes to file \"%s\"; wrote %d.  File is now "
            "damaged.\n",
            file_size, file_name, bytes_written);
    rc = 0;
  } else {
    rc = 1;
  }
  fclose(fp);
  return rc;
}


x86 assembly (clang, -O0):
<write_region_to_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe9b(%rip),%rsi
call   1050 <open_file_or_die@plt>
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%edx
mov    -0x20(%rbp),%rcx
mov    $0x1,%esi
call   1060 <fwrite@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0xc(%rbp),%eax
je     11be <write_region_to_file+0x7e>
mov    0x2e47(%rip),%rax
mov    (%rax),%edi
mov    -0xc(%rbp),%edx
mov    -0x18(%rbp),%rcx
mov    -0x24(%rbp),%r8d
lea    0xe56(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0x0,-0x28(%rbp)
jmp    11c5 <write_region_to_file+0x85>
movl   $0x1,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x28(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_region_to_file>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdx,%r14
mov    %esi,%ebp
mov    %rdi,%r12
lea    0xea9(%rip),%rsi
mov    %rdx,%rdi
call   1050 <open_file_or_die@plt>
mov    %rax,%r15
mov    $0x1,%ebx
mov    %r12,%rdi
mov    $0x1,%esi
mov    %ebp,%edx
mov    %rax,%rcx
call   1060 <fwrite@plt>
cmp    %ebp,%eax
je     119c <write_region_to_file+0x5c>
mov    0x2e5c(%rip),%rcx
mov    (%rcx),%edi
lea    0xe76(%rip),%rsi
mov    %ebp,%edx
mov    %r14,%rcx
mov    %eax,%r8d
call   1040 <fprintf@plt>
xor    %ebx,%ebx
mov    %r15,%rdi
call   1030 <fclose@plt>
mov    %ebx,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"wb\00"
@stderr = global i32 0
@.str.1 = [72 x i8] c"Could not write %d bytes to file \22%s\22; wrote %d. File is now damaged.\0A\00"
define i32 @write_region_to_file(i8* %0, i32 %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32*
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
store i8* %2, i8** %6
store i32 0, i32* %9
%10 = load i8*, i8** %6
%11 = call i32* @open_file_or_die(i8* %10, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %11, i32** %7
%12 = load i8*, i8** %4
%13 = load i32, i32* %5
%14 = load i32*, i32** %7
%15 = call i32 @fwrite(i8* %12, i32 1, i32 %13, i32* %14)
store i32 %15, i32* %8
%16 = load i32, i32* %8
%17 = load i32, i32* %5
%18 = icmp ne i32 %16, %17
br i1 %18, label %19, label %25
19: ; preds = %3
%20 = load i32, i32* @stderr
%21 = load i32, i32* %5
%22 = load i8*, i8** %6
%23 = load i32, i32* %8
%24 = call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([72 x i8], [72 x i8]* @.str.1, i64 0, i64 0), i32 %21, i8* %22, i32 %23)
store i32 0, i32* %9
br label %26
25: ; preds = %3
store i32 1, i32* %9
br label %26
26: ; preds = %25, %19
%27 = load i32*, i32** %7
%28 = call i32 @fclose(i32* %27)
%29 = load i32, i32* %9
ret i32 %29
}

LLVM IR (-O2):
@.str = [3 x i8] c"wb\00"
@stderr = global i32 0
@.str.1 = [72 x i8] c"Could not write %d bytes to file \22%s\22; wrote %d. File is now damaged.\0A\00"
define i32 @write_region_to_file(i8* nocapture %0, i32 %1, i8* %2) {
%4 = tail call i32* @open_file_or_die(i8* %2, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%5 = tail call i32 @fwrite(i8* %0, i32 1, i32 %1, i32* %4)
%6 = icmp eq i32 %5, %1
br i1 %6, label %10, label %7
7: ; preds = %3
%8 = load i32, i32* @stderr
%9 = tail call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([72 x i8], [72 x i8]* @.str.1, i64 0, i64 0), i32 %1, i8* %2, i32 %5)
br label %10
10: ; preds = %3, %7
%11 = phi i32 [ 0, %7 ], [ 1, %3 ]
%12 = tail call i32 @fclose(i32* %4)
ret i32 %11
}