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

/* Variables and functions */
scalar_t__ s_ring_buffer_xout(struct s_ring_buffer *, char *);
unsigned int s_ring_buffer_out(struct s_ring_buffer *rb, char *to,
                               unsigned int len) {
  int i;
  for (i = 0; i < len && s_ring_buffer_xout(rb, &to[i]); i++) {
  }
  return i;
}


x86 assembly (clang, -O0):
<s_ring_buffer_out>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%ecx
xor    %eax,%eax
cmp    -0x14(%rbp),%ecx
mov    %al,-0x19(%rbp)
jae    1159 <s_ring_buffer_out+0x49>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
movslq -0x18(%rbp),%rax
add    %rax,%rsi
call   1030 <s_ring_buffer_xout@plt>
cmp    $0x0,%rax
setne  %al
mov    %al,-0x19(%rbp)
mov    -0x19(%rbp),%al
test   $0x1,%al
jne    1169 <s_ring_buffer_out+0x59>
jmp    117c <s_ring_buffer_out+0x6c>
jmp    116e <s_ring_buffer_out+0x5e>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    112a <s_ring_buffer_out+0x1a>
mov    -0x18(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<s_ring_buffer_out>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
test   %edx,%edx
je     114f <s_ring_buffer_out+0x3f>
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 <s_ring_buffer_xout@plt>
test   %rax,%rax
je     1151 <s_ring_buffer_out+0x41>
add    $0x1,%rbx
cmp    %rbx,%r12
jne    1130 <s_ring_buffer_out+0x20>
mov    %r12d,%ebx
jmp    1151 <s_ring_buffer_out+0x41>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
%struct.s_ring_buffer = type { i32 }
define i32 @s_ring_buffer_out(%struct.s_ring_buffer* %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 i64 @s_ring_buffer_xout(%struct.s_ring_buffer* %0, i8* %9)
%11 = icmp eq i64 %10, 0
br i1 %11, label %15, label %12
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
}