#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*/ ****queue_s;
struct queue_node_s {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ *******Allocate_queue();
int /*<<< orphan*/ Done(int /*<<< orphan*/ *****, int, int);
int /*<<< orphan*/ Free_queue(int /*<<< orphan*/ *****);
int /*<<< orphan*/ Send_msg(int /*<<< orphan*/ ******, int, int, int);
int /*<<< orphan*/ Try_receive(int /*<<< orphan*/ *****, int);
int /*<<< orphan*/ Usage(char *);
int /*<<< orphan*/ free(int /*<<< orphan*/ ******);
int /*<<< orphan*/ ********malloc(int);
int omp_get_thread_num();
int /*<<< orphan*/ srandom(int);
int strtol(char *, int /*<<< orphan*/ *, int);
int main(int argc, char *argv[]) {
  int thread_count;
  int send_max;
  struct queue_s **msg_queues;
  int done_sending = 0;
  if (argc != 3)
    Usage(argv[0]);
  thread_count = strtol(argv[1], NULL, 10);
  send_max = strtol(argv[2], NULL, 10);
  if (thread_count <= 0 || send_max < 0)
    Usage(argv[0]);
  msg_queues = malloc(thread_count * sizeof(struct queue_node_s *));
#pragma omp parallel num_threads(thread_count) default(none)                   \
    shared(thread_count, send_max, msg_queues, done_sending)
  {
    int my_rank = omp_get_thread_num();
    int msg_number;
    srandom(my_rank);
    msg_queues[my_rank] = Allocate_queue();
#pragma omp barrier
    for (msg_number = 0; msg_number < send_max; msg_number++) {
      Send_msg(msg_queues, my_rank, thread_count, msg_number);
      Try_receive(msg_queues[my_rank], my_rank);
    }
#pragma omp atomic
    done_sending++;
    while (!Done(msg_queues[my_rank], done_sending, thread_count))
      Try_receive(msg_queues[my_rank], my_rank);
    Free_queue(msg_queues[my_rank]);
    free(msg_queues[my_rank]);
  }
  free(msg_queues);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x24(%rbp)
cmpl   $0x3,-0x8(%rbp)
je     11e3 <main+0x33>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   10a0 <Usage@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0xa,%edx
call   10b0 <strtol@plt>
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0xa,%edx
call   10b0 <strtol@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x14(%rbp)
jle    1229 <main+0x79>
cmpl   $0x0,-0x18(%rbp)
jge    1235 <main+0x85>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   10a0 <Usage@plt>
movslq -0x14(%rbp),%rax
shl    $0x3,%rax
mov    %eax,%edi
call   10d0 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    $0x0,%al
call   1060 <omp_get_thread_num@plt>
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%edi
call   1050 <srandom@plt>
mov    $0x0,%al
call   10c0 <Allocate_queue@plt>
mov    %rax,%rdx
mov    -0x20(%rbp),%rax
movslq -0x28(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    12b7 <main+0x107>
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%esi
mov    -0x14(%rbp),%edx
mov    -0x2c(%rbp),%ecx
call   1080 <Send_msg@plt>
mov    -0x20(%rbp),%rax
movslq -0x28(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
mov    -0x28(%rbp),%esi
call   1070 <Try_receive@plt>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    1277 <main+0xc7>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%rax
movslq -0x28(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
mov    -0x24(%rbp),%esi
mov    -0x14(%rbp),%edx
call   1040 <Done@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    12ec <main+0x13c>
jmp    1305 <main+0x155>
mov    -0x20(%rbp),%rax
movslq -0x28(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
mov    -0x28(%rbp),%esi
call   1070 <Try_receive@plt>
jmp    12c0 <main+0x110>
mov    -0x20(%rbp),%rax
movslq -0x28(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
call   1090 <Free_queue@plt>
mov    -0x20(%rbp),%rax
movslq -0x28(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%rbx
cmp    $0x3,%edi
je     11c8 <main+0x18>
mov    (%rbx),%rdi
call   10a0 <Usage@plt>
mov    0x8(%rbx),%rdi
xor    %esi,%esi
mov    $0xa,%edx
call   10b0 <strtol@plt>
mov    %eax,%r14d
mov    0x10(%rbx),%rdi
xor    %esi,%esi
mov    $0xa,%edx
call   10b0 <strtol@plt>
mov    %eax,%r15d
test   %r14d,%r14d
jle    11f8 <main+0x48>
test   %r15d,%r15d
jns    1200 <main+0x50>
mov    (%rbx),%rdi
call   10a0 <Usage@plt>
mov    %r14d,%edi
shl    $0x3,%edi
call   10d0 <malloc@plt>
mov    %rax,%r12
xor    %ebx,%ebx
xor    %eax,%eax
call   1060 <omp_get_thread_num@plt>
mov    %eax,%ebp
mov    %eax,%edi
call   1050 <srandom@plt>
xor    %eax,%eax
call   10c0 <Allocate_queue@plt>
movslq %ebp,%rbp
mov    %rax,(%r12,%rbp,8)
test   %r15d,%r15d
jle    1266 <main+0xb6>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %r12,%rdi
mov    %ebp,%esi
mov    %r14d,%edx
mov    %ebx,%ecx
call   1080 <Send_msg@plt>
mov    (%r12,%rbp,8),%rdi
mov    %ebp,%esi
call   1070 <Try_receive@plt>
add    $0x1,%ebx
cmp    %ebx,%r15d
jne    1240 <main+0x90>
mov    (%r12,%rbp,8),%rax
mov    %rax,%rdi
nopl   0x0(%rax)
mov    $0x1,%esi
mov    %r14d,%edx
call   1040 <Done@plt>
test   %eax,%eax
jne    1292 <main+0xe2>
mov    (%r12,%rbp,8),%rdi
mov    %ebp,%esi
call   1070 <Try_receive@plt>
mov    (%r12,%rbp,8),%rdi
jmp    1270 <main+0xc0>
mov    (%r12,%rbp,8),%rdi
call   1090 <Free_queue@plt>
mov    (%r12,%rbp,8),%rdi
call   1030 <free@plt>
mov    %r12,%rdi
call   1030 <free@plt>
xor    %eax,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.queue_s = type opaque
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.queue_s**
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %9
%12 = load i32, i32* %4
%13 = icmp ne i32 %12, 3
br i1 %13, label %14, label %19
14: ; preds = %2
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 0
%17 = load i8*, i8** %16
%18 = call i32 @Usage(i8* %17)
br label %19
19: ; preds = %14, %2
%20 = load i8**, i8*** %5
%21 = getelementptr inbounds i8*, i8** %20, i64 1
%22 = load i8*, i8** %21
%23 = call i32 @strtol(i8* %22, i32* null, i32 10)
store i32 %23, i32* %6
%24 = load i8**, i8*** %5
%25 = getelementptr inbounds i8*, i8** %24, i64 2
%26 = load i8*, i8** %25
%27 = call i32 @strtol(i8* %26, i32* null, i32 10)
store i32 %27, i32* %7
%28 = load i32, i32* %6
%29 = icmp sle i32 %28, 0
br i1 %29, label %33, label %30
30: ; preds = %19
%31 = load i32, i32* %7
%32 = icmp slt i32 %31, 0
br i1 %32, label %33, label %38
33: ; preds = %30, %19
%34 = load i8**, i8*** %5
%35 = getelementptr inbounds i8*, i8** %34, i64 0
%36 = load i8*, i8** %35
%37 = call i32 @Usage(i8* %36)
br label %38
38: ; preds = %33, %30
%39 = load i32, i32* %6
%40 = sext i32 %39 to i64
%41 = mul i64 %40, 8
%42 = trunc i64 %41 to i32
%43 = call i32******** @malloc(i32 %42)
%44 = bitcast i32******** %43 to %struct.queue_s**
store %struct.queue_s** %44, %struct.queue_s*** %8
%45 = call i32 (...) @omp_get_thread_num()
store i32 %45, i32* %10
%46 = load i32, i32* %10
%47 = call i32 @srandom(i32 %46)
%48 = call i32******* (...) @Allocate_queue()
%49 = bitcast i32******* %48 to %struct.queue_s*
%50 = load %struct.queue_s**, %struct.queue_s*** %8
%51 = load i32, i32* %10
%52 = sext i32 %51 to i64
%53 = getelementptr inbounds %struct.queue_s*, %struct.queue_s** %50, i64 %52
store %struct.queue_s* %49, %struct.queue_s** %53
store i32 0, i32* %11
br label %54
54: ; preds = %73, %38
%55 = load i32, i32* %11
%56 = load i32, i32* %7
%57 = icmp slt i32 %55, %56
br i1 %57, label %58, label %76
58: ; preds = %54
%59 = load %struct.queue_s**, %struct.queue_s*** %8
%60 = bitcast %struct.queue_s** %59 to i32******
%61 = load i32, i32* %10
%62 = load i32, i32* %6
%63 = load i32, i32* %11
%64 = call i32 @Send_msg(i32****** %60, i32 %61, i32 %62, i32 %63)
%65 = load %struct.queue_s**, %struct.queue_s*** %8
%66 = load i32, i32* %10
%67 = sext i32 %66 to i64
%68 = getelementptr inbounds %struct.queue_s*, %struct.queue_s** %65, i64 %67
%69 = load %struct.queue_s*, %struct.queue_s** %68
%70 = bitcast %struct.queue_s* %69 to i32*****
%71 = load i32, i32* %10
%72 = call i32 @Try_receive(i32***** %70, i32 %71)
br label %73
73: ; preds = %58
%74 = load i32, i32* %11
%75 = add nsw i32 %74, 1
store i32 %75, i32* %11
br label %54
76: ; preds = %54
%77 = load i32, i32* %9
%78 = add nsw i32 %77, 1
store i32 %78, i32* %9
br label %79
79: ; preds = %91, %76
%80 = load %struct.queue_s**, %struct.queue_s*** %8
%81 = load i32, i32* %10
%82 = sext i32 %81 to i64
%83 = getelementptr inbounds %struct.queue_s*, %struct.queue_s** %80, i64 %82
%84 = load %struct.queue_s*, %struct.queue_s** %83
%85 = bitcast %struct.queue_s* %84 to i32*****
%86 = load i32, i32* %9
%87 = load i32, i32* %6
%88 = call i32 @Done(i32***** %85, i32 %86, i32 %87)
%89 = icmp ne i32 %88, 0
%90 = xor i1 %89, true
br i1 %90, label %91, label %100
91: ; preds = %79
%92 = load %struct.queue_s**, %struct.queue_s*** %8
%93 = load i32, i32* %10
%94 = sext i32 %93 to i64
%95 = getelementptr inbounds %struct.queue_s*, %struct.queue_s** %92, i64 %94
%96 = load %struct.queue_s*, %struct.queue_s** %95
%97 = bitcast %struct.queue_s* %96 to i32*****
%98 = load i32, i32* %10
%99 = call i32 @Try_receive(i32***** %97, i32 %98)
br label %79
100: ; preds = %79
%101 = load %struct.queue_s**, %struct.queue_s*** %8
%102 = load i32, i32* %10
%103 = sext i32 %102 to i64
%104 = getelementptr inbounds %struct.queue_s*, %struct.queue_s** %101, i64 %103
%105 = load %struct.queue_s*, %struct.queue_s** %104
%106 = bitcast %struct.queue_s* %105 to i32*****
%107 = call i32 @Free_queue(i32***** %106)
%108 = load %struct.queue_s**, %struct.queue_s*** %8
%109 = load i32, i32* %10
%110 = sext i32 %109 to i64
%111 = getelementptr inbounds %struct.queue_s*, %struct.queue_s** %108, i64 %110
%112 = load %struct.queue_s*, %struct.queue_s** %111
%113 = bitcast %struct.queue_s* %112 to i32******
%114 = call i32 @free(i32****** %113)
%115 = load %struct.queue_s**, %struct.queue_s*** %8
%116 = bitcast %struct.queue_s** %115 to i32******
%117 = call i32 @free(i32****** %116)
ret i32 0
}

LLVM IR (-O2):
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 3
br i1 %3, label %7, label %4
4: ; preds = %2
%5 = load i8*, i8** %1
%6 = tail call i32 @Usage(i8* %5)
br label %7
7: ; preds = %4, %2
%8 = getelementptr inbounds i8*, i8** %1, i64 1
%9 = load i8*, i8** %8
%10 = tail call i32 @strtol(i8* nocapture %9, i32* null, i32 10)
%11 = getelementptr inbounds i8*, i8** %1, i64 2
%12 = load i8*, i8** %11
%13 = tail call i32 @strtol(i8* nocapture %12, i32* null, i32 10)
%14 = icmp slt i32 %10, 1
%15 = icmp slt i32 %13, 0
%16 = select i1 %14, i1 true, i1 %15
br i1 %16, label %17, label %20
17: ; preds = %7
%18 = load i8*, i8** %1
%19 = tail call i32 @Usage(i8* %18)
br label %20
20: ; preds = %7, %17
%21 = shl i32 %10, 3
%22 = tail call i32******** @malloc(i32 %21)
%23 = tail call i32 (...) @omp_get_thread_num()
%24 = tail call i32 @srandom(i32 %23)
%25 = tail call i32******* (...) @Allocate_queue()
%26 = sext i32 %23 to i64
%27 = getelementptr inbounds i32*******, i32******** %22, i64 %26
store i32******* %25, i32******** %27
%28 = icmp sgt i32 %13, 0
br i1 %28, label %31, label %29
29: ; preds = %20
%30 = bitcast i32******* %25 to i32*****
br label %36
31: ; preds = %20
%32 = bitcast i32******** %22 to i32******
%33 = bitcast i32******** %27 to i32******
br label %41
34: ; preds = %41
%35 = load i32*****, i32****** %33
br label %36
36: ; preds = %29, %34
%37 = phi i32***** [ %35, %34 ], [ %30, %29 ]
%38 = bitcast i32******** %27 to i32******
%39 = tail call i32 @Done(i32***** %37, i32 1, i32 %10)
%40 = icmp eq i32 %39, 0
br i1 %40, label %48, label %54
41: ; preds = %31, %41
%42 = phi i32 [ 0, %31 ], [ %46, %41 ]
%43 = tail call i32 @Send_msg(i32****** %32, i32 %23, i32 %10, i32 %42)
%44 = load i32*****, i32****** %33
%45 = tail call i32 @Try_receive(i32***** %44, i32 %23)
%46 = add nuw nsw i32 %42, 1
%47 = icmp eq i32 %46, %13
br i1 %47, label %34, label %41
48: ; preds = %36, %48
%49 = load i32*****, i32****** %38
%50 = tail call i32 @Try_receive(i32***** %49, i32 %23)
%51 = load i32*****, i32****** %38
%52 = tail call i32 @Done(i32***** %51, i32 1, i32 %10)
%53 = icmp eq i32 %52, 0
br i1 %53, label %48, label %54
54: ; preds = %48, %36
%55 = load i32*****, i32****** %38
%56 = tail call i32 @Free_queue(i32***** %55)
%57 = bitcast i32******** %27 to i32*******
%58 = load i32******, i32******* %57
%59 = tail call i32 @free(i32****** %58)
%60 = bitcast i32******** %22 to i32******
%61 = tail call i32 @free(i32****** %60)
ret i32 0
}