#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 serial {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ serial_getc(struct serial *);
int serial_has_data(struct serial *, int /*<<< orphan*/, int /*<<< orphan*/);
void serial_discard(struct serial *self, int max) {
  int i;
  do {
    i = serial_has_data(self, 0, 0);
    if (i) {
      serial_getc(self);
      if (max > 0)
        max--;
      if (max == 0)
        i = 0;
    }
  } while (i);
}


x86 assembly (clang, -O0):
<serial_discard>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
xor    %edx,%edx
mov    %edx,%esi
call   1030 <serial_has_data@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
je     117b <serial_discard+0x5b>
mov    -0x8(%rbp),%rdi
call   1040 <serial_getc@plt>
cmpl   $0x0,-0xc(%rbp)
jle    1165 <serial_discard+0x45>
mov    -0xc(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jne    1176 <serial_discard+0x56>
movl   $0x0,-0x10(%rbp)
jmp    117b <serial_discard+0x5b>
jmp    1180 <serial_discard+0x60>
cmpl   $0x0,-0x10(%rbp)
jne    112f <serial_discard+0xf>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<serial_discard>:
push   %r14
push   %rbx
push   %rax
mov    %esi,%ebx
mov    %rdi,%r14
nopl   0x0(%rax)
mov    %r14,%rdi
xor    %esi,%esi
xor    %edx,%edx
call   1030 <serial_has_data@plt>
test   %eax,%eax
je     1153 <serial_discard+0x33>
mov    %r14,%rdi
call   1040 <serial_getc@plt>
xor    %eax,%eax
test   %ebx,%ebx
setg   %al
sub    %eax,%ebx
jne    1130 <serial_discard+0x10>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.serial = type { i32 }
define void @serial_discard(%struct.serial* %0, i32 %1) {
%3 = alloca %struct.serial*
%4 = alloca i32
%5 = alloca i32
store %struct.serial* %0, %struct.serial** %3
store i32 %1, i32* %4
br label %6
6: ; preds = %25, %2
%7 = load %struct.serial*, %struct.serial** %3
%8 = call i32 @serial_has_data(%struct.serial* %7, i32 0, i32 0)
store i32 %8, i32* %5
%9 = load i32, i32* %5
%10 = icmp ne i32 %9, 0
br i1 %10, label %11, label %24
11: ; preds = %6
%12 = load %struct.serial*, %struct.serial** %3
%13 = call i32 @serial_getc(%struct.serial* %12)
%14 = load i32, i32* %4
%15 = icmp sgt i32 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %11
%17 = load i32, i32* %4
%18 = add nsw i32 %17, -1
store i32 %18, i32* %4
br label %19
19: ; preds = %16, %11
%20 = load i32, i32* %4
%21 = icmp eq i32 %20, 0
br i1 %21, label %22, label %23
22: ; preds = %19
store i32 0, i32* %5
br label %23
23: ; preds = %22, %19
br label %24
24: ; preds = %23, %6
br label %25
25: ; preds = %24
%26 = load i32, i32* %5
%27 = icmp ne i32 %26, 0
br i1 %27, label %6, label %28
28: ; preds = %25
ret void
}

LLVM IR (-O2):
%struct.serial = type { i32 }
define void @serial_discard(%struct.serial* %0, i32 %1) {
br label %3
3: ; preds = %7, %2
%4 = phi i32 [ %1, %2 ], [ %11, %7 ]
%5 = tail call i32 @serial_has_data(%struct.serial* %0, i32 0, i32 0)
%6 = icmp eq i32 %5, 0
br i1 %6, label %13, label %7
7: ; preds = %3
%8 = tail call i32 @serial_getc(%struct.serial* %0)
%9 = icmp sgt i32 %4, 0
%10 = sext i1 %9 to i32
%11 = add nsw i32 %4, %10
%12 = icmp eq i32 %11, 0
br i1 %12, label %13, label %3
13: ; preds = %3, %7
ret void
}