#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 */
struct stack {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ is_empty(struct stack *);
int peek(struct stack *);
int pop(struct stack *);
int /*<<< orphan*/ push(struct stack *, int);
void sort_stack(struct stack *stack, struct stack *aux) {
  while (!is_empty(stack)) {
    push(aux, pop(stack));
  }
  while (!is_empty(aux)) {
    int popped = 0;
    int to_insert = pop(aux);
    while (!is_empty(stack) && peek(stack) > to_insert) {
      push(aux, pop(stack));
      popped++;
    }
    push(stack, to_insert);
    while (popped != 0) {
      push(stack, pop(aux));
      popped--;
    }
  }
}


x86 assembly (clang, -O0):
<sort_stack>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <is_empty@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    116e <sort_stack+0x2e>
jmp    118f <sort_stack+0x4f>
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <pop@plt>
mov    -0x20(%rbp),%rdi
mov    %eax,%esi
call   1060 <push@plt>
jmp    1150 <sort_stack+0x10>
jmp    1194 <sort_stack+0x54>
mov    -0x10(%rbp),%rdi
call   1040 <is_empty@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    11b2 <sort_stack+0x72>
jmp    126f <sort_stack+0x12f>
movl   $0x0,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <pop@plt>
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <is_empty@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x21(%rbp)
jne    11f0 <sort_stack+0xb0>
mov    -0x8(%rbp),%rdi
call   1030 <peek@plt>
cmp    -0x18(%rbp),%eax
setg   %al
mov    %al,-0x21(%rbp)
mov    -0x21(%rbp),%al
test   $0x1,%al
jne    1200 <sort_stack+0xc0>
jmp    122a <sort_stack+0xea>
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <pop@plt>
mov    -0x30(%rbp),%rdi
mov    %eax,%esi
call   1060 <push@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    11c5 <sort_stack+0x85>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%esi
call   1060 <push@plt>
cmpl   $0x0,-0x14(%rbp)
je     126a <sort_stack+0x12a>
mov    -0x8(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <pop@plt>
mov    -0x38(%rbp),%rdi
mov    %eax,%esi
call   1060 <push@plt>
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
jmp    1236 <sort_stack+0xf6>
jmp    1194 <sort_stack+0x54>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sort_stack>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
nopl   (%rax)
call   1040 <is_empty@plt>
test   %eax,%eax
jne    117b <sort_stack+0x3b>
mov    %rbx,%rdi
call   1050 <pop@plt>
mov    %r14,%rdi
mov    %eax,%esi
call   1060 <push@plt>
mov    %rbx,%rdi
jmp    1150 <sort_stack+0x10>
mov    %rbx,%rdi
mov    %r15d,%esi
call   1060 <push@plt>
mov    %r14,%rdi
call   1040 <is_empty@plt>
test   %eax,%eax
jne    121c <sort_stack+0xdc>
mov    %r14,%rdi
call   1050 <pop@plt>
mov    %eax,%r15d
mov    %rbx,%rdi
call   1040 <is_empty@plt>
test   %eax,%eax
jne    1170 <sort_stack+0x30>
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %rbx,%rdi
call   1030 <peek@plt>
mov    %rbx,%rdi
cmp    %r15d,%eax
jle    11eb <sort_stack+0xab>
call   1050 <pop@plt>
mov    %r14,%rdi
mov    %eax,%esi
call   1060 <push@plt>
add    $0x1,%ebp
mov    %rbx,%rdi
call   1040 <is_empty@plt>
test   %eax,%eax
je     11b0 <sort_stack+0x70>
mov    %rbx,%rdi
mov    %r15d,%esi
call   1060 <push@plt>
jmp    1200 <sort_stack+0xc0>
mov    %r15d,%esi
call   1060 <push@plt>
test   %ebp,%ebp
je     117b <sort_stack+0x3b>
nopw   0x0(%rax,%rax,1)
mov    %r14,%rdi
call   1050 <pop@plt>
mov    %rbx,%rdi
mov    %eax,%esi
call   1060 <push@plt>
add    $0xffffffff,%ebp
jne    1200 <sort_stack+0xc0>
jmp    117b <sort_stack+0x3b>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stack = type { i32 }
define void @sort_stack(%struct.stack* %0, %struct.stack* %1) {
%3 = alloca %struct.stack*
%4 = alloca %struct.stack*
%5 = alloca i32
%6 = alloca i32
store %struct.stack* %0, %struct.stack** %3
store %struct.stack* %1, %struct.stack** %4
br label %7
7: ; preds = %12, %2
%8 = load %struct.stack*, %struct.stack** %3
%9 = call i32 @is_empty(%struct.stack* %8)
%10 = icmp ne i32 %9, 0
%11 = xor i1 %10, true
br i1 %11, label %12, label %17
12: ; preds = %7
%13 = load %struct.stack*, %struct.stack** %4
%14 = load %struct.stack*, %struct.stack** %3
%15 = call i32 @pop(%struct.stack* %14)
%16 = call i32 @push(%struct.stack* %13, i32 %15)
br label %7
17: ; preds = %7
br label %18
18: ; preds = %58, %17
%19 = load %struct.stack*, %struct.stack** %4
%20 = call i32 @is_empty(%struct.stack* %19)
%21 = icmp ne i32 %20, 0
%22 = xor i1 %21, true
br i1 %22, label %23, label %59
23: ; preds = %18
store i32 0, i32* %5
%24 = load %struct.stack*, %struct.stack** %4
%25 = call i32 @pop(%struct.stack* %24)
store i32 %25, i32* %6
br label %26
26: ; preds = %37, %23
%27 = load %struct.stack*, %struct.stack** %3
%28 = call i32 @is_empty(%struct.stack* %27)
%29 = icmp ne i32 %28, 0
br i1 %29, label %35, label %30
30: ; preds = %26
%31 = load %struct.stack*, %struct.stack** %3
%32 = call i32 @peek(%struct.stack* %31)
%33 = load i32, i32* %6
%34 = icmp sgt i32 %32, %33
br label %35
35: ; preds = %30, %26
%36 = phi i1 [ false, %26 ], [ %34, %30 ]
br i1 %36, label %37, label %44
37: ; preds = %35
%38 = load %struct.stack*, %struct.stack** %4
%39 = load %struct.stack*, %struct.stack** %3
%40 = call i32 @pop(%struct.stack* %39)
%41 = call i32 @push(%struct.stack* %38, i32 %40)
%42 = load i32, i32* %5
%43 = add nsw i32 %42, 1
store i32 %43, i32* %5
br label %26
44: ; preds = %35
%45 = load %struct.stack*, %struct.stack** %3
%46 = load i32, i32* %6
%47 = call i32 @push(%struct.stack* %45, i32 %46)
br label %48
48: ; preds = %51, %44
%49 = load i32, i32* %5
%50 = icmp ne i32 %49, 0
br i1 %50, label %51, label %58
51: ; preds = %48
%52 = load %struct.stack*, %struct.stack** %3
%53 = load %struct.stack*, %struct.stack** %4
%54 = call i32 @pop(%struct.stack* %53)
%55 = call i32 @push(%struct.stack* %52, i32 %54)
%56 = load i32, i32* %5
%57 = add nsw i32 %56, -1
store i32 %57, i32* %5
br label %48
58: ; preds = %48
br label %18
59: ; preds = %18
ret void
}

LLVM IR (-O2):
%struct.stack = type { i32 }
define void @sort_stack(%struct.stack* %0, %struct.stack* %1) {
%3 = tail call i32 @is_empty(%struct.stack* %0)
%4 = icmp eq i32 %3, 0
br i1 %4, label %8, label %5
5: ; preds = %8, %2
%6 = tail call i32 @is_empty(%struct.stack* %1)
%7 = icmp eq i32 %6, 0
br i1 %7, label %16, label %45
8: ; preds = %2, %8
%9 = tail call i32 @pop(%struct.stack* %0)
%10 = tail call i32 @push(%struct.stack* %1, i32 %9)
%11 = tail call i32 @is_empty(%struct.stack* %0)
%12 = icmp eq i32 %11, 0
br i1 %12, label %8, label %5
13: ; preds = %39, %20, %34
%14 = tail call i32 @is_empty(%struct.stack* %1)
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %45
16: ; preds = %5, %13
%17 = tail call i32 @pop(%struct.stack* %1)
%18 = tail call i32 @is_empty(%struct.stack* %0)
%19 = icmp eq i32 %18, 0
br i1 %19, label %22, label %20
20: ; preds = %16
%21 = tail call i32 @push(%struct.stack* %0, i32 %17)
br label %13
22: ; preds = %16, %26
%23 = phi i32 [ %29, %26 ], [ 0, %16 ]
%24 = tail call i32 @peek(%struct.stack* %0)
%25 = icmp sgt i32 %24, %17
br i1 %25, label %26, label %34
26: ; preds = %22
%27 = tail call i32 @pop(%struct.stack* %0)
%28 = tail call i32 @push(%struct.stack* %1, i32 %27)
%29 = add nuw nsw i32 %23, 1
%30 = tail call i32 @is_empty(%struct.stack* %0)
%31 = icmp eq i32 %30, 0
br i1 %31, label %22, label %32
32: ; preds = %26
%33 = tail call i32 @push(%struct.stack* %0, i32 %17)
br label %37
34: ; preds = %22
%35 = tail call i32 @push(%struct.stack* %0, i32 %17)
%36 = icmp eq i32 %23, 0
br i1 %36, label %13, label %37
37: ; preds = %32, %34
%38 = phi i32 [ %23, %34 ], [ %29, %32 ]
br label %39
39: ; preds = %37, %39
%40 = phi i32 [ %43, %39 ], [ %38, %37 ]
%41 = tail call i32 @pop(%struct.stack* %1)
%42 = tail call i32 @push(%struct.stack* %0, i32 %41)
%43 = add nsw i32 %40, -1
%44 = icmp eq i32 %43, 0
br i1 %44, label %13, label %39
45: ; preds = %13, %5
ret void
}