#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*/ SEEK_SET;
scalar_t__ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *);
scalar_t__ fseek(int /*<<< orphan*/ *, size_t, int /*<<< orphan*/);
int fwrite(void const *, size_t, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
int write_dump(size_t base, size_t len, const void *data, const char *dumpfile,
               int add) {
  FILE *f;
  f = fopen(dumpfile, add ? "r+b" : "wb");
  if (!f) {
    fprintf(stderr, "%s: ", dumpfile);
    perror("fopen");
    return -1;
  }
  if (fseek(f, base, SEEK_SET) != 0) {
    fprintf(stderr, "%s: ", dumpfile);
    perror("fseek");
    goto err_close;
  }
  if (fwrite(data, len, 1, f) != 1) {
    fprintf(stderr, "%s: ", dumpfile);
    perror("fwrite");
    goto err_close;
  }
  if (fclose(f)) {
    fprintf(stderr, "%s: ", dumpfile);
    perror("fclose");
    return -1;
  }
  return 0;
err_close:
  fclose(f);
  return -1;
}


x86 assembly (clang, -O0):
<write_dump>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8d,-0x2c(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x2c(%rbp),%ecx
lea    0xe7a(%rip),%rsi
lea    0xe6f(%rip),%rax
cmp    $0x0,%ecx
cmovne %rax,%rsi
call   1060 <fopen@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    11dd <write_dump+0x7d>
mov    0x2e2d(%rip),%rax
mov    (%rax),%edi
mov    -0x28(%rbp),%rdx
lea    0xe47(%rip),%rsi
call   1040 <fprintf@plt>
lea    0xe40(%rip),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12d0 <write_dump+0x170>
mov    -0x38(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    0x2dec(%rip),%rax
mov    (%rax),%edx
call   1050 <fseek@plt>
cmp    $0x0,%rax
je     1227 <write_dump+0xc7>
mov    0x2ddc(%rip),%rax
mov    (%rax),%edi
mov    -0x28(%rbp),%rdx
lea    0xdf6(%rip),%rsi
call   1040 <fprintf@plt>
lea    0xdf5(%rip),%rdi
call   1070 <perror@plt>
jmp    12c0 <write_dump+0x160>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x38(%rbp),%rcx
mov    $0x1,%edx
call   1080 <fwrite@plt>
cmp    $0x1,%eax
je     1270 <write_dump+0x110>
mov    0x2d93(%rip),%rax
mov    (%rax),%edi
mov    -0x28(%rbp),%rdx
lea    0xdad(%rip),%rsi
call   1040 <fprintf@plt>
lea    0xdb2(%rip),%rdi
call   1070 <perror@plt>
jmp    12c0 <write_dump+0x160>
mov    -0x38(%rbp),%rdi
call   1030 <fclose@plt>
cmp    $0x0,%rax
je     12b4 <write_dump+0x154>
mov    0x2d56(%rip),%rax
mov    (%rax),%edi
mov    -0x28(%rbp),%rdx
lea    0xd70(%rip),%rsi
call   1040 <fprintf@plt>
lea    0xd7c(%rip),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12d0 <write_dump+0x170>
movl   $0x0,-0x4(%rbp)
jmp    12d0 <write_dump+0x170>
mov    -0x38(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_dump>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rcx,%r14
mov    %rdx,%r12
mov    %rsi,%r15
mov    %rdi,%r13
test   %r8d,%r8d
lea    0xe85(%rip),%rax
lea    0xe7a(%rip),%rsi
cmove  %rax,%rsi
mov    %rcx,%rdi
call   1060 <fopen@plt>
test   %rax,%rax
je     1207 <write_dump+0xa7>
mov    %rax,%rbx
mov    0x2e37(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
mov    %r13,%rsi
call   1050 <fseek@plt>
test   %rax,%rax
jne    122d <write_dump+0xcd>
mov    %r12,%rdi
mov    %r15,%rsi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1080 <fwrite@plt>
cmp    $0x1,%eax
jne    1236 <write_dump+0xd6>
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
je     126a <write_dump+0x10a>
mov    0x2df8(%rip),%rax
mov    (%rax),%edi
lea    0xe16(%rip),%rsi
mov    %r14,%rdx
call   1040 <fprintf@plt>
lea    0xe1f(%rip),%rdi
call   1070 <perror@plt>
jmp    1265 <write_dump+0x105>
mov    0x2dd2(%rip),%rax
mov    (%rax),%edi
lea    0xdf0(%rip),%rsi
mov    %r14,%rdx
call   1040 <fprintf@plt>
lea    0xde6(%rip),%rdi
call   1070 <perror@plt>
jmp    1265 <write_dump+0x105>
lea    0xdde(%rip),%r15
jmp    123d <write_dump+0xdd>
lea    0xddb(%rip),%r15
mov    0x2d9c(%rip),%rax
mov    (%rax),%edi
lea    0xdba(%rip),%rsi
mov    %r14,%rdx
call   1040 <fprintf@plt>
mov    %r15,%rdi
call   1070 <perror@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    $0xffffffff,%eax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [4 x i8] c"r+b\00"
@.str.1 = [3 x i8] c"wb\00"
@stderr = global i32 0
@.str.2 = [5 x i8] c"%s: \00"
@.str.3 = [6 x i8] c"fopen\00"
@SEEK_SET = global i32 0
@.str.4 = [6 x i8] c"fseek\00"
@.str.5 = [7 x i8] c"fwrite\00"
@.str.6 = [7 x i8] c"fclose\00"
define i32 @write_dump(i64 %0, i64 %1, i8* %2, i8* %3, i32 %4) {
%6 = alloca i32
%7 = alloca i64
%8 = alloca i64
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i32
%12 = alloca i32*
store i64 %0, i64* %7
store i64 %1, i64* %8
store i8* %2, i8** %9
store i8* %3, i8** %10
store i32 %4, i32* %11
%13 = load i8*, i8** %10
%14 = load i32, i32* %11
%15 = icmp ne i32 %14, 0
%16 = zext i1 %15 to i64
%17 = select i1 %15, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0)
%18 = call i32* @fopen(i8* %13, i8* %17)
store i32* %18, i32** %12
%19 = load i32*, i32** %12
%20 = icmp ne i32* %19, null
br i1 %20, label %26, label %21
21: ; preds = %5
%22 = load i32, i32* @stderr
%23 = load i8*, i8** %10
%24 = call i32 @fprintf(i32 %22, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8* %23)
%25 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %6
br label %61
26: ; preds = %5
%27 = load i32*, i32** %12
%28 = load i64, i64* %7
%29 = load i32, i32* @SEEK_SET
%30 = call i64 @fseek(i32* %27, i64 %28, i32 %29)
%31 = icmp ne i64 %30, 0
br i1 %31, label %32, label %37
32: ; preds = %26
%33 = load i32, i32* @stderr
%34 = load i8*, i8** %10
%35 = call i32 @fprintf(i32 %33, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8* %34)
%36 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
br label %58
37: ; preds = %26
%38 = load i8*, i8** %9
%39 = load i64, i64* %8
%40 = load i32*, i32** %12
%41 = call i32 @fwrite(i8* %38, i64 %39, i32 1, i32* %40)
%42 = icmp ne i32 %41, 1
br i1 %42, label %43, label %48
43: ; preds = %37
%44 = load i32, i32* @stderr
%45 = load i8*, i8** %10
%46 = call i32 @fprintf(i32 %44, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8* %45)
%47 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0))
br label %58
48: ; preds = %37
%49 = load i32*, i32** %12
%50 = call i64 @fclose(i32* %49)
%51 = icmp ne i64 %50, 0
br i1 %51, label %52, label %57
52: ; preds = %48
%53 = load i32, i32* @stderr
%54 = load i8*, i8** %10
%55 = call i32 @fprintf(i32 %53, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8* %54)
%56 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0))
store i32 -1, i32* %6
br label %61
57: ; preds = %48
store i32 0, i32* %6
br label %61
58: ; preds = %43, %32
%59 = load i32*, i32** %12
%60 = call i64 @fclose(i32* %59)
store i32 -1, i32* %6
br label %61
61: ; preds = %58, %57, %52, %21
%62 = load i32, i32* %6
ret i32 %62
}

