#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);
scalar_t__ isEmpty(struct stack *);
int peek(struct stack *);
int pop(struct stack *);
int /*<<< orphan*/ printf(char *, int, ...);
int /*<<< orphan*/ push(struct stack *, int);
void printNextGreaterElement(int a[], int n) {
  struct stack *stack = createStack(n);
  for (int i = 0; i < n; i++) {
    if (isEmpty(stack) || peek(stack) > a[i]) {
      push(stack, a[i]);
    } else {
      printf("%d nge is %d\n", pop(stack), a[i]);
      push(stack, a[i]);
    }
  }
  while (!isEmpty(stack)) {
    printf("%d nge is -1\n", pop(stack));
  }
}


x86 assembly (clang, -O0):
<printNextGreaterElement>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0xc(%rbp),%edi
call   1060 <createStack@plt>
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    121f <printNextGreaterElement+0xbf>
mov    -0x18(%rbp),%rdi
call   1070 <isEmpty@plt>
cmp    $0x0,%rax
jne    11bb <printNextGreaterElement+0x5b>
mov    -0x18(%rbp),%rdi
call   1030 <peek@plt>
mov    -0x8(%rbp),%rcx
movslq -0x1c(%rbp),%rdx
cmp    (%rcx,%rdx,4),%eax
jle    11d4 <printNextGreaterElement+0x74>
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,4),%esi
call   1080 <push@plt>
jmp    120c <printNextGreaterElement+0xac>
mov    -0x18(%rbp),%rdi
call   1050 <pop@plt>
mov    %eax,%esi
mov    -0x8(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,4),%edx
lea    0xe0f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,4),%esi
call   1080 <push@plt>
jmp    1211 <printNextGreaterElement+0xb1>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1182 <printNextGreaterElement+0x22>
jmp    1224 <printNextGreaterElement+0xc4>
mov    -0x18(%rbp),%rdi
call   1070 <isEmpty@plt>
cmp    $0x0,%rax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    1243 <printNextGreaterElement+0xe3>
jmp    1261 <printNextGreaterElement+0x101>
mov    -0x18(%rbp),%rdi
call   1050 <pop@plt>
mov    %eax,%esi
lea    0xdb9(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1224 <printNextGreaterElement+0xc4>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<printNextGreaterElement>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %rdi,%r14
mov    %esi,%edi
call   1060 <createStack@plt>
mov    %rax,%r12
test   %ebp,%ebp
jle    11e7 <printNextGreaterElement+0x87>
mov    %ebp,%r13d
mov    %r14,%rbx
xor    %ebp,%ebp
jmp    11a9 <printNextGreaterElement+0x49>
nopl   0x0(%rax,%rax,1)
mov    (%r15),%esi
mov    %r12,%rdi
call   1080 <push@plt>
add    $0x1,%rbp
add    $0x4,%rbx
add    $0xffffffffffffffff,%r13
je     11e7 <printNextGreaterElement+0x87>
mov    %r12,%rdi
call   1070 <isEmpty@plt>
mov    %rbx,%r15
test   %rax,%rax
jne    1190 <printNextGreaterElement+0x30>
mov    %r12,%rdi
call   1030 <peek@plt>
mov    (%rbx),%esi
cmp    %esi,%eax
jg     1193 <printNextGreaterElement+0x33>
lea    (%r14,%rbp,4),%r15
mov    %r12,%rdi
call   1050 <pop@plt>
mov    (%rbx),%edx
lea    0xe24(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    1190 <printNextGreaterElement+0x30>
mov    %r12,%rdi
call   1070 <isEmpty@plt>
test   %rax,%rax
jne    1221 <printNextGreaterElement+0xc1>
lea    0xe13(%rip),%r14
nopl   0x0(%rax,%rax,1)
mov    %r12,%rdi
call   1050 <pop@plt>
mov    %r14,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %r12,%rdi
call   1070 <isEmpty@plt>
test   %rax,%rax
je     1200 <printNextGreaterElement+0xa0>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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