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

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int errno;
scalar_t__ mq_close(int);
int mq_open(char *, int /*<<< orphan*/, int /*<<< orphan*/,
            int /*<<< orphan*/ *);
int mq_receive(int, char *, int, unsigned int *);
int /*<<< orphan*/ printf(char *, ...);
int pthread_self();
int /*<<< orphan*/ usleep(int);
void *consumer(void *args) {
  int i;
  int err;
  int mqdes;
  char msg[32];
  unsigned int prio;
  printf("%d:start consumer\n", pthread_self());
  mqdes = mq_open("queue1", O_RDONLY, 0, NULL);
  if (mqdes == -1) {
    printf("Consumer failed to open queue\n");
    return NULL;
  }
  for (i = 0; i < 8; i++) {
    printf("%d:Waiting for Message\n", pthread_self());
    err = mq_receive(mqdes, msg, 32, &prio);
    if (err > 0) {
      printf("%d:Consume:%s\n", pthread_self(), msg);
    } else {
      printf("%d:Receive Error: %d\n", pthread_self(), errno);
      errno = 0;
    }
  }
  for (i = 0; i < 16; i++) {
    err = mq_receive(mqdes, msg, 32, &prio);
    if (err > 0) {
      printf("%d:Consume:%s\n", pthread_self(), msg);
    } else {
      printf("%d:Receive Error: %d\n", pthread_self(), errno);
      errno = 0;
    }
    usleep(200 * 1000);
  }
  printf("%d:Consumer Close mq\n", pthread_self());
  if (mq_close(mqdes) < 0) {
    printf("Consumer failed to close queue\n");
  }
  printf("%d:Consumer done\n", pthread_self());
  return NULL;
}


