#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;
};
typedef scalar_t__ int32_t;

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


x86 assembly (clang, -O0):
<serial_set_blocking>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1030 <memset@plt>
mov    -0x8(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1040 <tcgetattr@plt>
cmp    $0x0,%rax
je     1188 <serial_set_blocking+0x48>
lea    0xe82(%rip),%rdi
call   1060 <perror@plt>
jmp    11ec <serial_set_blocking+0xac>
mov    -0x10(%rbp),%rcx
xor    %edx,%edx
mov    $0x1,%eax
cmp    $0x0,%rcx
cmovne %eax,%edx
mov    -0x18(%rbp),%rax
mov    0x2e3b(%rip),%rcx
mov    (%rcx),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x18(%rbp),%rax
mov    0x2e1a(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x5,(%rax,%rcx,4)
mov    -0x8(%rbp),%rdi
mov    0x2e1d(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1050 <tcsetattr@plt>
cmp    $0x0,%rax
je     11ec <serial_set_blocking+0xac>
lea    0xe31(%rip),%rdi
call   1060 <perror@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<serial_set_blocking>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
lea    0x8(%rsp),%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1030 <memset@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
call   1040 <tcgetattr@plt>
test   %rax,%rax
jne    11c3 <serial_set_blocking+0x83>
xor    %eax,%eax
test   %r14,%r14
setne  %al
mov    0x8(%rsp),%rcx
mov    0x2e59(%rip),%rdx
mov    (%rdx),%rdx
mov    %eax,(%rcx,%rdx,4)
mov    0x2e3c(%rip),%rax
mov    (%rax),%rax
movl   $0x5,(%rcx,%rax,4)
mov    0x2e43(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %rbx,%rdi
call   1050 <tcsetattr@plt>
test   %rax,%rax
jne    11cc <serial_set_blocking+0x8c>
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xe36(%rip),%rdi
jmp    11d3 <serial_set_blocking+0x93>
lea    0xe45(%rip),%rdi
call   1060 <perror@plt>
jmp    11b9 <serial_set_blocking+0x79>

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

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