#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 EOF;
char getchar();
int /*<<< orphan*/ printf(char *, char);
void solve_part_2() {
  char key_pad[7][7] = {
      {0, 0, 0, 0, 0, 0, 0},       {0, 0, 0, '1', 0, 0, 0},
      {0, 0, '2', '3', '4', 0, 0}, {0, '5', '6', '7', '8', '9', 0},
      {0, 0, 'A', 'B', 'C', 0, 0}, {0, 0, 0, 'D', 0, 0, 0},
      {0, 0, 0, 0, 0, 0, 0},
  };
  char direction;
  int current_row = 3;
  int current_column = 1;
  while ((direction = getchar()) != EOF) {
    switch (direction) {
    case 'U':
      if (key_pad[current_row - 1][current_column]) {
        current_row--;
      }
      break;
    case 'L':
      if (key_pad[current_row][current_column - 1]) {
        current_column--;
      }
      break;
    case 'D':
      if (key_pad[current_row + 1][current_column]) {
        current_row++;
      }
      break;
    case 'R':
      if (key_pad[current_row][current_column + 1]) {
        current_column++;
      }
      break;
    default:
      printf("%c", key_pad[current_row][current_column]);
      break;
    }
  }
  printf("%c\n", key_pad[current_row][current_column]);
}


