#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 /*<<< orphan*/ swap(int *, int, int);
void cocktailShakerSort(int *array, int length) {
  int swapped, i;
  do {
    swapped = 0;
    for (i = 0; i < length - 2; i++) {
      if (array[i] > array[i + 1]) {
        swap(array, i, i + 1);
        swapped = 1;
      }
    }
    if (swapped = 0)
      break;
    swapped = 0;
    for (i = length - 2; i >= 0; i--) {
      if (array[i] > array[i + 1]) {
        swap(array, i, i + 1);
        swapped = 1;
      }
    }
  } while (swapped);
}


x86 assembly (clang, -O0):
<cocktailShakerSort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    -0xc(%rbp),%ecx
sub    $0x2,%ecx
cmp    %ecx,%eax
jge    118b <cocktailShakerSort+0x7b>
mov    -0x8(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
mov    -0x8(%rbp),%rcx
mov    -0x14(%rbp),%edx
add    $0x1,%edx
movslq %edx,%rdx
cmp    (%rcx,%rdx,4),%eax
jle    1178 <cocktailShakerSort+0x68>
mov    -0x8(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x14(%rbp),%edx
add    $0x1,%edx
call   1030 <swap@plt>
movl   $0x1,-0x10(%rbp)
jmp    117d <cocktailShakerSort+0x6d>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    112d <cocktailShakerSort+0x1d>
movl   $0x0,-0x10(%rbp)
xor    %eax,%eax
test   $0x1,%al
jne    11a1 <cocktailShakerSort+0x91>
jmp    11a6 <cocktailShakerSort+0x96>
jmp    121c <cocktailShakerSort+0x10c>
movl   $0x0,-0x10(%rbp)
mov    -0xc(%rbp),%eax
sub    $0x2,%eax
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jl     120d <cocktailShakerSort+0xfd>
mov    -0x8(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
mov    -0x8(%rbp),%rcx
mov    -0x14(%rbp),%edx
add    $0x1,%edx
movslq %edx,%rdx
cmp    (%rcx,%rdx,4),%eax
jle    11fa <cocktailShakerSort+0xea>
mov    -0x8(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x14(%rbp),%edx
add    $0x1,%edx
call   1030 <swap@plt>
movl   $0x1,-0x10(%rbp)
jmp    11ff <cocktailShakerSort+0xef>
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
jmp    11b6 <cocktailShakerSort+0xa6>
jmp    1212 <cocktailShakerSort+0x102>
cmpl   $0x0,-0x10(%rbp)
jne    111f <cocktailShakerSort+0xf>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cocktailShakerSort>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %esi,%r14d
mov    %rdi,%r15
lea    -0x2(%r14),%r12d
lea    0x1(%r12),%r13
jmp    1134 <cocktailShakerSort+0x24>
nopl   0x0(%rax)
test   %eax,%eax
je     11a8 <cocktailShakerSort+0x98>
cmp    $0x3,%r14d
jl     1170 <cocktailShakerSort+0x60>
xor    %esi,%esi
jmp    1148 <cocktailShakerSort+0x38>
xchg   %ax,%ax
mov    %rbx,%rsi
cmp    %rbx,%r12
je     1170 <cocktailShakerSort+0x60>
mov    (%r15,%rsi,4),%eax
lea    0x1(%rsi),%rbx
cmp    0x4(%r15,%rsi,4),%eax
jle    1140 <cocktailShakerSort+0x30>
lea    0x1(%rsi),%edx
mov    %r15,%rdi
call   1030 <swap@plt>
jmp    1140 <cocktailShakerSort+0x30>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
cmp    $0x2,%r14d
jl     11a8 <cocktailShakerSort+0x98>
xor    %eax,%eax
mov    %r13,%rbp
mov    %r12d,%ebx
jmp    1189 <cocktailShakerSort+0x79>
add    $0xffffffff,%ebx
add    $0xffffffffffffffff,%rbp
jle    1130 <cocktailShakerSort+0x20>
mov    -0x4(%r15,%rbp,4),%ecx
cmp    (%r15,%rbp,4),%ecx
jle    1180 <cocktailShakerSort+0x70>
lea    0x1(%rbx),%edx
mov    %r15,%rdi
mov    %ebx,%esi
call   1030 <swap@plt>
mov    $0x1,%eax
jmp    1180 <cocktailShakerSort+0x70>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define void @cocktailShakerSort(i32* %0, i32 %1) {
%3 = add i32 %1, -2
%4 = icmp sgt i32 %1, 2
%5 = icmp sgt i32 %1, 1
%6 = zext i32 %3 to i64
br label %7
7: ; preds = %40, %2
br i1 %4, label %9, label %8
8: ; preds = %21, %7
br i1 %5, label %23, label %42
9: ; preds = %7, %21
%10 = phi i64 [ %13, %21 ], [ 0, %7 ]
%11 = getelementptr inbounds i32, i32* %0, i64 %10
%12 = load i32, i32* %11
%13 = add nuw nsw i64 %10, 1
%14 = getelementptr inbounds i32, i32* %0, i64 %13
%15 = load i32, i32* %14
%16 = icmp sgt i32 %12, %15
br i1 %16, label %17, label %21
17: ; preds = %9
%18 = trunc i64 %13 to i32
%19 = trunc i64 %10 to i32
%20 = tail call i32 @swap(i32* %0, i32 %19, i32 %18)
br label %21
21: ; preds = %9, %17
%22 = icmp eq i64 %13, %6
br i1 %22, label %8, label %9
23: ; preds = %8, %36
%24 = phi i64 [ %38, %36 ], [ %6, %8 ]
%25 = phi i32 [ %37, %36 ], [ 0, %8 ]
%26 = trunc i64 %24 to i32
%27 = getelementptr inbounds i32, i32* %0, i64 %24
%28 = load i32, i32* %27
%29 = add nuw nsw i64 %24, 1
%30 = getelementptr inbounds i32, i32* %0, i64 %29
%31 = load i32, i32* %30
%32 = icmp sgt i32 %28, %31
br i1 %32, label %33, label %36
33: ; preds = %23
%34 = add nuw nsw i32 %26, 1
%35 = tail call i32 @swap(i32* %0, i32 %26, i32 %34)
br label %36
36: ; preds = %23, %33
%37 = phi i32 [ 1, %33 ], [ %25, %23 ]
%38 = add nsw i64 %24, -1
%39 = icmp sgt i64 %24, 0
br i1 %39, label %23, label %40
40: ; preds = %36
%41 = icmp eq i32 %37, 0
br i1 %41, label %42, label %7
42: ; preds = %8, %40
ret void
}