#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*/ freopen(char *, char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int scanf(char *, ...);
int /*<<< orphan*/ stdin;
int main(int argc, char *argv[]) {
  int stack[10001], tail, n, i;
  char c;
  freopen("input.txt", "r", stdin);
  while (scanf("%d", &n), n) {
    for (tail = i = 0; i < n; i++) {
      scanf("%c", &c);
      switch (c) {
      case 'P':
        scanf("%d", &stack[tail++]);
        break;
      case 'O':
        if (tail > 0) {
          tail--;
        }
        break;
      case 'A':
        if (tail) {
          printf("%d\n", stack[tail - 1]);
        } else {
          printf("E\n");
        }
        break;
      default:
        i--;
        break;
      }
    }
    printf("\n");
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x9c80,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e98(%rip),%rax
mov    (%rax),%edx
lea    0xea7(%rip),%rdi
lea    0xeaa(%rip),%rsi
call   1040 <freopen@plt>
lea    0xea0(%rip),%rdi
lea    -0x9c68(%rbp),%rsi
mov    $0x0,%al
call   1050 <scanf@plt>
cmpl   $0x0,-0x9c68(%rbp)
je     12da <main+0x1aa>
movl   $0x0,-0x9c6c(%rbp)
movl   $0x0,-0x9c64(%rbp)
mov    -0x9c6c(%rbp),%eax
cmp    -0x9c68(%rbp),%eax
jge    12c7 <main+0x197>
lea    0xe5b(%rip),%rdi
xor    %eax,%eax
lea    -0x9c6d(%rbp),%rsi
call   1050 <scanf@plt>
movsbl -0x9c6d(%rbp),%eax
mov    %eax,-0x9c74(%rbp)
sub    $0x41,%eax
je     125a <main+0x12a>
jmp    11dd <main+0xad>
mov    -0x9c74(%rbp),%eax
sub    $0x4f,%eax
je     1239 <main+0x109>
jmp    11f1 <main+0xc1>
mov    -0x9c74(%rbp),%eax
sub    $0x50,%eax
jne    129f <main+0x16f>
jmp    1205 <main+0xd5>
mov    -0x9c64(%rbp),%eax
mov    %eax,%ecx
add    $0x1,%ecx
mov    %ecx,-0x9c64(%rbp)
cltq
lea    -0x9c60(%rbp),%rsi
shl    $0x2,%rax
add    %rax,%rsi
lea    0xddf(%rip),%rdi
mov    $0x0,%al
call   1050 <scanf@plt>
jmp    12ae <main+0x17e>
cmpl   $0x0,-0x9c64(%rbp)
jle    1255 <main+0x125>
mov    -0x9c64(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x9c64(%rbp)
jmp    12ae <main+0x17e>
cmpl   $0x0,-0x9c64(%rbp)
je     128c <main+0x15c>
mov    -0x9c64(%rbp),%eax
sub    $0x1,%eax
cltq
mov    -0x9c60(%rbp,%rax,4),%esi
lea    0xd92(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    129a <main+0x16a>
lea    0xd83(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    12ae <main+0x17e>
mov    -0x9c6c(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x9c6c(%rbp)
jmp    12b3 <main+0x183>
mov    -0x9c6c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x9c6c(%rbp)
jmp    119b <main+0x6b>
lea    0xd49(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1165 <main+0x35>
xor    %eax,%eax
add    $0x9c80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x9c58,%rsp
mov    0x2e80(%rip),%rax
mov    (%rax),%edx
lea    0xe8f(%rip),%rdi
lea    0xe92(%rip),%rsi
call   1060 <freopen@plt>
lea    0xe88(%rip),%rdi
lea    0xc(%rsp),%rsi
xor    %eax,%eax
call   1070 <scanf@plt>
mov    0xc(%rsp),%eax
test   %eax,%eax
je     1276 <main+0x126>
lea    0xe6c(%rip),%r14
lea    0xb(%rsp),%rbx
lea    0xe63(%rip),%r12
lea    0xe56(%rip),%r13
jmp    11e5 <main+0x95>
nopl   0x0(%rax,%rax,1)
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    %r13,%rdi
lea    0xc(%rsp),%rsi
xor    %eax,%eax
call   1070 <scanf@plt>
mov    0xc(%rsp),%eax
test   %eax,%eax
je     1276 <main+0x126>
test   %eax,%eax
jle    11c0 <main+0x70>
xor    %ebp,%ebp
xor    %r15d,%r15d
jmp    1204 <main+0xb4>
xor    %eax,%eax
test   %r15d,%r15d
setg   %al
sub    %eax,%r15d
add    $0x1,%ebp
cmp    0xc(%rsp),%ebp
jge    11c0 <main+0x70>
mov    %r14,%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1070 <scanf@plt>
movsbl 0xb(%rsp),%eax
cmp    $0x41,%eax
je     1240 <main+0xf0>
cmp    $0x4f,%eax
je     11f0 <main+0xa0>
cmp    $0x50,%eax
jne    1260 <main+0x110>
movslq %r15d,%rax
add    $0x1,%r15d
lea    (%rsp,%rax,4),%rsi
add    $0x10,%rsi
mov    %r13,%rdi
xor    %eax,%eax
call   1070 <scanf@plt>
jmp    11fb <main+0xab>
test   %r15d,%r15d
je     1265 <main+0x115>
movslq %r15d,%rax
mov    0xc(%rsp,%rax,4),%esi
mov    %r12,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    11fb <main+0xab>
nopl   0x0(%rax,%rax,1)
add    $0xffffffff,%ebp
jmp    11fb <main+0xab>
lea    0xdaa(%rip),%rdi
call   1040 <puts@plt>
xor    %r15d,%r15d
jmp    11fb <main+0xab>
xor    %eax,%eax
add    $0x9c58,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [10 x i8] c"input.txt\00"
@.str.1 = [2 x i8] c"r\00"
@stdin = global i32 0
@.str.2 = [3 x i8] c"%d\00"
@.str.3 = [3 x i8] c"%c\00"
@.str.4 = [4 x i8] c"%d\0A\00"
@.str.5 = [3 x i8] c"E\0A\00"
@.str.6 = [2 x i8] c"\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [10001 x i32]
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* @stdin
%12 = call i32 @freopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 %11)
br label %13
13: ; preds = %59, %2
%14 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %8)
%15 = load i32, i32* %8
%16 = icmp ne i32 %15, 0
br i1 %16, label %17, label %61
17: ; preds = %13
store i32 0, i32* %9
store i32 0, i32* %7
br label %18
18: ; preds = %56, %17
%19 = load i32, i32* %9
%20 = load i32, i32* %8
%21 = icmp slt i32 %19, %20
br i1 %21, label %22, label %59
22: ; preds = %18
%23 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %10)
%24 = load i8, i8* %10
%25 = sext i8 %24 to i32
switch i32 %25, label %52 [
i32 80, label %26
i32 79, label %32
i32 65, label %39
]
26: ; preds = %22
%27 = load i32, i32* %7
%28 = add nsw i32 %27, 1
store i32 %28, i32* %7
%29 = sext i32 %27 to i64
%30 = getelementptr inbounds [10001 x i32], [10001 x i32]* %6, i64 0, i64 %29
%31 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %30)
br label %55
32: ; preds = %22
%33 = load i32, i32* %7
%34 = icmp sgt i32 %33, 0
br i1 %34, label %35, label %38
35: ; preds = %32
%36 = load i32, i32* %7
%37 = add nsw i32 %36, -1
store i32 %37, i32* %7
br label %38
38: ; preds = %35, %32
br label %55
39: ; preds = %22
%40 = load i32, i32* %7
%41 = icmp ne i32 %40, 0
br i1 %41, label %42, label %49
42: ; preds = %39
%43 = load i32, i32* %7
%44 = sub nsw i32 %43, 1
%45 = sext i32 %44 to i64
%46 = getelementptr inbounds [10001 x i32], [10001 x i32]* %6, i64 0, i64 %45
%47 = load i32, i32* %46
%48 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i32 %47)
br label %51
49: ; preds = %39
%50 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0))
br label %51
51: ; preds = %49, %42
br label %55
52: ; preds = %22
%53 = load i32, i32* %9
%54 = add nsw i32 %53, -1
store i32 %54, i32* %9
br label %55
55: ; preds = %52, %51, %38, %26
br label %56
56: ; preds = %55
%57 = load i32, i32* %9
%58 = add nsw i32 %57, 1
store i32 %58, i32* %9
br label %18
59: ; preds = %18
%60 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0))
br label %13
61: ; preds = %13
ret i32 0
}

