#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*/ pthread_t;

/* Variables and functions */
int /*<<< orphan*/ consumer_thread(void *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ producer_thread(void *);
int /*<<< orphan*/ pthread_join(int /*<<< orphan*/, int /*<<< orphan*/ *);
struct ringbuffer *ringbuffer_create(int);
int /*<<< orphan*/ ringbuffer_destroy(struct ringbuffer *);
int main(void) {
  struct ringbuffer *ring_buf;
  pthread_t produce_pid, consume_pid;
  ring_buf = ringbuffer_create(16);
  if (!ring_buf) {
    perror("ringbuffer_create()");
    exit(1);
  }
  printf("multi thread test.......\n");
  produce_pid = producer_thread((void *)ring_buf);
  consume_pid = consumer_thread((void *)ring_buf);
  pthread_join(produce_pid, NULL);
  pthread_join(consume_pid, NULL);
  ringbuffer_destroy(ring_buf);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x10,%edi
call   1060 <ringbuffer_create@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11be <main+0x3e>
lea    0xe51(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
lea    0xe4f(%rip),%rdi
call   1030 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1040 <producer_thread@plt>
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
call   1080 <consumer_thread@plt>
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
call   10a0 <pthread_join@plt>
mov    -0x18(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
call   10a0 <pthread_join@plt>
mov    -0x10(%rbp),%rdi
call   1050 <ringbuffer_destroy@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
mov    $0x10,%edi
call   1060 <ringbuffer_create@plt>
test   %rax,%rax
je     11d9 <main+0x59>
mov    %rax,%rbx
lea    0xe77(%rip),%rdi
call   1030 <puts@plt>
mov    %rbx,%rdi
call   1040 <producer_thread@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1080 <consumer_thread@plt>
mov    %eax,%r14d
mov    %ebp,%edi
xor    %esi,%esi
call   10a0 <pthread_join@plt>
mov    %r14d,%edi
xor    %esi,%esi
call   10a0 <pthread_join@plt>
mov    %rbx,%rdi
call   1050 <ringbuffer_destroy@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe20(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
%struct.ringbuffer = type { i32 }
@.str = [20 x i8] c"ringbuffer_create()\00"
@.str.1 = [26 x i8] c"multi thread test.......\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca %struct.ringbuffer*
%3 = alloca i32
%4 = alloca i32
store i32 0, i32* %1
%5 = call %struct.ringbuffer* @ringbuffer_create(i32 16)
store %struct.ringbuffer* %5, %struct.ringbuffer** %2
%6 = load %struct.ringbuffer*, %struct.ringbuffer** %2
%7 = icmp ne %struct.ringbuffer* %6, null
br i1 %7, label %11, label %8
8: ; preds = %0
%9 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
%10 = call i32 @exit(i32 1)
unreachable
11: ; preds = %0
%12 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
%13 = load %struct.ringbuffer*, %struct.ringbuffer** %2
%14 = bitcast %struct.ringbuffer* %13 to i8*
%15 = call i32 @producer_thread(i8* %14)
store i32 %15, i32* %3
%16 = load %struct.ringbuffer*, %struct.ringbuffer** %2
%17 = bitcast %struct.ringbuffer* %16 to i8*
%18 = call i32 @consumer_thread(i8* %17)
store i32 %18, i32* %4
%19 = load i32, i32* %3
%20 = call i32 @pthread_join(i32 %19, i32* null)
%21 = load i32, i32* %4
%22 = call i32 @pthread_join(i32 %21, i32* null)
%23 = load %struct.ringbuffer*, %struct.ringbuffer** %2
%24 = call i32 @ringbuffer_destroy(%struct.ringbuffer* %23)
ret i32 0
}

LLVM IR (-O2):
%struct.ringbuffer = type { i32 }
@.str = [20 x i8] c"ringbuffer_create()\00"
@str = [25 x i8] c"multi thread test.......\00"
define i32 @main() {
%1 = tail call %struct.ringbuffer* @ringbuffer_create(i32 16)
%2 = icmp eq %struct.ringbuffer* %1, null
br i1 %2, label %3, label %6
3: ; preds = %0
%4 = tail call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
%5 = tail call i32 @exit(i32 1)
unreachable
6: ; preds = %0
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str, i64 0, i64 0))
%8 = bitcast %struct.ringbuffer* %1 to i8*
%9 = tail call i32 @producer_thread(i8* %8)
%10 = tail call i32 @consumer_thread(i8* %8)
%11 = tail call i32 @pthread_join(i32 %9, i32* null)
%12 = tail call i32 @pthread_join(i32 %10, i32* null)
%13 = tail call i32 @ringbuffer_destroy(%struct.ringbuffer* %1)
ret i32 0
}