#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 */
struct termios {
  int *c_cc;
};

/* Variables and functions */
int /*<<< orphan*/ TCSANOW;
size_t VMIN;
size_t VTIME;
int errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ memset(struct termios *, int /*<<< orphan*/, int);
int /*<<< orphan*/ stderr;
scalar_t__ tcgetattr(int, struct termios *);
scalar_t__ tcsetattr(int, int /*<<< orphan*/, struct termios *);
void set_blocking(int fd, int should_block, unsigned timeout) {
  struct termios tty;
  memset(&tty, 0, sizeof tty);
  if (tcgetattr(fd, &tty) != 0) {
    fprintf(stderr, "error %d from tggetattr", errno);
    return;
  }
  tty.c_cc[VMIN] = should_block ? 1 : 0;
  tty.c_cc[VTIME] = timeout;
  if (tcsetattr(fd, TCSANOW, &tty) != 0)
    fprintf(stderr, "error %d setting term attributes", errno);
}


x86 assembly (clang, -O0):
<set_blocking>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1030 <memset@plt>
mov    -0x4(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   1050 <tcgetattr@plt>
cmp    $0x0,%rax
je     119a <set_blocking+0x5a>
mov    0x2e52(%rip),%rax
mov    (%rax),%edi
mov    0x2e41(%rip),%rax
mov    (%rax),%edx
lea    0xe70(%rip),%rsi
call   1040 <fprintf@plt>
jmp    120c <set_blocking+0xcc>
mov    -0x8(%rbp),%ecx
xor    %edx,%edx
mov    $0x1,%eax
cmp    $0x0,%ecx
cmovne %eax,%edx
mov    -0x18(%rbp),%rax
mov    0x2e2b(%rip),%rcx
mov    (%rcx),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0xc(%rbp),%edx
mov    -0x18(%rbp),%rax
mov    0x2df7(%rip),%rcx
mov    (%rcx),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x4(%rbp),%edi
mov    0x2e0f(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1060 <tcsetattr@plt>
cmp    $0x0,%rax
je     120c <set_blocking+0xcc>
mov    0x2ddb(%rip),%rax
mov    (%rax),%edi
mov    0x2dca(%rip),%rax
mov    (%rax),%edx
lea    0xe11(%rip),%rsi
call   1040 <fprintf@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_blocking>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%r14d
mov    %esi,%r15d
mov    %edi,%ebx
mov    %rsp,%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1030 <memset@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
call   1050 <tcgetattr@plt>
test   %rax,%rax
je     1179 <set_blocking+0x39>
lea    0xe89(%rip),%rsi
jmp    11bf <set_blocking+0x7f>
xor    %eax,%eax
test   %r15d,%r15d
setne  %al
mov    (%rsp),%rcx
mov    0x2e54(%rip),%rdx
mov    (%rdx),%rdx
mov    %eax,(%rcx,%rdx,4)
mov    0x2e27(%rip),%rax
mov    (%rax),%rax
mov    %r14d,(%rcx,%rax,4)
mov    0x2e41(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebx,%edi
call   1060 <tcsetattr@plt>
test   %rax,%rax
je     11d6 <set_blocking+0x96>
lea    0xe59(%rip),%rsi
mov    0x2e0a(%rip),%rax
mov    (%rax),%edi
mov    0x2df9(%rip),%rax
mov    (%rax),%edx
call   1040 <fprintf@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.termios = type { i32* }
@stderr = global i32 0
@.str = [24 x i8] c"error %d from tggetattr\00"
@errno = global i32 0
@VMIN = global i64 0
@VTIME = global i64 0
@TCSANOW = global i32 0
@.str.1 = [33 x i8] c"error %d setting term attributes\00"
define void @set_blocking(i32 %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.termios
store i32 %0, i32* %4
store i32 %1, i32* %5
store i32 %2, i32* %6
%8 = call i32 @memset(%struct.termios* %7, i32 0, i32 8)
%9 = load i32, i32* %4
%10 = call i64 @tcgetattr(i32 %9, %struct.termios* %7)
%11 = icmp ne i64 %10, 0
br i1 %11, label %12, label %16
12: ; preds = %3
%13 = load i32, i32* @stderr
%14 = load i32, i32* @errno
%15 = call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i32 %14)
br label %38
16: ; preds = %3
%17 = load i32, i32* %5
%18 = icmp ne i32 %17, 0
%19 = zext i1 %18 to i64
%20 = select i1 %18, i32 1, i32 0
%21 = getelementptr inbounds %struct.termios, %struct.termios* %7, i32 0, i32 0
%22 = load i32*, i32** %21
%23 = load i64, i64* @VMIN
%24 = getelementptr inbounds i32, i32* %22, i64 %23
store i32 %20, i32* %24
%25 = load i32, i32* %6
%26 = getelementptr inbounds %struct.termios, %struct.termios* %7, i32 0, i32 0
%27 = load i32*, i32** %26
%28 = load i64, i64* @VTIME
%29 = getelementptr inbounds i32, i32* %27, i64 %28
store i32 %25, i32* %29
%30 = load i32, i32* %4
%31 = load i32, i32* @TCSANOW
%32 = call i64 @tcsetattr(i32 %30, i32 %31, %struct.termios* %7)
%33 = icmp ne i64 %32, 0
br i1 %33, label %34, label %38
34: ; preds = %16
%35 = load i32, i32* @stderr
%36 = load i32, i32* @errno
%37 = call i32 @fprintf(i32 %35, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i32 %36)
br label %38
38: ; preds = %12, %34, %16
ret void
}

LLVM IR (-O2):
%struct.termios = type { i32* }
@stderr = global i32 0
@.str = [24 x i8] c"error %d from tggetattr\00"
@errno = global i32 0
@VMIN = global i64 0
@VTIME = global i64 0
@TCSANOW = global i32 0
@.str.1 = [33 x i8] c"error %d setting term attributes\00"
define void @set_blocking(i32 %0, i32 %1, i32 %2) {
%4 = alloca %struct.termios
%5 = bitcast %struct.termios* %4 to i8*
%6 = call i32 @memset(%struct.termios* %4, i32 0, i32 8)
%7 = call i64 @tcgetattr(i32 %0, %struct.termios* %4)
%8 = icmp eq i64 %7, 0
br i1 %8, label %9, label %21
9: ; preds = %3
%10 = icmp ne i32 %1, 0
%11 = zext i1 %10 to i32
%12 = getelementptr inbounds %struct.termios, %struct.termios* %4, i64 0, i32 0
%13 = load i32*, i32** %12
%14 = load i64, i64* @VMIN
%15 = getelementptr inbounds i32, i32* %13, i64 %14
store i32 %11, i32* %15
%16 = load i64, i64* @VTIME
%17 = getelementptr inbounds i32, i32* %13, i64 %16
store i32 %2, i32* %17
%18 = load i32, i32* @TCSANOW
%19 = call i64 @tcsetattr(i32 %0, i32 %18, %struct.termios* %4)
%20 = icmp eq i64 %19, 0
br i1 %20, label %26, label %21
21: ; preds = %9, %3
%22 = phi i8* [ getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), %3 ], [ getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), %9 ]
%23 = load i32, i32* @stderr
%24 = load i32, i32* @errno
%25 = call i32 @fprintf(i32 %23, i8* %22, i32 %24)
br label %26
26: ; preds = %21, %9
ret void
}