#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 */

/* Variables and functions */
int /*<<< orphan*/ TCIOFLUSH;
int /*<<< orphan*/ TIOCSETD;
int /*<<< orphan*/ close(int);
scalar_t__ ioctl(int, int /*<<< orphan*/, int *);
int /*<<< orphan*/ perror(char *);
scalar_t__ tcflush(int, int /*<<< orphan*/);
void release_uart(int tty_fd) {
  int ldisc = 0;
  if (tcflush(tty_fd, TCIOFLUSH) < 0) {
    perror("Failed to flush");
  }
  if (ioctl(tty_fd, TIOCSETD, &ldisc) < 0) {
    perror("Failed to set tty_fd to N_TTY");
  }
  close(tty_fd);
}


x86 assembly (clang, -O0):
<release_uart>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2e7c(%rip),%rax
mov    (%rax),%esi
call   1030 <tcflush@plt>
cmp    $0x0,%rax
jge    1179 <release_uart+0x39>
lea    0xe8c(%rip),%rdi
call   1060 <perror@plt>
mov    -0x4(%rbp),%edi
mov    0x2e5d(%rip),%rax
mov    (%rax),%esi
lea    -0x8(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0x0,%rax
jge    11a4 <release_uart+0x64>
lea    0xe71(%rip),%rdi
call   1060 <perror@plt>
mov    -0x4(%rbp),%edi
call   1050 <close@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<release_uart>:
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebx
movl   $0x0,0xc(%rsp)
mov    0x2e82(%rip),%rax
mov    (%rax),%esi
call   1030 <tcflush@plt>
test   %rax,%rax
js     1189 <release_uart+0x49>
mov    0x2e77(%rip),%rax
mov    (%rax),%esi
lea    0xc(%rsp),%rdx
mov    %ebx,%edi
call   1040 <ioctl@plt>
test   %rax,%rax
js     1197 <release_uart+0x57>
mov    %ebx,%edi
call   1050 <close@plt>
add    $0x10,%rsp
pop    %rbx
ret
lea    0xe70(%rip),%rdi
call   1060 <perror@plt>
jmp    1162 <release_uart+0x22>
lea    0xe72(%rip),%rdi
call   1060 <perror@plt>
jmp    117c <release_uart+0x3c>

LLVM IR (-O0):
@TCIOFLUSH = global i32 0
@.str = [16 x i8] c"Failed to flush\00"
@TIOCSETD = global i32 0
@.str.1 = [30 x i8] c"Failed to set tty_fd to N_TTY\00"
define void @release_uart(i32 %0) {
%2 = alloca i32
%3 = alloca i32
store i32 %0, i32* %2
store i32 0, i32* %3
%4 = load i32, i32* %2
%5 = load i32, i32* @TCIOFLUSH
%6 = call i64 @tcflush(i32 %4, i32 %5)
%7 = icmp slt i64 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %1
%9 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
br label %10
10: ; preds = %8, %1
%11 = load i32, i32* %2
%12 = load i32, i32* @TIOCSETD
%13 = call i64 @ioctl(i32 %11, i32 %12, i32* %3)
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %10
%16 = call i32 @perror(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0))
br label %17
17: ; preds = %15, %10
%18 = load i32, i32* %2
%19 = call i32 @close(i32 %18)
ret void
}

LLVM IR (-O2):
@TCIOFLUSH = global i32 0
@.str = [16 x i8] c"Failed to flush\00"
@TIOCSETD = global i32 0
@.str.1 = [30 x i8] c"Failed to set tty_fd to N_TTY\00"
define void @release_uart(i32 %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
store i32 0, i32* %2
%4 = load i32, i32* @TCIOFLUSH
%5 = tail call i64 @tcflush(i32 %0, i32 %4)
%6 = icmp slt i64 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %1
%8 = tail call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
br label %9
9: ; preds = %7, %1
%10 = load i32, i32* @TIOCSETD
%11 = call i64 @ioctl(i32 %0, i32 %10, i32* %2)
%12 = icmp slt i64 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %9
%14 = call i32 @perror(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0))
br label %15
15: ; preds = %13, %9
%16 = call i32 @close(i32 %0)
ret void
}