#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*/ fmerge(int /*<<< orphan*/ *, int /*<<< orphan*/ **, size_t,
                          int /*<<< orphan*/, int);
int fread_file(int /*<<< orphan*/ *, int *, int);
int /*<<< orphan*/ free(int *);
int /*<<< orphan*/ fwrite_file(int /*<<< orphan*/ *, int *, int);
scalar_t__ malloc(size_t);
int /*<<< orphan*/ merge_sort(int *, int /*<<< orphan*/, int);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
int /*<<< orphan*/ *tmpfile();
void fmerge_sort(FILE *f, size_t buffer_size, size_t max_size) {
  FILE **file_list = 0;
  int len = buffer_size / sizeof(int);
  int len_read = 0;
  int times = 0;
  int *buffer = (int *)malloc(buffer_size);
  file_list = (FILE **)malloc(max_size);
  while (1) {
    len_read = fread_file(f, buffer, len);
    if (!len_read) {
      break;
    }
    merge_sort(buffer, 0, len_read - 1);
    file_list[times] = tmpfile();
    fwrite_file(file_list[times], buffer, len_read);
    rewind(file_list[times]);
    times = times + 1;
  };
  free(buffer);
  rewind(f);
  fmerge(f, file_list, buffer_size, 0, times);
  free(file_list);
}


