#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*/ errno;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
size_t fread(char *, int, size_t, int /*<<< orphan*/ *);
char *fsize_str(size_t);
int /*<<< orphan*/ fwrite(char *, int, size_t, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, char *, char *, char const *);
int /*<<< orphan*/ sprintf(char *, char *, int /*<<< orphan*/, char const *);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int dump(FILE *in, size_t len, const char *suffix, const char *desc) {
  static int num;
  char fname[64];
  char buf[4096];
  FILE *out;
  sprintf(fname, "rip%04d.%s", num++, suffix);
  printf("dumping: %s [%s] (%s)\n", fname, fsize_str(len), desc);
  if (!(out = fopen(fname, "wb"))) {
    fprintf(stderr, "failed to open file: %s for writing: %s\n", fname,
            strerror(errno));
    return -1;
  }
  while (len > 0) {
    size_t res, rd = ((4096) < (len) ? (4096) : (len));
    if (!(res = fread(buf, 1, rd, in))) {
      fprintf(stderr, "unexpected EOF\n");
      fclose(out);
      return -1;
    }
    fwrite(buf, 1, res, out);
    len -= res;
  }
  fclose(out);
  return 0;
}


x86 assembly (clang, -O0):
<dump>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10b0,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
lea    -0x70(%rbp),%rdi
mov    0x2eb7(%rip),%edx
mov    %edx,%eax
add    $0x1,%eax
mov    %eax,0x2eac(%rip)
mov    -0x20(%rbp),%rcx
lea    0xe35(%rip),%rsi
call   1090 <sprintf@plt>
lea    -0x70(%rbp),%rax
mov    %rax,-0x1090(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <fsize_str@plt>
mov    -0x1090(%rbp),%rsi
mov    %rax,%rdx
mov    -0x28(%rbp),%rcx
lea    0xe12(%rip),%rdi
call   1060 <printf@plt>
lea    -0x70(%rbp),%rdi
lea    0xe19(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x1078(%rbp)
cmp    $0x0,%rax
jne    1271 <dump+0xe1>
mov    0x2dba(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x109c(%rbp)
lea    -0x70(%rbp),%rax
mov    %rax,-0x1098(%rbp)
mov    0x2d98(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    -0x109c(%rbp),%edi
mov    -0x1098(%rbp),%rdx
mov    %rax,%rcx
lea    0xdc7(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    135f <dump+0x1cf>
jmp    1276 <dump+0xe6>
cmpq   $0x0,-0x18(%rbp)
jbe    134c <dump+0x1bc>
mov    $0x1000,%eax
cmp    -0x18(%rbp),%rax
jae    12a1 <dump+0x111>
mov    $0x1000,%eax
mov    %rax,-0x10a8(%rbp)
jmp    12ac <dump+0x11c>
mov    -0x18(%rbp),%rax
mov    %rax,-0x10a8(%rbp)
mov    -0x10a8(%rbp),%rax
mov    %rax,-0x1088(%rbp)
lea    -0x1070(%rbp),%rdi
mov    -0x1088(%rbp),%rdx
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
mov    %rax,-0x1080(%rbp)
cmp    $0x0,%rax
jne    1316 <dump+0x186>
mov    0x2cf2(%rip),%rax
mov    (%rax),%edi
lea    0xd57(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    -0x1078(%rbp),%rdi
call   1050 <fclose@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    135f <dump+0x1cf>
lea    -0x1070(%rbp),%rdi
mov    -0x1080(%rbp),%rdx
mov    -0x1078(%rbp),%rcx
mov    $0x1,%esi
call   10a0 <fwrite@plt>
mov    -0x1080(%rbp),%rcx
mov    -0x18(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x18(%rbp)
jmp    1276 <dump+0xe6>
mov    -0x1078(%rbp),%rdi
call   1050 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10b0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dump>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1040,%rsp
mov    %rcx,%r15
mov    %rdx,%rcx
mov    %rsi,%r13
mov    %rdi,%r14
mov    0x2eba(%rip),%edx
lea    0x1(%rdx),%eax
mov    %eax,0x2eb1(%rip)
lea    0xe3e(%rip),%rsi
mov    %rsp,%rbx
mov    %rbx,%rdi
call   1090 <sprintf@plt>
mov    %r13,%rdi
call   1030 <fsize_str@plt>
lea    0xe2f(%rip),%rdi
mov    %rbx,%rsi
mov    %rax,%rdx
mov    %r15,%rcx
call   1060 <printf@plt>
lea    0xe31(%rip),%rsi
mov    %rbx,%rdi
call   1080 <fopen@plt>
test   %rax,%rax
je     125c <dump+0xcc>
mov    %rax,%r15
test   %r13,%r13
je     1250 <dump+0xc0>
lea    0x40(%rsp),%r12
nopl   0x0(%rax,%rax,1)
cmp    $0x1000,%r13
mov    $0x1000,%edx
cmovb  %r13,%rdx
mov    %r12,%rdi
mov    $0x1,%esi
mov    %r14,%rcx
call   1040 <fread@plt>
test   %rax,%rax
je     128b <dump+0xfb>
mov    %rax,%rbx
mov    %r12,%rdi
mov    $0x1,%esi
mov    %rax,%rdx
mov    %r15,%rcx
call   10a0 <fwrite@plt>
sub    %rbx,%r13
jne    1210 <dump+0x80>
mov    %r15,%rdi
call   1050 <fclose@plt>
xor    %eax,%eax
jmp    12af <dump+0x11f>
mov    0x2d7d(%rip),%rax
mov    (%rax),%ebx
mov    0x2d6c(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xdab(%rip),%rsi
mov    %rsp,%rdx
mov    %ebx,%edi
mov    %rax,%rcx
xor    %eax,%eax
call   1070 <fprintf@plt>
jmp    12aa <dump+0x11a>
mov    0x2d4e(%rip),%rax
mov    (%rax),%edi
lea    0xdb3(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    %r15,%rdi
call   1050 <fclose@plt>
mov    $0xffffffff,%eax
add    $0x1040,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@dump.num = internal global i32 0
@.str = [11 x i8] c"rip%04d.%s\00"
@.str.1 = [23 x i8] c"dumping: %s [%s] (%s)\0A\00"
@.str.2 = [3 x i8] c"wb\00"
@stderr = global i32 0
@.str.3 = [41 x i8] c"failed to open file: %s for writing: %s\0A\00"
@errno = global i32 0
@.str.4 = [16 x i8] c"unexpected EOF\0A\00"
define i32 @dump(i32* %0, i64 %1, i8* %2, i8* %3) {
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca [64 x i8]
%11 = alloca [4096 x i8]
%12 = alloca i32*
%13 = alloca i64
%14 = alloca i64
store i32* %0, i32** %6
store i64 %1, i64* %7
store i8* %2, i8** %8
store i8* %3, i8** %9
%15 = getelementptr inbounds [64 x i8], [64 x i8]* %10, i64 0, i64 0
%16 = load i32, i32* @dump.num
%17 = add nsw i32 %16, 1
store i32 %17, i32* @dump.num
%18 = load i8*, i8** %8
%19 = call i32 @sprintf(i8* %15, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %16, i8* %18)
%20 = getelementptr inbounds [64 x i8], [64 x i8]* %10, i64 0, i64 0
%21 = load i64, i64* %7
%22 = call i8* @fsize_str(i64 %21)
%23 = load i8*, i8** %9
%24 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %20, i8* %22, i8* %23)
%25 = getelementptr inbounds [64 x i8], [64 x i8]* %10, i64 0, i64 0
%26 = call i32* @fopen(i8* %25, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
store i32* %26, i32** %12
%27 = icmp ne i32* %26, null
br i1 %27, label %34, label %28
28: ; preds = %4
%29 = load i32, i32* @stderr
%30 = getelementptr inbounds [64 x i8], [64 x i8]* %10, i64 0, i64 0
%31 = load i32, i32* @errno
%32 = call i8* @strerror(i32 %31)
%33 = call i32 (i32, i8*, ...) @fprintf(i32 %29, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.3, i64 0, i64 0), i8* %30, i8* %32)
store i32 -1, i32* %5
br label %67
34: ; preds = %4
br label %35
35: ; preds = %56, %34
%36 = load i64, i64* %7
%37 = icmp ugt i64 %36, 0
br i1 %37, label %38, label %64
38: ; preds = %35
%39 = load i64, i64* %7
%40 = icmp ult i64 4096, %39
br i1 %40, label %41, label %42
41: ; preds = %38
br label %44
42: ; preds = %38
%43 = load i64, i64* %7
br label %44
44: ; preds = %42, %41
%45 = phi i64 [ 4096, %41 ], [ %43, %42 ]
store i64 %45, i64* %14
%46 = getelementptr inbounds [4096 x i8], [4096 x i8]* %11, i64 0, i64 0
%47 = load i64, i64* %14
%48 = load i32*, i32** %6
%49 = call i64 @fread(i8* %46, i32 1, i64 %47, i32* %48)
store i64 %49, i64* %13
%50 = icmp ne i64 %49, 0
br i1 %50, label %56, label %51
51: ; preds = %44
%52 = load i32, i32* @stderr
%53 = call i32 (i32, i8*, ...) @fprintf(i32 %52, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0))
%54 = load i32*, i32** %12
%55 = call i32 @fclose(i32* %54)
store i32 -1, i32* %5
br label %67
56: ; preds = %44
%57 = getelementptr inbounds [4096 x i8], [4096 x i8]* %11, i64 0, i64 0
%58 = load i64, i64* %13
%59 = load i32*, i32** %12
%60 = call i32 @fwrite(i8* %57, i32 1, i64 %58, i32* %59)
%61 = load i64, i64* %13
%62 = load i64, i64* %7
%63 = sub i64 %62, %61
store i64 %63, i64* %7
br label %35
64: ; preds = %35
%65 = load i32*, i32** %12
%66 = call i32 @fclose(i32* %65)
store i32 0, i32* %5
br label %67
67: ; preds = %64, %51, %28
%68 = load i32, i32* %5
ret i32 %68
}

LLVM IR (-O2):
@dump.num = internal unnamed_addr global i32 0
@.str = [11 x i8] c"rip%04d.%s\00"
@.str.1 = [23 x i8] c"dumping: %s [%s] (%s)\0A\00"
@.str.2 = [3 x i8] c"wb\00"
@stderr = global i32 0
@.str.3 = [41 x i8] c"failed to open file: %s for writing: %s\0A\00"
@errno = global i32 0
@.str.4 = [16 x i8] c"unexpected EOF\0A\00"
define i32 @dump(i32* nocapture %0, i64 %1, i8* %2, i8* %3) {
%5 = alloca [64 x i8]
%6 = alloca [4096 x i8]
%7 = getelementptr inbounds [64 x i8], [64 x i8]* %5, i64 0, i64 0
%8 = getelementptr inbounds [4096 x i8], [4096 x i8]* %6, i64 0, i64 0
%9 = load i32, i32* @dump.num
%10 = add nsw i32 %9, 1
store i32 %10, i32* @dump.num
%11 = call i32 @sprintf(i8* %7, i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %9, i8* %2)
%12 = tail call i8* @fsize_str(i64 %1)
%13 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %7, i8* %12, i8* %3)
%14 = call i32* @fopen(i8* %7, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%15 = icmp eq i32* %14, null
br i1 %15, label %18, label %16
16: ; preds = %4
%17 = icmp eq i64 %1, 0
br i1 %17, label %37, label %23
18: ; preds = %4
%19 = load i32, i32* @stderr
%20 = load i32, i32* @errno
%21 = call i8* @strerror(i32 %20)
%22 = call i32 (i32, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.3, i64 0, i64 0), i8* %7, i8* %21)
br label %39
23: ; preds = %16, %33
%24 = phi i64 [ %35, %33 ], [ %1, %16 ]
%25 = icmp ult i64 %24, 4096
%26 = select i1 %25, i64 %24, i64 4096
%27 = call i64 @fread(i8* %8, i32 1, i64 %26, i32* %0)
%28 = icmp eq i64 %27, 0
br i1 %28, label %29, label %33
29: ; preds = %23
%30 = load i32, i32* @stderr
%31 = call i32 (i32, i8*, ...) @fprintf(i32 %30, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0))
%32 = call i32 @fclose(i32* %14)
br label %39
33: ; preds = %23
%34 = call i32 @fwrite(i8* %8, i32 1, i64 %27, i32* %14)
%35 = sub i64 %24, %27
%36 = icmp eq i64 %35, 0
br i1 %36, label %37, label %23
37: ; preds = %33, %16
%38 = call i32 @fclose(i32* %14)
br label %39
39: ; preds = %29, %37, %18
%40 = phi i32 [ 0, %37 ], [ -1, %18 ], [ -1, %29 ]
ret i32 %40
}