#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*/ printf(char *);
int /*<<< orphan*/ scanf(char *, ...);
int main() {
  char dir[1500];
  int N, i, df = 0;
  scanf(" %d", &N);
  while (N != 0) {
    df = 0;
    scanf(" %s", dir);
    for (i = 0; i < N; i++) {
      switch (dir[i]) {
      case 'D':
        df++;
        break;
      case 'E':
        df--;
        break;
      }
      if (df > 3)
        df = 0;
      else if (df < 0)
        df = 3;
    }
    switch (df) {
    case 0:
      printf("N\n");
      break;
    case 1:
      printf("L\n");
      break;
    case 2:
      printf("S\n");
      break;
    case 3:
      printf("O\n");
      break;
    }
    scanf(" %d", &N);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x600,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x5ec(%rbp)
lea    0xecd(%rip),%rdi
lea    -0x5e4(%rbp),%rsi
mov    $0x0,%al
call   1040 <scanf@plt>
cmpl   $0x0,-0x5e4(%rbp)
je     12cb <main+0x1ab>
movl   $0x0,-0x5ec(%rbp)
lea    -0x5e0(%rbp),%rsi
lea    0xe9e(%rip),%rdi
mov    $0x0,%al
call   1040 <scanf@plt>
movl   $0x0,-0x5e8(%rbp)
mov    -0x5e8(%rbp),%eax
cmp    -0x5e4(%rbp),%eax
jge    1244 <main+0x124>
movslq -0x5e8(%rbp),%rax
movsbl -0x5e0(%rbp,%rax,1),%eax
ff
mov    %eax,-0x5f0(%rbp)
sub    $0x44,%eax
je     11d0 <main+0xb0>
jmp    11bc <main+0x9c>
mov    -0x5f0(%rbp),%eax
sub    $0x45,%eax
je     11e4 <main+0xc4>
jmp    11f3 <main+0xd3>
mov    -0x5ec(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x5ec(%rbp)
jmp    11f3 <main+0xd3>
mov    -0x5ec(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x5ec(%rbp)
cmpl   $0x3,-0x5ec(%rbp)
jle    120f <main+0xef>
movl   $0x0,-0x5ec(%rbp)
jmp    122b <main+0x10b>
cmpl   $0x0,-0x5ec(%rbp)
jge    1226 <main+0x106>
movl   $0x3,-0x5ec(%rbp)
jmp    122b <main+0x10b>
jmp    1230 <main+0x110>
mov    -0x5e8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x5e8(%rbp)
jmp    1187 <main+0x67>
mov    -0x5ec(%rbp),%eax
mov    %rax,-0x5f8(%rbp)
sub    $0x3,%rax
ja     12b1 <main+0x191>
mov    -0x5f8(%rbp),%rax
lea    0xd97(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
lea    0xd9f(%rip),%rdi
call   1030 <printf@plt>
jmp    12b1 <main+0x191>
lea    0xd91(%rip),%rdi
call   1030 <printf@plt>
jmp    12b1 <main+0x191>
lea    0xd83(%rip),%rdi
call   1030 <printf@plt>
jmp    12b1 <main+0x191>
lea    0xd75(%rip),%rdi
call   1030 <printf@plt>
lea    0xd58(%rip),%rdi
lea    -0x5e4(%rbp),%rsi
mov    $0x0,%al
call   1040 <scanf@plt>
jmp    1151 <main+0x31>
xor    %eax,%eax
add    $0x600,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x5f8,%rsp
lea    0xec8(%rip),%rdi
xor    %ebp,%ebp
lea    0xc(%rsp),%rsi
xor    %eax,%eax
call   1040 <scanf@plt>
cmpl   $0x0,0xc(%rsp)
je     12a6 <main+0x186>
lea    0xeac(%rip),%r14
lea    0x10(%rsp),%r15
lea    0xeaa(%rip),%r13
lea    0xe95(%rip),%r12
lea    0xc(%rsp),%rbx
jmp    119d <main+0x7d>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
call   1030 <puts@plt>
mov    %r12,%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1040 <scanf@plt>
cmpl   $0x0,0xc(%rsp)
je     12a6 <main+0x186>
mov    %r14,%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   1040 <scanf@plt>
mov    0xc(%rsp),%eax
mov    %r13,%rdi
test   %eax,%eax
jle    1180 <main+0x60>
cmp    $0x1,%eax
jne    11f0 <main+0xd0>
xor    %ecx,%ecx
xor    %edx,%edx
test   $0x1,%al
je     1287 <main+0x167>
movsbl 0x10(%rsp,%rcx,1),%eax
cmp    $0x44,%eax
je     1270 <main+0x150>
cmp    $0x45,%eax
jne    1273 <main+0x153>
add    $0xffffffff,%edx
jmp    1273 <main+0x153>
cs nopw 0x0(%rax,%rax,1)
nop
mov    %eax,%esi
and    $0xfffffffe,%esi
xor    %ecx,%ecx
xor    %edx,%edx
jmp    120f <main+0xef>
nopl   0x0(%rax,%rax,1)
cmp    $0x4,%edi
cmovge %ebp,%edx
add    $0x2,%rcx
cmp    %rcx,%rsi
je     11be <main+0x9e>
movsbl 0x10(%rsp,%rcx,1),%edi
cmp    $0x45,%edi
je     1230 <main+0x110>
cmp    $0x44,%edi
jne    1233 <main+0x113>
add    $0x1,%edx
jmp    1233 <main+0x113>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
add    $0xffffffff,%edx
mov    $0x3,%edi
test   %edx,%edx
js     123e <main+0x11e>
mov    %edx,%edi
cmp    $0x4,%edx
cmovge %ebp,%edi
movsbl 0x11(%rsp,%rcx,1),%edx
cmp    $0x44,%edx
je     1260 <main+0x140>
cmp    $0x45,%edx
jne    1263 <main+0x143>
add    $0xffffffff,%edi
jmp    1263 <main+0x143>
nopl   0x0(%rax,%rax,1)
add    $0x1,%edi
mov    $0x3,%edx
test   %edi,%edi
js     1200 <main+0xe0>
mov    %edi,%edx
jmp    1200 <main+0xe0>
add    $0x1,%edx
test   %edx,%edx
mov    %edx,%eax
mov    $0x3,%ecx
cmovs  %ecx,%eax
cmp    $0x4,%edx
cmovge %ebp,%eax
mov    %eax,%edx
cmp    $0x3,%edx
ja     1185 <main+0x65>
movslq %edx,%rax
lea    0xd76(%rip),%rcx
movslq (%rcx,%rax,4),%rdi
add    %rcx,%rdi
jmp    1180 <main+0x60>
xor    %eax,%eax
add    $0x5f8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [4 x i8] c" %d\00"
@.str.1 = [4 x i8] c" %s\00"
@str = [2 x i8] c"O\00"
@str.6 = [2 x i8] c"S\00"
@str.7 = [2 x i8] c"L\00"
@str.8 = [2 x i8] c"N\00"
@reltable.main = [4 x i32] [i32 trunc (i64 sub (i64 ptrtoint ([2 x i8]* @str.8 to i64), i64 ptrtoint ([4 x i32]* @reltable.main to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([2 x i8]* @str.7 to i64), i64 ptrtoint ([4 x i32]* @reltable.main to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([2 x i8]* @str.6 to i64), i64 ptrtoint ([4 x i32]* @reltable.main to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([2 x i8]* @str to i64), i64 ptrtoint ([4 x i32]* @reltable.main to i64)) to i32)]
define i32 @main() {
%1 = alloca [1500 x i8]
%2 = alloca i32
%3 = getelementptr inbounds [1500 x i8], [1500 x i8]* %1, i64 0, i64 0
%4 = bitcast i32* %2 to i8*
%5 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32* %2)
%6 = load i32, i32* %2
%7 = icmp eq i32 %6, 0
br i1 %7, label %85, label %8
8: ; preds = %0, %81
%9 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %3)
%10 = load i32, i32* %2
%11 = icmp sgt i32 %10, 0
br i1 %11, label %12, label %78
12: ; preds = %8
%13 = zext i32 %10 to i64
%14 = and i64 %13, 1
%15 = icmp eq i32 %10, 1
br i1 %15, label %52, label %16
16: ; preds = %12
%17 = and i64 %13, 4294967294
br label %18
18: ; preds = %43, %16
%19 = phi i64 [ 0, %16 ], [ %49, %43 ]
%20 = phi i32 [ 0, %16 ], [ %48, %43 ]
%21 = phi i64 [ 0, %16 ], [ %50, %43 ]
%22 = getelementptr inbounds [1500 x i8], [1500 x i8]* %1, i64 0, i64 %19
%23 = load i8, i8* %22
%24 = sext i8 %23 to i32
switch i32 %24, label %29 [
i32 68, label %25
i32 69, label %27
]
25: ; preds = %18
%26 = add nsw i32 %20, 1
br label %29
27: ; preds = %18
%28 = add nsw i32 %20, -1
br label %29
29: ; preds = %18, %27, %25
%30 = phi i32 [ %20, %18 ], [ %28, %27 ], [ %26, %25 ]
%31 = icmp sgt i32 %30, 3
%32 = icmp slt i32 %30, 0
%33 = select i1 %32, i32 3, i32 %30
%34 = select i1 %31, i32 0, i32 %33
%35 = or i64 %19, 1
%36 = getelementptr inbounds [1500 x i8], [1500 x i8]* %1, i64 0, i64 %35
%37 = load i8, i8* %36
%38 = sext i8 %37 to i32
switch i32 %38, label %43 [
i32 68, label %41
i32 69, label %39
]
39: ; preds = %29
%40 = add nsw i32 %34, -1
br label %43
41: ; preds = %29
%42 = add nsw i32 %34, 1
br label %43
43: ; preds = %41, %39, %29
%44 = phi i32 [ %34, %29 ], [ %40, %39 ], [ %42, %41 ]
%45 = icmp sgt i32 %44, 3
%46 = icmp slt i32 %44, 0
%47 = select i1 %46, i32 3, i32 %44
%48 = select i1 %45, i32 0, i32 %47
%49 = add nuw nsw i64 %19, 2
%50 = add i64 %21, 2
%51 = icmp eq i64 %50, %17
br i1 %51, label %52, label %18
52: ; preds = %43, %12
%53 = phi i32 [ undef, %12 ], [ %48, %43 ]
%54 = phi i64 [ 0, %12 ], [ %49, %43 ]
%55 = phi i32 [ 0, %12 ], [ %48, %43 ]
%56 = icmp eq i64 %14, 0
br i1 %56, label %71, label %57
57: ; preds = %52
%58 = getelementptr inbounds [1500 x i8], [1500 x i8]* %1, i64 0, i64 %54
%59 = load i8, i8* %58
%60 = sext i8 %59 to i32
switch i32 %60, label %65 [
i32 68, label %63
i32 69, label %61
]
61: ; preds = %57
%62 = add nsw i32 %55, -1
br label %65
63: ; preds = %57
%64 = add nsw i32 %55, 1
br label %65
65: ; preds = %63, %61, %57
%66 = phi i32 [ %55, %57 ], [ %62, %61 ], [ %64, %63 ]
%67 = icmp sgt i32 %66, 3
%68 = icmp slt i32 %66, 0
%69 = select i1 %68, i32 3, i32 %66
%70 = select i1 %67, i32 0, i32 %69
br label %71
71: ; preds = %52, %65
%72 = phi i32 [ %53, %52 ], [ %70, %65 ]
%73 = icmp ult i32 %72, 4
br i1 %73, label %74, label %81
74: ; preds = %71
%75 = sext i32 %72 to i64
%76 = shl i64 %75, 2
%77 = call i8* @llvm.load.relative.i64(i8* bitcast ([4 x i32]* @reltable.main to i8*), i64 %76)
br label %78
78: ; preds = %74, %8
%79 = phi i8* [ getelementptr inbounds ([2 x i8], [2 x i8]* @str.8, i64 0, i64 0), %8 ], [ %77, %74 ]
%80 = call i32 @puts(i8* dereferenceable(1) %79)
br label %81
81: ; preds = %71, %78
%82 = call i32 (i8*, ...) @scanf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32* %2)
%83 = load i32, i32* %2
%84 = icmp eq i32 %83, 0
br i1 %84, label %85, label %8
85: ; preds = %81, %0
ret i32 0
}