#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 carmen_serial_numChars(int);
int /*<<< orphan*/ read(int, unsigned char **, int);
int carmen_serial_ClearInputBuffer(int dev_fd) {
  int max_serial_buffer_size = 16384;
  unsigned char buffer[max_serial_buffer_size];
  int val = 0;
  int val_total = 0;
  val_total = carmen_serial_numChars(dev_fd);
  val = val_total;
  while (val > max_serial_buffer_size) {
    read(dev_fd, &buffer, max_serial_buffer_size);
    val -= max_serial_buffer_size;
  }
  if (val > 0)
    read(dev_fd, &buffer, val);
  return (val_total);
}


x86 assembly (clang, -O0):
<carmen_serial_ClearInputBuffer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x4000,-0x8(%rbp)
mov    -0x8(%rbp),%eax
mov    %rsp,%rcx
mov    %rcx,-0x10(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x28(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x4(%rbp),%edi
call   1040 <carmen_serial_numChars@plt>
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x8(%rbp),%eax
jle    11a2 <carmen_serial_ClearInputBuffer+0x82>
mov    -0x28(%rbp),%rsi
mov    -0x4(%rbp),%edi
mov    -0x8(%rbp),%edx
call   1030 <read@plt>
mov    -0x8(%rbp),%ecx
mov    -0x1c(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x1c(%rbp)
jmp    1177 <carmen_serial_ClearInputBuffer+0x57>
cmpl   $0x0,-0x1c(%rbp)
jle    11bb <carmen_serial_ClearInputBuffer+0x9b>
mov    -0x28(%rbp),%rsi
mov    -0x4(%rbp),%edi
mov    -0x1c(%rbp),%edx
call   1030 <read@plt>
mov    -0x20(%rbp),%eax
mov    -0x10(%rbp),%rcx
mov    %rcx,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<carmen_serial_ClearInputBuffer>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x4008,%rsp
mov    %edi,%ebp
call   1040 <carmen_serial_numChars@plt>
mov    %eax,%r14d
cmp    $0x4001,%eax
jl     1171 <carmen_serial_ClearInputBuffer+0x51>
mov    %rsp,%r15
mov    %r14d,%ebx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0x4000,%edx
call   1030 <read@plt>
lea    -0x4000(%rbx),%edx
cmp    $0x8000,%ebx
mov    %edx,%ebx
ja     1150 <carmen_serial_ClearInputBuffer+0x30>
jmp    1179 <carmen_serial_ClearInputBuffer+0x59>
mov    %r14d,%edx
test   %r14d,%r14d
jle    1183 <carmen_serial_ClearInputBuffer+0x63>
mov    %rsp,%rsi
mov    %ebp,%edi
call   1030 <read@plt>
mov    %r14d,%eax
add    $0x4008,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @carmen_serial_ClearInputBuffer(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %2
store i32 16384, i32* %3
%8 = load i32, i32* %3
%9 = zext i32 %8 to i64
%10 = call i8* @llvm.stacksave()
store i8* %10, i8** %4
%11 = alloca i8, i64 %9
store i64 %9, i64* %5
store i32 0, i32* %6
store i32 0, i32* %7
%12 = load i32, i32* %2
%13 = call i32 @carmen_serial_numChars(i32 %12)
store i32 %13, i32* %7
%14 = load i32, i32* %7
store i32 %14, i32* %6
br label %15
15: ; preds = %19, %1
%16 = load i32, i32* %6
%17 = load i32, i32* %3
%18 = icmp sgt i32 %16, %17
br i1 %18, label %19, label %27
19: ; preds = %15
%20 = load i32, i32* %2
%21 = bitcast i8* %11 to i8**
%22 = load i32, i32* %3
%23 = call i32 @read(i32 %20, i8** %21, i32 %22)
%24 = load i32, i32* %3
%25 = load i32, i32* %6
%26 = sub nsw i32 %25, %24
store i32 %26, i32* %6
br label %15
27: ; preds = %15
%28 = load i32, i32* %6
%29 = icmp sgt i32 %28, 0
br i1 %29, label %30, label %35
30: ; preds = %27
%31 = load i32, i32* %2
%32 = bitcast i8* %11 to i8**
%33 = load i32, i32* %6
%34 = call i32 @read(i32 %31, i8** %32, i32 %33)
br label %35
35: ; preds = %30, %27
%36 = load i32, i32* %7
%37 = load i8*, i8** %4
ret i32 %36
}

LLVM IR (-O2):
define i32 @carmen_serial_ClearInputBuffer(i32 %0) {
%2 = alloca [16384 x i8]
%3 = tail call i32 @carmen_serial_numChars(i32 %0)
%4 = icmp sgt i32 %3, 16384
br i1 %4, label %5, label %12
5: ; preds = %1
%6 = bitcast [16384 x i8]* %2 to i8**
br label %7
7: ; preds = %5, %7
%8 = phi i32 [ %3, %5 ], [ %10, %7 ]
%9 = call i32 @read(i32 %0, i8** %6, i32 16384)
%10 = add nsw i32 %8, -16384
%11 = icmp ugt i32 %8, 32768
br i1 %11, label %7, label %14
12: ; preds = %1
%13 = icmp sgt i32 %3, 0
br i1 %13, label %14, label %18
14: ; preds = %7, %12
%15 = phi i32 [ %3, %12 ], [ %10, %7 ]
%16 = bitcast [16384 x i8]* %2 to i8**
%17 = call i32 @read(i32 %0, i8** %16, i32 %15)
br label %18
18: ; preds = %14, %12
ret i32 %3
}