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

/* Variables and functions */
int ENOMEM;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, size_t, size_t);
int /*<<< orphan*/ free(char *);
char *malloc(size_t);
int /*<<< orphan*/ merge(char *, char *, size_t, int, size_t);
int /*<<< orphan*/ perror(char *);
int split(int /*<<< orphan*/ *, int /*<<< orphan*/, char *, char *, size_t);
int /*<<< orphan*/ stderr;
int external_merge_sort(FILE *f, off_t filesize, char *dir, size_t bufsize) {
  int chunks;
  char *buf;
  size_t chunk_offset;
  buf = malloc(bufsize);
  if (!buf) {
    perror("malloc");
    return -ENOMEM;
  }
  chunks = split(f, filesize, dir, buf, bufsize);
  if (chunks < 0) {
    free(buf);
    return -1;
  }
  chunk_offset = 0;
  while (chunk_offset < bufsize) {
    fprintf(stderr, "-> Merging chunks at offset %zu/%zu\n", chunk_offset,
            bufsize);
    merge(dir, buf, bufsize, chunks, chunk_offset);
    chunk_offset += bufsize / chunks;
  }
  free(buf);
  return 0;
}


x86 assembly (clang, -O0):
<external_merge_sort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
call   1070 <malloc@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    11ae <external_merge_sort+0x4e>
lea    0xe6a(%rip),%rdi
call   1080 <perror@plt>
xor    %eax,%eax
mov    0x2e3c(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    125d <external_merge_sort+0xfd>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x20(%rbp),%rdx
mov    -0x38(%rbp),%rcx
mov    -0x28(%rbp),%r8
call   1040 <split@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    11e8 <external_merge_sort+0x88>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    125d <external_merge_sort+0xfd>
movq   $0x0,-0x40(%rbp)
mov    -0x40(%rbp),%rax
cmp    -0x28(%rbp),%rax
jae    124d <external_merge_sort+0xed>
mov    0x2dd3(%rip),%rax
mov    (%rax),%edi
mov    -0x40(%rbp),%rdx
mov    -0x28(%rbp),%rcx
lea    0xdf1(%rip),%rsi
call   1050 <fprintf@plt>
mov    -0x20(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    -0x28(%rbp),%rdx
mov    -0x2c(%rbp),%ecx
mov    -0x40(%rbp),%r8
call   1060 <merge@plt>
mov    -0x28(%rbp),%rax
movslq -0x2c(%rbp),%rcx
xor    %edx,%edx
div    %rcx
add    -0x40(%rbp),%rax
mov    %rax,-0x40(%rbp)
jmp    11f0 <external_merge_sort+0x90>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<external_merge_sort>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,%rbx
mov    %rdx,%r14
mov    %esi,%r12d
mov    %rdi,%rbp
mov    %rcx,%rdi
call   1070 <malloc@plt>
test   %rax,%rax
je     1236 <external_merge_sort+0xd6>
mov    %rax,%r15
mov    %rbp,%rdi
mov    %r12d,%esi
mov    %r14,%rdx
mov    %rax,%rcx
mov    %rbx,%r8
call   1040 <split@plt>
test   %eax,%eax
js     121a <external_merge_sort+0xba>
mov    %r14,(%rsp)
test   %rbx,%rbx
je     120e <external_merge_sort+0xae>
mov    %eax,%r12d
mov    %eax,%r13d
xor    %ebp,%ebp
mov    %rbx,%r14
shr    $0x20,%r14
jmp    11d0 <external_merge_sort+0x70>
nopl   (%rax)
mov    %rbx,%rax
xor    %edx,%edx
div    %r13
add    %rax,%rbp
cmp    %rbx,%rbp
jae    120e <external_merge_sort+0xae>
mov    0x2e01(%rip),%rax
mov    (%rax),%edi
lea    0xe27(%rip),%rsi
mov    %rbp,%rdx
mov    %rbx,%rcx
call   1050 <fprintf@plt>
mov    (%rsp),%rdi
mov    %r15,%rsi
mov    %rbx,%rdx
mov    %r12d,%ecx
mov    %rbp,%r8
call   1060 <merge@plt>
test   %r14,%r14
jne    11c0 <external_merge_sort+0x60>
mov    %ebx,%eax
xor    %edx,%edx
div    %r13d
jmp    11c8 <external_merge_sort+0x68>
mov    %r15,%rdi
call   1030 <free@plt>
xor    %eax,%eax
jmp    1227 <external_merge_sort+0xc7>
mov    %r15,%rdi
call   1030 <free@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdc3(%rip),%rdi
call   1080 <perror@plt>
mov    0x2d97(%rip),%rcx
xor    %eax,%eax
sub    (%rcx),%eax
jmp    1227 <external_merge_sort+0xc7>

LLVM IR (-O0):
@.str = [7 x i8] c"malloc\00"
@ENOMEM = global i32 0
@stderr = global i32 0
@.str.1 = [37 x i8] c"-> Merging chunks at offset %zu/%zu\0A\00"
define i32 @external_merge_sort(i32* %0, i32 %1, i8* %2, i64 %3) {
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i32
%11 = alloca i8*
%12 = alloca i64
store i32* %0, i32** %6
store i32 %1, i32* %7
store i8* %2, i8** %8
store i64 %3, i64* %9
%13 = load i64, i64* %9
%14 = call i8* @malloc(i64 %13)
store i8* %14, i8** %11
%15 = load i8*, i8** %11
%16 = icmp ne i8* %15, null
br i1 %16, label %21, label %17
17: ; preds = %4
%18 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%19 = load i32, i32* @ENOMEM
%20 = sub nsw i32 0, %19
store i32 %20, i32* %5
br label %58
21: ; preds = %4
%22 = load i32*, i32** %6
%23 = load i32, i32* %7
%24 = load i8*, i8** %8
%25 = load i8*, i8** %11
%26 = load i64, i64* %9
%27 = call i32 @split(i32* %22, i32 %23, i8* %24, i8* %25, i64 %26)
store i32 %27, i32* %10
%28 = load i32, i32* %10
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %33
30: ; preds = %21
%31 = load i8*, i8** %11
%32 = call i32 @free(i8* %31)
store i32 -1, i32* %5
br label %58
33: ; preds = %21
store i64 0, i64* %12
br label %34
34: ; preds = %38, %33
%35 = load i64, i64* %12
%36 = load i64, i64* %9
%37 = icmp ult i64 %35, %36
br i1 %37, label %38, label %55
38: ; preds = %34
%39 = load i32, i32* @stderr
%40 = load i64, i64* %12
%41 = load i64, i64* %9
%42 = call i32 @fprintf(i32 %39, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i64 %40, i64 %41)
%43 = load i8*, i8** %8
%44 = load i8*, i8** %11
%45 = load i64, i64* %9
%46 = load i32, i32* %10
%47 = load i64, i64* %12
%48 = call i32 @merge(i8* %43, i8* %44, i64 %45, i32 %46, i64 %47)
%49 = load i64, i64* %9
%50 = load i32, i32* %10
%51 = sext i32 %50 to i64
%52 = udiv i64 %49, %51
%53 = load i64, i64* %12
%54 = add i64 %53, %52
store i64 %54, i64* %12
br label %34
55: ; preds = %34
%56 = load i8*, i8** %11
%57 = call i32 @free(i8* %56)
store i32 0, i32* %5
br label %58
58: ; preds = %55, %30, %17
%59 = load i32, i32* %5
ret i32 %59
}

LLVM IR (-O2):
@.str = [7 x i8] c"malloc\00"
@ENOMEM = global i32 0
@stderr = global i32 0
@.str.1 = [37 x i8] c"-> Merging chunks at offset %zu/%zu\0A\00"
define i32 @external_merge_sort(i32* %0, i32 %1, i8* %2, i64 %3) {
%5 = tail call i8* @malloc(i64 %3)
%6 = icmp eq i8* %5, null
br i1 %6, label %7, label %11
7: ; preds = %4
%8 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%9 = load i32, i32* @ENOMEM
%10 = sub nsw i32 0, %9
br label %30
11: ; preds = %4
%12 = tail call i32 @split(i32* %0, i32 %1, i8* %2, i8* %5, i64 %3)
%13 = icmp slt i32 %12, 0
br i1 %13, label %18, label %14
14: ; preds = %11
%15 = icmp eq i64 %3, 0
br i1 %15, label %28, label %16
16: ; preds = %14
%17 = zext i32 %12 to i64
br label %20
18: ; preds = %11
%19 = tail call i32 @free(i8* %5)
br label %30
20: ; preds = %16, %20
%21 = phi i64 [ 0, %16 ], [ %26, %20 ]
%22 = load i32, i32* @stderr
%23 = tail call i32 @fprintf(i32 %22, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i64 %21, i64 %3)
%24 = tail call i32 @merge(i8* %2, i8* %5, i64 %3, i32 %12, i64 %21)
%25 = udiv i64 %3, %17
%26 = add i64 %25, %21
%27 = icmp ult i64 %26, %3
br i1 %27, label %20, label %28
28: ; preds = %20, %14
%29 = tail call i32 @free(i8* %5)
br label %30
30: ; preds = %28, %18, %7
%31 = phi i32 [ -1, %18 ], [ 0, %28 ], [ %10, %7 ]
ret i32 %31
}