#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 /*<<< orphan*/ errno;
int /*<<< orphan*/ memset(struct termios *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, char *);
char *strerror(int /*<<< orphan*/);
scalar_t__ tcgetattr(int, struct termios *);
scalar_t__ tcsetattr(int, int /*<<< orphan*/, struct termios *);
void set_blocking(int fd, int should_block) {
  struct termios tty;
  memset(&tty, 0, sizeof tty);
  if (tcgetattr(fd, &tty) != 0) {
    printf("error %s from tggetattr", strerror(errno));
    return;
  }
  tty.c_cc[VMIN] = should_block ? 1 : 0;
  tty.c_cc[VTIME] = 5;
  if (tcsetattr(fd, TCSANOW, &tty) != 0)
    printf("error %s setting term attributes", strerror(errno));
}


x86 assembly (clang, -O0):
<set_blocking>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
lea    -0x10(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
mov    -0x4(%rbp),%edi
lea    -0x10(%rbp),%rsi
call   1050 <tcgetattr@plt>
cmp    $0x0,%rax
je     11a6 <set_blocking+0x56>
mov    0x2e45(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    %rax,%rsi
lea    0xe64(%rip),%rdi
call   1030 <printf@plt>
jmp    1218 <set_blocking+0xc8>
mov    -0x8(%rbp),%ecx
xor    %edx,%edx
mov    $0x1,%eax
cmp    $0x0,%ecx
cmovne %eax,%edx
mov    -0x10(%rbp),%rax
mov    0x2e1f(%rip),%rcx
mov    (%rcx),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x10(%rbp),%rax
mov    0x2df6(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x5,(%rax,%rcx,4)
mov    -0x4(%rbp),%edi
mov    0x2e02(%rip),%rax
mov    (%rax),%esi
lea    -0x10(%rbp),%rdx
call   1060 <tcsetattr@plt>
cmp    $0x0,%rax
je     1218 <set_blocking+0xc8>
mov    0x2dce(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    %rax,%rsi
lea    0xe05(%rip),%rdi
call   1030 <printf@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_blocking>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %esi,%r14d
mov    %edi,%ebx
lea    0x8(%rsp),%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
call   1050 <tcgetattr@plt>
test   %rax,%rax
je     1189 <set_blocking+0x39>
lea    0xe79(%rip),%rbx
jmp    11d5 <set_blocking+0x85>
xor    %eax,%eax
test   %r14d,%r14d
setne  %al
mov    0x8(%rsp),%rcx
mov    0x2e43(%rip),%rdx
mov    (%rdx),%rdx
mov    %eax,(%rcx,%rdx,4)
mov    0x2e1e(%rip),%rax
mov    (%rax),%rax
movl   $0x5,(%rcx,%rax,4)
mov    0x2e2d(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebx,%edi
call   1060 <tcsetattr@plt>
test   %rax,%rax
je     11ee <set_blocking+0x9e>
lea    0xe43(%rip),%rbx
mov    0x2df4(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    %rbx,%rdi
mov    %rax,%rsi
call   1030 <printf@plt>
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

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

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