#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;
  int /*<<< orphan*/ member_0;
};

/* Variables and functions */
int /*<<< orphan*/ TCSANOW;
size_t VMIN;
size_t VTIME;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
scalar_t__ tcgetattr(int, struct termios *);
scalar_t__ tcsetattr(int, int /*<<< orphan*/, struct termios *);
void set_mincount(int fd, int mcount) {
  struct termios tty = {0};
  if (tcgetattr(fd, &tty) < 0) {
    fprintf(stderr, "Error tcgetattr: %s\n", strerror(errno));
  } else {
    tty.c_cc[VMIN] = mcount ? 1 : 0;
    tty.c_cc[VTIME] = 5;
    if (tcsetattr(fd, TCSANOW, &tty) < 0) {
      fprintf(stderr, "Error tcsetattr: %s\n", strerror(errno));
    }
  }
}


x86 assembly (clang, -O0):
<set_mincount>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1030 <memset@plt>
mov    -0x4(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   1050 <tcgetattr@plt>
cmp    $0x0,%rax
jge    11b5 <set_mincount+0x65>
mov    0x2e45(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2e31(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x1c(%rbp),%edi
mov    %rax,%rdx
lea    0xe55(%rip),%rsi
call   1040 <fprintf@plt>
jmp    123b <set_mincount+0xeb>
mov    -0x8(%rbp),%ecx
xor    %edx,%edx
mov    $0x1,%eax
cmp    $0x0,%ecx
cmovne %eax,%edx
mov    -0x18(%rbp),%rax
mov    0x2e10(%rip),%rcx
mov    (%rcx),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x18(%rbp),%rax
mov    0x2ddf(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x5,(%rax,%rcx,4)
mov    -0x4(%rbp),%edi
mov    0x2df3(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1060 <tcsetattr@plt>
cmp    $0x0,%rax
jge    1236 <set_mincount+0xe6>
mov    0x2dbf(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2dab(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x20(%rbp),%edi
mov    %rax,%rdx
lea    0xde4(%rip),%rsi
call   1040 <fprintf@plt>
jmp    123b <set_mincount+0xeb>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_mincount>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    %esi,%ebp
mov    %edi,%ebx
xorps  %xmm0,%xmm0
movaps %xmm0,(%rsp)
mov    %rsp,%rsi
call   1040 <tcgetattr@plt>
test   %rax,%rax
js     11a8 <set_mincount+0x68>
xor    %eax,%eax
test   %ebp,%ebp
setne  %al
mov    (%rsp),%rcx
mov    0x2e70(%rip),%rdx
mov    (%rdx),%rdx
mov    %eax,(%rcx,%rdx,4)
mov    0x2e43(%rip),%rax
mov    (%rax),%rax
movl   $0x5,(%rcx,%rax,4)
mov    0x2e5a(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebx,%edi
call   1050 <tcsetattr@plt>
test   %rax,%rax
jns    11d3 <set_mincount+0x93>
lea    0xe6f(%rip),%rbx
jmp    11af <set_mincount+0x6f>
lea    0xe51(%rip),%rbx
mov    0x2e1a(%rip),%rax
mov    (%rax),%ebp
mov    0x2e09(%rip),%rax
mov    (%rax),%edi
call   1060 <strerror@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %rax,%rdx
call   1030 <fprintf@plt>
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret

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

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