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

/* Variables and functions */
int /*<<< orphan*/ printf(char *, ...);
int ringbuffer_data(struct ringbuffer *, struct ringbuffer_block *, int,
                    int /*<<< orphan*/, void **);
void dump(struct ringbuffer *rb, struct ringbuffer_block *blk, int size) {
  void *buffer;
  int sz = ringbuffer_data(rb, blk, size, 0, &buffer);
  char *data = buffer;
  if (data) {
    int i;
    for (i = 0; i < sz; i++) {
      printf("%d ", data[i]);
    }
    printf("\n");
  } else {
    printf("size = %d\n", sz);
  }
}


x86 assembly (clang, -O0):
<dump>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
xor    %ecx,%ecx
lea    -0x20(%rbp),%r8
call   1040 <ringbuffer_data@plt>
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     11ad <dump+0x8d>
movl   $0x0,-0x34(%rbp)
mov    -0x34(%rbp),%eax
cmp    -0x24(%rbp),%eax
jge    119a <dump+0x7a>
mov    -0x30(%rbp),%rax
movslq -0x34(%rbp),%rcx
movsbl (%rax,%rcx,1),%esi
lea    0xe7b(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    1166 <dump+0x46>
lea    0xe6c(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    11be <dump+0x9e>
mov    -0x24(%rbp),%esi
lea    0xe4d(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dump>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsp,%r8
xor    %ecx,%ecx
call   1050 <ringbuffer_data@plt>
mov    (%rsp),%r15
test   %r15,%r15
je     1184 <dump+0x54>
test   %eax,%eax
jle    1178 <dump+0x48>
mov    %eax,%r12d
lea    0xea7(%rip),%r14
xor    %ebx,%ebx
nopl   0x0(%rax,%rax,1)
movsbl (%r15,%rbx,1),%esi
mov    %r14,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
add    $0x1,%rbx
cmp    %rbx,%r12
jne    1160 <dump+0x30>
mov    $0xa,%edi
call   1030 <putchar@plt>
jmp    1194 <dump+0x64>
lea    0xe79(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.ringbuffer = type { i32 }
%struct.ringbuffer_block = type { i32 }
@.str = [4 x i8] c"%d \00"
@.str.1 = [2 x i8] c"\0A\00"
@.str.2 = [11 x i8] c"size = %d\0A\00"
define void @dump(%struct.ringbuffer* %0, %struct.ringbuffer_block* %1, i32 %2) {
%4 = alloca %struct.ringbuffer*
%5 = alloca %struct.ringbuffer_block*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i32
store %struct.ringbuffer* %0, %struct.ringbuffer** %4
store %struct.ringbuffer_block* %1, %struct.ringbuffer_block** %5
store i32 %2, i32* %6
%11 = load %struct.ringbuffer*, %struct.ringbuffer** %4
%12 = load %struct.ringbuffer_block*, %struct.ringbuffer_block** %5
%13 = load i32, i32* %6
%14 = call i32 @ringbuffer_data(%struct.ringbuffer* %11, %struct.ringbuffer_block* %12, i32 %13, i32 0, i8** %7)
store i32 %14, i32* %8
%15 = load i8*, i8** %7
store i8* %15, i8** %9
%16 = load i8*, i8** %9
%17 = icmp ne i8* %16, null
br i1 %17, label %18, label %36
18: ; preds = %3
store i32 0, i32* %10
br label %19
19: ; preds = %31, %18
%20 = load i32, i32* %10
%21 = load i32, i32* %8
%22 = icmp slt i32 %20, %21
br i1 %22, label %23, label %34
23: ; preds = %19
%24 = load i8*, i8** %9
%25 = load i32, i32* %10
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds i8, i8* %24, i64 %26
%28 = load i8, i8* %27
%29 = sext i8 %28 to i32
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %29)
br label %31
31: ; preds = %23
%32 = load i32, i32* %10
%33 = add nsw i32 %32, 1
store i32 %33, i32* %10
br label %19
34: ; preds = %19
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
br label %39
36: ; preds = %3
%37 = load i32, i32* %8
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i32 %37)
br label %39
39: ; preds = %36, %34
ret void
}

LLVM IR (-O2):
%struct.ringbuffer = type { i32 }
%struct.ringbuffer_block = type { i32 }
@.str = [4 x i8] c"%d \00"
@.str.2 = [11 x i8] c"size = %d\0A\00"
define void @dump(%struct.ringbuffer* %0, %struct.ringbuffer_block* %1, i32 %2) {
%4 = alloca i8*
%5 = bitcast i8** %4 to i8*
%6 = call i32 @ringbuffer_data(%struct.ringbuffer* %0, %struct.ringbuffer_block* %1, i32 %2, i32 0, i8** %4)
%7 = load i8*, i8** %4
%8 = icmp eq i8* %7, null
br i1 %8, label %23, label %9
9: ; preds = %3
%10 = icmp sgt i32 %6, 0
br i1 %10, label %11, label %21
11: ; preds = %9
%12 = zext i32 %6 to i64
br label %13
13: ; preds = %11, %13
%14 = phi i64 [ 0, %11 ], [ %19, %13 ]
%15 = getelementptr inbounds i8, i8* %7, i64 %14
%16 = load i8, i8* %15
%17 = sext i8 %16 to i32
%18 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %17)
%19 = add nuw nsw i64 %14, 1
%20 = icmp eq i64 %19, %12
br i1 %20, label %21, label %13
21: ; preds = %13, %9
%22 = call i32 @putchar(i32 10)
br label %25
23: ; preds = %3
%24 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i32 %6)
br label %25
25: ; preds = %23, %21
ret void
}