#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 */
typedef int /*<<< orphan*/ tmp2;
struct ringbuffer {
  int dummy;
};
typedef scalar_t__ ssize_t;

/* Variables and functions */
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, ...);
struct ringbuffer *rb_create(int);
scalar_t__ rb_dump(struct ringbuffer *, size_t *);
int /*<<< orphan*/ rb_get_space_free(struct ringbuffer *);
int /*<<< orphan*/ rb_get_space_used(struct ringbuffer *);
int /*<<< orphan*/ rb_read(struct ringbuffer *, char *, int);
scalar_t__ rb_write(struct ringbuffer *, char const *, int /*<<< orphan*/);
int /*<<< orphan*/ strlen(char const *);
int foo() {
  struct ringbuffer *rb = rb_create(1024);
  const char *tmp = "hello world";
  ssize_t ret = 0;
  size_t len = 0;
  int i;
  for (i = 0; i < 100; i++) {
    printf("free=%zu, used=%zu\n", rb_get_space_free(rb),
           rb_get_space_used(rb));
    ret = rb_write(rb, tmp, strlen(tmp));
    if (ret < 0) {
      break;
    }
  }
  printf("dump = %s\n", (char *)rb_dump(rb, &len));
  printf("rb_write len=%zu\n", len);
  char tmp2[9];
  memset(tmp2, 0, sizeof(tmp2));
  rb_read(rb, tmp2, sizeof(tmp2) - 1);
  printf("rb_read str=%s\n", tmp2);
  memset(tmp2, 0, sizeof(tmp2));
  rb_read(rb, tmp2, sizeof(tmp2) - 1);
  printf("rb_read str=%s\n", tmp2);
  return 0;
}