LLVM IR (-O2):
@.str = [10 x i8] c"input.txt\00"
@.str.1 = [2 x i8] c"r\00"
@stdin = global i32 0
@.str.2 = [3 x i8] c"%d\00"
@.str.3 = [3 x i8] c"%c\00"
@.str.4 = [4 x i8] c"%d\0A\00"
@str = [2 x i8] c"E\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [10001 x i32]
%4 = alloca i32
%5 = alloca i8
%6 = bitcast [10001 x i32]* %3 to i8*
%7 = bitcast i32* %4 to i8*
%8 = load i32, i32* @stdin
%9 = tail call i32 @freopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 %8)
%10 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %4)
%11 = load i32, i32* %4
%12 = icmp eq i32 %11, 0
br i1 %12, label %54, label %13
13: ; preds = %2, %49
%14 = phi i32 [ %52, %49 ], [ %11, %2 ]
%15 = icmp sgt i32 %14, 0
br i1 %15, label %16, label %49
16: ; preds = %13, %43
%17 = phi i32 [ %46, %43 ], [ 0, %13 ]
%18 = phi i32 [ %44, %43 ], [ 0, %13 ]
%19 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %5)
%20 = load i8, i8* %5
%21 = sext i8 %20 to i32
switch i32 %21, label %41 [
i32 80, label %22
i32 79, label %27
i32 65, label %31
]
22: ; preds = %16
%23 = add nsw i32 %18, 1
%24 = sext i32 %18 to i64
%25 = getelementptr inbounds [10001 x i32], [10001 x i32]* %3, i64 0, i64 %24
%26 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %25)
br label %43
27: ; preds = %16
%28 = icmp sgt i32 %18, 0
%29 = sext i1 %28 to i32
%30 = add nsw i32 %18, %29
br label %43
31: ; preds = %16
%32 = icmp eq i32 %18, 0
br i1 %32, label %39, label %33
33: ; preds = %31
%34 = add nsw i32 %18, -1
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds [10001 x i32], [10001 x i32]* %3, i64 0, i64 %35
%37 = load i32, i32* %36
%38 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i32 %37)
br label %43
39: ; preds = %31
%40 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([2 x i8], [2 x i8]* @str, i64 0, i64 0))
br label %43
41: ; preds = %16
%42 = add nsw i32 %17, -1
br label %43
43: ; preds = %27, %22, %41, %39, %33
%44 = phi i32 [ %18, %41 ], [ %18, %33 ], [ 0, %39 ], [ %23, %22 ], [ %30, %27 ]
%45 = phi i32 [ %42, %41 ], [ %17, %33 ], [ %17, %39 ], [ %17, %22 ], [ %17, %27 ]
%46 = add nsw i32 %45, 1
%47 = load i32, i32* %4
%48 = icmp slt i32 %46, %47
br i1 %48, label %16, label %49
49: ; preds = %43, %13
%50 = call i32 @putchar(i32 10)
%51 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %4)
%52 = load i32, i32* %4
%53 = icmp eq i32 %52, 0
br i1 %53, label %54, label %13
54: ; preds = %49, %2
ret i32 0
}