#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 log2(int);
int /*<<< orphan*/ merge(int /*<<< orphan*/ *, int, int, int);
void merge_sort(FILE *A, int p, int r) {
  int count = log2(r + 1);
  int d = 2, i, m;
  while (count--) {
    for (i = 0; i < ((r + 1) / d); i++) {
      m = i * d;
      merge(A, m, (d - 1) / 2 + m, d + m - 1);
    }
    d += 2;
  }
}


x86 assembly (clang, -O0):
<merge_sort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    -0x10(%rbp),%edi
add    $0x1,%edi
call   1030 <log2@plt>
mov    %eax,-0x14(%rbp)
movl   $0x2,-0x18(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x14(%rbp)
cmp    $0x0,%eax
je     11cd <merge_sort+0xad>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%eax
add    $0x1,%eax
cltd
idivl  -0x18(%rbp)
mov    %eax,%ecx
mov    -0x24(%rbp),%eax
cmp    %ecx,%eax
jge    11bf <merge_sort+0x9f>
mov    -0x1c(%rbp),%eax
imul   -0x18(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x20(%rbp),%esi
mov    -0x18(%rbp),%eax
sub    $0x1,%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,%edx
add    -0x20(%rbp),%edx
mov    -0x18(%rbp),%ecx
add    -0x20(%rbp),%ecx
sub    $0x1,%ecx
call   1040 <merge@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1162 <merge_sort+0x42>
mov    -0x18(%rbp),%eax
add    $0x2,%eax
mov    %eax,-0x18(%rbp)
jmp    1147 <merge_sort+0x27>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<merge_sort>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r12
add    $0x1,%edx
mov    %edx,(%rsp)
mov    %edx,%edi
call   1030 <log2@plt>
test   %eax,%eax
je     119c <merge_sort+0x7c>
mov    $0x2,%r13d
mov    $0x1,%r14d
xor    %r15d,%r15d
jmp    1164 <merge_sort+0x44>
add    $0x2,%r13d
add    $0x2,%r14d
add    $0x1,%r15d
mov    0x4(%rsp),%eax
test   %eax,%eax
je     119c <merge_sort+0x7c>
add    $0xffffffff,%eax
mov    %eax,0x4(%rsp)
mov    (%rsp),%eax
cltd
idiv   %r13d
test   %eax,%eax
jle    1150 <merge_sort+0x30>
mov    %eax,%ebp
xor    %ebx,%ebx
nopw   0x0(%rax,%rax,1)
lea    (%r15,%rbx,1),%edx
lea    (%r14,%rbx,1),%ecx
mov    %r12,%rdi
mov    %ebx,%esi
call   1040 <merge@plt>
add    %r13d,%ebx
add    $0xffffffff,%ebp
jne    1180 <merge_sort+0x60>
jmp    1150 <merge_sort+0x30>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @merge_sort(i32* %0, i32 %1, i32 %2) {
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i32* %0, i32** %4
store i32 %1, i32* %5
store i32 %2, i32* %6
%11 = load i32, i32* %6
%12 = add nsw i32 %11, 1
%13 = call i32 @log2(i32 %12)
store i32 %13, i32* %7
store i32 2, i32* %8
br label %14
14: ; preds = %45, %3
%15 = load i32, i32* %7
%16 = add nsw i32 %15, -1
store i32 %16, i32* %7
%17 = icmp ne i32 %15, 0
br i1 %17, label %18, label %48
18: ; preds = %14
store i32 0, i32* %9
br label %19
19: ; preds = %42, %18
%20 = load i32, i32* %9
%21 = load i32, i32* %6
%22 = add nsw i32 %21, 1
%23 = load i32, i32* %8
%24 = sdiv i32 %22, %23
%25 = icmp slt i32 %20, %24
br i1 %25, label %26, label %45
26: ; preds = %19
%27 = load i32, i32* %9
%28 = load i32, i32* %8
%29 = mul nsw i32 %27, %28
store i32 %29, i32* %10
%30 = load i32*, i32** %4
%31 = load i32, i32* %10
%32 = load i32, i32* %8
%33 = sub nsw i32 %32, 1
%34 = sdiv i32 %33, 2
%35 = load i32, i32* %10
%36 = add nsw i32 %34, %35
%37 = load i32, i32* %8
%38 = load i32, i32* %10
%39 = add nsw i32 %37, %38
%40 = sub nsw i32 %39, 1
%41 = call i32 @merge(i32* %30, i32 %31, i32 %36, i32 %40)
br label %42
42: ; preds = %26
%43 = load i32, i32* %9
%44 = add nsw i32 %43, 1
store i32 %44, i32* %9
br label %19
45: ; preds = %19
%46 = load i32, i32* %8
%47 = add nsw i32 %46, 2
store i32 %47, i32* %8
br label %14
48: ; preds = %14
ret void
}

LLVM IR (-O2):
define void @merge_sort(i32* %0, i32 %1, i32 %2) {
%4 = add nsw i32 %2, 1
%5 = tail call i32 @log2(i32 %4)
%6 = icmp eq i32 %5, 0
br i1 %6, label %27, label %7
7: ; preds = %3, %24
%8 = phi i32 [ %10, %24 ], [ %5, %3 ]
%9 = phi i32 [ %25, %24 ], [ 2, %3 ]
%10 = add nsw i32 %8, -1
%11 = sdiv i32 %4, %9
%12 = icmp sgt i32 %11, 0
br i1 %12, label %13, label %24
13: ; preds = %7
%14 = add nsw i32 %9, -1
%15 = lshr i32 %14, 1
br label %16
16: ; preds = %13, %16
%17 = phi i32 [ 0, %13 ], [ %22, %16 ]
%18 = mul nuw nsw i32 %17, %9
%19 = add nuw nsw i32 %18, %15
%20 = add i32 %14, %18
%21 = tail call i32 @merge(i32* %0, i32 %18, i32 %19, i32 %20)
%22 = add nuw nsw i32 %17, 1
%23 = icmp eq i32 %22, %11
br i1 %23, label %24, label %16
24: ; preds = %16, %7
%25 = add nuw nsw i32 %9, 2
%26 = icmp eq i32 %10, 0
br i1 %26, label %27, label %7
27: ; preds = %24, %3
ret void
}