#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 */
char mygetch();
int /*<<< orphan*/ printf(char *, ...);
int main(void) {
  char m;
  char q = 'n';
  int d = 0;
  int x = 0;
  int y = 0;
  printf("Controls:\n\nW to move forward.\nA to turn left.\nS to backstep.\nD "
         "to turn right.\n\n");
  do {
    m = mygetch();
    switch (m) {
    case 'w':
      printf("You move forward.\n");
      switch (d) {
      case 0:
        y = y + 1;
        break;
      case 1:
        x = x + 1;
        break;
      case 2:
        y = y - 1;
        break;
      case 3:
        x = x - 1;
        break;
      default:
        break;
      }
      break;
    case 'a':
      printf("You turn left.\n");
      if (d == 0) {
        d = 3;
      } else if (d > 0) {
        d = d - 1;
      }
      break;
    case 'd':
      printf("You turn right.\n");
      if (d == 3) {
        d = 0;
      } else if (d < 3) {
        d = d + 1;
      }
    }
    printf("Your location is: %d,%d\n\n", x, y);
  } while (q != y);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movb   $0x6e,-0x6(%rbp)
movl   $0x0,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
lea    0xec1(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %eax,%eax
call   1030 <mygetch@plt>
mov    %al,-0x5(%rbp)
movsbl -0x5(%rbp),%eax
mov    %eax,-0x18(%rbp)
sub    $0x61,%eax
je     120c <main+0xec>
jmp    1175 <main+0x55>
mov    -0x18(%rbp),%eax
sub    $0x64,%eax
je     124d <main+0x12d>
jmp    1186 <main+0x66>
mov    -0x18(%rbp),%eax
sub    $0x77,%eax
jne    128e <main+0x16e>
jmp    1197 <main+0x77>
lea    0xec2(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    -0xc(%rbp),%eax
mov    %rax,-0x20(%rbp)
sub    $0x3,%rax
ja     1202 <main+0xe2>
mov    -0x20(%rbp),%rax
lea    0xe3f(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1207 <main+0xe7>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    1207 <main+0xe7>
mov    -0x14(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1207 <main+0xe7>
mov    -0x10(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    1207 <main+0xe7>
jmp    1207 <main+0xe7>
jmp    128e <main+0x16e>
lea    0xe60(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
cmpl   $0x0,-0xc(%rbp)
jne    1230 <main+0x110>
movl   $0x3,-0xc(%rbp)
jmp    1248 <main+0x128>
cmpl   $0x0,-0xc(%rbp)
jle    1243 <main+0x123>
mov    -0xc(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    1248 <main+0x128>
jmp    128e <main+0x16e>
lea    0xe2f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
cmpl   $0x3,-0xc(%rbp)
jne    1271 <main+0x151>
movl   $0x0,-0xc(%rbp)
jmp    1289 <main+0x169>
cmpl   $0x3,-0xc(%rbp)
jge    1284 <main+0x164>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    1289 <main+0x169>
jmp    128e <main+0x16e>
mov    -0x10(%rbp),%esi
mov    -0x14(%rbp),%edx
lea    0xdf9(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movsbl -0x6(%rbp),%eax
cmp    -0x14(%rbp),%eax
jne    1156 <main+0x36>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
lea    0xee8(%rip),%rdi
call   1030 <puts@plt>
lea    0xec2(%rip),%r13
lea    0xf24(%rip),%r15
lea    0xea4(%rip),%r14
xor    %r12d,%r12d
xor    %ebp,%ebp
xor    %ebx,%ebx
jmp    11a3 <main+0x73>
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r15,%rdi
call   1030 <puts@plt>
cmp    $0x3,%r12d
lea    0x1(%r12),%eax
mov    $0x0,%ecx
cmove  %ecx,%eax
mov    %eax,%r12d
mov    %r13,%rdi
mov    %ebp,%esi
mov    %ebx,%edx
xor    %eax,%eax
call   1050 <printf@plt>
cmp    $0x6e,%ebx
je     1235 <main+0x105>
xor    %eax,%eax
call   1040 <mygetch@plt>
movsbl %al,%eax
cmp    $0x61,%eax
je     11f0 <main+0xc0>
cmp    $0x64,%eax
je     1170 <main+0x40>
cmp    $0x77,%eax
jne    118c <main+0x5c>
lea    0xed5(%rip),%rdi
call   1030 <puts@plt>
cmp    $0x3,%r12d
ja     118c <main+0x5c>
mov    %r12d,%eax
movslq (%r14,%rax,4),%rax
add    %r14,%rax
jmp    *%rax
add    $0x1,%ebx
xor    %r12d,%r12d
jmp    118c <main+0x5c>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
lea    0xe92(%rip),%rdi
call   1030 <puts@plt>
sub    $0x1,%r12d
mov    $0x3,%eax
cmovb  %eax,%r12d
jmp    118c <main+0x5c>
add    $0x1,%ebp
mov    $0x1,%r12d
jmp    118c <main+0x5c>
add    $0xffffffff,%ebx
mov    $0x2,%r12d
jmp    118c <main+0x5c>
add    $0xffffffff,%ebp
mov    $0x3,%r12d
jmp    118c <main+0x5c>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [80 x i8] c"Controls:\0A\0AW to move forward.\0AA to turn left.\0AS to backstep.\0AD to turn right.\0A\0A\00"
@.str.1 = [19 x i8] c"You move forward.\0A\00"
@.str.2 = [16 x i8] c"You turn left.\0A\00"
@.str.3 = [17 x i8] c"You turn right.\0A\00"
@.str.4 = [26 x i8] c"Your location is: %d,%d\0A\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8
%3 = alloca i8
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i32 0, i32* %1
store i8 110, i8* %3
store i32 0, i32* %4
store i32 0, i32* %5
store i32 0, i32* %6
%7 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([80 x i8], [80 x i8]* @.str, i64 0, i64 0))
br label %8
8: ; preds = %59, %0
%9 = call signext i8 (...) @mygetch()
store i8 %9, i8* %2
%10 = load i8, i8* %2
%11 = sext i8 %10 to i32
switch i32 %11, label %55 [
i32 119, label %12
i32 97, label %29
i32 100, label %42
]
12: ; preds = %8
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
%14 = load i32, i32* %4
switch i32 %14, label %27 [
i32 0, label %15
i32 1, label %18
i32 2, label %21
i32 3, label %24
]
15: ; preds = %12
%16 = load i32, i32* %6
%17 = add nsw i32 %16, 1
store i32 %17, i32* %6
br label %28
18: ; preds = %12
%19 = load i32, i32* %5
%20 = add nsw i32 %19, 1
store i32 %20, i32* %5
br label %28
21: ; preds = %12
%22 = load i32, i32* %6
%23 = sub nsw i32 %22, 1
store i32 %23, i32* %6
br label %28
24: ; preds = %12
%25 = load i32, i32* %5
%26 = sub nsw i32 %25, 1
store i32 %26, i32* %5
br label %28
27: ; preds = %12
br label %28
28: ; preds = %27, %24, %21, %18, %15
br label %55
29: ; preds = %8
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
%31 = load i32, i32* %4
%32 = icmp eq i32 %31, 0
br i1 %32, label %33, label %34
33: ; preds = %29
store i32 3, i32* %4
br label %41
34: ; preds = %29
%35 = load i32, i32* %4
%36 = icmp sgt i32 %35, 0
br i1 %36, label %37, label %40
37: ; preds = %34
%38 = load i32, i32* %4
%39 = sub nsw i32 %38, 1
store i32 %39, i32* %4
br label %40
40: ; preds = %37, %34
br label %41
41: ; preds = %40, %33
br label %55
42: ; preds = %8
%43 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0))
%44 = load i32, i32* %4
%45 = icmp eq i32 %44, 3
br i1 %45, label %46, label %47
46: ; preds = %42
store i32 0, i32* %4
br label %54
47: ; preds = %42
%48 = load i32, i32* %4
%49 = icmp slt i32 %48, 3
br i1 %49, label %50, label %53
50: ; preds = %47
%51 = load i32, i32* %4
%52 = add nsw i32 %51, 1
store i32 %52, i32* %4
br label %53
53: ; preds = %50, %47
br label %54
54: ; preds = %53, %46
br label %55
55: ; preds = %54, %8, %41, %28
%56 = load i32, i32* %5
%57 = load i32, i32* %6
%58 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0), i32 %56, i32 %57)
br label %59
59: ; preds = %55
%60 = load i8, i8* %3
%61 = sext i8 %60 to i32
%62 = load i32, i32* %6
%63 = icmp ne i32 %61, %62
br i1 %63, label %8, label %64
64: ; preds = %59
ret i32 0
}

LLVM IR (-O2):
@.str.4 = [26 x i8] c"Your location is: %d,%d\0A\0A\00"
@str = [79 x i8] c"Controls:\0A\0AW to move forward.\0AA to turn left.\0AS to backstep.\0AD to turn right.\0A\00"
@str.5 = [16 x i8] c"You turn right.\00"
@str.6 = [15 x i8] c"You turn left.\00"
@str.7 = [18 x i8] c"You move forward.\00"
define i32 @main() {
%1 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([79 x i8], [79 x i8]* @str, i64 0, i64 0))
br label %2
2: ; preds = %28, %0
%3 = phi i32 [ 0, %0 ], [ %29, %28 ]
%4 = phi i32 [ 0, %0 ], [ %30, %28 ]
%5 = phi i32 [ 0, %0 ], [ %31, %28 ]
%6 = tail call signext i8 (...) @mygetch()
%7 = sext i8 %6 to i32
switch i32 %7, label %28 [
i32 119, label %8
i32 97, label %18
i32 100, label %23
]
8: ; preds = %2
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str.7, i64 0, i64 0))
switch i32 %3, label %28 [
i32 0, label %10
i32 1, label %12
i32 2, label %14
i32 3, label %16
]
10: ; preds = %8
%11 = add nsw i32 %5, 1
br label %28
12: ; preds = %8
%13 = add nsw i32 %4, 1
br label %28
14: ; preds = %8
%15 = add nsw i32 %5, -1
br label %28
16: ; preds = %8
%17 = add nsw i32 %4, -1
br label %28
18: ; preds = %2
%19 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str.6, i64 0, i64 0))
%20 = icmp eq i32 %3, 0
%21 = add nsw i32 %3, -1
%22 = select i1 %20, i32 3, i32 %21
br label %28
23: ; preds = %2
%24 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str.5, i64 0, i64 0))
%25 = icmp eq i32 %3, 3
%26 = add nsw i32 %3, 1
%27 = select i1 %25, i32 0, i32 %26
br label %28
28: ; preds = %23, %18, %10, %12, %14, %16, %8, %2
%29 = phi i32 [ %3, %2 ], [ %3, %8 ], [ 3, %16 ], [ 2, %14 ], [ 1, %12 ], [ 0, %10 ], [ %22, %18 ], [ %27, %23 ]
%30 = phi i32 [ %4, %2 ], [ %4, %8 ], [ %17, %16 ], [ %4, %14 ], [ %13, %12 ], [ %4, %10 ], [ %4, %18 ], [ %4, %23 ]
%31 = phi i32 [ %5, %2 ], [ %5, %8 ], [ %5, %16 ], [ %15, %14 ], [ %5, %12 ], [ %11, %10 ], [ %5, %18 ], [ %5, %23 ]
%32 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0), i32 %30, i32 %31)
%33 = icmp eq i32 %31, 110
br i1 %33, label %34, label %2
34: ; preds = %28
ret i32 0
}