#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 gbser__fill_buffer(void *, unsigned int, unsigned int *);
scalar_t__ gbser__read_buffer(void *, void **, unsigned int *);
int gbser_read_wait(void *handle, void *buf, unsigned len, unsigned ms) {
  int got = 0;
  while (len > 0 && ms != 0) {
    int rc;
    if (rc = gbser__fill_buffer(handle, len, &ms), rc < 0) {
      return rc;
    }
    got += gbser__read_buffer(handle, &buf, &len);
  }
  return got;
}


x86 assembly (clang, -O0):
<gbser_read_wait>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %ecx,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x1c(%rbp)
mov    %al,-0x29(%rbp)
jbe    1156 <gbser_read_wait+0x36>
cmpl   $0x0,-0x20(%rbp)
setne  %al
mov    %al,-0x29(%rbp)
mov    -0x29(%rbp),%al
test   $0x1,%al
jne    1166 <gbser_read_wait+0x46>
jmp    11b1 <gbser_read_wait+0x91>
mov    -0x10(%rbp),%rdi
mov    -0x1c(%rbp),%esi
lea    -0x20(%rbp),%rdx
call   1040 <gbser__fill_buffer@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jge    118e <gbser_read_wait+0x6e>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11b7 <gbser_read_wait+0x97>
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
lea    -0x1c(%rbp),%rdx
call   1030 <gbser__read_buffer@plt>
mov    %rax,%rcx
movslq -0x24(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x24(%rbp)
jmp    113d <gbser_read_wait+0x1d>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gbser_read_wait>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsi,0x8(%rsp)
mov    %edx,0x4(%rsp)
mov    %ecx,(%rsp)
xor    %ebx,%ebx
test   %edx,%edx
je     1193 <gbser_read_wait+0x73>
test   %ecx,%ecx
je     1193 <gbser_read_wait+0x73>
mov    %rdi,%r14
xor    %ebx,%ebx
mov    %rsp,%r15
lea    0x8(%rsp),%r12
lea    0x4(%rsp),%r13
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r14,%rdi
mov    %edx,%esi
mov    %r15,%rdx
call   1040 <gbser__fill_buffer@plt>
test   %eax,%eax
js     1191 <gbser_read_wait+0x71>
mov    %r14,%rdi
mov    %r12,%rsi
mov    %r13,%rdx
call   1030 <gbser__read_buffer@plt>
add    %eax,%ebx
mov    0x4(%rsp),%edx
test   %edx,%edx
je     1193 <gbser_read_wait+0x73>
cmpl   $0x0,(%rsp)
jne    1160 <gbser_read_wait+0x40>
jmp    1193 <gbser_read_wait+0x73>
mov    %eax,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i32 @gbser_read_wait(i8* %0, i8* %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i8* %0, i8** %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store i32 %3, i32* %9
store i32 0, i32* %10
br label %12
12: ; preds = %28, %4
%13 = load i32, i32* %8
%14 = icmp ugt i32 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %12
%16 = load i32, i32* %9
%17 = icmp ne i32 %16, 0
br label %18
18: ; preds = %15, %12
%19 = phi i1 [ false, %12 ], [ %17, %15 ]
br i1 %19, label %20, label %35
20: ; preds = %18
%21 = load i8*, i8** %6
%22 = load i32, i32* %8
%23 = call i32 @gbser__fill_buffer(i8* %21, i32 %22, i32* %9)
store i32 %23, i32* %11
%24 = load i32, i32* %11
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %20
%27 = load i32, i32* %11
store i32 %27, i32* %5
br label %37
28: ; preds = %20
%29 = load i8*, i8** %6
%30 = call i64 @gbser__read_buffer(i8* %29, i8** %7, i32* %8)
%31 = load i32, i32* %10
%32 = sext i32 %31 to i64
%33 = add nsw i64 %32, %30
%34 = trunc i64 %33 to i32
store i32 %34, i32* %10
br label %12
35: ; preds = %18
%36 = load i32, i32* %10
store i32 %36, i32* %5
br label %37
37: ; preds = %35, %26
%38 = load i32, i32* %5
ret i32 %38
}

LLVM IR (-O2):
define i32 @gbser_read_wait(i8* %0, i8* %1, i32 %2, i32 %3) {
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
store i8* %1, i8** %5
store i32 %2, i32* %6
store i32 %3, i32* %7
%8 = icmp ne i32 %2, 0
%9 = icmp ne i32 %3, 0
%10 = and i1 %8, %9
br i1 %10, label %11, label %25
11: ; preds = %4, %16
%12 = phi i32 [ %20, %16 ], [ %2, %4 ]
%13 = phi i32 [ %19, %16 ], [ 0, %4 ]
%14 = call i32 @gbser__fill_buffer(i8* %0, i32 %12, i32* %7)
%15 = icmp slt i32 %14, 0
br i1 %15, label %25, label %16
16: ; preds = %11
%17 = call i64 @gbser__read_buffer(i8* %0, i8** %5, i32* %6)
%18 = trunc i64 %17 to i32
%19 = add i32 %13, %18
%20 = load i32, i32* %6
%21 = icmp ne i32 %20, 0
%22 = load i32, i32* %7
%23 = icmp ne i32 %22, 0
%24 = select i1 %21, i1 %23, i1 false
br i1 %24, label %11, label %25
25: ; preds = %16, %11, %4
%26 = phi i32 [ 0, %4 ], [ %14, %11 ], [ %19, %16 ]
ret i32 %26
}