x86 assembly (clang, -O0):
<fmerge_sort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x10(%rbp),%rax
shr    $0x2,%rax
mov    %eax,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x10(%rbp),%rdi
call   1080 <malloc@plt>
mov    %rax,-0x38(%rbp)
mov    -0x18(%rbp),%rdi
call   1080 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1050 <fread_file@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jne    11f1 <fmerge_sort+0x71>
jmp    124f <fmerge_sort+0xcf>
mov    -0x38(%rbp),%rdi
mov    -0x28(%rbp),%edx
sub    $0x1,%edx
xor    %esi,%esi
call   10a0 <merge_sort@plt>
mov    $0x0,%al
call   1070 <tmpfile@plt>
mov    %rax,%rdx
mov    -0x20(%rbp),%rax
movslq -0x2c(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x20(%rbp),%rax
movslq -0x2c(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
mov    -0x38(%rbp),%rsi
mov    -0x28(%rbp),%edx
call   1060 <fwrite_file@plt>
mov    -0x20(%rbp),%rax
movslq -0x2c(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
call   1040 <rewind@plt>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    11cf <fmerge_sort+0x4f>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
mov    -0x8(%rbp),%rdi
call   1040 <rewind@plt>
mov    -0x8(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x10(%rbp),%rdx
mov    -0x2c(%rbp),%r8d
xor    %ecx,%ecx
call   1090 <fmerge@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fmerge_sort>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%rbp
mov    %rdi,%r15
mov    %rsi,%r13
shr    $0x2,%r13
mov    %rsi,(%rsp)
mov    %rsi,%rdi
call   1080 <malloc@plt>
mov    %rax,%rbx
mov    %rbp,%rdi
call   1080 <malloc@plt>
mov    %rax,%r12
mov    %r15,%rdi
mov    %rbx,%rsi
mov    %r13d,%edx
call   1050 <fread_file@plt>
test   %eax,%eax
je     121b <fmerge_sort+0x9b>
mov    %eax,%r14d
xor    %ebp,%ebp
nopl   0x0(%rax)
lea    -0x1(%r14),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   10a0 <merge_sort@plt>
xor    %eax,%eax
call   1070 <tmpfile@plt>
mov    %rax,(%r12,%rbp,8)
mov    %rax,%rdi
mov    %rbx,%rsi
mov    %r14d,%edx
call   1060 <fwrite_file@plt>
mov    (%r12,%rbp,8),%rdi
call   1040 <rewind@plt>
add    $0x1,%rbp
mov    %r15,%rdi
mov    %rbx,%rsi
mov    %r13d,%edx
call   1050 <fread_file@plt>
mov    %eax,%r14d
test   %eax,%eax
jne    11d0 <fmerge_sort+0x50>
jmp    121d <fmerge_sort+0x9d>
xor    %ebp,%ebp
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r15,%rdi
call   1040 <rewind@plt>
mov    %r15,%rdi
mov    %r12,%rsi
mov    (%rsp),%rdx
xor    %ecx,%ecx
mov    %ebp,%r8d
call   1090 <fmerge@plt>
mov    %r12,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <free@plt>

LLVM IR (-O0):
define void @fmerge_sort(i32* %0, i64 %1, i64 %2) {
%4 = alloca i32*
%5 = alloca i64
%6 = alloca i64
%7 = alloca i32**
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32*
store i32* %0, i32** %4
store i64 %1, i64* %5
store i64 %2, i64* %6
store i32** null, i32*** %7
%12 = load i64, i64* %5
%13 = udiv i64 %12, 4
%14 = trunc i64 %13 to i32
store i32 %14, i32* %8
store i32 0, i32* %9
store i32 0, i32* %10
%15 = load i64, i64* %5
%16 = call i64 @malloc(i64 %15)
%17 = inttoptr i64 %16 to i32*
store i32* %17, i32** %11
%18 = load i64, i64* %6
%19 = call i64 @malloc(i64 %18)
%20 = inttoptr i64 %19 to i32**
store i32** %20, i32*** %7
br label %21
21: ; preds = %3, %29
%22 = load i32*, i32** %4
%23 = load i32*, i32** %11
%24 = load i32, i32* %8
%25 = call i32 @fread_file(i32* %22, i32* %23, i32 %24)
store i32 %25, i32* %9
%26 = load i32, i32* %9
%27 = icmp ne i32 %26, 0
br i1 %27, label %29, label %28
28: ; preds = %21
br label %55
29: ; preds = %21
%30 = load i32*, i32** %11
%31 = load i32, i32* %9
%32 = sub nsw i32 %31, 1
%33 = call i32 @merge_sort(i32* %30, i32 0, i32 %32)
%34 = call i32* (...) @tmpfile()
%35 = load i32**, i32*** %7
%36 = load i32, i32* %10
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds i32*, i32** %35, i64 %37
store i32* %34, i32** %38
%39 = load i32**, i32*** %7
%40 = load i32, i32* %10
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds i32*, i32** %39, i64 %41
%43 = load i32*, i32** %42
%44 = load i32*, i32** %11
%45 = load i32, i32* %9
%46 = call i32 @fwrite_file(i32* %43, i32* %44, i32 %45)
%47 = load i32**, i32*** %7
%48 = load i32, i32* %10
%49 = sext i32 %48 to i64
%50 = getelementptr inbounds i32*, i32** %47, i64 %49
%51 = load i32*, i32** %50
%52 = call i32 @rewind(i32* %51)
%53 = load i32, i32* %10
%54 = add nsw i32 %53, 1
store i32 %54, i32* %10
br label %21
55: ; preds = %28
%56 = load i32*, i32** %11
%57 = call i32 @free(i32* %56)
%58 = load i32*, i32** %4
%59 = call i32 @rewind(i32* %58)
%60 = load i32*, i32** %4
%61 = load i32**, i32*** %7
%62 = load i64, i64* %5
%63 = load i32, i32* %10
%64 = call i32 @fmerge(i32* %60, i32** %61, i64 %62, i32 0, i32 %63)
%65 = load i32**, i32*** %7
%66 = bitcast i32** %65 to i32*
%67 = call i32 @free(i32* %66)
ret void
}

LLVM IR (-O2):
define void @fmerge_sort(i32* %0, i64 %1, i64 %2) {
%4 = lshr i64 %1, 2
%5 = trunc i64 %4 to i32
%6 = tail call i64 @malloc(i64 %1)
%7 = inttoptr i64 %6 to i32*
%8 = tail call i64 @malloc(i64 %2)
%9 = inttoptr i64 %8 to i32**
%10 = tail call i32 @fread_file(i32* %0, i32* %7, i32 %5)
%11 = icmp eq i32 %10, 0
br i1 %11, label %27, label %12
12: ; preds = %3, %12
%13 = phi i64 [ %22, %12 ], [ 0, %3 ]
%14 = phi i32 [ %23, %12 ], [ %10, %3 ]
%15 = add nsw i32 %14, -1
%16 = tail call i32 @merge_sort(i32* %7, i32 0, i32 %15)
%17 = tail call i32* (...) @tmpfile()
%18 = getelementptr inbounds i32*, i32** %9, i64 %13
store i32* %17, i32** %18
%19 = tail call i32 @fwrite_file(i32* %17, i32* %7, i32 %14)
%20 = load i32*, i32** %18
%21 = tail call i32 @rewind(i32* %20)
%22 = add nuw i64 %13, 1
%23 = tail call i32 @fread_file(i32* %0, i32* %7, i32 %5)
%24 = icmp eq i32 %23, 0
br i1 %24, label %25, label %12
25: ; preds = %12
%26 = trunc i64 %22 to i32
br label %27
27: ; preds = %25, %3
%28 = phi i32 [ 0, %3 ], [ %26, %25 ]
%29 = tail call i32 @free(i32* %7)
%30 = tail call i32 @rewind(i32* %0)
%31 = tail call i32 @fmerge(i32* %0, i32** %9, i64 %1, i32 0, i32 %28)
%32 = inttoptr i64 %8 to i32*
%33 = tail call i32 @free(i32* %32)
ret void
}