#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 {
  int dummy;
};
typedef int /*<<< orphan*/ i;

/* Variables and functions */
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ ringbuffer_get(struct ringbuffer *, char *, int);
scalar_t__ ringbuffer_is_empty(struct ringbuffer *);
int ringbuffer_len(struct ringbuffer *);
int /*<<< orphan*/ sleep(int);
void *consumer_proc(void *arg) {
  unsigned int cnt;
  struct ringbuffer *ring_buf = (struct ringbuffer *)arg;
  cnt = 0;
  while (1) {
    sleep(2);
    printf("------------------------------------------\n");
    printf("get data from ring buffer.\n");
    {
      char i;
      if (ringbuffer_is_empty(ring_buf)) {
        printf("buffer is empty !\n");
        sleep(1);
        continue;
      }
      if (cnt != 0 && !(cnt % 16))
        printf("\n");
      ringbuffer_get(ring_buf, &i, sizeof(i));
      printf("data is: %d \n", i);
      cnt++;
    }
    printf("ring buffer length: %u\n", ringbuffer_len(ring_buf));
    printf("------------------------------------------\n");
  }
}


x86 assembly (clang, -O0):
<consumer_proc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
movl   $0x0,-0xc(%rbp)
mov    $0x2,%edi
call   1070 <sleep@plt>
lea    0xe84(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xea2(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1030 <ringbuffer_is_empty@plt>
cmp    $0x0,%rax
je     11c1 <consumer_proc+0x71>
lea    0xe9d(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x1,%edi
call   1070 <sleep@plt>
jmp    116b <consumer_proc+0x1b>
cmpl   $0x0,-0xc(%rbp)
je     11e8 <consumer_proc+0x98>
mov    -0xc(%rbp),%eax
and    $0xf,%eax
cmp    $0x0,%eax
jne    11e8 <consumer_proc+0x98>
lea    0xe86(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x18(%rbp),%rdi
lea    -0x19(%rbp),%rsi
mov    $0x1,%edx
call   1040 <ringbuffer_get@plt>
movsbl -0x19(%rbp),%esi
lea    0xe56(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
mov    -0x18(%rbp),%rdi
call   1060 <ringbuffer_len@plt>
mov    %eax,%esi
lea    0xe42(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xdcb(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    116b <consumer_proc+0x1b>

x86 assembly (clang, -O2):
<consumer_proc>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%rbx
lea    0xebc(%rip),%r14
lea    0xe9a(%rip),%r15
lea    0xe7b(%rip),%rbp
lea    0xed2(%rip),%r12
xor    %r13d,%r13d
jmp    11b2 <consumer_proc+0x42>
nop
mov    %r12,%rdi
call   1040 <puts@plt>
mov    $0x1,%edi
call   1090 <sleep@plt>
mov    $0x2,%edi
call   1090 <sleep@plt>
mov    %r14,%rdi
call   1040 <puts@plt>
mov    %r15,%rdi
call   1040 <puts@plt>
mov    %rbx,%rdi
call   1050 <ringbuffer_is_empty@plt>
test   %rax,%rax
jne    11a0 <consumer_proc+0x30>
mov    %r13d,%eax
and    $0xf,%eax
test   %r13d,%r13d
je     11f2 <consumer_proc+0x82>
test   %eax,%eax
jne    11f2 <consumer_proc+0x82>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    %rbx,%rdi
lea    0x7(%rsp),%rsi
mov    $0x1,%edx
call   1060 <ringbuffer_get@plt>
movsbl 0x7(%rsp),%esi
lea    0xdf0(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
add    $0x1,%r13d
mov    %rbx,%rdi
call   1080 <ringbuffer_len@plt>
mov    %rbp,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1070 <printf@plt>
mov    %r14,%rdi
call   1040 <puts@plt>
jmp    11b2 <consumer_proc+0x42>

LLVM IR (-O0):
%struct.ringbuffer = type { i32 }
@.str = [44 x i8] c"------------------------------------------\0A\00"
@.str.1 = [28 x i8] c"get data from ring buffer.\0A\00"
@.str.2 = [19 x i8] c"buffer is empty !\0A\00"
@.str.3 = [2 x i8] c"\0A\00"
@.str.4 = [14 x i8] c"data is: %d \0A\00"
@.str.5 = [24 x i8] c"ring buffer length: %u\0A\00"
define i8* @consumer_proc(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca %struct.ringbuffer*
%5 = alloca i8
store i8* %0, i8** %2
%6 = load i8*, i8** %2
%7 = bitcast i8* %6 to %struct.ringbuffer*
store %struct.ringbuffer* %7, %struct.ringbuffer** %4
store i32 0, i32* %3
br label %8
8: ; preds = %1, %15, %27
%9 = call i32 @sleep(i32 2)
%10 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str, i64 0, i64 0))
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
%12 = load %struct.ringbuffer*, %struct.ringbuffer** %4
%13 = call i64 @ringbuffer_is_empty(%struct.ringbuffer* %12)
%14 = icmp ne i64 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %8
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
%17 = call i32 @sleep(i32 1)
br label %8
18: ; preds = %8
%19 = load i32, i32* %3
%20 = icmp ne i32 %19, 0
br i1 %20, label %21, label %27
21: ; preds = %18
%22 = load i32, i32* %3
%23 = urem i32 %22, 16
%24 = icmp ne i32 %23, 0
br i1 %24, label %27, label %25
25: ; preds = %21
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
br label %27
27: ; preds = %25, %21, %18
%28 = load %struct.ringbuffer*, %struct.ringbuffer** %4
%29 = call i32 @ringbuffer_get(%struct.ringbuffer* %28, i8* %5, i32 1)
%30 = load i8, i8* %5
%31 = sext i8 %30 to i32
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0), i32 %31)
%33 = load i32, i32* %3
%34 = add i32 %33, 1
store i32 %34, i32* %3
%35 = load %struct.ringbuffer*, %struct.ringbuffer** %4
%36 = call i32 @ringbuffer_len(%struct.ringbuffer* %35)
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i64 0, i64 0), i32 %36)
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str, i64 0, i64 0))
br label %8
}

