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


x86 assembly (clang, -O0):
<set_blocking>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1030 <memset@plt>
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   1040 <tcgetattr@plt>
cmp    $0x0,%rax
je     118c <set_blocking+0x4c>
lea    0xe85(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1200 <set_blocking+0xc0>
mov    -0xc(%rbp),%ecx
xor    %edx,%edx
mov    $0x1,%eax
cmp    $0x0,%ecx
cmovne %eax,%edx
mov    -0x18(%rbp),%rax
mov    0x2e39(%rip),%rcx
mov    (%rcx),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x18(%rbp),%rax
mov    0x2e18(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x5,(%rax,%rcx,4)
mov    -0x8(%rbp),%edi
mov    0x2e1c(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1050 <tcsetattr@plt>
cmp    $0x0,%rax
je     11f9 <set_blocking+0xb9>
lea    0xe2d(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1200 <set_blocking+0xc0>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_blocking>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%r15d
mov    %edi,%ebp
mov    %rsp,%rbx
xor    %r14d,%r14d
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1030 <memset@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
call   1040 <tcgetattr@plt>
test   %rax,%rax
jne    11c0 <set_blocking+0x80>
xor    %eax,%eax
test   %r15d,%r15d
setne  %al
mov    (%rsp),%rcx
mov    0x2e5d(%rip),%rdx
mov    (%rdx),%rdx
mov    %eax,(%rcx,%rdx,4)
mov    0x2e40(%rip),%rax
mov    (%rax),%rax
movl   $0x5,(%rcx,%rax,4)
mov    0x2e47(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebp,%edi
call   1050 <tcsetattr@plt>
test   %rax,%rax
jne    11c9 <set_blocking+0x89>
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe39(%rip),%rdi
jmp    11d0 <set_blocking+0x90>
lea    0xe45(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%r14d
jmp    11b2 <set_blocking+0x72>

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

LLVM IR (-O2):
%struct.termios = type { i32* }
@.str = [21 x i8] c"error from tggetattr\00"
@VMIN = global i64 0
@VTIME = global i64 0
@TCSANOW = global i32 0
@.str.1 = [30 x i8] c"error setting term attributes\00"
define i32 @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 %23, label %20
20: ; preds = %8, %2
%21 = phi i8* [ getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), %2 ], [ getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), %8 ]
%22 = call i32 @perror(i8* %21)
br label %23
23: ; preds = %20, %8
%24 = phi i32 [ 0, %8 ], [ -1, %20 ]
ret i32 %24
}