#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*/ B9600;
int /*<<< orphan*/ TCIOFLUSH;
int /*<<< orphan*/ TCSANOW;
size_t VMIN;
size_t VTIME;
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ cfmakeraw(struct termios *);
scalar_t__ cfsetspeed(struct termios *, int /*<<< orphan*/);
int errno;
int /*<<< orphan*/ grf_logging_log(int, char *, int, ...);
int /*<<< orphan*/ strerror(int);
int tcdrain(int);
scalar_t__ tcflush(int, int /*<<< orphan*/);
scalar_t__ tcgetattr(int, struct termios *);
scalar_t__ tcsetattr(int, int /*<<< orphan*/, struct termios *);
int grf_uart_setup(int fd) {
  assert(fd >= 0);
  struct termios tty_attr;
  grf_logging_log(2, ("Setting up UART %d..."), fd);
  if (tcgetattr(fd, &tty_attr)) {
    grf_logging_log(0, ("Getting TTY attributes of %d failed: %s"), fd,
                    strerror(errno));
    return errno;
  }
  if (tcflush(fd, TCIOFLUSH)) {
    grf_logging_log(0, ("Flushing of %d failed: %s"), fd, strerror(errno));
    return errno;
  }
  if (cfsetspeed(&tty_attr, B9600)) {
    grf_logging_log(0, ("Setting speed of TTY %d failed: %s"), fd,
                    strerror(errno));
    return errno;
  }
  cfmakeraw(&tty_attr);
  if (tcflush(fd, TCIOFLUSH)) {
    grf_logging_log(0, ("Flushing of %d failed: %s"), fd, strerror(errno));
    return errno;
  }
  tty_attr.c_cc[VMIN] = 0;
  tty_attr.c_cc[VTIME] = 10;
  if (tcsetattr(fd, TCSANOW, &tty_attr)) {
    grf_logging_log(0, ("Setting TTY attributes of %d failed: %s"), fd,
                    strerror(errno));
    return errno;
  }
  return tcdrain(fd);
}


