#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 getche();
int /*<<< orphan*/ putch(int);
int reply(char *str) {
  int c, i;
  while (1) {
    c = getche() & 0xdf;
    for (i = 0; str[i] != '\0';)
      if ((char)c == str[i++]) {
        putch(13);
        putch(10);
        return i;
      }
    putch(7);
  }
}


x86 assembly (clang, -O0):
<reply>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    $0x0,%al
call   1030 <getche@plt>
and    $0xdf,%eax
mov    %eax,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x0,%eax
je     119d <reply+0x7d>
mov    -0xc(%rbp),%eax
movsbl %al,%eax
mov    -0x8(%rbp),%rcx
mov    -0x10(%rbp),%edx
mov    %edx,%esi
add    $0x1,%esi
mov    %esi,-0x10(%rbp)
movslq %edx,%rdx
movsbl (%rcx,%rdx,1),%ecx
cmp    %ecx,%eax
jne    1198 <reply+0x78>
mov    $0xd,%edi
call   1040 <putch@plt>
mov    $0xa,%edi
call   1040 <putch@plt>
mov    -0x10(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret
jmp    1142 <reply+0x22>
mov    $0x7,%edi
call   1040 <putch@plt>
jmp    112c <reply+0xc>

x86 assembly (clang, -O2):
<reply>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
xor    %eax,%eax
call   1030 <getche@plt>
movsbl %al,%eax
and    $0xffffffdf,%eax
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
movsbl (%r14,%rbx,1),%ecx
test   %ecx,%ecx
je     1160 <reply+0x40>
add    $0x1,%rbx
cmp    %ecx,%eax
jne    1140 <reply+0x20>
jmp    116c <reply+0x4c>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    $0x7,%edi
call   1040 <putch@plt>
jmp    1127 <reply+0x7>
mov    $0xd,%edi
call   1040 <putch@plt>
mov    $0xa,%edi
call   1040 <putch@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
define i32 @reply(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
store i8* %0, i8** %2
br label %5
5: ; preds = %1, %33
%6 = call i32 (...) @getche()
%7 = and i32 %6, 223
store i32 %7, i32* %3
store i32 0, i32* %4
br label %8
8: ; preds = %32, %5
%9 = load i8*, i8** %2
%10 = load i32, i32* %4
%11 = sext i32 %10 to i64
%12 = getelementptr inbounds i8, i8* %9, i64 %11
%13 = load i8, i8* %12
%14 = sext i8 %13 to i32
%15 = icmp ne i32 %14, 0
br i1 %15, label %16, label %33
16: ; preds = %8
%17 = load i32, i32* %3
%18 = trunc i32 %17 to i8
%19 = sext i8 %18 to i32
%20 = load i8*, i8** %2
%21 = load i32, i32* %4
%22 = add nsw i32 %21, 1
store i32 %22, i32* %4
%23 = sext i32 %21 to i64
%24 = getelementptr inbounds i8, i8* %20, i64 %23
%25 = load i8, i8* %24
%26 = sext i8 %25 to i32
%27 = icmp eq i32 %19, %26
br i1 %27, label %28, label %32
28: ; preds = %16
%29 = call i32 @putch(i32 13)
%30 = call i32 @putch(i32 10)
%31 = load i32, i32* %4
ret i32 %31
32: ; preds = %16
br label %8
33: ; preds = %8
%34 = call i32 @putch(i32 7)
br label %5
}

LLVM IR (-O2):
define i32 @reply(i8* nocapture %0) {
br label %2
2: ; preds = %20, %1
%3 = tail call i32 (...) @getche()
%4 = shl i32 %3, 24
%5 = ashr exact i32 %4, 24
%6 = and i32 %5, -33
br label %7
7: ; preds = %12, %2
%8 = phi i64 [ %14, %12 ], [ 0, %2 ]
%9 = getelementptr inbounds i8, i8* %0, i64 %8
%10 = load i8, i8* %9
%11 = icmp eq i8 %10, 0
br i1 %11, label %20, label %12
12: ; preds = %7
%13 = sext i8 %10 to i32
%14 = add nuw i64 %8, 1
%15 = icmp eq i32 %6, %13
br i1 %15, label %16, label %7
16: ; preds = %12
%17 = trunc i64 %14 to i32
%18 = tail call i32 @putch(i32 13)
%19 = tail call i32 @putch(i32 10)
ret i32 %17
20: ; preds = %7
%21 = tail call i32 @putch(i32 7)
br label %2
}