#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*/ readchar(int, unsigned char *, int);
int spaceball_waitchar(int fd, unsigned char c, unsigned char *d, int timeout) {
  unsigned char b = 0;
  while (!readchar(fd, &b, timeout)) {
    if (b == 0x0a)
      continue;
    *d++ = b;
    if (b == c)
      break;
  }
  *d = 0;
  return -(b != c);
}


x86 assembly (clang, -O0):
<spaceball_waitchar>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %sil,%al
mov    %edi,-0x4(%rbp)
mov    %al,-0x5(%rbp)
mov    %rdx,-0x10(%rbp)
mov    %ecx,-0x14(%rbp)
movb   $0x0,-0x15(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x14(%rbp),%edx
lea    -0x15(%rbp),%rsi
call   1030 <readchar@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    1150 <spaceball_waitchar+0x40>
jmp    1190 <spaceball_waitchar+0x80>
movzbl -0x15(%rbp),%eax
cmp    $0xa,%eax
jne    1162 <spaceball_waitchar+0x52>
jmp    112c <spaceball_waitchar+0x1c>
mov    -0x15(%rbp),%cl
mov    -0x10(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x10(%rbp)
mov    %cl,(%rax)
movzbl -0x15(%rbp),%eax
movzbl -0x5(%rbp),%ecx
cmp    %ecx,%eax
jne    118b <spaceball_waitchar+0x7b>
jmp    1190 <spaceball_waitchar+0x80>
jmp    112c <spaceball_waitchar+0x1c>
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
movzbl -0x15(%rbp),%eax
movzbl -0x5(%rbp),%ecx
cmp    %ecx,%eax
setne  %al
and    $0x1,%al
movzbl %al,%ecx
xor    %eax,%eax
sub    %ecx,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<spaceball_waitchar>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %ecx,%ebp
mov    %rdx,%r15
mov    %esi,%r14d
mov    %edi,%ebx
movb   $0x0,0xf(%rsp)
lea    0xf(%rsp),%r12
mov    %ebx,%edi
mov    %r12,%rsi
mov    %ebp,%edx
call   1030 <readchar@plt>
test   %eax,%eax
jne    1155 <spaceball_waitchar+0x45>
movzbl 0xf(%rsp),%eax
cmp    $0xa,%al
je     1130 <spaceball_waitchar+0x20>
mov    %al,(%r15)
add    $0x1,%r15
cmp    %r14b,%al
jne    1130 <spaceball_waitchar+0x20>
movb   $0x0,(%r15)
xor    %eax,%eax
cmp    %r14b,0xf(%rsp)
setne  %al
neg    %eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @spaceball_waitchar(i32 %0, i8 zeroext %1, i8* %2, i32 %3) {
%5 = alloca i32
%6 = alloca i8
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i8
store i32 %0, i32* %5
store i8 %1, i8* %6
store i8* %2, i8** %7
store i32 %3, i32* %8
store i8 0, i8* %9
br label %10
10: ; preds = %31, %20, %4
%11 = load i32, i32* %5
%12 = load i32, i32* %8
%13 = call i32 @readchar(i32 %11, i8* %9, i32 %12)
%14 = icmp ne i32 %13, 0
%15 = xor i1 %14, true
br i1 %15, label %16, label %32
16: ; preds = %10
%17 = load i8, i8* %9
%18 = zext i8 %17 to i32
%19 = icmp eq i32 %18, 10
br i1 %19, label %20, label %21
20: ; preds = %16
br label %10
21: ; preds = %16
%22 = load i8, i8* %9
%23 = load i8*, i8** %7
%24 = getelementptr inbounds i8, i8* %23, i32 1
store i8* %24, i8** %7
store i8 %22, i8* %23
%25 = load i8, i8* %9
%26 = zext i8 %25 to i32
%27 = load i8, i8* %6
%28 = zext i8 %27 to i32
%29 = icmp eq i32 %26, %28
br i1 %29, label %30, label %31
30: ; preds = %21
br label %32
31: ; preds = %21
br label %10
32: ; preds = %30, %10
%33 = load i8*, i8** %7
store i8 0, i8* %33
%34 = load i8, i8* %9
%35 = zext i8 %34 to i32
%36 = load i8, i8* %6
%37 = zext i8 %36 to i32
%38 = icmp ne i32 %35, %37
%39 = zext i1 %38 to i32
%40 = sub nsw i32 0, %39
ret i32 %40
}

LLVM IR (-O2):
define i32 @spaceball_waitchar(i32 %0, i8 zeroext %1, i8* nocapture writeonly %2, i32 %3) {
%5 = alloca i8
store i8 0, i8* %5
br label %6
6: ; preds = %14, %4
%7 = phi i8* [ %15, %14 ], [ %2, %4 ]
br label %8
8: ; preds = %6, %11
%9 = call i32 @readchar(i32 %0, i8* %5, i32 %3)
%10 = icmp eq i32 %9, 0
br i1 %10, label %11, label %17
11: ; preds = %8
%12 = load i8, i8* %5
%13 = icmp eq i8 %12, 10
br i1 %13, label %8, label %14
14: ; preds = %11
%15 = getelementptr inbounds i8, i8* %7, i64 1
store i8 %12, i8* %7
%16 = icmp eq i8 %12, %1
br i1 %16, label %17, label %6
17: ; preds = %14, %8
%18 = phi i8* [ %7, %8 ], [ %15, %14 ]
store i8 0, i8* %18
%19 = load i8, i8* %5
%20 = icmp ne i8 %19, %1
%21 = sext i1 %20 to i32
ret i32 %21
}