LLVM IR (-O2):
%struct.ringbuffer = type { i32 }
@.str.4 = [14 x i8] c"data is: %d \0A\00"
@.str.5 = [24 x i8] c"ring buffer length: %u\0A\00"
@str.6 = [27 x i8] c"get data from ring buffer.\00"
@str.7 = [43 x i8] c"------------------------------------------\00"
@str.8 = [18 x i8] c"buffer is empty !\00"
define noalias i8* @consumer_proc(i8* %0) {
%2 = alloca i8
%3 = bitcast i8* %0 to %struct.ringbuffer*
br label %4
4: ; preds = %26, %1
%5 = phi i32 [ %31, %26 ], [ 0, %1 ]
%6 = call i32 @sleep(i32 2)
%7 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([43 x i8], [43 x i8]* @str.7, i64 0, i64 0))
%8 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str.6, i64 0, i64 0))
%9 = call i64 @ringbuffer_is_empty(%struct.ringbuffer* %3)
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %18
11: ; preds = %18, %4
%12 = icmp ne i32 %5, 0
%13 = and i32 %5, 15
%14 = icmp eq i32 %13, 0
%15 = and i1 %12, %14
br i1 %15, label %16, label %26
16: ; preds = %11
%17 = call i32 @putchar(i32 10)
br label %26
18: ; preds = %4, %18
%19 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str.8, i64 0, i64 0))
%20 = call i32 @sleep(i32 1)
%21 = call i32 @sleep(i32 2)
%22 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([43 x i8], [43 x i8]* @str.7, i64 0, i64 0))
%23 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str.6, i64 0, i64 0))
%24 = call i64 @ringbuffer_is_empty(%struct.ringbuffer* %3)
%25 = icmp eq i64 %24, 0
br i1 %25, label %11, label %18
26: ; preds = %11, %16
%27 = call i32 @ringbuffer_get(%struct.ringbuffer* %3, i8* %2, i32 1)
%28 = load i8, i8* %2
%29 = sext i8 %28 to i32
%30 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0), i32 %29)
%31 = add i32 %5, 1
%32 = call i32 @ringbuffer_len(%struct.ringbuffer* %3)
%33 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i64 0, i64 0), i32 %32)
%34 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([43 x i8], [43 x i8]* @str.7, i64 0, i64 0))
br label %4
}