#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 */
struct stack *createStack(int);
int /*<<< orphan*/ isEmpty(struct stack *);
size_t peek(struct stack *);
int /*<<< orphan*/ pop(struct stack *);
int /*<<< orphan*/ push(struct stack *, int);
void calculateSpan(int price[], int n, int span[]) {
  struct stack *stack = createStack(n);
  for (int i = 0; i < n; i++) {
    int cnt = 1;
    while (!isEmpty(stack) && price[peek(stack)] < price[i]) {
      cnt += span[peek(stack)];
      pop(stack);
    }
    push(stack, i);
    span[i] = cnt;
  }
}


x86 assembly (clang, -O0):
<calculateSpan>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0xc(%rbp),%edi
call   1050 <createStack@plt>
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    1235 <calculateSpan+0xe5>
movl   $0x1,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
call   1060 <isEmpty@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x29(%rbp)
jne    11ce <calculateSpan+0x7e>
mov    -0x8(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <peek@plt>
mov    %rax,%rcx
mov    -0x38(%rbp),%rax
mov    (%rax,%rcx,4),%eax
mov    -0x8(%rbp),%rcx
movslq -0x24(%rbp),%rdx
cmp    (%rcx,%rdx,4),%eax
setl   %al
mov    %al,-0x29(%rbp)
mov    -0x29(%rbp),%al
test   $0x1,%al
jne    11de <calculateSpan+0x8e>
jmp    120d <calculateSpan+0xbd>
mov    -0x18(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <peek@plt>
mov    %rax,%rcx
mov    -0x40(%rbp),%rax
mov    (%rax,%rcx,4),%eax
add    -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <pop@plt>
jmp    1189 <calculateSpan+0x39>
mov    -0x20(%rbp),%rdi
mov    -0x24(%rbp),%esi
call   1070 <push@plt>
mov    -0x28(%rbp),%edx
mov    -0x18(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1176 <calculateSpan+0x26>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<calculateSpan>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %esi,%ebp
mov    %rdi,%r15
mov    %esi,%edi
call   1050 <createStack@plt>
test   %ebp,%ebp
jle    11e4 <calculateSpan+0x94>
mov    %rax,%rbx
mov    %ebp,%r13d
xor    %r12d,%r12d
jmp    1198 <calculateSpan+0x48>
nopl   0x0(%rax)
mov    %rbx,%rdi
mov    %r12d,%esi
call   1070 <push@plt>
mov    %ebp,(%r14,%r12,4)
add    $0x1,%r12
cmp    %r13,%r12
je     11e4 <calculateSpan+0x94>
mov    %rbx,%rdi
call   1060 <isEmpty@plt>
mov    $0x1,%ebp
test   %eax,%eax
jne    1180 <calculateSpan+0x30>
mov    $0x1,%ebp
xchg   %ax,%ax
mov    %rbx,%rdi
call   1030 <peek@plt>
mov    (%r15,%rax,4),%eax
cmp    (%r15,%r12,4),%eax
jge    1180 <calculateSpan+0x30>
mov    %rbx,%rdi
call   1030 <peek@plt>
add    (%r14,%rax,4),%ebp
mov    %rbx,%rdi
call   1040 <pop@plt>
mov    %rbx,%rdi
call   1060 <isEmpty@plt>
test   %eax,%eax
je     11b0 <calculateSpan+0x60>
jmp    1180 <calculateSpan+0x30>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.stack = type { i32 }
define void @calculateSpan(i32* nocapture %0, i32 %1, i32* nocapture %2) {
%4 = tail call %struct.stack* @createStack(i32 %1)
%5 = icmp sgt i32 %1, 0
br i1 %5, label %6, label %14
6: ; preds = %3
%7 = zext i32 %1 to i64
br label %8
8: ; preds = %6, %30
%9 = phi i64 [ 0, %6 ], [ %35, %30 ]
%10 = tail call i32 @isEmpty(%struct.stack* %4)
%11 = icmp eq i32 %10, 0
br i1 %11, label %12, label %30
12: ; preds = %8
%13 = getelementptr inbounds i32, i32* %0, i64 %9
br label %15
14: ; preds = %30, %3
ret void
15: ; preds = %12, %22
%16 = phi i32 [ 1, %12 ], [ %26, %22 ]
%17 = tail call i64 @peek(%struct.stack* %4)
%18 = getelementptr inbounds i32, i32* %0, i64 %17
%19 = load i32, i32* %18
%20 = load i32, i32* %13
%21 = icmp slt i32 %19, %20
br i1 %21, label %22, label %30
22: ; preds = %15
%23 = tail call i64 @peek(%struct.stack* %4)
%24 = getelementptr inbounds i32, i32* %2, i64 %23
%25 = load i32, i32* %24
%26 = add nsw i32 %25, %16
%27 = tail call i32 @pop(%struct.stack* %4)
%28 = tail call i32 @isEmpty(%struct.stack* %4)
%29 = icmp eq i32 %28, 0
br i1 %29, label %15, label %30
30: ; preds = %15, %22, %8
%31 = phi i32 [ 1, %8 ], [ %26, %22 ], [ %16, %15 ]
%32 = trunc i64 %9 to i32
%33 = tail call i32 @push(%struct.stack* %4, i32 %32)
%34 = getelementptr inbounds i32, i32* %2, i64 %9
store i32 %31, i32* %34
%35 = add nuw nsw i64 %9, 1
%36 = icmp eq i64 %35, %7
br i1 %36, label %14, label %8
}