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

/* Variables and functions */
void *__ptr_ring_consume(struct ptr_ring *);
int __ptr_ring_consume_batched(struct ptr_ring *r, void **array, int n) {
  void *ptr;
  int i;
  for (i = 0; i < n; i++) {
    ptr = __ptr_ring_consume(r);
    if (!ptr)
      break;
    array[i] = ptr;
  }
  return i;
}


x86 assembly (clang, -O0):
<__ptr_ring_consume_batched>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    1171 <__ptr_ring_consume_batched+0x61>
mov    -0x8(%rbp),%rdi
call   1030 <__ptr_ring_consume@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1153 <__ptr_ring_consume_batched+0x43>
jmp    1171 <__ptr_ring_consume_batched+0x61>
mov    -0x20(%rbp),%rdx
mov    -0x10(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    112a <__ptr_ring_consume_batched+0x1a>
mov    -0x24(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<__ptr_ring_consume_batched>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
test   %edx,%edx
jle    114f <__ptr_ring_consume_batched+0x3f>
mov    %rsi,%r14
mov    %rdi,%r15
mov    %edx,%r12d
xor    %ebx,%ebx
nopw   0x0(%rax,%rax,1)
mov    %r15,%rdi
call   1030 <__ptr_ring_consume@plt>
test   %rax,%rax
je     1151 <__ptr_ring_consume_batched+0x41>
mov    %rax,(%r14,%rbx,8)
add    $0x1,%rbx
cmp    %rbx,%r12
jne    1130 <__ptr_ring_consume_batched+0x20>
mov    %r12d,%ebx
jmp    1151 <__ptr_ring_consume_batched+0x41>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
%struct.ptr_ring = type { i32 }
define i32 @__ptr_ring_consume_batched(%struct.ptr_ring* %0, i8** nocapture writeonly %1, i32 %2) {
%4 = icmp sgt i32 %2, 0
br i1 %4, label %5, label %17
5: ; preds = %3
%6 = zext i32 %2 to i64
br label %7
7: ; preds = %5, %11
%8 = phi i64 [ 0, %5 ], [ %13, %11 ]
%9 = tail call i8* @__ptr_ring_consume(%struct.ptr_ring* %0)
%10 = icmp eq i8* %9, null
br i1 %10, label %15, label %11
11: ; preds = %7
%12 = getelementptr inbounds i8*, i8** %1, i64 %8
store i8* %9, i8** %12
%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 = %11, %15, %3
%18 = phi i32 [ 0, %3 ], [ %16, %15 ], [ %2, %11 ]
ret i32 %18
}