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

/* Variables and functions */
int bitonic_stage(unsigned int, unsigned int, unsigned int, int,
                  int (*)(void *, unsigned int, unsigned int, unsigned int),
                  int (*)(void *, unsigned int, unsigned int, unsigned int),
                  void *);
int ulog2(unsigned int);
int bitonic(unsigned inputs, unsigned period,
            int (*swap)(void *user, unsigned x, unsigned y0, unsigned y1),
            int (*delay)(void *user, unsigned x, unsigned y, unsigned n),
            void *user) {
  unsigned x = 0;
  unsigned i, j;
  for (i = 0; i < ulog2(inputs * period) - 1; ++i) {
    j = i + 1;
    while (j--) {
      int status = bitonic_stage(inputs, period, x, 1 << j, swap, delay, user);
      if (status < 0)
        return -1;
      ++x;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<bitonic>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
movl   $0x0,-0x30(%rbp)
mov    -0x30(%rbp),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x8(%rbp),%edi
imul   -0xc(%rbp),%edi
call   1040 <ulog2@plt>
mov    %eax,%ecx
mov    -0x3c(%rbp),%eax
sub    $0x1,%ecx
cmp    %ecx,%eax
jae    11eb <bitonic+0xcb>
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%eax
mov    %eax,%ecx
add    $0xffffffff,%ecx
mov    %ecx,-0x34(%rbp)
cmp    $0x0,%eax
je     11d8 <bitonic+0xb8>
mov    -0x8(%rbp),%edi
mov    -0xc(%rbp),%esi
mov    -0x2c(%rbp),%edx
mov    -0x34(%rbp),%ecx
mov    $0x1,%eax
shl    %cl,%eax
mov    %eax,%ecx
mov    -0x18(%rbp),%r8
mov    -0x20(%rbp),%r9
mov    -0x28(%rbp),%rax
mov    %rax,(%rsp)
call   1030 <bitonic_stage@plt>
mov    %eax,-0x38(%rbp)
cmpl   $0x0,-0x38(%rbp)
jge    11ca <bitonic+0xaa>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f2 <bitonic+0xd2>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    1173 <bitonic+0x53>
jmp    11dd <bitonic+0xbd>
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
jmp    1148 <bitonic+0x28>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bitonic>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %r8,0x10(%rsp)
mov    %rcx,%r15
mov    %rdx,%r12
mov    %esi,%ebp
mov    %edi,%ebx
mov    %esi,%edi
imul   %ebx,%edi
mov    %edi,0xc(%rsp)
call   1040 <ulog2@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0x1,%ecx
je     11d1 <bitonic+0xb1>
xor    %ecx,%ecx
xor    %r13d,%r13d
mov    %ecx,%r14d
lea    0x1(%r14),%eax
mov    %eax,0x8(%rsp)
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
cmp    $0xffffffff,%r14d
je     11b0 <bitonic+0x90>
mov    $0x1,%eax
mov    %r14d,%ecx
shl    %cl,%eax
mov    0x10(%rsp),%rcx
mov    %rcx,(%rsp)
mov    %ebx,%edi
mov    %ebp,%esi
mov    %r13d,%edx
mov    %eax,%ecx
mov    %r12,%r8
mov    %r15,%r9
call   1030 <bitonic_stage@plt>
add    $0x1,%r13d
add    $0xffffffff,%r14d
test   %eax,%eax
jns    1170 <bitonic+0x50>
jmp    11cc <bitonic+0xac>
nopl   0x0(%rax,%rax,1)
mov    0xc(%rsp),%edi
call   1040 <ulog2@plt>
mov    %eax,%ecx
add    $0xffffffff,%ecx
xor    %eax,%eax
mov    0x8(%rsp),%edx
cmp    %ecx,%edx
mov    %edx,%ecx
jb     1159 <bitonic+0x39>
jmp    11d1 <bitonic+0xb1>
mov    $0xffffffff,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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