#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 */
struct stat {
  size_t st_size;
};
typedef size_t off_t;
typedef scalar_t__ clock_t;

/* Variables and functions */
double CLOCKS_PER_SEC;
int /*<<< orphan*/ EXIT_FAILURE;
scalar_t__ clock();
scalar_t__ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ external_merge_sort(char const *, size_t, char *, size_t);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
char *mkdtemp(char *);
int /*<<< orphan*/ perror(char *);
scalar_t__ stat(char const *, struct stat *);
int /*<<< orphan*/ stderr;
size_t strtol(char const *, int /*<<< orphan*/ *, int /*<<< orphan*/);
int main(int argc, const char *argv[]) {
  char *dirpath;
  size_t bufsize;
  char dirname[] = "sort.XXXXXX";
  struct stat sb;
  off_t file_size;
  clock_t start, end;
  if (argc != 3) {
    fprintf(stderr, "Usage: %s <file to sort> <buffer size>\n", argv[0]);
    exit(EXIT_FAILURE);
  }
  errno = 0;
  bufsize = strtol(argv[2], NULL, 0);
  if (errno) {
    perror("strtol");
    exit(EXIT_FAILURE);
  }
  if (stat(argv[1], &sb)) {
    perror("stat");
    exit(EXIT_FAILURE);
  }
  file_size = sb.st_size;
  if (file_size % bufsize) {
    fprintf(stderr, "Buffer size %zu is not divisor of file size %zu ", bufsize,
            file_size);
    fprintf(stderr, "but this is required.\n");
    fprintf(stderr, "You may, for example, choose buffer size %zu\n",
            file_size / 10);
    exit(EXIT_FAILURE);
  }
  dirpath = mkdtemp(dirname);
  if (dirpath == NULL) {
    perror("mkdtemp");
    exit(EXIT_FAILURE);
  }
  start = clock();
  if (external_merge_sort(argv[1], file_size, dirpath, bufsize)) {
    fprintf(stderr, "Failed to sort %s\n", argv[1]);
    exit(EXIT_FAILURE);
  }
  end = clock();
  fprintf(stderr, "%ld bytes sorted in %f seconds\n", file_size,
          (double)(end - start) / CLOCKS_PER_SEC);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0xe63(%rip),%rax
mov    %rax,-0x2c(%rbp)
mov    0xe61(%rip),%eax
mov    %eax,-0x24(%rbp)
cmpl   $0x3,-0x8(%rbp)
je     11e0 <main+0x60>
mov    0x2e1d(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe41(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    0x2def(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    0x2de9(%rip),%rax
movq   $0x0,(%rax)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1060 <strtol@plt>
mov    %rax,-0x20(%rbp)
mov    0x2dc4(%rip),%rax
cmpq   $0x0,(%rax)
je     1230 <main+0xb0>
lea    0xe17(%rip),%rdi
call   1080 <perror@plt>
mov    0x2d9f(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    -0x38(%rbp),%rsi
call   1050 <stat@plt>
cmp    $0x0,%rax
je     1265 <main+0xe5>
lea    0xde9(%rip),%rdi
call   1080 <perror@plt>
mov    0x2d6a(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    -0x38(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rax
xor    %ecx,%ecx
mov    %ecx,%edx
divq   -0x20(%rbp)
cmp    $0x0,%rdx
je     12ef <main+0x16f>
mov    0x2d4e(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rdx
mov    -0x40(%rbp),%rcx
lea    0xda5(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    0x2d2f(%rip),%rax
mov    (%rax),%edi
lea    0xdbf(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    0x2d18(%rip),%rax
mov    (%rax),%edi
mov    -0x40(%rbp),%rax
mov    $0xa,%ecx
xor    %edx,%edx
div    %rcx
mov    %rax,%rdx
lea    0xdae(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    0x2ce0(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
lea    -0x2c(%rbp),%rdi
call   1070 <mkdtemp@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1321 <main+0x1a1>
lea    0xda8(%rip),%rdi
call   1080 <perror@plt>
mov    0x2cae(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    $0x0,%al
call   1030 <clock@plt>
mov    %rax,-0x48(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    -0x40(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rcx
call   1090 <external_merge_sort@plt>
cmp    $0x0,%rax
je     137c <main+0x1fc>
mov    0x2c82(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xd57(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    0x2c53(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    $0x0,%al
call   1030 <clock@plt>
mov    %rax,-0x50(%rbp)
mov    0x2c4a(%rip),%rax
mov    (%rax),%edi
mov    -0x40(%rbp),%rdx
mov    -0x50(%rbp),%rax
sub    -0x48(%rbp),%rax
cvtsi2sd %rax,%xmm0
mov    0x2c40(%rip),%rax
divsd  (%rax),%xmm0
lea    0xd1e(%rip),%rsi
mov    $0x1,%al
call   1040 <fprintf@plt>
xor    %eax,%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    %rsi,%r14
movabs $0x5858582e74726f73,%rax
58 58 58
mov    %rax,0x10(%rsp)
movl   $0x585858,0x18(%rsp)
cmp    $0x3,%edi
jne    12f1 <main+0x171>
mov    0x2e19(%rip),%rbx
movq   $0x0,(%rbx)
mov    0x10(%r14),%rdi
xor    %esi,%esi
xor    %edx,%edx
call   1060 <strtol@plt>
cmpq   $0x0,(%rbx)
jne    1306 <main+0x186>
mov    %rax,%r15
mov    0x8(%r14),%rdi
lea    0x8(%rsp),%rsi
call   1050 <stat@plt>
test   %rax,%rax
jne    130f <main+0x18f>
mov    0x8(%rsp),%r12
mov    %r15,%rax
or     %r12,%rax
shr    $0x20,%rax
je     1294 <main+0x114>
mov    %r12,%rax
xor    %edx,%edx
div    %r15
test   %rdx,%rdx
jne    12a5 <main+0x125>
lea    0x10(%rsp),%rdi
call   1070 <mkdtemp@plt>
test   %rax,%rax
je     1318 <main+0x198>
mov    %rax,%rbx
xor    %eax,%eax
call   1030 <clock@plt>
mov    %rax,%r13
mov    0x8(%r14),%rdi
mov    %r12,%rsi
mov    %rbx,%rdx
mov    %r15,%rcx
call   1090 <external_merge_sort@plt>
test   %rax,%rax
jne    1326 <main+0x1a6>
xor    %eax,%eax
call   1030 <clock@plt>
mov    0x2d7a(%rip),%rcx
sub    %r13,%rax
cvtsi2sd %rax,%xmm0
mov    (%rcx),%edi
mov    0x2d79(%rip),%rax
divsd  (%rax),%xmm0
lea    0xe57(%rip),%rsi
mov    %r12,%rdx
mov    $0x1,%al
call   1040 <fprintf@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret
mov    %r12d,%eax
xor    %edx,%edx
div    %r15d
test   %rdx,%rdx
je     1215 <main+0x95>
mov    0x2d2c(%rip),%rbx
mov    (%rbx),%edi
lea    0xd8b(%rip),%rsi
mov    %r15,%rdx
mov    %r12,%rcx
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    (%rbx),%edi
lea    0xda6(%rip),%rsi
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    (%rbx),%edi
movabs $0xcccccccccccccccd,%rcx
cc cc cc
mov    %r12,%rax
mul    %rcx
shr    $0x3,%rdx
lea    0xd99(%rip),%rsi
jmp    133a <main+0x1ba>
mov    0x2ce0(%rip),%rax
mov    (%rax),%edi
mov    (%r14),%rdx
lea    0xd08(%rip),%rsi
jmp    133a <main+0x1ba>
lea    0xd27(%rip),%rdi
jmp    131f <main+0x19f>
lea    0xd25(%rip),%rdi
jmp    131f <main+0x19f>
lea    0xd97(%rip),%rdi
call   1080 <perror@plt>
jmp    1341 <main+0x1c1>
mov    0x2cab(%rip),%rax
mov    (%rax),%edi
mov    0x8(%r14),%rdx
lea    0xd84(%rip),%rsi
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    0x2c80(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
%struct.stat = type { i64 }
@__const.main.dirname = [12 x i8] c"sort.XXXXXX\00"
@stderr = global i32 0
@.str = [40 x i8] c"Usage: %s <file to sort> <buffer size>\0A\00"
@EXIT_FAILURE = global i32 0
@errno = global i64 0
@.str.1 = [7 x i8] c"strtol\00"
@.str.2 = [5 x i8] c"stat\00"
@.str.3 = [49 x i8] c"Buffer size %zu is not divisor of file size %zu \00"
@.str.4 = [23 x i8] c"but this is required.\0A\00"
@.str.5 = [46 x i8] c"You may, for example, choose buffer size %zu\0A\00"
@.str.6 = [8 x i8] c"mkdtemp\00"
@.str.7 = [19 x i8] c"Failed to sort %s\0A\00"
@.str.8 = [32 x i8] c"%ld bytes sorted in %f seconds\0A\00"
@CLOCKS_PER_SEC = global double 0.000000e+00
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i64
%8 = alloca [12 x i8]
%9 = alloca %struct.stat
%10 = alloca i64
%11 = alloca i64
%12 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%13 = bitcast [12 x i8]* %8 to i8*
%14 = load i32, i32* %4
%15 = icmp ne i32 %14, 3
br i1 %15, label %16, label %24
16: ; preds = %2
%17 = load i32, i32* @stderr
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 0
%20 = load i8*, i8** %19
%21 = call i32 (i32, i8*, ...) @fprintf(i32 %17, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0), i8* %20)
%22 = load i32, i32* @EXIT_FAILURE
%23 = call i32 @exit(i32 %22)
unreachable
24: ; preds = %2
store i64 0, i64* @errno
%25 = load i8**, i8*** %5
%26 = getelementptr inbounds i8*, i8** %25, i64 2
%27 = load i8*, i8** %26
%28 = call i64 @strtol(i8* %27, i32* null, i32 0)
store i64 %28, i64* %7
%29 = load i64, i64* @errno
%30 = icmp ne i64 %29, 0
br i1 %30, label %31, label %35
31: ; preds = %24
%32 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%33 = load i32, i32* @EXIT_FAILURE
%34 = call i32 @exit(i32 %33)
unreachable
35: ; preds = %24
%36 = load i8**, i8*** %5
%37 = getelementptr inbounds i8*, i8** %36, i64 1
%38 = load i8*, i8** %37
%39 = call i64 @stat(i8* %38, %struct.stat* %9)
%40 = icmp ne i64 %39, 0
br i1 %40, label %41, label %45
41: ; preds = %35
%42 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%43 = load i32, i32* @EXIT_FAILURE
%44 = call i32 @exit(i32 %43)
unreachable
45: ; preds = %35
%46 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 0
%47 = load i64, i64* %46
store i64 %47, i64* %10
%48 = load i64, i64* %10
%49 = load i64, i64* %7
%50 = urem i64 %48, %49
%51 = icmp ne i64 %50, 0
br i1 %51, label %52, label %65
52: ; preds = %45
%53 = load i32, i32* @stderr
%54 = load i64, i64* %7
%55 = load i64, i64* %10
%56 = call i32 (i32, i8*, ...) @fprintf(i32 %53, i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.3, i64 0, i64 0), i64 %54, i64 %55)
%57 = load i32, i32* @stderr
%58 = call i32 (i32, i8*, ...) @fprintf(i32 %57, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0))
%59 = load i32, i32* @stderr
%60 = load i64, i64* %10
%61 = udiv i64 %60, 10
%62 = call i32 (i32, i8*, ...) @fprintf(i32 %59, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.5, i64 0, i64 0), i64 %61)
%63 = load i32, i32* @EXIT_FAILURE
%64 = call i32 @exit(i32 %63)
unreachable
65: ; preds = %45
%66 = getelementptr inbounds [12 x i8], [12 x i8]* %8, i64 0, i64 0
%67 = call i8* @mkdtemp(i8* %66)
store i8* %67, i8** %6
%68 = load i8*, i8** %6
%69 = icmp eq i8* %68, null
br i1 %69, label %70, label %74
70: ; preds = %65
%71 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.6, i64 0, i64 0))
%72 = load i32, i32* @EXIT_FAILURE
%73 = call i32 @exit(i32 %72)
unreachable
74: ; preds = %65
%75 = call i64 (...) @clock()
store i64 %75, i64* %11
%76 = load i8**, i8*** %5
%77 = getelementptr inbounds i8*, i8** %76, i64 1
%78 = load i8*, i8** %77
%79 = load i64, i64* %10
%80 = load i8*, i8** %6
%81 = load i64, i64* %7
%82 = call i64 @external_merge_sort(i8* %78, i64 %79, i8* %80, i64 %81)
%83 = icmp ne i64 %82, 0
br i1 %83, label %84, label %92
84: ; preds = %74
%85 = load i32, i32* @stderr
%86 = load i8**, i8*** %5
%87 = getelementptr inbounds i8*, i8** %86, i64 1
%88 = load i8*, i8** %87
%89 = call i32 (i32, i8*, ...) @fprintf(i32 %85, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.7, i64 0, i64 0), i8* %88)
%90 = load i32, i32* @EXIT_FAILURE
%91 = call i32 @exit(i32 %90)
unreachable
92: ; preds = %74
%93 = call i64 (...) @clock()
store i64 %93, i64* %12
%94 = load i32, i32* @stderr
%95 = load i64, i64* %10
%96 = load i64, i64* %12
%97 = load i64, i64* %11
%98 = sub nsw i64 %96, %97
%99 = sitofp i64 %98 to double
%100 = load double, double* @CLOCKS_PER_SEC
%101 = fdiv double %99, %100
%102 = call i32 (i32, i8*, ...) @fprintf(i32 %94, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.8, i64 0, i64 0), i64 %95, double %101)
ret i32 0
}

LLVM IR (-O2):
%struct.stat = type { i64 }
@__const.main.dirname = [12 x i8] c"sort.XXXXXX\00"
@stderr = global i32 0
@.str = [40 x i8] c"Usage: %s <file to sort> <buffer size>\0A\00"
@EXIT_FAILURE = global i32 0
@errno = global i64 0
@.str.1 = [7 x i8] c"strtol\00"
@.str.2 = [5 x i8] c"stat\00"
@.str.3 = [49 x i8] c"Buffer size %zu is not divisor of file size %zu \00"
@.str.4 = [23 x i8] c"but this is required.\0A\00"
@.str.5 = [46 x i8] c"You may, for example, choose buffer size %zu\0A\00"
@.str.6 = [8 x i8] c"mkdtemp\00"
@.str.7 = [19 x i8] c"Failed to sort %s\0A\00"
@.str.8 = [32 x i8] c"%ld bytes sorted in %f seconds\0A\00"
@CLOCKS_PER_SEC = global double 0.000000e+00
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [12 x i8]
%4 = alloca %struct.stat
%5 = getelementptr inbounds [12 x i8], [12 x i8]* %3, i64 0, i64 0
%6 = bitcast %struct.stat* %4 to i8*
%7 = icmp eq i32 %0, 3
br i1 %7, label %14, label %8
8: ; preds = %2
%9 = load i32, i32* @stderr
%10 = load i8*, i8** %1
%11 = tail call i32 (i32, i8*, ...) @fprintf(i32 %9, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0), i8* %10)
%12 = load i32, i32* @EXIT_FAILURE
%13 = tail call i32 @exit(i32 %12)
unreachable
14: ; preds = %2
store i64 0, i64* @errno
%15 = getelementptr inbounds i8*, i8** %1, i64 2
%16 = load i8*, i8** %15
%17 = tail call i64 @strtol(i8* nocapture %16, i32* null, i32 0)
%18 = load i64, i64* @errno
%19 = icmp eq i64 %18, 0
br i1 %19, label %24, label %20
20: ; preds = %14
%21 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%22 = load i32, i32* @EXIT_FAILURE
%23 = tail call i32 @exit(i32 %22)
unreachable
24: ; preds = %14
%25 = getelementptr inbounds i8*, i8** %1, i64 1
%26 = load i8*, i8** %25
%27 = call i64 @stat(i8* %26, %struct.stat* %4)
%28 = icmp eq i64 %27, 0
br i1 %28, label %33, label %29
29: ; preds = %24
%30 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%31 = load i32, i32* @EXIT_FAILURE
%32 = call i32 @exit(i32 %31)
unreachable
33: ; preds = %24
%34 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%35 = load i64, i64* %34
%36 = urem i64 %35, %17
%37 = icmp eq i64 %36, 0
br i1 %37, label %48, label %38
38: ; preds = %33
%39 = load i32, i32* @stderr
%40 = call i32 (i32, i8*, ...) @fprintf(i32 %39, i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.3, i64 0, i64 0), i64 %17, i64 %35)
%41 = load i32, i32* @stderr
%42 = call i32 (i32, i8*, ...) @fprintf(i32 %41, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0))
%43 = load i32, i32* @stderr
%44 = udiv i64 %35, 10
%45 = call i32 (i32, i8*, ...) @fprintf(i32 %43, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.5, i64 0, i64 0), i64 %44)
%46 = load i32, i32* @EXIT_FAILURE
%47 = call i32 @exit(i32 %46)
unreachable
48: ; preds = %33
%49 = call i8* @mkdtemp(i8* %5)
%50 = icmp eq i8* %49, null
br i1 %50, label %51, label %55
51: ; preds = %48
%52 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.6, i64 0, i64 0))
%53 = load i32, i32* @EXIT_FAILURE
%54 = call i32 @exit(i32 %53)
unreachable
55: ; preds = %48
%56 = call i64 (...) @clock()
%57 = load i8*, i8** %25
%58 = call i64 @external_merge_sort(i8* %57, i64 %35, i8* %49, i64 %17)
%59 = icmp eq i64 %58, 0
br i1 %59, label %66, label %60
60: ; preds = %55
%61 = load i32, i32* @stderr
%62 = load i8*, i8** %25
%63 = call i32 (i32, i8*, ...) @fprintf(i32 %61, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.7, i64 0, i64 0), i8* %62)
%64 = load i32, i32* @EXIT_FAILURE
%65 = call i32 @exit(i32 %64)
unreachable
66: ; preds = %55
%67 = call i64 (...) @clock()
%68 = load i32, i32* @stderr
%69 = sub nsw i64 %67, %56
%70 = sitofp i64 %69 to double
%71 = load double, double* @CLOCKS_PER_SEC
%72 = fdiv double %70, %71
%73 = call i32 (i32, i8*, ...) @fprintf(i32 %68, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.8, i64 0, i64 0), i64 %35, double %72)
ret i32 0
}