#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;
scalar_t__ close(int);
int /*<<< orphan*/ perror(char *);
scalar_t__ tcdrain(int);
scalar_t__ tcflush(int, int /*<<< orphan*/);
int serial_close(int filedescriptor) {
  if (tcdrain(filedescriptor) < 0) {
    perror("serial_close: tcdrain");
    return -1;
  }
  if (tcflush(filedescriptor, TCIOFLUSH) < 0) {
    perror("serial_close: tcflush");
    return -2;
  }
  if (close(filedescriptor) < 0) {
    perror("serial_close: close");
    return -3;
  }
  return 0;
}


x86 assembly (clang, -O0):
<serial_close>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%edi
call   1030 <tcdrain@plt>
cmp    $0x0,%rax
jge    1175 <serial_close+0x35>
lea    0xe9c(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d9 <serial_close+0x99>
mov    -0x8(%rbp),%edi
mov    0x2e61(%rip),%rax
mov    (%rax),%esi
call   1040 <tcflush@plt>
cmp    $0x0,%rax
jge    11a8 <serial_close+0x68>
lea    0xe7f(%rip),%rdi
call   1060 <perror@plt>
movl   $0xfffffffe,-0x4(%rbp)
jmp    11d9 <serial_close+0x99>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
cmp    $0x0,%rax
jge    11d2 <serial_close+0x92>
lea    0xe6b(%rip),%rdi
call   1060 <perror@plt>
movl   $0xfffffffd,-0x4(%rbp)
jmp    11d9 <serial_close+0x99>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<serial_close>:
push   %rbx
mov    %edi,%ebx
call   1030 <tcdrain@plt>
test   %rax,%rax
js     1174 <serial_close+0x34>
mov    0x2e8c(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1040 <tcflush@plt>
test   %rax,%rax
js     1182 <serial_close+0x42>
mov    %ebx,%edi
call   1050 <close@plt>
xor    %ebx,%ebx
test   %rax,%rax
js     1190 <serial_close+0x50>
mov    %ebx,%eax
pop    %rbx
ret
mov    $0xffffffff,%ebx
lea    0xe80(%rip),%rdi
jmp    119c <serial_close+0x5c>
mov    $0xfffffffe,%ebx
lea    0xe88(%rip),%rdi
jmp    119c <serial_close+0x5c>
mov    $0xfffffffd,%ebx
lea    0xe90(%rip),%rdi
call   1060 <perror@plt>
jmp    1170 <serial_close+0x30>

LLVM IR (-O0):
@.str = [22 x i8] c"serial_close: tcdrain\00"
@TCIOFLUSH = global i32 0
@.str.1 = [22 x i8] c"serial_close: tcflush\00"
@.str.2 = [20 x i8] c"serial_close: close\00"
define i32 @serial_close(i32 %0) {
%2 = alloca i32
%3 = alloca i32
store i32 %0, i32* %3
%4 = load i32, i32* %3
%5 = call i64 @tcdrain(i32 %4)
%6 = icmp slt i64 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %1
%8 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %2
br label %23
9: ; preds = %1
%10 = load i32, i32* %3
%11 = load i32, i32* @TCIOFLUSH
%12 = call i64 @tcflush(i32 %10, i32 %11)
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %9
%15 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
store i32 -2, i32* %2
br label %23
16: ; preds = %9
%17 = load i32, i32* %3
%18 = call i64 @close(i32 %17)
%19 = icmp slt i64 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %16
%21 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
store i32 -3, i32* %2
br label %23
22: ; preds = %16
store i32 0, i32* %2
br label %23
23: ; preds = %22, %20, %14, %7
%24 = load i32, i32* %2
ret i32 %24
}

LLVM IR (-O2):
@.str = [22 x i8] c"serial_close: tcdrain\00"
@TCIOFLUSH = global i32 0
@.str.1 = [22 x i8] c"serial_close: tcflush\00"
@.str.2 = [20 x i8] c"serial_close: close\00"
define i32 @serial_close(i32 %0) {
%2 = tail call i64 @tcdrain(i32 %0)
%3 = icmp slt i64 %2, 0
br i1 %3, label %11, label %4
4: ; preds = %1
%5 = load i32, i32* @TCIOFLUSH
%6 = tail call i64 @tcflush(i32 %0, i32 %5)
%7 = icmp slt i64 %6, 0
br i1 %7, label %11, label %8
8: ; preds = %4
%9 = tail call i64 @close(i32 %0)
%10 = icmp slt i64 %9, 0
br i1 %10, label %11, label %15
11: ; preds = %8, %4, %1
%12 = phi i8* [ getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), %1 ], [ getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), %4 ], [ getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), %8 ]
%13 = phi i32 [ -1, %1 ], [ -2, %4 ], [ -3, %8 ]
%14 = tail call i32 @perror(i8* %12)
br label %15
15: ; preds = %11, %8
%16 = phi i32 [ 0, %8 ], [ %13, %11 ]
ret i32 %16
}