#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*/ STDOUT_FILENO;
int /*<<< orphan*/ chat_logf(char *, int);
int /*<<< orphan*/ fatal(int, char *);
int fcntl(int /*<<< orphan*/, int /*<<< orphan*/, int);
int /*<<< orphan*/ usleep(int);
int write(int /*<<< orphan*/, char *, int);
int put_char(int c) {
  int status;
  char ch = c;
  usleep(10000);
  status = write(STDOUT_FILENO, &ch, 1);
  switch (status) {
  case 1:
    return (0);
  default:
    chat_logf("warning: write() on stdout 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):
<put_char>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%al
mov    %al,-0xd(%rbp)
mov    $0x2710,%edi
call   1070 <usleep@plt>
mov    0x2e7e(%rip),%rax
mov    (%rax),%edi
lea    -0xd(%rbp),%rsi
mov    $0x1,%edx
call   1040 <write@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x14(%rbp)
sub    $0xffffffff,%eax
je     11c5 <put_char+0x75>
jmp    1199 <put_char+0x49>
mov    -0x14(%rbp),%eax
sub    $0x1,%eax
jne    11b6 <put_char+0x66>
jmp    11aa <put_char+0x5a>
movl   $0x0,-0x4(%rbp)
jmp    1236 <put_char+0xe6>
mov    -0xc(%rbp),%esi
lea    0xe40(%rip),%rdi
call   1050 <chat_logf@plt>
mov    0x2dfc(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
mov    %edx,%edi
call   1030 <fcntl@plt>
mov    %eax,-0xc(%rbp)
cmp    $0xffffffff,%eax
jne    11f4 <put_char+0xa4>
mov    $0x2,%edi
lea    0xe38(%rip),%rsi
call   1060 <fatal@plt>
mov    0x2ddd(%rip),%rax
mov    (%rax),%esi
mov    -0xc(%rbp),%edx
mov    0x2dc9(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    %eax,%edx
xor    %edi,%edi
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
jne    122f <put_char+0xdf>
mov    $0x2,%edi
lea    0xe24(%rip),%rsi
call   1060 <fatal@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<put_char>:
push   %rbx
sub    $0x10,%rsp
mov    %dil,0xf(%rsp)
mov    $0x2710,%edi
call   1070 <usleep@plt>
mov    0x2e85(%rip),%rax
mov    (%rax),%edi
lea    0xf(%rsp),%rsi
mov    $0x1,%edx
call   1040 <write@plt>
cmp    $0xffffffff,%eax
je     1196 <put_char+0x46>
xor    %ebx,%ebx
cmp    $0x1,%eax
je     11fa <put_char+0xaa>
lea    0xe71(%rip),%rdi
mov    %eax,%esi
call   1050 <chat_logf@plt>
mov    0x2e2b(%rip),%rax
mov    (%rax),%esi
xor    %edi,%edi
xor    %edx,%edx
call   1030 <fcntl@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
jne    11c0 <put_char+0x70>
lea    0xe71(%rip),%rsi
mov    $0x2,%edi
call   1060 <fatal@plt>
mov    0x2e11(%rip),%rax
mov    (%rax),%esi
mov    0x2e00(%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    11fa <put_char+0xaa>
lea    0xe5e(%rip),%rsi
mov    $0x2,%edi
call   1060 <fatal@plt>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@STDOUT_FILENO = global i32 0
@.str = [39 x i8] c"warning: write() on stdout 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 @put_char(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8
store i32 %0, i32* %3
%6 = load i32, i32* %3
%7 = trunc i32 %6 to i8
store i8 %7, i8* %5
%8 = call i32 @usleep(i32 10000)
%9 = load i32, i32* @STDOUT_FILENO
%10 = call i32 @write(i32 %9, i8* %5, i32 1)
store i32 %10, i32* %4
%11 = load i32, i32* %4
switch i32 %11, label %13 [
i32 1, label %12
i32 -1, label %16
]
12: ; preds = %1
store i32 0, i32* %2
br label %33
13: ; preds = %1
%14 = load i32, i32* %4
%15 = call i32 @chat_logf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0), i32 %14)
br label %16
16: ; preds = %1, %13
%17 = load i32, i32* @F_GETFL
%18 = call i32 @fcntl(i32 0, i32 %17, i32 0)
store i32 %18, i32* %4
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %22
20: ; preds = %16
%21 = call i32 @fatal(i32 2, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0))
br label %22
22: ; preds = %20, %16
%23 = load i32, i32* @F_SETFL
%24 = load i32, i32* %4
%25 = load i32, i32* @O_NDELAY
%26 = xor i32 %25, -1
%27 = and i32 %24, %26
%28 = call i32 @fcntl(i32 0, i32 %23, i32 %27)
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %32
30: ; preds = %22
%31 = call i32 @fatal(i32 2, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0))
br label %32
32: ; preds = %30, %22
store i32 -1, i32* %2
br label %33
33: ; preds = %32, %12
%34 = load i32, i32* %2
ret i32 %34
}

LLVM IR (-O2):
@STDOUT_FILENO = global i32 0
@.str = [39 x i8] c"warning: write() on stdout 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 @put_char(i32 %0) {
%2 = alloca i8
%3 = trunc i32 %0 to i8
store i8 %3, i8* %2
%4 = tail call i32 @usleep(i32 10000)
%5 = load i32, i32* @STDOUT_FILENO
%6 = call i32 @write(i32 %5, i8* %2, i32 1)
switch i32 %6, label %7 [
i32 1, label %24
i32 -1, label %9
]
7: ; preds = %1
%8 = tail call i32 @chat_logf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0), i32 %6)
br label %9
9: ; preds = %1, %7
%10 = load i32, i32* @F_GETFL
%11 = tail call i32 @fcntl(i32 0, i32 %10, i32 0)
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %15
13: ; preds = %9
%14 = tail call i32 @fatal(i32 2, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0))
br label %15
15: ; preds = %13, %9
%16 = load i32, i32* @F_SETFL
%17 = load i32, i32* @O_NDELAY
%18 = xor i32 %17, -1
%19 = and i32 %11, %18
%20 = tail call i32 @fcntl(i32 0, i32 %16, i32 %19)
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %24
22: ; preds = %15
%23 = tail call i32 @fatal(i32 2, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0))
br label %24
24: ; preds = %15, %22, %1
%25 = phi i32 [ 0, %1 ], [ -1, %22 ], [ -1, %15 ]
ret i32 %25
}