x86 assembly (clang, -O0):
<foo>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    $0x400,%edi
call   10a0 <rb_create@plt>
mov    %rax,-0x8(%rbp)
lea    0xe53(%rip),%rax
mov    %rax,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
cmpl   $0x64,-0x24(%rbp)
jge    1249 <foo+0xb9>
mov    -0x8(%rbp),%rdi
call   1080 <rb_get_space_free@plt>
mov    %eax,-0x44(%rbp)
mov    -0x8(%rbp),%rdi
call   1070 <rb_get_space_used@plt>
mov    -0x44(%rbp),%esi
mov    %eax,%edx
lea    0xe19(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x40(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    %eax,%edx
call   10b0 <rb_write@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jge    1236 <foo+0xa6>
jmp    1249 <foo+0xb9>
jmp    123b <foo+0xab>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    11c8 <foo+0x38>
mov    -0x8(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1050 <rb_dump@plt>
mov    %rax,%rsi
lea    0xdc0(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x20(%rbp),%rsi
lea    0xdb9(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x2d(%rbp),%rdi
xor    %esi,%esi
mov    $0x9,%edx
call   1060 <memset@plt>
mov    -0x8(%rbp),%rdi
lea    -0x2d(%rbp),%rsi
mov    $0x8,%edx
call   1090 <rb_read@plt>
lea    -0x2d(%rbp),%rsi
lea    0xd97(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x2d(%rbp),%rdi
xor    %esi,%esi
mov    $0x9,%edx
call   1060 <memset@plt>
mov    -0x8(%rbp),%rdi
lea    -0x2d(%rbp),%rsi
mov    $0x8,%edx
call   1090 <rb_read@plt>
lea    -0x2d(%rbp),%rsi
lea    0xd63(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<foo>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    $0x400,%edi
call   10a0 <rb_create@plt>
mov    %rax,%r12
movq   $0x0,0x8(%rsp)
mov    $0x63,%ebx
lea    0xe4e(%rip),%r14
lea    0xe3b(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r12,%rdi
call   1080 <rb_get_space_free@plt>
mov    %eax,%ebp
mov    %r12,%rdi
call   1070 <rb_get_space_used@plt>
mov    %r14,%rdi
mov    %ebp,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <printf@plt>
mov    %r15,%rdi
call   1030 <strlen@plt>
mov    %r12,%rdi
mov    %r15,%rsi
mov    %eax,%edx
call   10b0 <rb_write@plt>
sub    $0x1,%ebx
setb   %cl
test   %rax,%rax
js     1214 <foo+0x84>
test   %cl,%cl
je     11d0 <foo+0x40>
lea    0x8(%rsp),%rsi
mov    %r12,%rdi
call   1050 <rb_dump@plt>
lea    0xdf8(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x8(%rsp),%rsi
lea    0xded(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0x17(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x9,%edx
call   1060 <memset@plt>
mov    %r12,%rdi
mov    %r14,%rsi
mov    $0x8,%edx
call   1090 <rb_read@plt>
lea    0xdcd(%rip),%rbp
mov    %rbp,%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x9,%edx
call   1060 <memset@plt>
mov    %r12,%rdi
mov    %r14,%rsi
mov    $0x8,%edx
call   1090 <rb_read@plt>
mov    %rbp,%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.ringbuffer = type { i32 }
@.str = [12 x i8] c"hello world\00"
@.str.1 = [20 x i8] c"free=%zu, used=%zu\0A\00"
@.str.2 = [11 x i8] c"dump = %s\0A\00"
@.str.3 = [18 x i8] c"rb_write len=%zu\0A\00"
@.str.4 = [16 x i8] c"rb_read str=%s\0A\00"
define i32 @foo() {
%1 = alloca %struct.ringbuffer*
%2 = alloca i8*
%3 = alloca i64
%4 = alloca i64
%5 = alloca i32
%6 = alloca [9 x i8]
%7 = call %struct.ringbuffer* @rb_create(i32 1024)
store %struct.ringbuffer* %7, %struct.ringbuffer** %1
store i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8** %2
store i64 0, i64* %3
store i64 0, i64* %4
store i32 0, i32* %5
br label %8
8: ; preds = %26, %0
%9 = load i32, i32* %5
%10 = icmp slt i32 %9, 100
br i1 %10, label %11, label %29
11: ; preds = %8
%12 = load %struct.ringbuffer*, %struct.ringbuffer** %1
%13 = call i32 @rb_get_space_free(%struct.ringbuffer* %12)
%14 = load %struct.ringbuffer*, %struct.ringbuffer** %1
%15 = call i32 @rb_get_space_used(%struct.ringbuffer* %14)
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i32 %13, i32 %15)
%17 = load %struct.ringbuffer*, %struct.ringbuffer** %1
%18 = load i8*, i8** %2
%19 = load i8*, i8** %2
%20 = call i32 @strlen(i8* %19)
%21 = call i64 @rb_write(%struct.ringbuffer* %17, i8* %18, i32 %20)
store i64 %21, i64* %3
%22 = load i64, i64* %3
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %25
24: ; preds = %11
br label %29
25: ; preds = %11
br label %26
26: ; preds = %25
%27 = load i32, i32* %5
%28 = add nsw i32 %27, 1
store i32 %28, i32* %5
br label %8
29: ; preds = %24, %8
%30 = load %struct.ringbuffer*, %struct.ringbuffer** %1
%31 = call i64 @rb_dump(%struct.ringbuffer* %30, i64* %4)
%32 = inttoptr i64 %31 to i8*
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i8* %32)
%34 = load i64, i64* %4
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i64 %34)
%36 = getelementptr inbounds [9 x i8], [9 x i8]* %6, i64 0, i64 0
%37 = call i32 @memset(i8* %36, i32 0, i32 9)
%38 = load %struct.ringbuffer*, %struct.ringbuffer** %1
%39 = getelementptr inbounds [9 x i8], [9 x i8]* %6, i64 0, i64 0
%40 = call i32 @rb_read(%struct.ringbuffer* %38, i8* %39, i32 8)
%41 = getelementptr inbounds [9 x i8], [9 x i8]* %6, i64 0, i64 0
%42 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0), i8* %41)
%43 = getelementptr inbounds [9 x i8], [9 x i8]* %6, i64 0, i64 0
%44 = call i32 @memset(i8* %43, i32 0, i32 9)
%45 = load %struct.ringbuffer*, %struct.ringbuffer** %1
%46 = getelementptr inbounds [9 x i8], [9 x i8]* %6, i64 0, i64 0
%47 = call i32 @rb_read(%struct.ringbuffer* %45, i8* %46, i32 8)
%48 = getelementptr inbounds [9 x i8], [9 x i8]* %6, i64 0, i64 0
%49 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0), i8* %48)
ret i32 0
}

LLVM IR (-O2):
%struct.ringbuffer = type { i32 }
@.str = [12 x i8] c"hello world\00"
@.str.1 = [20 x i8] c"free=%zu, used=%zu\0A\00"
@.str.2 = [11 x i8] c"dump = %s\0A\00"
@.str.3 = [18 x i8] c"rb_write len=%zu\0A\00"
@.str.4 = [16 x i8] c"rb_read str=%s\0A\00"
define i32 @foo() {
%1 = alloca i64
%2 = alloca [9 x i8]
%3 = tail call %struct.ringbuffer* @rb_create(i32 1024)
%4 = bitcast i64* %1 to i8*
store i64 0, i64* %1
br label %5
5: ; preds = %5, %0
%6 = phi i32 [ 0, %0 ], [ %13, %5 ]
%7 = tail call i32 @rb_get_space_free(%struct.ringbuffer* %3)
%8 = tail call i32 @rb_get_space_used(%struct.ringbuffer* %3)
%9 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i32 %7, i32 %8)
%10 = tail call i32 @strlen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%11 = tail call i64 @rb_write(%struct.ringbuffer* %3, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %10)
%12 = icmp slt i64 %11, 0
%13 = add nuw nsw i32 %6, 1
%14 = icmp eq i32 %13, 100
%15 = select i1 %12, i1 true, i1 %14
br i1 %15, label %16, label %5
16: ; preds = %5
%17 = call i64 @rb_dump(%struct.ringbuffer* %3, i64* %1)
%18 = inttoptr i64 %17 to i8*
%19 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i8* %18)
%20 = load i64, i64* %1
%21 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i64 %20)
%22 = getelementptr inbounds [9 x i8], [9 x i8]* %2, i64 0, i64 0
%23 = call i32 @memset(i8* %22, i32 0, i32 9)
%24 = call i32 @rb_read(%struct.ringbuffer* %3, i8* %22, i32 8)
%25 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0), i8* %22)
%26 = call i32 @memset(i8* %22, i32 0, i32 9)
%27 = call i32 @rb_read(%struct.ringbuffer* %3, i8* %22, i32 8)
%28 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0), i8* %22)
ret i32 0
}