#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;

/* Variables and functions */
int ENOMEM;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ free(char *);
char *malloc(size_t);
scalar_t__ merge(char *, char *, size_t, int);
int /*<<< orphan*/ perror(char *);
int split(char const *, int /*<<< orphan*/, char *, char *, size_t);
int /*<<< orphan*/ stderr;
int external_merge_sort(const char *inputpath, 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(inputpath, filesize, dir, buf, bufsize);
  if (chunks < 0) {
    free(buf);
    return -1;
  }
  if (merge(dir, buf, bufsize, chunks)) {
    fprintf(stderr, "Failed to merge chunks\n");
    free(buf);
    return -1;
  }
  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    1240 <external_merge_sort+0xe0>
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    1240 <external_merge_sort+0xe0>
mov    -0x20(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    -0x28(%rbp),%rdx
mov    -0x2c(%rbp),%ecx
call   1060 <merge@plt>
cmp    $0x0,%rax
je     1230 <external_merge_sort+0xd0>
mov    0x2dcb(%rip),%rax
mov    (%rax),%edi
lea    0xdf1(%rip),%rsi
call   1050 <fprintf@plt>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1240 <external_merge_sort+0xe0>
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   %r12
push   %rbx
mov    %rcx,%r14
mov    %rdx,%r15
mov    %esi,%r12d
mov    %rdi,%rbp
mov    %rcx,%rdi
call   1070 <malloc@plt>
test   %rax,%rax
je     11e1 <external_merge_sort+0x81>
mov    %rax,%rbx
mov    %rbp,%rdi
mov    %r12d,%esi
mov    %r15,%rdx
mov    %rax,%rcx
mov    %r14,%r8
call   1040 <split@plt>
test   %eax,%eax
js     11c6 <external_merge_sort+0x66>
mov    %r15,%rdi
mov    %rbx,%rsi
mov    %r14,%rdx
mov    %eax,%ecx
call   1060 <merge@plt>
test   %rax,%rax
je     11d5 <external_merge_sort+0x75>
mov    0x2e20(%rip),%rax
mov    (%rax),%edi
lea    0xe46(%rip),%rsi
call   1050 <fprintf@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    $0xffffffff,%eax
jmp    11f8 <external_merge_sort+0x98>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %eax,%eax
jmp    11f8 <external_merge_sort+0x98>
lea    0xe18(%rip),%rdi
call   1080 <perror@plt>
mov    0x2dec(%rip),%rcx
xor    %eax,%eax
sub    (%rcx),%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [7 x i8] c"malloc\00"
@ENOMEM = global i32 0
@stderr = global i32 0
@.str.1 = [24 x i8] c"Failed to merge chunks\0A\00"
define i32 @external_merge_sort(i8* %0, i32 %1, i8* %2, i64 %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i32
%11 = alloca i8*
%12 = alloca i64
store i8* %0, i8** %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 %48
21: ; preds = %4
%22 = load i8*, i8** %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(i8* %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 %48
33: ; preds = %21
%34 = load i8*, i8** %8
%35 = load i8*, i8** %11
%36 = load i64, i64* %9
%37 = load i32, i32* %10
%38 = call i64 @merge(i8* %34, i8* %35, i64 %36, i32 %37)
%39 = icmp ne i64 %38, 0
br i1 %39, label %40, label %45
40: ; preds = %33
%41 = load i32, i32* @stderr
%42 = call i32 @fprintf(i32 %41, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
%43 = load i8*, i8** %11
%44 = call i32 @free(i8* %43)
store i32 -1, i32* %5
br label %48
45: ; preds = %33
%46 = load i8*, i8** %11
%47 = call i32 @free(i8* %46)
store i32 0, i32* %5
br label %48
48: ; preds = %45, %40, %30, %17
%49 = load i32, i32* %5
ret i32 %49
}

LLVM IR (-O2):
@.str = [7 x i8] c"malloc\00"
@ENOMEM = global i32 0
@stderr = global i32 0
@.str.1 = [24 x i8] c"Failed to merge chunks\0A\00"
define i32 @external_merge_sort(i8* %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 %25
11: ; preds = %4
%12 = tail call i32 @split(i8* %0, i32 %1, i8* %2, i8* %5, i64 %3)
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %11
%15 = tail call i32 @free(i8* %5)
br label %25
16: ; preds = %11
%17 = tail call i64 @merge(i8* %2, i8* %5, i64 %3, i32 %12)
%18 = icmp eq i64 %17, 0
br i1 %18, label %23, label %19
19: ; preds = %16
%20 = load i32, i32* @stderr
%21 = tail call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
%22 = tail call i32 @free(i8* %5)
br label %25
23: ; preds = %16
%24 = tail call i32 @free(i8* %5)
br label %25
25: ; preds = %23, %19, %14, %7
%26 = phi i32 [ -1, %14 ], [ -1, %19 ], [ 0, %23 ], [ %10, %7 ]
ret i32 %26
}