x86 assembly (clang, -O0):
<consumer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    $0x0,%al
call   1060 <pthread_self@plt>
mov    %eax,%esi
lea    0xe84(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    0x2e5e(%rip),%rax
mov    (%rax),%esi
lea    0xe80(%rip),%rdi
xor    %edx,%edx
xor    %eax,%eax
mov    %eax,%ecx
call   1050 <mq_open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11c6 <consumer+0x66>
lea    0xe68(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    135e <consumer+0x1fe>
movl   $0x0,-0x14(%rbp)
cmpl   $0x8,-0x14(%rbp)
jge    1270 <consumer+0x110>
mov    $0x0,%al
call   1060 <pthread_self@plt>
mov    %eax,%esi
lea    0xe52(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x1c(%rbp),%edi
lea    -0x40(%rbp),%rsi
mov    $0x20,%edx
lea    -0x44(%rbp),%rcx
call   1070 <mq_receive@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jle    1230 <consumer+0xd0>
mov    $0x0,%al
call   1060 <pthread_self@plt>
mov    %eax,%esi
lea    -0x40(%rbp),%rdx
lea    0xe2d(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    125d <consumer+0xfd>
mov    $0x0,%al
call   1060 <pthread_self@plt>
mov    %eax,%esi
mov    0x2d98(%rip),%rax
mov    (%rax),%edx
lea    0xe17(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    0x2d81(%rip),%rax
movl   $0x0,(%rax)
jmp    1262 <consumer+0x102>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    11cd <consumer+0x6d>
movl   $0x0,-0x14(%rbp)
cmpl   $0x10,-0x14(%rbp)
jge    1308 <consumer+0x1a8>
mov    -0x1c(%rbp),%edi
lea    -0x40(%rbp),%rsi
mov    $0x20,%edx
lea    -0x44(%rbp),%rcx
call   1070 <mq_receive@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jle    12c3 <consumer+0x163>
mov    $0x0,%al
call   1060 <pthread_self@plt>
mov    %eax,%esi
lea    -0x40(%rbp),%rdx
lea    0xd9a(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    12f0 <consumer+0x190>
mov    $0x0,%al
call   1060 <pthread_self@plt>
mov    %eax,%esi
mov    0x2d05(%rip),%rax
mov    (%rax),%edx
lea    0xd84(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    0x2cee(%rip),%rax
movl   $0x0,(%rax)
mov    $0x30d40,%edi
call   1080 <usleep@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1277 <consumer+0x117>
mov    $0x0,%al
call   1060 <pthread_self@plt>
mov    %eax,%esi
lea    0xd5e(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x1c(%rbp),%edi
call   1040 <mq_close@plt>
cmp    $0x0,%rax
jge    133f <consumer+0x1df>
lea    0xd54(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x0,%al
call   1060 <pthread_self@plt>
mov    %eax,%esi
lea    0xd5d(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<consumer>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
xor    %eax,%eax
call   1070 <pthread_self@plt>
lea    0xe74(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x2e4c(%rip),%rax
mov    (%rax),%esi
lea    0xe6e(%rip),%rdi
xor    %edx,%edx
xor    %ecx,%ecx
call   1060 <mq_open@plt>
cmp    $0xffffffff,%eax
je     1319 <consumer+0x1a9>
mov    %eax,%ebx
mov    $0x8,%r13d
lea    0xe54(%rip),%r14
lea    0x10(%rsp),%r15
lea    0xc(%rsp),%r12
jmp    1207 <consumer+0x97>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    0x2df1(%rip),%rbp
mov    0x0(%rbp),%edx
lea    0xe50(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
movl   $0x0,0x0(%rbp)
add    $0xffffffff,%r13d
je     1252 <consumer+0xe2>
xor    %eax,%eax
call   1070 <pthread_self@plt>
mov    %r14,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0x20,%edx
mov    %r12,%rcx
call   1080 <mq_receive@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   1070 <pthread_self@plt>
test   %ebp,%ebp
jle    11e0 <consumer+0x70>
lea    0xdf2(%rip),%rdi
mov    %eax,%esi
mov    %r15,%rdx
xor    %eax,%eax
call   1040 <printf@plt>
add    $0xffffffff,%r13d
jne    1207 <consumer+0x97>
mov    $0x10,%r12d
lea    0x10(%rsp),%r13
lea    0xc(%rsp),%r14
lea    0xdc9(%rip),%r15
jmp    12a1 <consumer+0x131>
nopl   0x0(%rax,%rax,1)
mov    0x2d61(%rip),%rbp
mov    0x0(%rbp),%edx
lea    0xdc0(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
movl   $0x0,0x0(%rbp)
mov    $0x30d40,%edi
call   1090 <usleep@plt>
add    $0xffffffff,%r12d
je     12d1 <consumer+0x161>
mov    %ebx,%edi
mov    %r13,%rsi
mov    $0x20,%edx
mov    %r14,%rcx
call   1080 <mq_receive@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   1070 <pthread_self@plt>
test   %ebp,%ebp
jle    1270 <consumer+0x100>
mov    %r15,%rdi
mov    %eax,%esi
mov    %r13,%rdx
xor    %eax,%eax
call   1040 <printf@plt>
jmp    1291 <consumer+0x121>
xor    %eax,%eax
call   1070 <pthread_self@plt>
lea    0xd78(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %ebx,%edi
call   1050 <mq_close@plt>
test   %rax,%rax
jns    1300 <consumer+0x190>
lea    0xd84(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   1070 <pthread_self@plt>
lea    0xd5f(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    1325 <consumer+0x1b5>
lea    0xd7e(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [19 x i8] c"%d:start consumer\0A\00"
@.str.1 = [7 x i8] c"queue1\00"
@O_RDONLY = global i32 0
@.str.2 = [31 x i8] c"Consumer failed to open queue\0A\00"
@.str.3 = [24 x i8] c"%d:Waiting for Message\0A\00"
@.str.4 = [15 x i8] c"%d:Consume:%s\0A\00"
@.str.5 = [22 x i8] c"%d:Receive Error: %d\0A\00"
@errno = global i32 0
@.str.6 = [22 x i8] c"%d:Consumer Close mq\0A\00"
@.str.7 = [32 x i8] c"Consumer failed to close queue\0A\00"
@.str.8 = [18 x i8] c"%d:Consumer done\0A\00"
define i8* @consumer(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca [32 x i8]
%8 = alloca i32
store i8* %0, i8** %3
%9 = call i32 (...) @pthread_self()
%10 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %9)
%11 = load i32, i32* @O_RDONLY
%12 = call i32 @mq_open(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32 %11, i32 0, i32* null)
store i32 %12, i32* %6
%13 = load i32, i32* %6
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %17
15: ; preds = %1
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0))
store i8* null, i8** %2
br label %75
17: ; preds = %1
store i32 0, i32* %4
br label %18
18: ; preds = %38, %17
%19 = load i32, i32* %4
%20 = icmp slt i32 %19, 8
br i1 %20, label %21, label %41
21: ; preds = %18
%22 = call i32 (...) @pthread_self()
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i32 %22)
%24 = load i32, i32* %6
%25 = getelementptr inbounds [32 x i8], [32 x i8]* %7, i64 0, i64 0
%26 = call i32 @mq_receive(i32 %24, i8* %25, i32 32, i32* %8)
store i32 %26, i32* %5
%27 = load i32, i32* %5
%28 = icmp sgt i32 %27, 0
br i1 %28, label %29, label %33
29: ; preds = %21
%30 = call i32 (...) @pthread_self()
%31 = getelementptr inbounds [32 x i8], [32 x i8]* %7, i64 0, i64 0
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0), i32 %30, i8* %31)
br label %37
33: ; preds = %21
%34 = call i32 (...) @pthread_self()
%35 = load i32, i32* @errno
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0), i32 %34, i32 %35)
store i32 0, i32* @errno
br label %37
37: ; preds = %33, %29
br label %38
38: ; preds = %37
%39 = load i32, i32* %4
%40 = add nsw i32 %39, 1
store i32 %40, i32* %4
br label %18
41: ; preds = %18
store i32 0, i32* %4
br label %42
42: ; preds = %61, %41
%43 = load i32, i32* %4
%44 = icmp slt i32 %43, 16
br i1 %44, label %45, label %64
45: ; preds = %42
%46 = load i32, i32* %6
%47 = getelementptr inbounds [32 x i8], [32 x i8]* %7, i64 0, i64 0
%48 = call i32 @mq_receive(i32 %46, i8* %47, i32 32, i32* %8)
store i32 %48, i32* %5
%49 = load i32, i32* %5
%50 = icmp sgt i32 %49, 0
br i1 %50, label %51, label %55
51: ; preds = %45
%52 = call i32 (...) @pthread_self()
%53 = getelementptr inbounds [32 x i8], [32 x i8]* %7, i64 0, i64 0
%54 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0), i32 %52, i8* %53)
br label %59
55: ; preds = %45
%56 = call i32 (...) @pthread_self()
%57 = load i32, i32* @errno
%58 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0), i32 %56, i32 %57)
store i32 0, i32* @errno
br label %59
59: ; preds = %55, %51
%60 = call i32 @usleep(i32 200000)
br label %61
61: ; preds = %59
%62 = load i32, i32* %4
%63 = add nsw i32 %62, 1
store i32 %63, i32* %4
br label %42
64: ; preds = %42
%65 = call i32 (...) @pthread_self()
%66 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.6, i64 0, i64 0), i32 %65)
%67 = load i32, i32* %6
%68 = call i64 @mq_close(i32 %67)
%69 = icmp slt i64 %68, 0
br i1 %69, label %70, label %72
70: ; preds = %64
%71 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.7, i64 0, i64 0))
br label %72
72: ; preds = %70, %64
%73 = call i32 (...) @pthread_self()
%74 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.8, i64 0, i64 0), i32 %73)
store i8* null, i8** %2
br label %75
75: ; preds = %72, %15
%76 = load i8*, i8** %2
ret i8* %76
}

