#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 float cc_t;

/* Variables and functions */
int /*<<< orphan*/ TCSANOW;
size_t VMIN;
size_t VTIME;
int /*<<< orphan*/ assert(int);
int errno;
int /*<<< orphan*/ grf_logging_log(int, char *, int, ...);
float strerror(int);
scalar_t__ tcdrain(int);
scalar_t__ tcgetattr(int, struct termios *);
scalar_t__ tcsetattr(int, int /*<<< orphan*/, struct termios *);
int grf_uart_set_timeout(int fd, cc_t timeout) {
  assert(fd >= 0);
  struct termios tty_attr;
  grf_logging_log(2, ("Setting timeout of %d to %.1f seconds..."), fd,
                  timeout / 10.0f);
  if (tcgetattr(fd, &tty_attr)) {
    grf_logging_log(0, ("Getting TTY attributes of %d failed: %s"), fd,
                    strerror(errno));
    return errno;
  }
  if (timeout == 0)
    tty_attr.c_cc[VMIN] = 1;
  else
    tty_attr.c_cc[VMIN] = 0;
  tty_attr.c_cc[VTIME] = timeout;
  grf_logging_log(3, ("  vmin  = %u..."), tty_attr.c_cc[VMIN]);
  grf_logging_log(3, ("  vtime = %u..."), tty_attr.c_cc[VTIME]);
  if (tcsetattr(fd, TCSANOW, &tty_attr)) {
    grf_logging_log(0, ("Setting timeout of %d to %u failed: %s"), fd, timeout,
                    strerror(errno));
    return errno;
  }
  if (tcdrain(fd)) {
    grf_logging_log(0, ("Draining data of TTY %d failed: %s"), fd,
                    strerror(errno));
    return errno;
  }
  return 0;
}


