#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*/ TCIFLUSH;
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ stderr;
int tcflush(int, int /*<<< orphan*/);
int /*<<< orphan*/ usleep(int);
int write(int, char *, int);
int writeport(int fd, char *data) {
  int len = 1, n = 0;
  n = 1;
  while (n != 0) {
    n = tcflush(fd, TCIFLUSH);
  }
  if (n == 0)
    printf("successful buffer flush \n");
  usleep(5000);
  n = write(fd, data, len);
  usleep(500000);
  if (n < 0) {
    fputs("write failed!\n", stderr);
    return 0;
  }
  return 1;
}


x86 assembly (clang, -O0):
<writeport>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x1,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
movl   $0x1,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
je     1197 <writeport+0x47>
mov    -0x8(%rbp),%edi
mov    0x2e58(%rip),%rax
mov    (%rax),%esi
call   1040 <tcflush@plt>
mov    %eax,-0x18(%rbp)
jmp    1174 <writeport+0x24>
cmpl   $0x0,-0x18(%rbp)
jne    11ad <writeport+0x5d>
lea    0xe58(%rip),%rdi
call   1050 <printf@plt>
mov    $0x1388,%edi
call   1070 <usleep@plt>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x18(%rbp)
mov    $0x7a120,%edi
call   1070 <usleep@plt>
cmpl   $0x0,-0x18(%rbp)
jge    11fe <writeport+0xae>
mov    0x2df4(%rip),%rax
mov    (%rax),%esi
lea    0xe2d(%rip),%rdi
call   1060 <fputs@plt>
movl   $0x0,-0x4(%rbp)
jmp    1205 <writeport+0xb5>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<writeport>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %edi,%ebp
mov    0x2e80(%rip),%rbx
mov    (%rbx),%esi
mov    %ebp,%edi
call   1050 <tcflush@plt>
test   %eax,%eax
jne    1160 <writeport+0x10>
lea    0xe9b(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1388,%edi
call   1070 <usleep@plt>
mov    $0x1,%ebx
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1040 <write@plt>
mov    %eax,%ebp
mov    $0x7a120,%edi
call   1070 <usleep@plt>
test   %ebp,%ebp
jns    11be <writeport+0x6e>
mov    0x2e2a(%rip),%rax
mov    (%rax),%esi
lea    0xe49(%rip),%rdi
call   1060 <fputs@plt>
xor    %ebx,%ebx
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@TCIFLUSH = global i32 0
@.str = [26 x i8] c"successful buffer flush \0A\00"
@.str.1 = [15 x i8] c"write failed!\0A\00"
@stderr = global i32 0
define i32 @writeport(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
store i32 1, i32* %6
store i32 0, i32* %7
store i32 1, i32* %7
br label %8
8: ; preds = %11, %2
%9 = load i32, i32* %7
%10 = icmp ne i32 %9, 0
br i1 %10, label %11, label %15
11: ; preds = %8
%12 = load i32, i32* %4
%13 = load i32, i32* @TCIFLUSH
%14 = call i32 @tcflush(i32 %12, i32 %13)
store i32 %14, i32* %7
br label %8
15: ; preds = %8
%16 = load i32, i32* %7
%17 = icmp eq i32 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %15
%19 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
br label %20
20: ; preds = %18, %15
%21 = call i32 @usleep(i32 5000)
%22 = load i32, i32* %4
%23 = load i8*, i8** %5
%24 = load i32, i32* %6
%25 = call i32 @write(i32 %22, i8* %23, i32 %24)
store i32 %25, i32* %7
%26 = call i32 @usleep(i32 500000)
%27 = load i32, i32* %7
%28 = icmp slt i32 %27, 0
br i1 %28, label %29, label %32
29: ; preds = %20
%30 = load i32, i32* @stderr
%31 = call i32 @fputs(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i32 %30)
store i32 0, i32* %3
br label %33
32: ; preds = %20
store i32 1, i32* %3
br label %33
33: ; preds = %32, %29
%34 = load i32, i32* %3
ret i32 %34
}

LLVM IR (-O2):
@TCIFLUSH = global i32 0
@.str.1 = [15 x i8] c"write failed!\0A\00"
@stderr = global i32 0
@str = [25 x i8] c"successful buffer flush \00"
define i32 @writeport(i32 %0, i8* nocapture %1) {
br label %3
3: ; preds = %2, %3
%4 = load i32, i32* @TCIFLUSH
%5 = tail call i32 @tcflush(i32 %0, i32 %4)
%6 = icmp eq i32 %5, 0
br i1 %6, label %7, label %3
7: ; preds = %3
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str, i64 0, i64 0))
%9 = tail call i32 @usleep(i32 5000)
%10 = tail call i32 @write(i32 %0, i8* %1, i32 1)
%11 = tail call i32 @usleep(i32 500000)
%12 = icmp slt i32 %10, 0
br i1 %12, label %13, label %16
13: ; preds = %7
%14 = load i32, i32* @stderr
%15 = tail call i32 @fputs(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i32 %14)
br label %16
16: ; preds = %7, %13
%17 = phi i32 [ 0, %13 ], [ 1, %7 ]
ret i32 %17
}