LLVM IR (-O2):
@.str = [4 x i8] c"r+b\00"
@.str.1 = [3 x i8] c"wb\00"
@stderr = global i32 0
@.str.2 = [5 x i8] c"%s: \00"
@.str.3 = [6 x i8] c"fopen\00"
@SEEK_SET = global i32 0
@.str.4 = [6 x i8] c"fseek\00"
@.str.5 = [7 x i8] c"fwrite\00"
@.str.6 = [7 x i8] c"fclose\00"
define i32 @write_dump(i64 %0, i64 %1, i8* nocapture %2, i8* %3, i32 %4) {
%6 = icmp eq i32 %4, 0
%7 = select i1 %6, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0)
%8 = tail call i32* @fopen(i8* %3, i8* %7)
%9 = icmp eq i32* %8, null
br i1 %9, label %10, label %14
10: ; preds = %5
%11 = load i32, i32* @stderr
%12 = tail call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8* %3)
%13 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
br label %34
14: ; preds = %5
%15 = load i32, i32* @SEEK_SET
%16 = tail call i64 @fseek(i32* %8, i64 %0, i32 %15)
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %28
18: ; preds = %14
%19 = tail call i32 @fwrite(i8* %2, i64 %1, i32 1, i32* %8)
%20 = icmp eq i32 %19, 1
br i1 %20, label %21, label %28
21: ; preds = %18
%22 = tail call i64 @fclose(i32* %8)
%23 = icmp eq i64 %22, 0
br i1 %23, label %34, label %24
24: ; preds = %21
%25 = load i32, i32* @stderr
%26 = tail call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8* %3)
%27 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0))
br label %34
28: ; preds = %18, %14
%29 = phi i8* [ getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), %14 ], [ getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0), %18 ]
%30 = load i32, i32* @stderr
%31 = tail call i32 @fprintf(i32 %30, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8* %3)
%32 = tail call i32 @perror(i8* %29)
%33 = tail call i64 @fclose(i32* %8)
br label %34
34: ; preds = %21, %28, %24, %10
%35 = phi i32 [ -1, %28 ], [ -1, %24 ], [ -1, %10 ], [ 0, %21 ]
ret i32 %35
}