x86 assembly (clang, -O0):
<grf_uart_set_timeout>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
movss  %xmm0,-0xc(%rbp)
cmpl   $0x0,-0x8(%rbp)
setge  %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
mov    -0x8(%rbp),%edx
movss  -0xc(%rbp),%xmm0
movss  0xe6f(%rip),%xmm1
divss  %xmm1,%xmm0
cvtss2sd %xmm0,%xmm0
mov    $0x2,%edi
lea    0xe5f(%rip),%rsi
mov    $0x1,%al
call   1060 <grf_logging_log@plt>
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   1050 <tcgetattr@plt>
cmp    $0x0,%rax
je     11fe <grf_uart_set_timeout+0x9e>
mov    -0x8(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2e01(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x1c(%rbp),%edx
cvtss2sd %xmm0,%xmm0
xor    %edi,%edi
lea    0xe47(%rip),%rsi
mov    $0x1,%al
call   1060 <grf_logging_log@plt>
mov    0x2ddc(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1368 <grf_uart_set_timeout+0x208>
movss  -0xc(%rbp),%xmm0
xorps  %xmm1,%xmm1
ucomiss %xmm1,%xmm0
jne    122f <grf_uart_set_timeout+0xcf>
jp     122f <grf_uart_set_timeout+0xcf>
mov    -0x18(%rbp),%rax
mov    0x2dc0(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x1,(%rax,%rcx,4)
jmp    1244 <grf_uart_set_timeout+0xe4>
mov    -0x18(%rbp),%rax
mov    0x2da6(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x0,(%rax,%rcx,4)
cvttss2si -0xc(%rbp),%edx
mov    -0x18(%rbp),%rax
mov    0x2d74(%rip),%rcx
mov    (%rcx),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x18(%rbp),%rax
mov    0x2d7b(%rip),%rcx
mov    (%rcx),%rcx
mov    (%rax,%rcx,4),%edx
mov    $0x3,%edi
lea    0xdde(%rip),%rsi
mov    $0x0,%al
call   1060 <grf_logging_log@plt>
mov    -0x18(%rbp),%rax
mov    0x2d3f(%rip),%rcx
mov    (%rcx),%rcx
mov    (%rax,%rcx,4),%edx
mov    $0x3,%edi
lea    0xdca(%rip),%rsi
mov    $0x0,%al
call   1060 <grf_logging_log@plt>
mov    -0x8(%rbp),%edi
mov    0x2d3c(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1070 <tcsetattr@plt>
cmp    $0x0,%rax
je     1313 <grf_uart_set_timeout+0x1b3>
mov    -0x8(%rbp),%eax
mov    %eax,-0x2c(%rbp)
movss  -0xc(%rbp),%xmm0
cvtss2sd %xmm0,%xmm0
movsd  %xmm0,-0x28(%rbp)
mov    0x2cf4(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x2c(%rbp),%edx
movaps %xmm0,%xmm1
movsd  -0x28(%rbp),%xmm0
cvtss2sd %xmm1,%xmm1
xor    %edi,%edi
lea    0xd7a(%rip),%rsi
mov    $0x2,%al
call   1060 <grf_logging_log@plt>
mov    0x2cc7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1368 <grf_uart_set_timeout+0x208>
mov    -0x8(%rbp),%edi
call   1030 <tcdrain@plt>
cmp    $0x0,%rax
je     1361 <grf_uart_set_timeout+0x201>
mov    -0x8(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    0x2c9e(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x30(%rbp),%edx
cvtss2sd %xmm0,%xmm0
xor    %edi,%edi
lea    0xd53(%rip),%rsi
mov    $0x1,%al
call   1060 <grf_logging_log@plt>
mov    0x2c79(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1368 <grf_uart_set_timeout+0x208>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<grf_uart_set_timeout>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
movss  %xmm0,(%rsp)
mov    %edi,%ebx
not    %edi
shr    $0x1f,%edi
call   1040 <assert@plt>
movss  (%rsp),%xmm0
divss  0xe7a(%rip),%xmm0
cvtss2sd %xmm0,%xmm0
lea    0xe73(%rip),%rsi
mov    $0x2,%edi
mov    %ebx,%edx
mov    $0x1,%al
call   1060 <grf_logging_log@plt>
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
call   1050 <tcgetattr@plt>
test   %rax,%rax
je     11da <grf_uart_set_timeout+0x7a>
mov    0x2e19(%rip),%rbp
mov    0x0(%rbp),%edi
call   1080 <strerror@plt>
cvtss2sd %xmm0,%xmm0
lea    0xe63(%rip),%rsi
xor    %edi,%edi
mov    %ebx,%edx
mov    $0x1,%al
call   1060 <grf_logging_log@plt>
jmp    129c <grf_uart_set_timeout+0x13c>
xorps  %xmm0,%xmm0
movss  (%rsp),%xmm1
cmpeqss %xmm1,%xmm0
movd   %xmm0,%eax
and    $0x1,%eax
mov    0x8(%rsp),%rcx
mov    0x2de6(%rip),%rdx
mov    (%rdx),%rdx
mov    %eax,(%rcx,%rdx,4)
cvttss2si %xmm1,%eax
mov    0x2dbd(%rip),%r14
mov    (%r14),%rsi
mov    %eax,(%rcx,%rsi,4)
mov    (%rcx,%rdx,4),%edx
lea    0xe3a(%rip),%rsi
xor    %ebp,%ebp
mov    $0x3,%edi
xor    %eax,%eax
call   1060 <grf_logging_log@plt>
mov    0x8(%rsp),%rax
mov    (%r14),%rcx
mov    (%rax,%rcx,4),%edx
lea    0xe2a(%rip),%rsi
mov    $0x3,%edi
xor    %eax,%eax
call   1060 <grf_logging_log@plt>
mov    0x2d9a(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebx,%edi
call   1070 <tcsetattr@plt>
test   %rax,%rax
je     12aa <grf_uart_set_timeout+0x14a>
movss  (%rsp),%xmm0
cvtss2sd %xmm0,%xmm0
movsd  %xmm0,(%rsp)
mov    0x2d5a(%rip),%rbp
mov    0x0(%rbp),%edi
call   1080 <strerror@plt>
xorps  %xmm1,%xmm1
cvtss2sd %xmm0,%xmm1
lea    0xde9(%rip),%rsi
xor    %edi,%edi
mov    %ebx,%edx
movsd  (%rsp),%xmm0
mov    $0x2,%al
call   1060 <grf_logging_log@plt>
mov    0x0(%rbp),%ebp
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    %ebx,%edi
call   1030 <tcdrain@plt>
test   %rax,%rax
je     129f <grf_uart_set_timeout+0x13f>
mov    0x2d13(%rip),%rbp
mov    0x0(%rbp),%edi
call   1080 <strerror@plt>
cvtss2sd %xmm0,%xmm0
lea    0xdcc(%rip),%rsi
jmp    11ca <grf_uart_set_timeout+0x6a>

LLVM IR (-O0):
%struct.termios = type { i32* }
@.str = [41 x i8] c"Setting timeout of %d to %.1f seconds...\00"
@.str.1 = [40 x i8] c"Getting TTY attributes of %d failed: %s\00"
@errno = global i32 0
@VMIN = global i64 0
@VTIME = global i64 0
@.str.2 = [16 x i8] c" vmin = %u...\00"
@.str.3 = [16 x i8] c" vtime = %u...\00"
@TCSANOW = global i32 0
@.str.4 = [39 x i8] c"Setting timeout of %d to %u failed: %s\00"
@.str.5 = [35 x i8] c"Draining data of TTY %d failed: %s\00"
define i32 @grf_uart_set_timeout(i32 %0, float %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca float
%6 = alloca %struct.termios
store i32 %0, i32* %4
store float %1, float* %5
%7 = load i32, i32* %4
%8 = icmp sge i32 %7, 0
%9 = zext i1 %8 to i32
%10 = call i32 @assert(i32 %9)
%11 = load i32, i32* %4
%12 = load float, float* %5
%13 = fdiv float %12, 1.000000e+01
%14 = fpext float %13 to double
%15 = call i32 (i32, i8*, i32, ...) @grf_logging_log(i32 2, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0), i32 %11, double %14)
%16 = load i32, i32* %4
%17 = call i64 @tcgetattr(i32 %16, %struct.termios* %6)
%18 = icmp ne i64 %17, 0
br i1 %18, label %19, label %26
19: ; preds = %2
%20 = load i32, i32* %4
%21 = load i32, i32* @errno
%22 = call float @strerror(i32 %21)
%23 = fpext float %22 to double
%24 = call i32 (i32, i8*, i32, ...) @grf_logging_log(i32 0, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0), i32 %20, double %23)
%25 = load i32, i32* @errno
store i32 %25, i32* %3
br label %83
26: ; preds = %2
%27 = load float, float* %5
%28 = fcmp oeq float %27, 0.000000e+00
br i1 %28, label %29, label %34
29: ; preds = %26
%30 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 0
%31 = load i32*, i32** %30
%32 = load i64, i64* @VMIN
%33 = getelementptr inbounds i32, i32* %31, i64 %32
store i32 1, i32* %33
br label %39
34: ; preds = %26
%35 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 0
%36 = load i32*, i32** %35
%37 = load i64, i64* @VMIN
%38 = getelementptr inbounds i32, i32* %36, i64 %37
store i32 0, i32* %38
br label %39
39: ; preds = %34, %29
%40 = load float, float* %5
%41 = fptosi float %40 to i32
%42 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 0
%43 = load i32*, i32** %42
%44 = load i64, i64* @VTIME
%45 = getelementptr inbounds i32, i32* %43, i64 %44
store i32 %41, i32* %45
%46 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 0
%47 = load i32*, i32** %46
%48 = load i64, i64* @VMIN
%49 = getelementptr inbounds i32, i32* %47, i64 %48
%50 = load i32, i32* %49
%51 = call i32 (i32, i8*, i32, ...) @grf_logging_log(i32 3, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i32 %50)
%52 = getelementptr inbounds %struct.termios, %struct.termios* %6, i32 0, i32 0
%53 = load i32*, i32** %52
%54 = load i64, i64* @VTIME
%55 = getelementptr inbounds i32, i32* %53, i64 %54
%56 = load i32, i32* %55
%57 = call i32 (i32, i8*, i32, ...) @grf_logging_log(i32 3, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i32 %56)
%58 = load i32, i32* %4
%59 = load i32, i32* @TCSANOW
%60 = call i64 @tcsetattr(i32 %58, i32 %59, %struct.termios* %6)
%61 = icmp ne i64 %60, 0
br i1 %61, label %62, label %71
62: ; preds = %39
%63 = load i32, i32* %4
%64 = load float, float* %5
%65 = fpext float %64 to double
%66 = load i32, i32* @errno
%67 = call float @strerror(i32 %66)
%68 = fpext float %67 to double
%69 = call i32 (i32, i8*, i32, ...) @grf_logging_log(i32 0, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.4, i64 0, i64 0), i32 %63, double %65, double %68)
%70 = load i32, i32* @errno
store i32 %70, i32* %3
br label %83
71: ; preds = %39
%72 = load i32, i32* %4
%73 = call i64 @tcdrain(i32 %72)
%74 = icmp ne i64 %73, 0
br i1 %74, label %75, label %82
75: ; preds = %71
%76 = load i32, i32* %4
%77 = load i32, i32* @errno
%78 = call float @strerror(i32 %77)
%79 = fpext float %78 to double
%80 = call i32 (i32, i8*, i32, ...) @grf_logging_log(i32 0, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.5, i64 0, i64 0), i32 %76, double %79)
%81 = load i32, i32* @errno
store i32 %81, i32* %3
br label %83
82: ; preds = %71
store i32 0, i32* %3
br label %83
83: ; preds = %82, %75, %62, %19
%84 = load i32, i32* %3
ret i32 %84
}

LLVM IR (-O2):
%struct.termios = type { i32* }
@.str = [41 x i8] c"Setting timeout of %d to %.1f seconds...\00"
@.str.1 = [40 x i8] c"Getting TTY attributes of %d failed: %s\00"
@errno = global i32 0
@VMIN = global i64 0
@VTIME = global i64 0
@.str.2 = [16 x i8] c" vmin = %u...\00"
@.str.3 = [16 x i8] c" vtime = %u...\00"
@TCSANOW = global i32 0
@.str.4 = [39 x i8] c"Setting timeout of %d to %u failed: %s\00"
@.str.5 = [35 x i8] c"Draining data of TTY %d failed: %s\00"
define i32 @grf_uart_set_timeout(i32 %0, float %1) {
%3 = alloca %struct.termios
%4 = xor i32 %0, -1
%5 = lshr i32 %4, 31
%6 = tail call i32 @assert(i32 %5)
%7 = bitcast %struct.termios* %3 to i8*
%8 = fdiv float %1, 1.000000e+01
%9 = fpext float %8 to double
%10 = tail call i32 (i32, i8*, i32, ...) @grf_logging_log(i32 2, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0), i32 %0, double %9)
%11 = call i64 @tcgetattr(i32 %0, %struct.termios* %3)
%12 = icmp eq i64 %11, 0
br i1 %12, label %19, label %13
13: ; preds = %2
%14 = load i32, i32* @errno
%15 = call float @strerror(i32 %14)
%16 = fpext float %15 to double
%17 = call i32 (i32, i8*, i32, ...) @grf_logging_log(i32 0, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0), i32 %0, double %16)
%18 = load i32, i32* @errno
br label %57
19: ; preds = %2
%20 = fcmp oeq float %1, 0.000000e+00
%21 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 0
%22 = load i32*, i32** %21
%23 = load i64, i64* @VMIN
%24 = getelementptr inbounds i32, i32* %22, i64 %23
%25 = zext i1 %20 to i32
store i32 %25, i32* %24
%26 = fptosi float %1 to i32
%27 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 0
%28 = load i64, i64* @VTIME
%29 = getelementptr inbounds i32, i32* %22, i64 %28
store i32 %26, i32* %29
%30 = getelementptr inbounds i32, i32* %22, i64 %23
%31 = load i32, i32* %30
%32 = call i32 (i32, i8*, i32, ...) @grf_logging_log(i32 3, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i32 %31)
%33 = load i32*, i32** %27
%34 = load i64, i64* @VTIME
%35 = getelementptr inbounds i32, i32* %33, i64 %34
%36 = load i32, i32* %35
%37 = call i32 (i32, i8*, i32, ...) @grf_logging_log(i32 3, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i32 %36)
%38 = load i32, i32* @TCSANOW
%39 = call i64 @tcsetattr(i32 %0, i32 %38, %struct.termios* %3)
%40 = icmp eq i64 %39, 0
br i1 %40, label %48, label %41
41: ; preds = %19
%42 = fpext float %1 to double
%43 = load i32, i32* @errno
%44 = call float @strerror(i32 %43)
%45 = fpext float %44 to double
%46 = call i32 (i32, i8*, i32, ...) @grf_logging_log(i32 0, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.4, i64 0, i64 0), i32 %0, double %42, double %45)
%47 = load i32, i32* @errno
br label %57
48: ; preds = %19
%49 = call i64 @tcdrain(i32 %0)
%50 = icmp eq i64 %49, 0
br i1 %50, label %57, label %51
51: ; preds = %48
%52 = load i32, i32* @errno
%53 = call float @strerror(i32 %52)
%54 = fpext float %53 to double
%55 = call i32 (i32, i8*, i32, ...) @grf_logging_log(i32 0, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.5, i64 0, i64 0), i32 %0, double %54)
%56 = load i32, i32* @errno
br label %57
57: ; preds = %48, %51, %41, %13
%58 = phi i32 [ %18, %13 ], [ %47, %41 ], [ %56, %51 ], [ 0, %48 ]
ret i32 %58
}