#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 timeval {
  scalar_t__ tv_usec;
  scalar_t__ tv_sec;
};
typedef int /*<<< orphan*/ fd_set;

/* Variables and functions */
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int /*<<< orphan*/ read(int, unsigned char *, int);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int /*<<< orphan*/ usleep(int);
void tp_serial_flush_input(int fd) {
  struct timeval tv;
  fd_set rfds;
  unsigned char junk;
  FD_ZERO(&rfds);
  FD_SET(fd, &rfds);
  tv.tv_sec = 0;
  tv.tv_usec = 0;
  usleep(50000);
  while (select(fd + 1, &rfds, NULL, NULL, &tv) == 1) {
    read(fd, &junk, 1);
    FD_ZERO(&rfds);
    FD_SET(fd, &rfds);
    tv.tv_sec = 0;
    tv.tv_usec = 0;
  }
}


x86 assembly (clang, -O0):
<tp_serial_flush_input>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
lea    -0x1c(%rbp),%rdi
call   1040 <FD_ZERO@plt>
mov    -0x4(%rbp),%edi
lea    -0x1c(%rbp),%rsi
call   1060 <FD_SET@plt>
movq   $0x0,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
mov    $0xc350,%edi
call   1070 <usleep@plt>
mov    -0x4(%rbp),%edi
add    $0x1,%edi
lea    -0x1c(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x18(%rbp),%r8
mov    %rcx,%rdx
call   1050 <select@plt>
cmp    $0x1,%eax
jne    11e8 <tp_serial_flush_input+0x98>
mov    -0x4(%rbp),%edi
lea    -0x1d(%rbp),%rsi
mov    $0x1,%edx
call   1030 <read@plt>
lea    -0x1c(%rbp),%rdi
call   1040 <FD_ZERO@plt>
mov    -0x4(%rbp),%edi
lea    -0x1c(%rbp),%rsi
call   1060 <FD_SET@plt>
movq   $0x0,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
jmp    118a <tp_serial_flush_input+0x3a>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tp_serial_flush_input>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    %edi,%ebx
lea    0xc(%rsp),%rbp
mov    %rbp,%rdi
call   1040 <FD_ZERO@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
call   1060 <FD_SET@plt>
xorps  %xmm0,%xmm0
movaps %xmm0,0x10(%rsp)
mov    $0xc350,%edi
call   1070 <usleep@plt>
lea    0x1(%rbx),%r15d
lea    0x10(%rsp),%r8
mov    %r15d,%edi
mov    %rbp,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
call   1050 <select@plt>
cmp    $0x1,%eax
jne    1200 <tp_serial_flush_input+0xb0>
lea    0xb(%rsp),%r14
lea    0xc(%rsp),%rbp
lea    0x10(%rsp),%r12
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1030 <read@plt>
mov    %rbp,%rdi
call   1040 <FD_ZERO@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
call   1060 <FD_SET@plt>
xorps  %xmm0,%xmm0
movaps %xmm0,0x10(%rsp)
mov    %r15d,%edi
mov    %rbp,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
mov    %r12,%r8
call   1050 <select@plt>
cmp    $0x1,%eax
je     11c0 <tp_serial_flush_input+0x70>
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i64, i64 }
define void @tp_serial_flush_input(i32 %0) {
%2 = alloca i32
%3 = alloca %struct.timeval
%4 = alloca i32
%5 = alloca i8
store i32 %0, i32* %2
%6 = call i32 @FD_ZERO(i32* %4)
%7 = load i32, i32* %2
%8 = call i32 @FD_SET(i32 %7, i32* %4)
%9 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i32 0, i32 1
store i64 0, i64* %9
%10 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i32 0, i32 0
store i64 0, i64* %10
%11 = call i32 @usleep(i32 50000)
br label %12
12: ; preds = %17, %1
%13 = load i32, i32* %2
%14 = add nsw i32 %13, 1
%15 = call i32 @select(i32 %14, i32* %4, i32* null, i32* null, %struct.timeval* %3)
%16 = icmp eq i32 %15, 1
br i1 %16, label %17, label %25
17: ; preds = %12
%18 = load i32, i32* %2
%19 = call i32 @read(i32 %18, i8* %5, i32 1)
%20 = call i32 @FD_ZERO(i32* %4)
%21 = load i32, i32* %2
%22 = call i32 @FD_SET(i32 %21, i32* %4)
%23 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i32 0, i32 1
store i64 0, i64* %23
%24 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i32 0, i32 0
store i64 0, i64* %24
br label %12
25: ; preds = %12
ret void
}

LLVM IR (-O2):
%struct.timeval = type { i64, i64 }
define void @tp_serial_flush_input(i32 %0) {
%2 = alloca %struct.timeval
%3 = alloca i32
%4 = alloca i8
%5 = bitcast %struct.timeval* %2 to i8*
%6 = bitcast i32* %3 to i8*
%7 = call i32 @FD_ZERO(i32* %3)
%8 = call i32 @FD_SET(i32 %0, i32* %3)
%9 = bitcast %struct.timeval* %2 to i8*
%10 = call i32 @usleep(i32 50000)
%11 = add nsw i32 %0, 1
%12 = call i32 @select(i32 %11, i32* %3, i32* null, i32* null, %struct.timeval* %2)
%13 = icmp eq i32 %12, 1
br i1 %13, label %14, label %22
14: ; preds = %1
%15 = bitcast %struct.timeval* %2 to i8*
br label %16
16: ; preds = %14, %16
%17 = call i32 @read(i32 %0, i8* %4, i32 1)
%18 = call i32 @FD_ZERO(i32* %3)
%19 = call i32 @FD_SET(i32 %0, i32* %3)
%20 = call i32 @select(i32 %11, i32* %3, i32* null, i32* null, %struct.timeval* %2)
%21 = icmp eq i32 %20, 1
br i1 %21, label %16, label %22
22: ; preds = %16, %1
ret void
}