#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 ERR;
int /*<<< orphan*/ event_loopexit(int /*<<< orphan*/ *);
int getch();
int /*<<< orphan*/ input_handlekey(int, void *);
void stdin_cb(int fd, short which, void *arg) {
  int ch, chs = 0;
  while ((ch = getch()) != ERR) {
    chs++;
    input_handlekey(ch, arg);
  }
  if (chs == 0)
    event_loopexit(NULL);
}


x86 assembly (clang, -O0):
<stdin_cb>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %si,%ax
mov    %edi,-0x4(%rbp)
mov    %ax,-0x6(%rbp)
mov    %rdx,-0x10(%rbp)
movl   $0x0,-0x18(%rbp)
mov    $0x0,%al
call   1030 <getch@plt>
mov    %eax,-0x14(%rbp)
mov    0x2e8a(%rip),%rcx
cmp    (%rcx),%eax
je     1180 <stdin_cb+0x50>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1050 <input_handlekey@plt>
jmp    114d <stdin_cb+0x1d>
cmpl   $0x0,-0x18(%rbp)
jne    1193 <stdin_cb+0x63>
xor    %eax,%eax
mov    %eax,%edi
call   1040 <event_loopexit@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<stdin_cb>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%rbx
xor    %eax,%eax
call   1030 <getch@plt>
mov    0x2ea0(%rip),%rbp
mov    0x0(%rbp),%r14d
cmp    %r14d,%eax
je     117b <stdin_cb+0x4b>
mov    %eax,%r15d
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %eax,%edi
mov    %rbx,%rsi
call   1050 <input_handlekey@plt>
xor    %eax,%eax
call   1030 <getch@plt>
cmp    0x0(%rbp),%eax
jne    1160 <stdin_cb+0x30>
cmp    %r14d,%r15d
jne    118c <stdin_cb+0x5c>
xor    %edi,%edi
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <event_loopexit@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@ERR = global i32 0
define void @stdin_cb(i32 %0, i16 signext %1, i8* %2) {
%4 = alloca i32
%5 = alloca i16
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %4
store i16 %1, i16* %5
store i8* %2, i8** %6
store i32 0, i32* %8
br label %9
9: ; preds = %13, %3
%10 = call i32 (...) @getch()
store i32 %10, i32* %7
%11 = load i32, i32* @ERR
%12 = icmp ne i32 %10, %11
br i1 %12, label %13, label %19
13: ; preds = %9
%14 = load i32, i32* %8
%15 = add nsw i32 %14, 1
store i32 %15, i32* %8
%16 = load i32, i32* %7
%17 = load i8*, i8** %6
%18 = call i32 @input_handlekey(i32 %16, i8* %17)
br label %9
19: ; preds = %9
%20 = load i32, i32* %8
%21 = icmp eq i32 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %19
%23 = call i32 @event_loopexit(i32* null)
br label %24
24: ; preds = %22, %19
ret void
}

LLVM IR (-O2):
@ERR = global i32 0
define void @stdin_cb(i32 %0, i16 signext %1, i8* %2) {
%4 = tail call i32 (...) @getch()
%5 = load i32, i32* @ERR
%6 = icmp eq i32 %4, %5
br i1 %6, label %14, label %7
7: ; preds = %3, %7
%8 = phi i32 [ %10, %7 ], [ %4, %3 ]
%9 = tail call i32 @input_handlekey(i32 %8, i8* %2)
%10 = tail call i32 (...) @getch()
%11 = load i32, i32* @ERR
%12 = icmp eq i32 %10, %11
br i1 %12, label %13, label %7
13: ; preds = %7
br i1 %6, label %14, label %16
14: ; preds = %3, %13
%15 = tail call i32 @event_loopexit(i32* null)
br label %16
16: ; preds = %14, %13
ret void
}