#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*/ int64_t;

/* Variables and functions */
int /*<<< orphan*/ sorter_grail_swap_n(int /*<<< orphan*/ *,
                                       int /*<<< orphan*/ *, int);
void sorter_grail_rotate(int64_t *a, int l1, int l2) {
  while (l1 && l2) {
    if (l1 <= l2) {
      sorter_grail_swap_n(a, a + l1, l1);
      a += l1;
      l2 -= l1;
    } else {
      sorter_grail_swap_n(a + (l1 - l2), a + l1, l2);
      l1 -= l2;
    }
  }
}


x86 assembly (clang, -O0):
<sorter_grail_rotate>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0xc(%rbp)
mov    %al,-0x11(%rbp)
je     113b <sorter_grail_rotate+0x2b>
cmpl   $0x0,-0x10(%rbp)
setne  %al
mov    %al,-0x11(%rbp)
mov    -0x11(%rbp),%al
test   $0x1,%al
jne    114b <sorter_grail_rotate+0x3b>
jmp    11d1 <sorter_grail_rotate+0xc1>
mov    -0xc(%rbp),%eax
cmp    -0x10(%rbp),%eax
jg     1197 <sorter_grail_rotate+0x87>
mov    -0x8(%rbp),%rdi
mov    -0x8(%rbp),%rsi
movslq -0xc(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rsi
mov    -0xc(%rbp),%edx
call   1030 <sorter_grail_swap_n@plt>
mov    -0xc(%rbp),%ecx
mov    -0x8(%rbp),%rax
movslq %ecx,%rcx
shl    $0x2,%rcx
add    %rcx,%rax
mov    %rax,-0x8(%rbp)
mov    -0xc(%rbp),%ecx
mov    -0x10(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x10(%rbp)
jmp    11cc <sorter_grail_rotate+0xbc>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%eax
sub    -0x10(%rbp),%eax
cltq
shl    $0x2,%rax
add    %rax,%rdi
mov    -0x8(%rbp),%rsi
movslq -0xc(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rsi
mov    -0x10(%rbp),%edx
call   1030 <sorter_grail_swap_n@plt>
mov    -0x10(%rbp),%ecx
mov    -0xc(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0xc(%rbp)
jmp    1122 <sorter_grail_rotate+0x12>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sorter_grail_rotate>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %esi,%esi
je     1181 <sorter_grail_rotate+0x71>
mov    %edx,%r15d
test   %edx,%edx
je     1181 <sorter_grail_rotate+0x71>
mov    %esi,%r13d
mov    %rdi,%r14
nopl   0x0(%rax)
mov    %r13d,%ebp
movslq %r13d,%rbx
sub    %r15d,%ebp
jle    1160 <sorter_grail_rotate+0x50>
movslq %ebp,%rax
lea    (%r14,%rax,4),%rdi
lea    (%r14,%rbx,4),%rsi
mov    %r15d,%edx
call   1030 <sorter_grail_swap_n@plt>
mov    %ebp,%r13d
test   %r13d,%r13d
jne    117c <sorter_grail_rotate+0x6c>
jmp    1181 <sorter_grail_rotate+0x71>
nopl   0x0(%rax,%rax,1)
lea    (%r14,%rbx,4),%r12
mov    %r14,%rdi
mov    %r12,%rsi
mov    %ebx,%edx
call   1030 <sorter_grail_swap_n@plt>
sub    %ebx,%r15d
mov    %r12,%r14
test   %r13d,%r13d
je     1181 <sorter_grail_rotate+0x71>
test   %r15d,%r15d
jne    1130 <sorter_grail_rotate+0x20>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @sorter_grail_rotate(i32* %0, i32 %1, i32 %2) {
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i32
store i32* %0, i32** %4
store i32 %1, i32* %5
store i32 %2, i32* %6
br label %7
7: ; preds = %50, %3
%8 = load i32, i32* %5
%9 = icmp ne i32 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %7
%11 = load i32, i32* %6
%12 = icmp ne i32 %11, 0
br label %13
13: ; preds = %10, %7
%14 = phi i1 [ false, %7 ], [ %12, %10 ]
br i1 %14, label %15, label %51
15: ; preds = %13
%16 = load i32, i32* %5
%17 = load i32, i32* %6
%18 = icmp sle i32 %16, %17
br i1 %18, label %19, label %34
19: ; preds = %15
%20 = load i32*, i32** %4
%21 = load i32*, i32** %4
%22 = load i32, i32* %5
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds i32, i32* %21, i64 %23
%25 = load i32, i32* %5
%26 = call i32 @sorter_grail_swap_n(i32* %20, i32* %24, i32 %25)
%27 = load i32, i32* %5
%28 = load i32*, i32** %4
%29 = sext i32 %27 to i64
%30 = getelementptr inbounds i32, i32* %28, i64 %29
store i32* %30, i32** %4
%31 = load i32, i32* %5
%32 = load i32, i32* %6
%33 = sub nsw i32 %32, %31
store i32 %33, i32* %6
br label %50
34: ; preds = %15
%35 = load i32*, i32** %4
%36 = load i32, i32* %5
%37 = load i32, i32* %6
%38 = sub nsw i32 %36, %37
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds i32, i32* %35, i64 %39
%41 = load i32*, i32** %4
%42 = load i32, i32* %5
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds i32, i32* %41, i64 %43
%45 = load i32, i32* %6
%46 = call i32 @sorter_grail_swap_n(i32* %40, i32* %44, i32 %45)
%47 = load i32, i32* %6
%48 = load i32, i32* %5
%49 = sub nsw i32 %48, %47
store i32 %49, i32* %5
br label %50
50: ; preds = %34, %19
br label %7
51: ; preds = %13
ret void
}

LLVM IR (-O2):
define void @sorter_grail_rotate(i32* %0, i32 %1, i32 %2) {
%4 = icmp ne i32 %1, 0
%5 = icmp ne i32 %2, 0
%6 = and i1 %4, %5
br i1 %6, label %7, label %31
7: ; preds = %3, %24
%8 = phi i32 [ %27, %24 ], [ %2, %3 ]
%9 = phi i32 [ %26, %24 ], [ %1, %3 ]
%10 = phi i32* [ %25, %24 ], [ %0, %3 ]
%11 = icmp sgt i32 %9, %8
br i1 %11, label %17, label %12
12: ; preds = %7
%13 = sext i32 %9 to i64
%14 = getelementptr inbounds i32, i32* %10, i64 %13
%15 = tail call i32 @sorter_grail_swap_n(i32* %10, i32* %14, i32 %9)
%16 = sub nsw i32 %8, %9
br label %24
17: ; preds = %7
%18 = sub nsw i32 %9, %8
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds i32, i32* %10, i64 %19
%21 = sext i32 %9 to i64
%22 = getelementptr inbounds i32, i32* %10, i64 %21
%23 = tail call i32 @sorter_grail_swap_n(i32* %20, i32* %22, i32 %8)
br label %24
24: ; preds = %17, %12
%25 = phi i32* [ %14, %12 ], [ %10, %17 ]
%26 = phi i32 [ %9, %12 ], [ %18, %17 ]
%27 = phi i32 [ %16, %12 ], [ %8, %17 ]
%28 = icmp ne i32 %26, 0
%29 = icmp ne i32 %27, 0
%30 = select i1 %28, i1 %29, i1 false
br i1 %30, label %7, label %31
31: ; preds = %24, %3
ret void
}