LLVM IR (-O2):
@.str = [19 x i8] c"%d:start consumer\0A\00"
@.str.1 = [7 x i8] c"queue1\00"
@O_RDONLY = global i32 0
@.str.3 = [24 x i8] c"%d:Waiting for Message\0A\00"
@.str.4 = [15 x i8] c"%d:Consume:%s\0A\00"
@.str.5 = [22 x i8] c"%d:Receive Error: %d\0A\00"
@errno = global i32 0
@.str.6 = [22 x i8] c"%d:Consumer Close mq\0A\00"
@.str.8 = [18 x i8] c"%d:Consumer done\0A\00"
@str = [31 x i8] c"Consumer failed to close queue\00"
@str.9 = [30 x i8] c"Consumer failed to open queue\00"
define noalias i8* @consumer(i8* nocapture readnone %0) {
%2 = alloca [32 x i8]
%3 = alloca i32
%4 = getelementptr inbounds [32 x i8], [32 x i8]* %2, i64 0, i64 0
%5 = bitcast i32* %3 to i8*
%6 = tail call i32 (...) @pthread_self()
%7 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %6)
%8 = load i32, i32* @O_RDONLY
%9 = tail call i32 @mq_open(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32 %8, i32 0, i32* null)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %13
11: ; preds = %1
%12 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @str.9, i64 0, i64 0))
br label %52
13: ; preds = %1, %25
%14 = phi i32 [ %26, %25 ], [ 0, %1 ]
%15 = call i32 (...) @pthread_self()
%16 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i32 %15)
%17 = call i32 @mq_receive(i32 %9, i8* %4, i32 32, i32* %3)
%18 = icmp sgt i32 %17, 0
%19 = call i32 (...) @pthread_self()
br i1 %18, label %20, label %22
20: ; preds = %13
%21 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0), i32 %19, i8* %4)
br label %25
22: ; preds = %13
%23 = load i32, i32* @errno
%24 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0), i32 %19, i32 %23)
store i32 0, i32* @errno
br label %25
25: ; preds = %20, %22
%26 = add nuw nsw i32 %14, 1
%27 = icmp eq i32 %26, 8
br i1 %27, label %28, label %13
28: ; preds = %25, %38
%29 = phi i32 [ %40, %38 ], [ 0, %25 ]
%30 = call i32 @mq_receive(i32 %9, i8* %4, i32 32, i32* %3)
%31 = icmp sgt i32 %30, 0
%32 = call i32 (...) @pthread_self()
br i1 %31, label %33, label %35
33: ; preds = %28
%34 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0), i32 %32, i8* %4)
br label %38
35: ; preds = %28
%36 = load i32, i32* @errno
%37 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0), i32 %32, i32 %36)
store i32 0, i32* @errno
br label %38
38: ; preds = %35, %33
%39 = call i32 @usleep(i32 200000)
%40 = add nuw nsw i32 %29, 1
%41 = icmp eq i32 %40, 16
br i1 %41, label %42, label %28
42: ; preds = %38
%43 = call i32 (...) @pthread_self()
%44 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.6, i64 0, i64 0), i32 %43)
%45 = call i64 @mq_close(i32 %9)
%46 = icmp slt i64 %45, 0
br i1 %46, label %47, label %49
47: ; preds = %42
%48 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @str, i64 0, i64 0))
br label %49
49: ; preds = %47, %42
%50 = call i32 (...) @pthread_self()
%51 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.8, i64 0, i64 0), i32 %50)
br label %52
52: ; preds = %49, %11
ret i8* null
}