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

/* Variables and functions */
int /*<<< orphan*/ ringbuffer_pop(struct ringbuffer *, unsigned char *);
unsigned int ringbuffer_read(struct ringbuffer *buffer, unsigned char *b,
                             unsigned int len) {
  unsigned int l = 0;
  for (int i = 0; i < len; i++) {
    if (!ringbuffer_pop(buffer, &b[i])) {
      l++;
    } else {
      break;
    }
  }
  return l;
}


x86 assembly (clang, -O0):
<ringbuffer_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jae    1180 <ringbuffer_read+0x70>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
movslq -0x1c(%rbp),%rax
add    %rax,%rsi
call   1030 <ringbuffer_pop@plt>
cmp    $0x0,%eax
jne    1168 <ringbuffer_read+0x58>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    116d <ringbuffer_read+0x5d>
jmp    1180 <ringbuffer_read+0x70>
jmp    1172 <ringbuffer_read+0x62>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1131 <ringbuffer_read+0x21>
mov    -0x18(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ringbuffer_read>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
test   %edx,%edx
je     114e <ringbuffer_read+0x3e>
mov    %rsi,%r14
mov    %rdi,%r15
mov    %edx,%r12d
xor    %ebx,%ebx
nopw   0x0(%rax,%rax,1)
lea    (%r14,%rbx,1),%rsi
mov    %r15,%rdi
call   1030 <ringbuffer_pop@plt>
test   %eax,%eax
jne    1150 <ringbuffer_read+0x40>
add    $0x1,%rbx
cmp    %rbx,%r12
jne    1130 <ringbuffer_read+0x20>
mov    %r12d,%ebx
jmp    1150 <ringbuffer_read+0x40>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.ringbuffer = type { i32 }
define i32 @ringbuffer_read(%struct.ringbuffer* %0, i8* %1, i32 %2) {
%4 = alloca %struct.ringbuffer*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store %struct.ringbuffer* %0, %struct.ringbuffer** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
store i32 0, i32* %7
store i32 0, i32* %8
br label %9
9: ; preds = %26, %3
%10 = load i32, i32* %8
%11 = load i32, i32* %6
%12 = icmp ult i32 %10, %11
br i1 %12, label %13, label %29
13: ; preds = %9
%14 = load %struct.ringbuffer*, %struct.ringbuffer** %4
%15 = load i8*, i8** %5
%16 = load i32, i32* %8
%17 = sext i32 %16 to i64
%18 = getelementptr inbounds i8, i8* %15, i64 %17
%19 = call i32 @ringbuffer_pop(%struct.ringbuffer* %14, i8* %18)
%20 = icmp ne i32 %19, 0
br i1 %20, label %24, label %21
21: ; preds = %13
%22 = load i32, i32* %7
%23 = add i32 %22, 1
store i32 %23, i32* %7
br label %25
24: ; preds = %13
br label %29
25: ; preds = %21
br label %26
26: ; preds = %25
%27 = load i32, i32* %8
%28 = add nsw i32 %27, 1
store i32 %28, i32* %8
br label %9
29: ; preds = %24, %9
%30 = load i32, i32* %7
ret i32 %30
}

LLVM IR (-O2):
%struct.ringbuffer = type { i32 }
define i32 @ringbuffer_read(%struct.ringbuffer* %0, i8* %1, i32 %2) {
%4 = icmp eq i32 %2, 0
br i1 %4, label %17, label %5
5: ; preds = %3
%6 = zext i32 %2 to i64
br label %7
7: ; preds = %5, %12
%8 = phi i64 [ 0, %5 ], [ %13, %12 ]
%9 = getelementptr inbounds i8, i8* %1, i64 %8
%10 = tail call i32 @ringbuffer_pop(%struct.ringbuffer* %0, i8* %9)
%11 = icmp eq i32 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %7
%13 = add nuw nsw i64 %8, 1
%14 = icmp eq i64 %13, %6
br i1 %14, label %17, label %7
15: ; preds = %7
%16 = trunc i64 %8 to i32
br label %17
17: ; preds = %12, %15, %3
%18 = phi i32 [ 0, %3 ], [ %16, %15 ], [ %2, %12 ]
ret i32 %18
}