#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*/ F_GETFL;
int /*<<< orphan*/ F_SETFL;
int O_NDELAY;
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ chat_logf(char *, int);
int /*<<< orphan*/ fatal(int, char *);
int fcntl(int /*<<< orphan*/, int /*<<< orphan*/, int);
int read(int /*<<< orphan*/, char *, int);
int get_char(void) {
  int status;
  char c;
  status = read(STDIN_FILENO, &c, 1);
  switch (status) {
  case 1:
    return ((int)c & 0x7F);
  default:
    chat_logf("warning: read() on stdin returned %d", status);
  case -1:
    if ((status = fcntl(0, F_GETFL, 0)) == -1)
      fatal(2, "Can't get file mode flags on stdin: %m");
    if (fcntl(0, F_SETFL, status & ~O_NDELAY) == -1)
      fatal(2, "Can't set file mode flags on stdin: %m");
    return (-1);
  }
}


x86 assembly (clang, -O0):
<get_char>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    0x2e79(%rip),%rax
mov    (%rax),%edi
lea    -0x9(%rbp),%rsi
mov    $0x1,%edx
call   1040 <read@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x10(%rbp)
sub    $0xffffffff,%eax
je     11a5 <get_char+0x65>
jmp    1176 <get_char+0x36>
mov    -0x10(%rbp),%eax
sub    $0x1,%eax
jne    1196 <get_char+0x56>
jmp    1187 <get_char+0x47>
movsbl -0x9(%rbp),%eax
and    $0x7f,%eax
mov    %eax,-0x4(%rbp)
jmp    1216 <get_char+0xd6>
mov    -0x8(%rbp),%esi
lea    0xe60(%rip),%rdi
call   1050 <chat_logf@plt>
mov    0x2e24(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
mov    %edx,%edi
call   1030 <fcntl@plt>
mov    %eax,-0x8(%rbp)
cmp    $0xffffffff,%eax
jne    11d4 <get_char+0x94>
mov    $0x2,%edi
lea    0xe56(%rip),%rsi
call   1060 <fatal@plt>
mov    0x2e05(%rip),%rax
mov    (%rax),%esi
mov    -0x8(%rbp),%edx
mov    0x2df1(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    %eax,%edx
xor    %edi,%edi
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
jne    120f <get_char+0xcf>
mov    $0x2,%edi
lea    0xe42(%rip),%rsi
call   1060 <fatal@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_char>:
push   %rbx
sub    $0x10,%rsp
mov    0x2e7c(%rip),%rax
mov    (%rax),%edi
lea    0xf(%rsp),%rsi
mov    $0x1,%edx
call   1040 <read@plt>
cmp    $0xffffffff,%eax
je     117f <get_char+0x3f>
cmp    $0x1,%eax
jne    1171 <get_char+0x31>
movzbl 0xf(%rsp),%ebx
and    $0x7f,%ebx
jmp    11e3 <get_char+0xa3>
lea    0xe88(%rip),%rdi
mov    %eax,%esi
call   1050 <chat_logf@plt>
mov    0x2e4a(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
xor    %edx,%edx
call   1030 <fcntl@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
jne    11a9 <get_char+0x69>
lea    0xe86(%rip),%rsi
mov    $0x2,%edi
call   1060 <fatal@plt>
mov    0x2e30(%rip),%rax
mov    (%rax),%esi
mov    0x2e1f(%rip),%rax
mov    (%rax),%eax
not    %eax
and    %eax,%ebx
xor    %edi,%edi
mov    %ebx,%edx
call   1030 <fcntl@plt>
mov    $0xffffffff,%ebx
cmp    $0xffffffff,%eax
jne    11e3 <get_char+0xa3>
lea    0xe73(%rip),%rsi
mov    $0x2,%edi
call   1060 <fatal@plt>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@STDIN_FILENO = global i32 0
@.str = [37 x i8] c"warning: read() on stdin returned %d\00"
@F_GETFL = global i32 0
@.str.1 = [39 x i8] c"Can't get file mode flags on stdin: %m\00"
@F_SETFL = global i32 0
@O_NDELAY = global i32 0
@.str.2 = [39 x i8] c"Can't set file mode flags on stdin: %m\00"
define i32 @get_char() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i8
%4 = load i32, i32* @STDIN_FILENO
%5 = call i32 @read(i32 %4, i8* %3, i32 1)
store i32 %5, i32* %2
%6 = load i32, i32* %2
switch i32 %6, label %11 [
i32 1, label %7
i32 -1, label %14
]
7: ; preds = %0
%8 = load i8, i8* %3
%9 = sext i8 %8 to i32
%10 = and i32 %9, 127
store i32 %10, i32* %1
br label %31
11: ; preds = %0
%12 = load i32, i32* %2
%13 = call i32 @chat_logf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i32 %12)
br label %14
14: ; preds = %0, %11
%15 = load i32, i32* @F_GETFL
%16 = call i32 @fcntl(i32 0, i32 %15, i32 0)
store i32 %16, i32* %2
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %20
18: ; preds = %14
%19 = call i32 @fatal(i32 2, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0))
br label %20
20: ; preds = %18, %14
%21 = load i32, i32* @F_SETFL
%22 = load i32, i32* %2
%23 = load i32, i32* @O_NDELAY
%24 = xor i32 %23, -1
%25 = and i32 %22, %24
%26 = call i32 @fcntl(i32 0, i32 %21, i32 %25)
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %30
28: ; preds = %20
%29 = call i32 @fatal(i32 2, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0))
br label %30
30: ; preds = %28, %20
store i32 -1, i32* %1
br label %31
31: ; preds = %30, %7
%32 = load i32, i32* %1
ret i32 %32
}

LLVM IR (-O2):
@STDIN_FILENO = global i32 0
@.str = [37 x i8] c"warning: read() on stdin returned %d\00"
@F_GETFL = global i32 0
@.str.1 = [39 x i8] c"Can't get file mode flags on stdin: %m\00"
@F_SETFL = global i32 0
@O_NDELAY = global i32 0
@.str.2 = [39 x i8] c"Can't set file mode flags on stdin: %m\00"
define i32 @get_char() {
%1 = alloca i8
%2 = load i32, i32* @STDIN_FILENO
%3 = call i32 @read(i32 %2, i8* %1, i32 1)
switch i32 %3, label %8 [
i32 1, label %4
i32 -1, label %10
]
4: ; preds = %0
%5 = load i8, i8* %1
%6 = and i8 %5, 127
%7 = zext i8 %6 to i32
br label %25
8: ; preds = %0
%9 = tail call i32 @chat_logf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i32 %3)
br label %10
10: ; preds = %0, %8
%11 = load i32, i32* @F_GETFL
%12 = tail call i32 @fcntl(i32 0, i32 %11, i32 0)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %16
14: ; preds = %10
%15 = tail call i32 @fatal(i32 2, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0))
br label %16
16: ; preds = %14, %10
%17 = load i32, i32* @F_SETFL
%18 = load i32, i32* @O_NDELAY
%19 = xor i32 %18, -1
%20 = and i32 %12, %19
%21 = tail call i32 @fcntl(i32 0, i32 %17, i32 %20)
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %25
23: ; preds = %16
%24 = tail call i32 @fatal(i32 2, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0))
br label %25
25: ; preds = %16, %23, %4
%26 = phi i32 [ %7, %4 ], [ -1, %23 ], [ -1, %16 ]
ret i32 %26
}