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

/* Variables and functions */
scalar_t__ rb_putc(struct ringbuf *, int /*<<< orphan*/);
int rb_write(struct ringbuf *rb, const void *data, int len) {
  int i = 0;
  char *d_ptr = (char *)data;
  while (len > 0 && rb_putc(rb, *d_ptr++)) {
    len--;
    i++;
  }
  return (i);
}


x86 assembly (clang, -O0):
<rb_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x14(%rbp)
mov    %al,-0x21(%rbp)
jle    1166 <rb_write+0x56>
mov    -0x8(%rbp),%rdi
mov    -0x20(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x20(%rbp)
movsbl (%rax),%esi
call   1030 <rb_putc@plt>
cmp    $0x0,%rax
setne  %al
mov    %al,-0x21(%rbp)
mov    -0x21(%rbp),%al
test   $0x1,%al
jne    1176 <rb_write+0x66>
jmp    118d <rb_write+0x7d>
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1132 <rb_write+0x22>
mov    -0x18(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rb_write>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
test   %edx,%edx
jle    114d <rb_write+0x3d>
mov    %rsi,%r15
mov    %rdi,%r12
mov    %edx,%r14d
xor    %ebx,%ebx
nopw   0x0(%rax,%rax,1)
movsbl (%r15,%rbx,1),%esi
mov    %r12,%rdi
call   1030 <rb_putc@plt>
test   %rax,%rax
je     1152 <rb_write+0x42>
add    $0x1,%rbx
cmp    %ebx,%r14d
jne    1130 <rb_write+0x20>
jmp    1155 <rb_write+0x45>
xor    %r14d,%r14d
jmp    1155 <rb_write+0x45>
mov    %ebx,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

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