x86 assembly (clang, -O0):
<grf_uart_setup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
setge  %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    -0x8(%rbp),%edx
mov    $0x2,%edi
lea    0xe45(%rip),%rsi
mov    $0x0,%al
call   1090 <grf_logging_log@plt>
mov    -0x8(%rbp),%edi
lea    -0x10(%rbp),%rsi
call   1080 <tcgetattr@plt>
cmp    $0x0,%rax
je     1212 <grf_uart_setup+0x82>
mov    -0x8(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    0x2de3(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    -0x14(%rbp),%edx
mov    %eax,%ecx
xor    %edi,%edi
lea    0xe1c(%rip),%rsi
mov    $0x0,%al
call   1090 <grf_logging_log@plt>
mov    0x2dc0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    13a9 <grf_uart_setup+0x219>
mov    -0x8(%rbp),%edi
mov    0x2da4(%rip),%rax
mov    (%rax),%esi
call   1040 <tcflush@plt>
cmp    $0x0,%rax
je     1267 <grf_uart_setup+0xd7>
mov    -0x8(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    0x2d8e(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    -0x18(%rbp),%edx
mov    %eax,%ecx
xor    %edi,%edi
lea    0xdef(%rip),%rsi
mov    $0x0,%al
call   1090 <grf_logging_log@plt>
mov    0x2d6b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    13a9 <grf_uart_setup+0x219>
mov    0x2d72(%rip),%rax
mov    (%rax),%esi
lea    -0x10(%rbp),%rdi
call   1050 <cfsetspeed@plt>
cmp    $0x0,%rax
je     12bd <grf_uart_setup+0x12d>
mov    -0x8(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2d38(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    -0x1c(%rbp),%edx
mov    %eax,%ecx
xor    %edi,%edi
lea    0xdb3(%rip),%rsi
mov    $0x0,%al
call   1090 <grf_logging_log@plt>
mov    0x2d15(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    13a9 <grf_uart_setup+0x219>
lea    -0x10(%rbp),%rdi
call   1070 <cfmakeraw@plt>
mov    -0x8(%rbp),%edi
mov    0x2cf0(%rip),%rax
mov    (%rax),%esi
call   1040 <tcflush@plt>
cmp    $0x0,%rax
je     131b <grf_uart_setup+0x18b>
mov    -0x8(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    0x2cda(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    -0x20(%rbp),%edx
mov    %eax,%ecx
xor    %edi,%edi
lea    0xd3b(%rip),%rsi
mov    $0x0,%al
call   1090 <grf_logging_log@plt>
mov    0x2cb7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    13a9 <grf_uart_setup+0x219>
mov    -0x10(%rbp),%rax
mov    0x2cb2(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x0,(%rax,%rcx,4)
mov    -0x10(%rbp),%rax
mov    0x2c7d(%rip),%rcx
mov    (%rcx),%rcx
movl   $0xa,(%rax,%rcx,4)
mov    -0x8(%rbp),%edi
mov    0x2c99(%rip),%rax
mov    (%rax),%esi
lea    -0x10(%rbp),%rdx
call   10a0 <tcsetattr@plt>
cmp    $0x0,%rax
je     139e <grf_uart_setup+0x20e>
mov    -0x8(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    0x2c57(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    -0x24(%rbp),%edx
mov    %eax,%ecx
xor    %edi,%edi
lea    0xcf5(%rip),%rsi
mov    $0x0,%al
call   1090 <grf_logging_log@plt>
mov    0x2c34(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    13a9 <grf_uart_setup+0x219>
mov    -0x8(%rbp),%edi
call   1030 <tcdrain@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<grf_uart_setup>:
push   %r14
push   %rbx
push   %rax
mov    %edi,%ebx
not    %edi
shr    $0x1f,%edi
call   1060 <assert@plt>
lea    0xe59(%rip),%rsi
mov    $0x2,%edi
mov    %ebx,%edx
xor    %eax,%eax
call   1090 <grf_logging_log@plt>
mov    %rsp,%rsi
mov    %ebx,%edi
call   1080 <tcgetattr@plt>
test   %rax,%rax
je     11dc <grf_uart_setup+0x4c>
mov    0x2dfd(%rip),%r14
mov    (%r14),%edi
call   10b0 <strerror@plt>
lea    0xe3c(%rip),%rsi
jmp    1236 <grf_uart_setup+0xa6>
mov    0x2ddd(%rip),%r14
mov    (%r14),%esi
mov    %ebx,%edi
call   1040 <tcflush@plt>
test   %rax,%rax
je     120a <grf_uart_setup+0x7a>
mov    0x2dcf(%rip),%r14
mov    (%r14),%edi
call   10b0 <strerror@plt>
lea    0xe36(%rip),%rsi
jmp    1236 <grf_uart_setup+0xa6>
mov    0x2dcf(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdi
call   1050 <cfsetspeed@plt>
test   %rax,%rax
je     124e <grf_uart_setup+0xbe>
mov    0x2da1(%rip),%r14
mov    (%r14),%edi
call   10b0 <strerror@plt>
lea    0xe22(%rip),%rsi
xor    %edi,%edi
mov    %ebx,%edx
mov    %eax,%ecx
xor    %eax,%eax
call   1090 <grf_logging_log@plt>
mov    (%r14),%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
mov    %rsp,%rdi
call   1070 <cfmakeraw@plt>
mov    (%r14),%esi
mov    %ebx,%edi
call   1040 <tcflush@plt>
test   %rax,%rax
jne    11f2 <grf_uart_setup+0x62>
mov    (%rsp),%rax
mov    0x2d68(%rip),%rcx
mov    (%rcx),%rcx
movl   $0x0,(%rax,%rcx,4)
mov    0x2d37(%rip),%rcx
mov    (%rcx),%rcx
movl   $0xa,(%rax,%rcx,4)
mov    0x2d56(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebx,%edi
call   10a0 <tcsetattr@plt>
test   %rax,%rax
je     12be <grf_uart_setup+0x12e>
mov    0x2d1e(%rip),%r14
mov    (%r14),%edi
call   10b0 <strerror@plt>
lea    0xdc2(%rip),%rsi
jmp    1236 <grf_uart_setup+0xa6>
mov    %ebx,%edi
call   1030 <tcdrain@plt>
jmp    1246 <grf_uart_setup+0xb6>

LLVM IR (-O0):
%struct.termios = type { i32* }
@.str = [22 x i8] c"Setting up UART %d...\00"
@.str.1 = [40 x i8] c"Getting TTY attributes of %d failed: %s\00"
@errno = global i32 0
@TCIOFLUSH = global i32 0
@.str.2 = [26 x i8] c"Flushing of %d failed: %s\00"
@B9600 = global i32 0
@.str.3 = [35 x i8] c"Setting speed of TTY %d failed: %s\00"
@VMIN = global i64 0
@VTIME = global i64 0
@TCSANOW = global i32 0
@.str.4 = [40 x i8] c"Setting TTY attributes of %d failed: %s\00"
define i32 @grf_uart_setup(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.termios
store i32 %0, i32* %3
%5 = load i32, i32* %3
%6 = icmp sge i32 %5, 0
%7 = zext i1 %6 to i32
%8 = call i32 @assert(i32 %7)
%9 = load i32, i32* %3
%10 = call i32 (i32, i8*, i32, ...) @grf_logging_log(i32 2, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i32 %9)
%11 = load i32, i32* %3
%12 = call i64 @tcgetattr(i32 %11, %struct.termios* %4)
%13 = icmp ne i64 %12, 0
br i1 %13, label %14, label %20
14: ; preds = %1
%15 = load i32, i32* %3
%16 = load i32, i32* @errno
%17 = call i32 @strerror(i32 %16)
%18 = 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 %15, i32 %17)
%19 = load i32, i32* @errno
store i32 %19, i32* %2
br label %75
20: ; preds = %1
%21 = load i32, i32* %3
%22 = load i32, i32* @TCIOFLUSH
%23 = call i64 @tcflush(i32 %21, i32 %22)
%24 = icmp ne i64 %23, 0
br i1 %24, label %25, label %31
25: ; preds = %20
%26 = load i32, i32* %3
%27 = load i32, i32* @errno
%28 = call i32 @strerror(i32 %27)
%29 = call i32 (i32, i8*, i32, ...) @grf_logging_log(i32 0, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i32 %26, i32 %28)
%30 = load i32, i32* @errno
store i32 %30, i32* %2
br label %75
31: ; preds = %20
%32 = load i32, i32* @B9600
%33 = call i64 @cfsetspeed(%struct.termios* %4, i32 %32)
%34 = icmp ne i64 %33, 0
br i1 %34, label %35, label %41
35: ; preds = %31
%36 = load i32, i32* %3
%37 = load i32, i32* @errno
%38 = call i32 @strerror(i32 %37)
%39 = call i32 (i32, i8*, i32, ...) @grf_logging_log(i32 0, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.3, i64 0, i64 0), i32 %36, i32 %38)
%40 = load i32, i32* @errno
store i32 %40, i32* %2
br label %75
41: ; preds = %31
%42 = call i32 @cfmakeraw(%struct.termios* %4)
%43 = load i32, i32* %3
%44 = load i32, i32* @TCIOFLUSH
%45 = call i64 @tcflush(i32 %43, i32 %44)
%46 = icmp ne i64 %45, 0
br i1 %46, label %47, label %53
47: ; preds = %41
%48 = load i32, i32* %3
%49 = load i32, i32* @errno
%50 = call i32 @strerror(i32 %49)
%51 = call i32 (i32, i8*, i32, ...) @grf_logging_log(i32 0, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i32 %48, i32 %50)
%52 = load i32, i32* @errno
store i32 %52, i32* %2
br label %75
53: ; preds = %41
%54 = getelementptr inbounds %struct.termios, %struct.termios* %4, i32 0, i32 0
%55 = load i32*, i32** %54
%56 = load i64, i64* @VMIN
%57 = getelementptr inbounds i32, i32* %55, i64 %56
store i32 0, i32* %57
%58 = getelementptr inbounds %struct.termios, %struct.termios* %4, i32 0, i32 0
%59 = load i32*, i32** %58
%60 = load i64, i64* @VTIME
%61 = getelementptr inbounds i32, i32* %59, i64 %60
store i32 10, i32* %61
%62 = load i32, i32* %3
%63 = load i32, i32* @TCSANOW
%64 = call i64 @tcsetattr(i32 %62, i32 %63, %struct.termios* %4)
%65 = icmp ne i64 %64, 0
br i1 %65, label %66, label %72
66: ; preds = %53
%67 = load i32, i32* %3
%68 = load i32, i32* @errno
%69 = call i32 @strerror(i32 %68)
%70 = call i32 (i32, i8*, i32, ...) @grf_logging_log(i32 0, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.4, i64 0, i64 0), i32 %67, i32 %69)
%71 = load i32, i32* @errno
store i32 %71, i32* %2
br label %75
72: ; preds = %53
%73 = load i32, i32* %3
%74 = call i32 @tcdrain(i32 %73)
store i32 %74, i32* %2
br label %75
75: ; preds = %72, %66, %47, %35, %25, %14
%76 = load i32, i32* %2
ret i32 %76
}

LLVM IR (-O2):
%struct.termios = type { i32* }
@.str = [22 x i8] c"Setting up UART %d...\00"
@.str.1 = [40 x i8] c"Getting TTY attributes of %d failed: %s\00"
@errno = global i32 0
@TCIOFLUSH = global i32 0
@.str.2 = [26 x i8] c"Flushing of %d failed: %s\00"
@B9600 = global i32 0
@.str.3 = [35 x i8] c"Setting speed of TTY %d failed: %s\00"
@VMIN = global i64 0
@VTIME = global i64 0
@TCSANOW = global i32 0
@.str.4 = [40 x i8] c"Setting TTY attributes of %d failed: %s\00"
define i32 @grf_uart_setup(i32 %0) {
%2 = alloca %struct.termios
%3 = xor i32 %0, -1
%4 = lshr i32 %3, 31
%5 = tail call i32 @assert(i32 %4)
%6 = bitcast %struct.termios* %2 to i8*
%7 = tail call i32 (i32, i8*, i32, ...) @grf_logging_log(i32 2, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i32 %0)
%8 = call i64 @tcgetattr(i32 %0, %struct.termios* %2)
%9 = icmp eq i64 %8, 0
br i1 %9, label %15, label %10
10: ; preds = %1
%11 = load i32, i32* @errno
%12 = call i32 @strerror(i32 %11)
%13 = 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, i32 %12)
%14 = load i32, i32* @errno
br label %60
15: ; preds = %1
%16 = load i32, i32* @TCIOFLUSH
%17 = call i64 @tcflush(i32 %0, i32 %16)
%18 = icmp eq i64 %17, 0
br i1 %18, label %24, label %19
19: ; preds = %15
%20 = load i32, i32* @errno
%21 = call i32 @strerror(i32 %20)
%22 = call i32 (i32, i8*, i32, ...) @grf_logging_log(i32 0, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i32 %0, i32 %21)
%23 = load i32, i32* @errno
br label %60
24: ; preds = %15
%25 = load i32, i32* @B9600
%26 = call i64 @cfsetspeed(%struct.termios* %2, i32 %25)
%27 = icmp eq i64 %26, 0
br i1 %27, label %33, label %28
28: ; preds = %24
%29 = load i32, i32* @errno
%30 = call i32 @strerror(i32 %29)
%31 = call i32 (i32, i8*, i32, ...) @grf_logging_log(i32 0, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.3, i64 0, i64 0), i32 %0, i32 %30)
%32 = load i32, i32* @errno
br label %60
33: ; preds = %24
%34 = call i32 @cfmakeraw(%struct.termios* %2)
%35 = load i32, i32* @TCIOFLUSH
%36 = call i64 @tcflush(i32 %0, i32 %35)
%37 = icmp eq i64 %36, 0
br i1 %37, label %43, label %38
38: ; preds = %33
%39 = load i32, i32* @errno
%40 = call i32 @strerror(i32 %39)
%41 = call i32 (i32, i8*, i32, ...) @grf_logging_log(i32 0, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i32 %0, i32 %40)
%42 = load i32, i32* @errno
br label %60
43: ; preds = %33
%44 = getelementptr inbounds %struct.termios, %struct.termios* %2, i64 0, i32 0
%45 = load i32*, i32** %44
%46 = load i64, i64* @VMIN
%47 = getelementptr inbounds i32, i32* %45, i64 %46
store i32 0, i32* %47
%48 = load i64, i64* @VTIME
%49 = getelementptr inbounds i32, i32* %45, i64 %48
store i32 10, i32* %49
%50 = load i32, i32* @TCSANOW
%51 = call i64 @tcsetattr(i32 %0, i32 %50, %struct.termios* %2)
%52 = icmp eq i64 %51, 0
br i1 %52, label %58, label %53
53: ; preds = %43
%54 = load i32, i32* @errno
%55 = call i32 @strerror(i32 %54)
%56 = call i32 (i32, i8*, i32, ...) @grf_logging_log(i32 0, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.4, i64 0, i64 0), i32 %0, i32 %55)
%57 = load i32, i32* @errno
br label %60
58: ; preds = %43
%59 = call i32 @tcdrain(i32 %0)
br label %60
60: ; preds = %58, %53, %38, %28, %19, %10
%61 = phi i32 [ %14, %10 ], [ %23, %19 ], [ %32, %28 ], [ %42, %38 ], [ %57, %53 ], [ %59, %58 ]
ret i32 %61
}