#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*/ STDIN_FILENO;
int read(int /*<<< orphan*/, char *, int);
void ttyresp(char *buf, int bufsz) {
  for (int i = 0; i < bufsz; i++) {
    if (read(STDIN_FILENO, buf + i, 1) != 1)
      break;
    if (buf[i] == 'R')
      break;
  }
}


x86 assembly (clang, -O0):
<ttyresp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    118b <ttyresp+0x7b>
mov    0x2ea7(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rsi
movslq -0x10(%rbp),%rax
add    %rax,%rsi
mov    $0x1,%edx
call   1030 <read@plt>
cmp    $0x1,%eax
je     115e <ttyresp+0x4e>
jmp    118b <ttyresp+0x7b>
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x52,%eax
jne    1178 <ttyresp+0x68>
jmp    118b <ttyresp+0x7b>
jmp    117d <ttyresp+0x6d>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    1126 <ttyresp+0x16>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ttyresp>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
test   %esi,%esi
jle    115b <ttyresp+0x4b>
mov    %rdi,%rbx
mov    %esi,%r14d
mov    $0x1,%r12d
mov    0x2eb1(%rip),%r15
nop
mov    (%r15),%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1030 <read@plt>
cmp    $0x1,%eax
jne    115b <ttyresp+0x4b>
cmpb   $0x52,(%rbx)
je     115b <ttyresp+0x4b>
add    $0x1,%rbx
lea    0x1(%r12),%rax
cmp    %r14,%r12
mov    %rax,%r12
jb     1130 <ttyresp+0x20>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
@STDIN_FILENO = global i32 0
define void @ttyresp(i8* nocapture %0, i32 %1) {
%3 = icmp sgt i32 %1, 0
br i1 %3, label %4, label %18
4: ; preds = %2
%5 = zext i32 %1 to i64
br label %6
6: ; preds = %12, %4
%7 = phi i64 [ %15, %12 ], [ 0, %4 ]
%8 = load i32, i32* @STDIN_FILENO
%9 = getelementptr inbounds i8, i8* %0, i64 %7
%10 = tail call i32 @read(i32 %8, i8* %9, i32 1)
%11 = icmp eq i32 %10, 1
br i1 %11, label %12, label %18
12: ; preds = %6
%13 = load i8, i8* %9
%14 = icmp ne i8 %13, 82
%15 = add nuw nsw i64 %7, 1
%16 = icmp ult i64 %15, %5
%17 = select i1 %14, i1 %16, i1 false
br i1 %17, label %6, label %18
18: ; preds = %6, %12, %2
ret void
}