#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 uint32_t;
struct ring_buffer {
  int dummy;
};
typedef int /*<<< orphan*/ pthread_t;
typedef int /*<<< orphan*/ pthread_mutex_t;

/* Variables and functions */
int /*<<< orphan*/ consumer_thread(void *);
int errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ free(int /*<<< orphan*/ *);
scalar_t__ malloc(int);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ producer_thread(void *);
int /*<<< orphan*/ pthread_join(int /*<<< orphan*/, int /*<<< orphan*/ *);
int /*<<< orphan*/ pthread_mutex_destroy(int /*<<< orphan*/ *);
scalar_t__ pthread_mutex_init(int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ ring_buffer_free(struct ring_buffer *);
struct ring_buffer *ring_buffer_init(void *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
char *strerror(int);
int main() {
  void *buffer = NULL;
  uint32_t size = 0;
  pthread_t consume_pid, produce_pid;
  struct ring_buffer *ring_buf = NULL;
  pthread_mutex_t *f_lock = (pthread_mutex_t *)malloc(sizeof(pthread_mutex_t));
  if (pthread_mutex_init(f_lock, NULL) != 0) {
    fprintf(stderr, "Failed init mutex,errno:%u,reason:%s\n", errno,
            strerror(errno));
    return -1;
  }
  buffer = (void *)malloc(1024 * 1024);
  if (!buffer) {
    fprintf(stderr, "Failed to malloc memory.\n");
    return -1;
  }
  size = 1024 * 1024;
  ring_buf = ring_buffer_init(buffer, size, f_lock);
  if (!ring_buf) {
    fprintf(stderr, "Failed to init ring buffer.\n");
    return -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);
  ring_buffer_free(ring_buf);
  pthread_mutex_destroy(f_lock);
  free(f_lock);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
movq   $0x0,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movq   $0x0,-0x28(%rbp)
mov    $0x4,%edi
call   10a0 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   10e0 <pthread_mutex_init@plt>
cmp    $0x0,%rax
je     1254 <main+0x94>
mov    0x2dce(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
mov    0x2dba(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    0x2dae(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
mov    -0x38(%rbp),%edi
mov    -0x34(%rbp),%edx
mov    %rax,%rcx
lea    0xdbf(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1337 <main+0x177>
mov    $0x100000,%edi
call   10a0 <malloc@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1290 <main+0xd0>
mov    0x2d6c(%rip),%rax
mov    (%rax),%edi
lea    0xda9(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1337 <main+0x177>
movl   $0x100000,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x30(%rbp),%rdx
call   1090 <ring_buffer_init@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    12d9 <main+0x119>
mov    0x2d23(%rip),%rax
mov    (%rax),%edi
lea    0xd7a(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1337 <main+0x177>
lea    0xd7d(%rip),%rdi
call   1040 <printf@plt>
mov    -0x28(%rbp),%rdi
call   1060 <producer_thread@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x28(%rbp),%rdi
call   10b0 <consumer_thread@plt>
mov    %eax,-0x18(%rbp)
mov    -0x1c(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
call   10c0 <pthread_join@plt>
mov    -0x18(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
call   10c0 <pthread_join@plt>
mov    -0x28(%rbp),%rdi
call   1070 <ring_buffer_free@plt>
mov    -0x30(%rbp),%rdi
call   1050 <pthread_mutex_destroy@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    $0x4,%edi
call   10a0 <malloc@plt>
mov    %rax,%r14
mov    %rax,%rdi
xor    %esi,%esi
call   10e0 <pthread_mutex_init@plt>
test   %rax,%rax
je     1217 <main+0x57>
mov    0x2df5(%rip),%rax
mov    (%rax),%ebx
mov    0x2de4(%rip),%rax
mov    (%rax),%ebp
mov    %ebp,%edi
call   10d0 <strerror@plt>
lea    0xdfc(%rip),%rsi
mov    %ebx,%edi
mov    %ebp,%edx
mov    %rax,%rcx
xor    %eax,%eax
call   1080 <fprintf@plt>
jmp    12b7 <main+0xf7>
mov    $0x100000,%edi
call   10a0 <malloc@plt>
test   %rax,%rax
je     128e <main+0xce>
mov    %rax,%rdi
mov    $0x100000,%esi
mov    %r14,%rdx
call   1090 <ring_buffer_init@plt>
test   %rax,%rax
je     12a0 <main+0xe0>
mov    %rax,%r15
lea    0xe18(%rip),%rdi
call   1040 <puts@plt>
mov    %r15,%rdi
call   1060 <producer_thread@plt>
mov    %eax,%ebp
mov    %r15,%rdi
call   10b0 <consumer_thread@plt>
mov    %eax,%r12d
xor    %ebx,%ebx
mov    %ebp,%edi
xor    %esi,%esi
call   10c0 <pthread_join@plt>
mov    %r12d,%edi
xor    %esi,%esi
call   10c0 <pthread_join@plt>
mov    %r15,%rdi
call   1070 <ring_buffer_free@plt>
mov    %r14,%rdi
call   1050 <pthread_mutex_destroy@plt>
mov    %r14,%rdi
call   1030 <free@plt>
jmp    12bc <main+0xfc>
mov    0x2d4b(%rip),%rax
mov    (%rax),%edi
lea    0xd88(%rip),%rsi
jmp    12b0 <main+0xf0>
mov    0x2d39(%rip),%rax
mov    (%rax),%edi
lea    0xd90(%rip),%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.ring_buffer = type { i32 }
@stderr = global i32 0
@.str = [38 x i8] c"Failed init mutex,errno:%u,reason:%s\0A\00"
@errno = global i32 0
@.str.1 = [26 x i8] c"Failed to malloc memory.\0A\00"
@.str.2 = [29 x i8] c"Failed to init ring buffer.\0A\00"
@.str.3 = [26 x i8] c"multi thread test.......\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.ring_buffer*
%7 = alloca i32*
store i32 0, i32* %1
store i8* null, i8** %2
store i32 0, i32* %3
store %struct.ring_buffer* null, %struct.ring_buffer** %6
%8 = call i64 @malloc(i32 4)
%9 = inttoptr i64 %8 to i32*
store i32* %9, i32** %7
%10 = load i32*, i32** %7
%11 = call i64 @pthread_mutex_init(i32* %10, i32* null)
%12 = icmp ne i64 %11, 0
br i1 %12, label %13, label %19
13: ; preds = %0
%14 = load i32, i32* @stderr
%15 = load i32, i32* @errno
%16 = load i32, i32* @errno
%17 = call i8* @strerror(i32 %16)
%18 = call i32 (i32, i8*, ...) @fprintf(i32 %14, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0), i32 %15, i8* %17)
store i32 -1, i32* %1
br label %55
19: ; preds = %0
%20 = call i64 @malloc(i32 1048576)
%21 = inttoptr i64 %20 to i8*
store i8* %21, i8** %2
%22 = load i8*, i8** %2
%23 = icmp ne i8* %22, null
br i1 %23, label %27, label %24
24: ; preds = %19
%25 = load i32, i32* @stderr
%26 = call i32 (i32, i8*, ...) @fprintf(i32 %25, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %1
br label %55
27: ; preds = %19
store i32 1048576, i32* %3
%28 = load i8*, i8** %2
%29 = load i32, i32* %3
%30 = load i32*, i32** %7
%31 = call %struct.ring_buffer* @ring_buffer_init(i8* %28, i32 %29, i32* %30)
store %struct.ring_buffer* %31, %struct.ring_buffer** %6
%32 = load %struct.ring_buffer*, %struct.ring_buffer** %6
%33 = icmp ne %struct.ring_buffer* %32, null
br i1 %33, label %37, label %34
34: ; preds = %27
%35 = load i32, i32* @stderr
%36 = call i32 (i32, i8*, ...) @fprintf(i32 %35, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %1
br label %55
37: ; preds = %27
%38 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0))
%39 = load %struct.ring_buffer*, %struct.ring_buffer** %6
%40 = bitcast %struct.ring_buffer* %39 to i8*
%41 = call i32 @producer_thread(i8* %40)
store i32 %41, i32* %5
%42 = load %struct.ring_buffer*, %struct.ring_buffer** %6
%43 = bitcast %struct.ring_buffer* %42 to i8*
%44 = call i32 @consumer_thread(i8* %43)
store i32 %44, i32* %4
%45 = load i32, i32* %5
%46 = call i32 @pthread_join(i32 %45, i32* null)
%47 = load i32, i32* %4
%48 = call i32 @pthread_join(i32 %47, i32* null)
%49 = load %struct.ring_buffer*, %struct.ring_buffer** %6
%50 = call i32 @ring_buffer_free(%struct.ring_buffer* %49)
%51 = load i32*, i32** %7
%52 = call i32 @pthread_mutex_destroy(i32* %51)
%53 = load i32*, i32** %7
%54 = call i32 @free(i32* %53)
store i32 0, i32* %1
br label %55
55: ; preds = %37, %34, %24, %13
%56 = load i32, i32* %1
ret i32 %56
}

LLVM IR (-O2):
%struct.ring_buffer = type { i32 }
@stderr = global i32 0
@.str = [38 x i8] c"Failed init mutex,errno:%u,reason:%s\0A\00"
@errno = global i32 0
@.str.1 = [26 x i8] c"Failed to malloc memory.\0A\00"
@.str.2 = [29 x i8] c"Failed to init ring buffer.\0A\00"
@str = [25 x i8] c"multi thread test.......\00"
define i32 @main() {
%1 = tail call i64 @malloc(i32 4)
%2 = inttoptr i64 %1 to i32*
%3 = tail call i64 @pthread_mutex_init(i32* %2, i32* null)
%4 = icmp eq i64 %3, 0
br i1 %4, label %10, label %5
5: ; preds = %0
%6 = load i32, i32* @stderr
%7 = load i32, i32* @errno
%8 = tail call i8* @strerror(i32 %7)
%9 = tail call i32 (i32, i8*, ...) @fprintf(i32 %6, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0), i32 %7, i8* %8)
br label %33
10: ; preds = %0
%11 = tail call i64 @malloc(i32 1048576)
%12 = icmp eq i64 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %10
%14 = load i32, i32* @stderr
%15 = tail call i32 (i32, i8*, ...) @fprintf(i32 %14, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
br label %33
16: ; preds = %10
%17 = inttoptr i64 %11 to i8*
%18 = tail call %struct.ring_buffer* @ring_buffer_init(i8* %17, i32 1048576, i32* %2)
%19 = icmp eq %struct.ring_buffer* %18, null
br i1 %19, label %20, label %23
20: ; preds = %16
%21 = load i32, i32* @stderr
%22 = tail call i32 (i32, i8*, ...) @fprintf(i32 %21, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0))
br label %33
23: ; preds = %16
%24 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str, i64 0, i64 0))
%25 = bitcast %struct.ring_buffer* %18 to i8*
%26 = tail call i32 @producer_thread(i8* %25)
%27 = tail call i32 @consumer_thread(i8* %25)
%28 = tail call i32 @pthread_join(i32 %26, i32* null)
%29 = tail call i32 @pthread_join(i32 %27, i32* null)
%30 = tail call i32 @ring_buffer_free(%struct.ring_buffer* %18)
%31 = tail call i32 @pthread_mutex_destroy(i32* %2)
%32 = tail call i32 @free(i32* %2)
br label %33
33: ; preds = %23, %20, %13, %5
%34 = phi i32 [ -1, %5 ], [ 0, %23 ], [ -1, %20 ], [ -1, %13 ]
ret i32 %34
}