#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_push(struct ringbuffer *, unsigned char);
unsigned int ringbuffer_write(struct ringbuffer *buffer, unsigned char *b,
                              unsigned int len) {
  unsigned int l = 0;
  for (int i = 0; i < len; i++) {
    if (!ringbuffer_push(buffer, b[i])) {
      l++;
    } else {
      break;
    }
  }
  return l;
}


x86 assembly (clang, -O0):
<ringbuffer_write>:
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    1181 <ringbuffer_write+0x71>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movzbl (%rax,%rcx,1),%esi
call   1030 <ringbuffer_push@plt>
cmp    $0x0,%eax
jne    1169 <ringbuffer_write+0x59>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    116e <ringbuffer_write+0x5e>
jmp    1181 <ringbuffer_write+0x71>
jmp    1173 <ringbuffer_write+0x63>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1131 <ringbuffer_write+0x21>
mov    -0x18(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ringbuffer_write>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
test   %edx,%edx
je     114f <ringbuffer_write+0x3f>
mov    %rsi,%r14
mov    %rdi,%r15
mov    %edx,%r12d
xor    %ebx,%ebx
nopw   0x0(%rax,%rax,1)
movzbl (%r14,%rbx,1),%esi
mov    %r15,%rdi
call   1030 <ringbuffer_push@plt>
test   %eax,%eax
jne    1151 <ringbuffer_write+0x41>
add    $0x1,%rbx
cmp    %rbx,%r12
jne    1130 <ringbuffer_write+0x20>
mov    %r12d,%ebx
jmp    1151 <ringbuffer_write+0x41>
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_write(%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 = %27, %3
%10 = load i32, i32* %8
%11 = load i32, i32* %6
%12 = icmp ult i32 %10, %11
br i1 %12, label %13, label %30
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 = load i8, i8* %18
%20 = call i32 @ringbuffer_push(%struct.ringbuffer* %14, i8 zeroext %19)
%21 = icmp ne i32 %20, 0
br i1 %21, label %25, label %22
22: ; preds = %13
%23 = load i32, i32* %7
%24 = add i32 %23, 1
store i32 %24, i32* %7
br label %26
25: ; preds = %13
br label %30
26: ; preds = %22
br label %27
27: ; preds = %26
%28 = load i32, i32* %8
%29 = add nsw i32 %28, 1
store i32 %29, i32* %8
br label %9
30: ; preds = %25, %9
%31 = load i32, i32* %7
ret i32 %31
}

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