#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*/ print_array(int *, int, int);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ swap(int *, int *);
int *zsort(int array[], int len) {
  int steps = (len - 1) * ((len - 1) + 1) / 2;
  int switches = 0;
  int total_steps = 0;
  int direction = 1;
  int step = 1;
  int index = 0;
  while (step <= steps) {
    int *num1 = &array[index];
    int *num2 = &array[index + 1];
    print_array(array, len, index);
    if (*num1 > *num2) {
      swap(num1, num2);
    }
    if (step - total_steps == (len - 1 - switches)) {
      direction *= (-1);
      switches++;
      total_steps = step;
    } else {
    }
    index += direction;
    step++;
  }
  printf(" Steps: %d\n", steps);
  return array;
}


x86 assembly (clang, -O0):
<zsort>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0xc(%rbp),%eax
sub    $0x1,%eax
mov    -0xc(%rbp),%ecx
sub    $0x1,%ecx
add    $0x1,%ecx
imul   %ecx,%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %eax,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
movl   $0x1,-0x1c(%rbp)
movl   $0x1,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x10(%rbp),%eax
jg     1232 <zsort+0x102>
mov    -0x8(%rbp),%rax
movslq -0x24(%rbp),%rcx
shl    $0x2,%rcx
add    %rcx,%rax
mov    %rax,-0x30(%rbp)
mov    -0x8(%rbp),%rax
mov    -0x24(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
shl    $0x2,%rcx
add    %rcx,%rax
mov    %rax,-0x38(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x24(%rbp),%edx
call   1030 <print_array@plt>
mov    -0x30(%rbp),%rax
mov    (%rax),%eax
mov    -0x38(%rbp),%rcx
cmp    (%rcx),%eax
jle    11e4 <zsort+0xb4>
mov    -0x30(%rbp),%rdi
mov    -0x38(%rbp),%rsi
call   1050 <swap@plt>
mov    -0x20(%rbp),%eax
sub    -0x18(%rbp),%eax
mov    -0xc(%rbp),%ecx
sub    $0x1,%ecx
sub    -0x14(%rbp),%ecx
cmp    %ecx,%eax
jne    1216 <zsort+0xe6>
imul   $0xffffffff,-0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    -0x20(%rbp),%eax
mov    %eax,-0x18(%rbp)
jmp    121b <zsort+0xeb>
jmp    121b <zsort+0xeb>
mov    -0x1c(%rbp),%eax
add    -0x24(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    117f <zsort+0x4f>
mov    -0x10(%rbp),%esi
lea    0xdc4(%rip),%rdi
call   1040 <printf@plt>
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<zsort>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rdi,0x8(%rsp)
lea    -0x1(%rsi),%eax
mov    %eax,0x10(%rsp)
mov    %rsi,0x20(%rsp)
imul   %esi,%eax
mov    %eax,%ecx
shr    $0x1f,%ecx
add    %eax,%ecx
sar    %ecx
mov    %ecx,0x4(%rsp)
cmp    $0x2,%eax
jge    1188 <zsort+0x58>
lea    0xe95(%rip),%rdi
mov    0x4(%rsp),%esi
call   1040 <printf@plt>
mov    0x8(%rsp),%rax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x4(%rsp),%eax
neg    %eax
mov    %rax,0x18(%rsp)
mov    $0x1,%r14d
xor    %edi,%edi
xor    %r12d,%r12d
xor    %r13d,%r13d
mov    $0x1,%ebx
mov    0x8(%rsp),%r15
jmp    11f5 <zsort+0xc5>
nopl   (%rax)
mov    %r14d,%eax
sub    %r12d,%eax
mov    0x10(%rsp),%ecx
mov    0x14(%rsp),%edi
sub    %edi,%ecx
mov    %ebx,%edx
neg    %edx
xor    %esi,%esi
cmp    %ecx,%eax
cmove  %edx,%ebx
sete   %sil
cmove  %r14d,%r12d
add    %esi,%edi
add    %ebx,%r13d
mov    0x18(%rsp),%rax
add    %r14d,%eax
add    $0x1,%eax
mov    %r14d,%ecx
add    $0x1,%ecx
mov    %ecx,%r14d
cmp    $0x1,%eax
je     1164 <zsort+0x34>
mov    %edi,0x14(%rsp)
movslq %r13d,%rbp
mov    %r15,%rdi
mov    0x20(%rsp),%rsi
mov    %ebp,%edx
call   1030 <print_array@plt>
mov    (%r15,%rbp,4),%eax
cmp    0x4(%r15,%rbp,4),%eax
jle    11b0 <zsort+0x80>
mov    0x8(%rsp),%rax
lea    (%rax,%rbp,4),%rdi
lea    (%rax,%rbp,4),%rsi
add    $0x4,%rsi
call   1050 <swap@plt>
jmp    11b0 <zsort+0x80>

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

LLVM IR (-O2):
@.str = [12 x i8] c" Steps: %d\0A\00"
define i32* @zsort(i32* returned %0, i32 %1) {
%3 = add nsw i32 %1, -1
%4 = mul nsw i32 %3, %1
%5 = sdiv i32 %4, 2
%6 = icmp slt i32 %4, 2
br i1 %6, label %36, label %7
7: ; preds = %2, %24
%8 = phi i32 [ %32, %24 ], [ 0, %2 ]
%9 = phi i32 [ %30, %24 ], [ 0, %2 ]
%10 = phi i32 [ %33, %24 ], [ 0, %2 ]
%11 = phi i32 [ %34, %24 ], [ 1, %2 ]
%12 = phi i32 [ %29, %24 ], [ 1, %2 ]
%13 = sext i32 %10 to i64
%14 = getelementptr inbounds i32, i32* %0, i64 %13
%15 = add nsw i32 %10, 1
%16 = sext i32 %15 to i64
%17 = getelementptr inbounds i32, i32* %0, i64 %16
%18 = tail call i32 @print_array(i32* %0, i32 %1, i32 %10)
%19 = load i32, i32* %14
%20 = load i32, i32* %17
%21 = icmp sgt i32 %19, %20
br i1 %21, label %22, label %24
22: ; preds = %7
%23 = tail call i32 @swap(i32* %14, i32* %17)
br label %24
24: ; preds = %22, %7
%25 = sub nsw i32 %11, %9
%26 = sub nsw i32 %3, %8
%27 = icmp eq i32 %25, %26
%28 = sub nsw i32 0, %12
%29 = select i1 %27, i32 %28, i32 %12
%30 = select i1 %27, i32 %11, i32 %9
%31 = zext i1 %27 to i32
%32 = add nuw nsw i32 %8, %31
%33 = add nsw i32 %29, %10
%34 = add nuw nsw i32 %11, 1
%35 = icmp eq i32 %11, %5
br i1 %35, label %36, label %7
36: ; preds = %24, %2
%37 = tail call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %5)
ret i32* %0
}