#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*/ clear_grid();
char getchar();
int /*<<< orphan*/ output_ray(int, int, int, int, char *);
int /*<<< orphan*/ read_grid();
int /*<<< orphan*/ read_ray(int *, int *, int *, int *, char *);
int /*<<< orphan*/ scanf(char *, unsigned int *);
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ ungetc(int, int /*<<< orphan*/);
int main(void) {
  int c;
  while ((c = getchar()) != 'X') {
    ungetc(c, stdin);
    clear_grid();
    read_grid();
    unsigned lines;
    scanf("%u", &lines);
    for (unsigned i = 0; i < lines; i++) {
      int line, col, vx, vy;
      char str[3] = {0};
      do {
        if (getchar() == '\r')
          getchar();
      } while (0);
      read_ray(&line, &col, &vx, &vy, str);
      output_ray(line, col, vx, vy, str);
    }
    do {
      if (getchar() == '\r')
        getchar();
    } while (0);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   1050 <getchar@plt>
movsbl %al,%eax
mov    %eax,-0x8(%rbp)
cmp    $0x58,%eax
je     127b <main+0xfb>
mov    -0x8(%rbp),%edi
mov    0x2e39(%rip),%rax
mov    (%rax),%esi
call   1060 <ungetc@plt>
mov    $0x0,%al
call   1080 <clear_grid@plt>
mov    $0x0,%al
call   1070 <read_grid@plt>
lea    0xe35(%rip),%rdi
lea    -0xc(%rbp),%rsi
call   1090 <scanf@plt>
movl   $0x0,-0x10(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0xc(%rbp),%eax
jae    1252 <main+0xd2>
lea    -0x23(%rbp),%rdi
xor    %esi,%esi
mov    $0x3,%edx
call   1040 <memset@plt>
mov    $0x0,%al
call   1050 <getchar@plt>
movsbl %al,%eax
cmp    $0xd,%eax
jne    1211 <main+0x91>
mov    $0x0,%al
call   1050 <getchar@plt>
jmp    1216 <main+0x96>
lea    -0x23(%rbp),%r8
lea    -0x14(%rbp),%rdi
lea    -0x18(%rbp),%rsi
lea    -0x1c(%rbp),%rdx
lea    -0x20(%rbp),%rcx
call   1030 <read_ray@plt>
mov    -0x14(%rbp),%edi
mov    -0x18(%rbp),%esi
mov    -0x1c(%rbp),%edx
mov    -0x20(%rbp),%ecx
lea    -0x23(%rbp),%r8
call   10a0 <output_ray@plt>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    11db <main+0x5b>
jmp    1257 <main+0xd7>
mov    $0x0,%al
call   1050 <getchar@plt>
movsbl %al,%eax
cmp    $0xd,%eax
jne    1271 <main+0xf1>
mov    $0x0,%al
call   1050 <getchar@plt>
jmp    1276 <main+0xf6>
jmp    118f <main+0xf>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
xor    %eax,%eax
call   1040 <getchar@plt>
cmp    $0x58,%al
jne    119a <main+0x2a>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0x14(%rsp),%r12
lea    0x10(%rsp),%r13
lea    0xc(%rsp),%rbx
lea    0x8(%rsp),%rbp
mov    %rsp,%r14
jmp    11cb <main+0x5b>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
xor    %eax,%eax
call   1040 <getchar@plt>
cmp    $0x58,%al
je     1189 <main+0x19>
movsbl %al,%edi
mov    0x2e13(%rip),%rax
mov    (%rax),%esi
call   1050 <ungetc@plt>
xor    %eax,%eax
call   1070 <clear_grid@plt>
xor    %eax,%eax
call   1060 <read_grid@plt>
lea    0xe0f(%rip),%rdi
lea    0x4(%rsp),%rsi
call   1080 <scanf@plt>
cmpl   $0x0,0x4(%rsp)
je     1270 <main+0x100>
xor    %r15d,%r15d
jmp    1247 <main+0xd7>
nopw   0x0(%rax,%rax,1)
mov    %r12,%rdi
mov    %r13,%rsi
mov    %rbx,%rdx
mov    %rbp,%rcx
mov    %r14,%r8
call   1030 <read_ray@plt>
mov    0x14(%rsp),%edi
mov    0x10(%rsp),%esi
mov    0xc(%rsp),%edx
mov    0x8(%rsp),%ecx
mov    %r14,%r8
call   1090 <output_ray@plt>
add    $0x1,%r15d
cmp    0x4(%rsp),%r15d
jae    1270 <main+0x100>
movb   $0x0,0x2(%rsp)
movw   $0x0,(%rsp)
xor    %eax,%eax
call   1040 <getchar@plt>
cmp    $0xd,%al
jne    1210 <main+0xa0>
xor    %eax,%eax
call   1040 <getchar@plt>
jmp    1210 <main+0xa0>
cs nopw 0x0(%rax,%rax,1)
xor    %eax,%eax
call   1040 <getchar@plt>
cmp    $0xd,%al
jne    11c0 <main+0x50>
xor    %eax,%eax
call   1040 <getchar@plt>
jmp    11c0 <main+0x50>

LLVM IR (-O0):
@stdin = global i32 0
@.str = [3 x i8] c"%u\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca [3 x i8]
store i32 0, i32* %1
br label %10
10: ; preds = %54, %0
%11 = call signext i8 (...) @getchar()
%12 = sext i8 %11 to i32
store i32 %12, i32* %2
%13 = icmp ne i32 %12, 88
br i1 %13, label %14, label %55
14: ; preds = %10
%15 = load i32, i32* %2
%16 = load i32, i32* @stdin
%17 = call i32 @ungetc(i32 %15, i32 %16)
%18 = call i32 (...) @clear_grid()
%19 = call i32 (...) @read_grid()
%20 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %3)
store i32 0, i32* %4
br label %21
21: ; preds = %43, %14
%22 = load i32, i32* %4
%23 = load i32, i32* %3
%24 = icmp ult i32 %22, %23
br i1 %24, label %25, label %46
25: ; preds = %21
%26 = bitcast [3 x i8]* %9 to i8*
br label %27
27: ; preds = %25
%28 = call signext i8 (...) @getchar()
%29 = sext i8 %28 to i32
%30 = icmp eq i32 %29, 13
br i1 %30, label %31, label %33
31: ; preds = %27
%32 = call signext i8 (...) @getchar()
br label %33
33: ; preds = %31, %27
br label %34
34: ; preds = %33
%35 = getelementptr inbounds [3 x i8], [3 x i8]* %9, i64 0, i64 0
%36 = call i32 @read_ray(i32* %5, i32* %6, i32* %7, i32* %8, i8* %35)
%37 = load i32, i32* %5
%38 = load i32, i32* %6
%39 = load i32, i32* %7
%40 = load i32, i32* %8
%41 = getelementptr inbounds [3 x i8], [3 x i8]* %9, i64 0, i64 0
%42 = call i32 @output_ray(i32 %37, i32 %38, i32 %39, i32 %40, i8* %41)
br label %43
43: ; preds = %34
%44 = load i32, i32* %4
%45 = add i32 %44, 1
store i32 %45, i32* %4
br label %21
46: ; preds = %21
br label %47
47: ; preds = %46
%48 = call signext i8 (...) @getchar()
%49 = sext i8 %48 to i32
%50 = icmp eq i32 %49, 13
br i1 %50, label %51, label %53
51: ; preds = %47
%52 = call signext i8 (...) @getchar()
br label %53
53: ; preds = %51, %47
br label %54
54: ; preds = %53
br label %10
55: ; preds = %10
ret i32 0
}

LLVM IR (-O2):
@stdin = global i32 0
@.str = [3 x i8] c"%u\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca [3 x i8]
%7 = call signext i8 (...) @getchar()
%8 = icmp eq i8 %7, 88
br i1 %8, label %50, label %9
9: ; preds = %0
%10 = bitcast i32* %1 to i8*
%11 = bitcast i32* %2 to i8*
%12 = bitcast i32* %3 to i8*
%13 = bitcast i32* %4 to i8*
%14 = bitcast i32* %5 to i8*
%15 = getelementptr inbounds [3 x i8], [3 x i8]* %6, i64 0, i64 0
br label %16
16: ; preds = %9, %47
%17 = phi i8 [ %7, %9 ], [ %48, %47 ]
%18 = sext i8 %17 to i32
%19 = load i32, i32* @stdin
%20 = call i32 @ungetc(i32 %18, i32 %19)
%21 = call i32 (...) @clear_grid()
%22 = call i32 (...) @read_grid()
%23 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %1)
%24 = load i32, i32* %1
%25 = icmp eq i32 %24, 0
br i1 %25, label %26, label %29
26: ; preds = %35, %16
%27 = call signext i8 (...) @getchar()
%28 = icmp eq i8 %27, 13
br i1 %28, label %45, label %47
29: ; preds = %16, %35
%30 = phi i32 [ %42, %35 ], [ 0, %16 ]
%31 = call signext i8 (...) @getchar()
%32 = icmp eq i8 %31, 13
br i1 %32, label %33, label %35
33: ; preds = %29
%34 = call signext i8 (...) @getchar()
br label %35
35: ; preds = %33, %29
%36 = call i32 @read_ray(i32* %2, i32* %3, i32* %4, i32* %5, i8* %15)
%37 = load i32, i32* %2
%38 = load i32, i32* %3
%39 = load i32, i32* %4
%40 = load i32, i32* %5
%41 = call i32 @output_ray(i32 %37, i32 %38, i32 %39, i32 %40, i8* %15)
%42 = add nuw i32 %30, 1
%43 = load i32, i32* %1
%44 = icmp ult i32 %42, %43
br i1 %44, label %29, label %26
45: ; preds = %26
%46 = call signext i8 (...) @getchar()
br label %47
47: ; preds = %45, %26
%48 = call signext i8 (...) @getchar()
%49 = icmp eq i8 %48, 88
br i1 %49, label %50, label %16
50: ; preds = %47, %0
ret i32 0
}