x86 assembly (clang, -O0):
<solve_part_2>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
lea    -0x40(%rbp),%rdi
lea    0xf0d(%rip),%rsi
mov    $0x31,%edx
call   1050 <memcpy@plt>
movl   $0x3,-0x48(%rbp)
movl   $0x1,-0x4c(%rbp)
mov    $0x0,%al
call   1040 <getchar@plt>
mov    %al,-0x41(%rbp)
movsbl %al,%eax
mov    0x2e79(%rip),%rcx
movsbl (%rcx),%ecx
cmp    %ecx,%eax
je     128c <solve_part_2+0x15c>
movsbl -0x41(%rbp),%eax
add    $0xffffffbc,%eax
mov    %eax,%ecx
mov    %rcx,-0x58(%rbp)
sub    $0x11,%eax
ja     1264 <solve_part_2+0x134>
mov    -0x58(%rbp),%rax
lea    0xe65(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
mov    -0x48(%rbp),%eax
sub    $0x1,%eax
movslq %eax,%rcx
lea    -0x40(%rbp),%rax
imul   $0x7,%rcx,%rcx
add    %rcx,%rax
movslq -0x4c(%rbp),%rcx
cmpb   $0x0,(%rax,%rcx,1)
je     11cf <solve_part_2+0x9f>
mov    -0x48(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x48(%rbp)
jmp    1287 <solve_part_2+0x157>
movslq -0x48(%rbp),%rcx
lea    -0x40(%rbp),%rax
imul   $0x7,%rcx,%rcx
add    %rcx,%rax
mov    -0x4c(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
cmpb   $0x0,(%rax,%rcx,1)
je     11ff <solve_part_2+0xcf>
mov    -0x4c(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x4c(%rbp)
jmp    1287 <solve_part_2+0x157>
mov    -0x48(%rbp),%eax
add    $0x1,%eax
movslq %eax,%rcx
lea    -0x40(%rbp),%rax
imul   $0x7,%rcx,%rcx
add    %rcx,%rax
movslq -0x4c(%rbp),%rcx
cmpb   $0x0,(%rax,%rcx,1)
je     122f <solve_part_2+0xff>
mov    -0x48(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x48(%rbp)
jmp    1287 <solve_part_2+0x157>
movslq -0x48(%rbp),%rcx
lea    -0x40(%rbp),%rax
imul   $0x7,%rcx,%rcx
add    %rcx,%rax
mov    -0x4c(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
cmpb   $0x0,(%rax,%rcx,1)
je     125f <solve_part_2+0x12f>
mov    -0x4c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4c(%rbp)
jmp    1287 <solve_part_2+0x157>
movslq -0x48(%rbp),%rcx
lea    -0x40(%rbp),%rax
imul   $0x7,%rcx,%rcx
add    %rcx,%rax
movslq -0x4c(%rbp),%rcx
lea    0xe03(%rip),%rdi
movsbl (%rax,%rcx,1),%esi
call   1030 <printf@plt>
jmp    115b <solve_part_2+0x2b>
movslq -0x48(%rbp),%rcx
lea    -0x40(%rbp),%rax
imul   $0x7,%rcx,%rcx
add    %rcx,%rax
movslq -0x4c(%rbp),%rcx
lea    0xdde(%rip),%rdi
movsbl (%rax,%rcx,1),%esi
call   1030 <printf@plt>
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<solve_part_2>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
xor    %eax,%eax
call   1040 <getchar@plt>
mov    0x2eaf(%rip),%r15
cmp    (%r15),%al
jne    1181 <solve_part_2+0x61>
mov    $0x1,%ebp
mov    $0x3,%ebx
movslq %ebx,%rax
movslq %ebp,%rcx
lea    0x0(,%rax,8),%rdx
sub    %rax,%rdx
lea    0xef0(%rip),%rax
add    %rdx,%rax
movsbl (%rcx,%rax,1),%esi
lea    0xf16(%rip),%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <printf@plt>
mov    $0x3,%ebx
mov    $0x1,%ebp
lea    0xebe(%rip),%r12
lea    0xee8(%rip),%r14
lea    0xe60(%rip),%r13
jmp    11e0 <solve_part_2+0xc0>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
movslq %ebx,%rax
movslq %ebp,%rcx
lea    0x0(,%rax,8),%rdx
sub    %rax,%rdx
add    %r12,%rdx
movsbl (%rcx,%rdx,1),%esi
mov    %r14,%rdi
call   1030 <printf@plt>
xor    %eax,%eax
call   1040 <getchar@plt>
cmp    (%r15),%al
je     1148 <solve_part_2+0x28>
movsbl %al,%eax
add    $0xffffffbc,%eax
cmp    $0x11,%eax
ja     11b0 <solve_part_2+0x90>
movslq 0x0(%r13,%rax,4),%rax
add    %r13,%rax
jmp    *%rax
movslq %ebx,%rax
movslq %ebp,%rcx
lea    0x8(,%rax,8),%rdx
add    $0x1,%rax
sub    %rax,%rdx
add    %r12,%rdx
cmpb   $0x0,(%rcx,%rdx,1)
je     11d0 <solve_part_2+0xb0>
add    $0x1,%ebx
jmp    11d0 <solve_part_2+0xb0>
nopl   0x0(%rax,%rax,1)
movslq %ebx,%rax
movslq %ebp,%rcx
lea    0x0(,%rax,8),%rdx
sub    %rax,%rdx
add    %r12,%rdx
cmpb   $0x0,-0x1(%rcx,%rdx,1)
je     11d0 <solve_part_2+0xb0>
add    $0xffffffff,%ebp
jmp    11d0 <solve_part_2+0xb0>
movslq %ebx,%rax
movslq %ebp,%rcx
lea    0x0(,%rax,8),%rdx
sub    %rax,%rdx
add    %r12,%rdx
cmpb   $0x0,0x1(%rcx,%rdx,1)
je     11d0 <solve_part_2+0xb0>
add    $0x1,%ebp
jmp    11d0 <solve_part_2+0xb0>
nopw   0x0(%rax,%rax,1)
movslq %ebx,%rax
movslq %ebp,%rcx
lea    -0x8(,%rax,8),%rdx
ff
add    $0xffffffffffffffff,%rax
sub    %rax,%rdx
add    %r12,%rdx
cmpb   $0x0,(%rcx,%rdx,1)
je     11d0 <solve_part_2+0xb0>
add    $0xffffffff,%ebx
jmp    11d0 <solve_part_2+0xb0>

LLVM IR (-O0):
@__const.solve_part_2.key_pad = [7 x [7 x i8]] [[7 x i8] zeroinitializer, [7 x i8] c"\00\00\001\00\00\00", [7 x i8] c"\00\00234\00\00", [7 x i8] c"\0056789\00", [7 x i8] c"\00\00ABC\00\00", [7 x i8] c"\00\00\00D\00\00\00", [7 x i8] zeroinitializer]
@EOF = global i8 0
@.str = [3 x i8] c"%c\00"
@.str.1 = [4 x i8] c"%c\0A\00"
define void @solve_part_2() {
%1 = alloca [7 x [7 x i8]]
%2 = alloca i8
%3 = alloca i32
%4 = alloca i32
%5 = bitcast [7 x [7 x i8]]* %1 to i8*
store i32 3, i32* %3
store i32 1, i32* %4
br label %6
6: ; preds = %80, %0
%7 = call signext i8 (...) @getchar()
store i8 %7, i8* %2
%8 = sext i8 %7 to i32
%9 = load i8, i8* @EOF
%10 = sext i8 %9 to i32
%11 = icmp ne i32 %8, %10
br i1 %11, label %12, label %81
12: ; preds = %6
%13 = load i8, i8* %2
%14 = sext i8 %13 to i32
switch i32 %14, label %71 [
i32 85, label %15
i32 76, label %29
i32 68, label %43
i32 82, label %57
]
15: ; preds = %12
%16 = load i32, i32* %3
%17 = sub nsw i32 %16, 1
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds [7 x [7 x i8]], [7 x [7 x i8]]* %1, i64 0, i64 %18
%20 = load i32, i32* %4
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds [7 x i8], [7 x i8]* %19, i64 0, i64 %21
%23 = load i8, i8* %22
%24 = icmp ne i8 %23, 0
br i1 %24, label %25, label %28
25: ; preds = %15
%26 = load i32, i32* %3
%27 = add nsw i32 %26, -1
store i32 %27, i32* %3
br label %28
28: ; preds = %25, %15
br label %80
29: ; preds = %12
%30 = load i32, i32* %3
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds [7 x [7 x i8]], [7 x [7 x i8]]* %1, i64 0, i64 %31
%33 = load i32, i32* %4
%34 = sub nsw i32 %33, 1
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds [7 x i8], [7 x i8]* %32, i64 0, i64 %35
%37 = load i8, i8* %36
%38 = icmp ne i8 %37, 0
br i1 %38, label %39, label %42
39: ; preds = %29
%40 = load i32, i32* %4
%41 = add nsw i32 %40, -1
store i32 %41, i32* %4
br label %42
42: ; preds = %39, %29
br label %80
43: ; preds = %12
%44 = load i32, i32* %3
%45 = add nsw i32 %44, 1
%46 = sext i32 %45 to i64
%47 = getelementptr inbounds [7 x [7 x i8]], [7 x [7 x i8]]* %1, i64 0, i64 %46
%48 = load i32, i32* %4
%49 = sext i32 %48 to i64
%50 = getelementptr inbounds [7 x i8], [7 x i8]* %47, i64 0, i64 %49
%51 = load i8, i8* %50
%52 = icmp ne i8 %51, 0
br i1 %52, label %53, label %56
53: ; preds = %43
%54 = load i32, i32* %3
%55 = add nsw i32 %54, 1
store i32 %55, i32* %3
br label %56
56: ; preds = %53, %43
br label %80
57: ; preds = %12
%58 = load i32, i32* %3
%59 = sext i32 %58 to i64
%60 = getelementptr inbounds [7 x [7 x i8]], [7 x [7 x i8]]* %1, i64 0, i64 %59
%61 = load i32, i32* %4
%62 = add nsw i32 %61, 1
%63 = sext i32 %62 to i64
%64 = getelementptr inbounds [7 x i8], [7 x i8]* %60, i64 0, i64 %63
%65 = load i8, i8* %64
%66 = icmp ne i8 %65, 0
br i1 %66, label %67, label %70
67: ; preds = %57
%68 = load i32, i32* %4
%69 = add nsw i32 %68, 1
store i32 %69, i32* %4
br label %70
70: ; preds = %67, %57
br label %80
71: ; preds = %12
%72 = load i32, i32* %3
%73 = sext i32 %72 to i64
%74 = getelementptr inbounds [7 x [7 x i8]], [7 x [7 x i8]]* %1, i64 0, i64 %73
%75 = load i32, i32* %4
%76 = sext i32 %75 to i64
%77 = getelementptr inbounds [7 x i8], [7 x i8]* %74, i64 0, i64 %76
%78 = load i8, i8* %77
%79 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8 signext %78)
br label %80
80: ; preds = %71, %70, %56, %42, %28
br label %6
81: ; preds = %6
%82 = load i32, i32* %3
%83 = sext i32 %82 to i64
%84 = getelementptr inbounds [7 x [7 x i8]], [7 x [7 x i8]]* %1, i64 0, i64 %83
%85 = load i32, i32* %4
%86 = sext i32 %85 to i64
%87 = getelementptr inbounds [7 x i8], [7 x i8]* %84, i64 0, i64 %86
%88 = load i8, i8* %87
%89 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8 signext %88)
ret void
}

LLVM IR (-O2):
@__const.solve_part_2.key_pad = [7 x [7 x i8]] [[7 x i8] zeroinitializer, [7 x i8] c"\00\00\001\00\00\00", [7 x i8] c"\00\00234\00\00", [7 x i8] c"\0056789\00", [7 x i8] c"\00\00ABC\00\00", [7 x i8] c"\00\00\00D\00\00\00", [7 x i8] zeroinitializer]
@EOF = global i8 0
@.str = [3 x i8] c"%c\00"
@.str.1 = [4 x i8] c"%c\0A\00"
define void @solve_part_2() {
%1 = tail call signext i8 (...) @getchar()
%2 = load i8, i8* @EOF
%3 = icmp eq i8 %1, %2
br i1 %3, label %53, label %4
4: ; preds = %0, %47
%5 = phi i8 [ %50, %47 ], [ %1, %0 ]
%6 = phi i32 [ %49, %47 ], [ 1, %0 ]
%7 = phi i32 [ %48, %47 ], [ 3, %0 ]
%8 = sext i8 %5 to i32
switch i32 %8, label %41 [
i32 85, label %9
i32 76, label %17
i32 68, label %25
i32 82, label %33
]
9: ; preds = %4
%10 = add nsw i32 %7, -1
%11 = sext i32 %10 to i64
%12 = sext i32 %6 to i64
%13 = getelementptr inbounds [7 x [7 x i8]], [7 x [7 x i8]]* @__const.solve_part_2.key_pad, i64 0, i64 %11, i64 %12
%14 = load i8, i8* %13
%15 = icmp eq i8 %14, 0
%16 = select i1 %15, i32 %7, i32 %10
br label %47
17: ; preds = %4
%18 = sext i32 %7 to i64
%19 = add nsw i32 %6, -1
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds [7 x [7 x i8]], [7 x [7 x i8]]* @__const.solve_part_2.key_pad, i64 0, i64 %18, i64 %20
%22 = load i8, i8* %21
%23 = icmp eq i8 %22, 0
%24 = select i1 %23, i32 %6, i32 %19
br label %47
25: ; preds = %4
%26 = add nsw i32 %7, 1
%27 = sext i32 %26 to i64
%28 = sext i32 %6 to i64
%29 = getelementptr inbounds [7 x [7 x i8]], [7 x [7 x i8]]* @__const.solve_part_2.key_pad, i64 0, i64 %27, i64 %28
%30 = load i8, i8* %29
%31 = icmp eq i8 %30, 0
%32 = select i1 %31, i32 %7, i32 %26
br label %47
33: ; preds = %4
%34 = sext i32 %7 to i64
%35 = add nsw i32 %6, 1
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds [7 x [7 x i8]], [7 x [7 x i8]]* @__const.solve_part_2.key_pad, i64 0, i64 %34, i64 %36
%38 = load i8, i8* %37
%39 = icmp eq i8 %38, 0
%40 = select i1 %39, i32 %6, i32 %35
br label %47
41: ; preds = %4
%42 = sext i32 %7 to i64
%43 = sext i32 %6 to i64
%44 = getelementptr inbounds [7 x [7 x i8]], [7 x [7 x i8]]* @__const.solve_part_2.key_pad, i64 0, i64 %42, i64 %43
%45 = load i8, i8* %44
%46 = tail call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8 signext %45)
br label %47
47: ; preds = %33, %25, %17, %9, %41
%48 = phi i32 [ %7, %41 ], [ %16, %9 ], [ %7, %17 ], [ %32, %25 ], [ %7, %33 ]
%49 = phi i32 [ %6, %41 ], [ %6, %9 ], [ %24, %17 ], [ %6, %25 ], [ %40, %33 ]
%50 = tail call signext i8 (...) @getchar()
%51 = load i8, i8* @EOF
%52 = icmp eq i8 %50, %51
br i1 %52, label %53, label %4
53: ; preds = %47, %0
%54 = phi i32 [ 3, %0 ], [ %48, %47 ]
%55 = phi i32 [ 1, %0 ], [ %49, %47 ]
%56 = sext i32 %54 to i64
%57 = sext i32 %55 to i64
%58 = getelementptr inbounds [7 x [7 x i8]], [7 x [7 x i8]]* @__const.solve_part_2.key_pad, i64 0, i64 %56, i64 %57
%59 = load i8, i8* %58
%60 = tail call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8 signext %59)
ret void
}