#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_RDWR;
scalar_t__ mq_close(int);
int mq_open(char *, int /*<<< orphan*/, int /*<<< orphan*/,
            int /*<<< orphan*/ *);
int mq_send(int, char *, int, int);
int /*<<< orphan*/ printf(char *, ...);
int pthread_self();
int /*<<< orphan*/ sprintf(char *, char *, int);
int /*<<< orphan*/ usleep(int);
void *producer(void *args) {
  int i;
  int err;
  int mqdes;
  int prio;
  char msg[32];
  printf("%d:Producing\n", pthread_self());
  mqdes = mq_open("queue1", O_RDWR, 0, NULL);
  if (mqdes == -1) {
    printf("Producer failed to open queue\n");
    return NULL;
  }
  for (i = 0; i < 8; i++) {
    sprintf(msg, "message:%d", i);
    prio = i;
    printf("%d:Sending message\n", pthread_self());
    err = mq_send(mqdes, msg, 32, prio);
    if (err > 0) {
      printf("%d:Produce:%s\n", pthread_self(), msg);
    }
    usleep(100 * 1000);
  }
  for (i = 0; i < 16; i++) {
    sprintf(msg, "message:%d", i);
    prio = i;
    err = mq_send(mqdes, msg, 32, prio);
    if (err > 0) {
      printf("%d:Produce:%s\n", pthread_self(), msg);
    }
  }
  printf("%d:Producer Close mq\n", pthread_self());
  if (mq_close(mqdes) < 0) {
    printf("%d:Producer failed to close queue\n", pthread_self());
  }
  printf("%d:Producer done\n", pthread_self());
  return NULL;
}


x86 assembly (clang, -O0):
<producer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    $0x0,%al
call   1070 <pthread_self@plt>
mov    %eax,%esi
lea    0xe74(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    0x2e4e(%rip),%rax
mov    (%rax),%esi
lea    0xe6b(%rip),%rdi
xor    %edx,%edx
xor    %eax,%eax
mov    %eax,%ecx
call   1060 <mq_open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11d6 <producer+0x66>
lea    0xe53(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movq   $0x0,-0x8(%rbp)
jmp    1343 <producer+0x1d3>
movl   $0x0,-0x14(%rbp)
cmpl   $0x8,-0x14(%rbp)
jge    126b <producer+0xfb>
lea    -0x40(%rbp),%rdi
mov    -0x14(%rbp),%edx
lea    0xe3f(%rip),%rsi
call   1080 <sprintf@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    $0x0,%al
call   1070 <pthread_self@plt>
mov    %eax,%esi
lea    0xe2f(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x1c(%rbp),%edi
lea    -0x40(%rbp),%rsi
mov    -0x20(%rbp),%ecx
mov    $0x20,%edx
call   1050 <mq_send@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jle    1253 <producer+0xe3>
mov    $0x0,%al
call   1070 <pthread_self@plt>
mov    %eax,%esi
lea    -0x40(%rbp),%rdx
lea    0xe07(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x186a0,%edi
call   1090 <usleep@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    11dd <producer+0x6d>
movl   $0x0,-0x14(%rbp)
cmpl   $0x10,-0x14(%rbp)
jge    12e4 <producer+0x174>
lea    -0x40(%rbp),%rdi
mov    -0x14(%rbp),%edx
lea    0xdaa(%rip),%rsi
call   1080 <sprintf@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%edi
lea    -0x40(%rbp),%rsi
mov    -0x20(%rbp),%ecx
mov    $0x20,%edx
call   1050 <mq_send@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jle    12d1 <producer+0x161>
mov    $0x0,%al
call   1070 <pthread_self@plt>
mov    %eax,%esi
lea    -0x40(%rbp),%rdx
lea    0xd89(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    12d6 <producer+0x166>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1272 <producer+0x102>
mov    $0x0,%al
call   1070 <pthread_self@plt>
mov    %eax,%esi
lea    0xd6e(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x1c(%rbp),%edi
call   1040 <mq_close@plt>
cmp    $0x0,%rax
jge    1324 <producer+0x1b4>
mov    $0x0,%al
call   1070 <pthread_self@plt>
mov    %eax,%esi
lea    0xd5b(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x0,%al
call   1070 <pthread_self@plt>
mov    %eax,%esi
lea    0xd67(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<producer>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
xor    %eax,%eax
call   1080 <pthread_self@plt>
lea    0xe64(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x2e3c(%rip),%rax
mov    (%rax),%esi
lea    0xe59(%rip),%rdi
xor    %edx,%edx
xor    %ecx,%ecx
call   1070 <mq_open@plt>
cmp    $0xffffffff,%eax
je     1307 <producer+0x187>
mov    %eax,%r13d
lea    0xe44(%rip),%r14
mov    %rsp,%rbx
lea    0xe45(%rip),%r15
lea    0xe52(%rip),%r12
xor    %ebp,%ebp
jmp    1202 <producer+0x82>
cs nopw 0x0(%rax,%rax,1)
mov    $0x186a0,%edi
call   10a0 <usleep@plt>
add    $0x1,%ebp
cmp    $0x8,%ebp
je     1250 <producer+0xd0>
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %ebp,%edx
call   1090 <sprintf@plt>
xor    %eax,%eax
call   1080 <pthread_self@plt>
mov    %r15,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %r13d,%edi
mov    %rbx,%rsi
mov    $0x20,%edx
mov    %ebp,%ecx
call   1060 <mq_send@plt>
test   %eax,%eax
jle    11f0 <producer+0x70>
xor    %eax,%eax
call   1080 <pthread_self@plt>
mov    %r12,%rdi
mov    %eax,%esi
mov    %rbx,%rdx
xor    %eax,%eax
call   1040 <printf@plt>
jmp    11f0 <producer+0x70>
lea    0xdbe(%rip),%r15
mov    %rsp,%rbp
lea    0xdd3(%rip),%r14
xor    %ebx,%ebx
jmp    1278 <producer+0xf8>
cs nopw 0x0(%rax,%rax,1)
nop
add    $0x1,%ebx
cmp    $0x10,%ebx
je     12b3 <producer+0x133>
mov    %rbp,%rdi
mov    %r15,%rsi
mov    %ebx,%edx
call   1090 <sprintf@plt>
mov    %r13d,%edi
mov    %rbp,%rsi
mov    $0x20,%edx
mov    %ebx,%ecx
call   1060 <mq_send@plt>
test   %eax,%eax
jle    1270 <producer+0xf0>
xor    %eax,%eax
call   1080 <pthread_self@plt>
mov    %r14,%rdi
mov    %eax,%esi
mov    %rbp,%rdx
xor    %eax,%eax
call   1040 <printf@plt>
jmp    1270 <producer+0xf0>
xor    %eax,%eax
call   1080 <pthread_self@plt>
lea    0xd82(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %r13d,%edi
call   1050 <mq_close@plt>
test   %rax,%rax
jns    12ee <producer+0x16e>
xor    %eax,%eax
call   1080 <pthread_self@plt>
lea    0xd74(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
call   1080 <pthread_self@plt>
lea    0xd80(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    1313 <producer+0x193>
lea    0xd80(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [14 x i8] c"%d:Producing\0A\00"
@.str.1 = [7 x i8] c"queue1\00"
@O_RDWR = global i32 0
@.str.2 = [31 x i8] c"Producer failed to open queue\0A\00"
@.str.3 = [11 x i8] c"message:%d\00"
@.str.4 = [20 x i8] c"%d:Sending message\0A\00"
@.str.5 = [15 x i8] c"%d:Produce:%s\0A\00"
@.str.6 = [22 x i8] c"%d:Producer Close mq\0A\00"
@.str.7 = [35 x i8] c"%d:Producer failed to close queue\0A\00"
@.str.8 = [18 x i8] c"%d:Producer done\0A\00"
define i8* @producer(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca [32 x i8]
store i8* %0, i8** %3
%9 = call i32 (...) @pthread_self()
%10 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %9)
%11 = load i32, i32* @O_RDWR
%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 %78
17: ; preds = %1
store i32 0, i32* %4
br label %18
18: ; preds = %40, %17
%19 = load i32, i32* %4
%20 = icmp slt i32 %19, 8
br i1 %20, label %21, label %43
21: ; preds = %18
%22 = getelementptr inbounds [32 x i8], [32 x i8]* %8, i64 0, i64 0
%23 = load i32, i32* %4
%24 = call i32 @sprintf(i8* %22, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i32 %23)
%25 = load i32, i32* %4
store i32 %25, i32* %7
%26 = call i32 (...) @pthread_self()
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0), i32 %26)
%28 = load i32, i32* %6
%29 = getelementptr inbounds [32 x i8], [32 x i8]* %8, i64 0, i64 0
%30 = load i32, i32* %7
%31 = call i32 @mq_send(i32 %28, i8* %29, i32 32, i32 %30)
store i32 %31, i32* %5
%32 = load i32, i32* %5
%33 = icmp sgt i32 %32, 0
br i1 %33, label %34, label %38
34: ; preds = %21
%35 = call i32 (...) @pthread_self()
%36 = getelementptr inbounds [32 x i8], [32 x i8]* %8, i64 0, i64 0
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0), i32 %35, i8* %36)
br label %38
38: ; preds = %34, %21
%39 = call i32 @usleep(i32 100000)
br label %40
40: ; preds = %38
%41 = load i32, i32* %4
%42 = add nsw i32 %41, 1
store i32 %42, i32* %4
br label %18
43: ; preds = %18
store i32 0, i32* %4
br label %44
44: ; preds = %63, %43
%45 = load i32, i32* %4
%46 = icmp slt i32 %45, 16
br i1 %46, label %47, label %66
47: ; preds = %44
%48 = getelementptr inbounds [32 x i8], [32 x i8]* %8, i64 0, i64 0
%49 = load i32, i32* %4
%50 = call i32 @sprintf(i8* %48, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i32 %49)
%51 = load i32, i32* %4
store i32 %51, i32* %7
%52 = load i32, i32* %6
%53 = getelementptr inbounds [32 x i8], [32 x i8]* %8, i64 0, i64 0
%54 = load i32, i32* %7
%55 = call i32 @mq_send(i32 %52, i8* %53, i32 32, i32 %54)
store i32 %55, i32* %5
%56 = load i32, i32* %5
%57 = icmp sgt i32 %56, 0
br i1 %57, label %58, label %62
58: ; preds = %47
%59 = call i32 (...) @pthread_self()
%60 = getelementptr inbounds [32 x i8], [32 x i8]* %8, i64 0, i64 0
%61 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0), i32 %59, i8* %60)
br label %62
62: ; preds = %58, %47
br label %63
63: ; preds = %62
%64 = load i32, i32* %4
%65 = add nsw i32 %64, 1
store i32 %65, i32* %4
br label %44
66: ; preds = %44
%67 = call i32 (...) @pthread_self()
%68 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.6, i64 0, i64 0), i32 %67)
%69 = load i32, i32* %6
%70 = call i64 @mq_close(i32 %69)
%71 = icmp slt i64 %70, 0
br i1 %71, label %72, label %75
72: ; preds = %66
%73 = call i32 (...) @pthread_self()
%74 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.7, i64 0, i64 0), i32 %73)
br label %75
75: ; preds = %72, %66
%76 = call i32 (...) @pthread_self()
%77 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.8, i64 0, i64 0), i32 %76)
store i8* null, i8** %2
br label %78
78: ; preds = %75, %15
%79 = load i8*, i8** %2
ret i8* %79
}

LLVM IR (-O2):
@.str = [14 x i8] c"%d:Producing\0A\00"
@.str.1 = [7 x i8] c"queue1\00"
@O_RDWR = global i32 0
@.str.3 = [11 x i8] c"message:%d\00"
@.str.4 = [20 x i8] c"%d:Sending message\0A\00"
@.str.5 = [15 x i8] c"%d:Produce:%s\0A\00"
@.str.6 = [22 x i8] c"%d:Producer Close mq\0A\00"
@.str.7 = [35 x i8] c"%d:Producer failed to close queue\0A\00"
@.str.8 = [18 x i8] c"%d:Producer done\0A\00"
@str = [30 x i8] c"Producer failed to open queue\00"
define noalias i8* @producer(i8* nocapture readnone %0) {
%2 = alloca [32 x i8]
%3 = getelementptr inbounds [32 x i8], [32 x i8]* %2, i64 0, i64 0
%4 = tail call i32 (...) @pthread_self()
%5 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %4)
%6 = load i32, i32* @O_RDWR
%7 = tail call i32 @mq_open(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32 %6, i32 0, i32* null)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %11
9: ; preds = %1
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @str, i64 0, i64 0))
br label %47
11: ; preds = %1, %21
%12 = phi i32 [ %23, %21 ], [ 0, %1 ]
%13 = call i32 @sprintf(i8* %3, i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i32 %12)
%14 = call i32 (...) @pthread_self()
%15 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0), i32 %14)
%16 = call i32 @mq_send(i32 %7, i8* %3, i32 32, i32 %12)
%17 = icmp sgt i32 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %11
%19 = call i32 (...) @pthread_self()
%20 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0), i32 %19, i8* %3)
br label %21
21: ; preds = %18, %11
%22 = call i32 @usleep(i32 100000)
%23 = add nuw nsw i32 %12, 1
%24 = icmp eq i32 %23, 8
br i1 %24, label %25, label %11
25: ; preds = %21, %33
%26 = phi i32 [ %34, %33 ], [ 0, %21 ]
%27 = call i32 @sprintf(i8* %3, i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i32 %26)
%28 = call i32 @mq_send(i32 %7, i8* %3, i32 32, i32 %26)
%29 = icmp sgt i32 %28, 0
br i1 %29, label %30, label %33
30: ; preds = %25
%31 = call i32 (...) @pthread_self()
%32 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0), i32 %31, i8* %3)
br label %33
33: ; preds = %25, %30
%34 = add nuw nsw i32 %26, 1
%35 = icmp eq i32 %34, 16
br i1 %35, label %36, label %25
36: ; preds = %33
%37 = call i32 (...) @pthread_self()
%38 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.6, i64 0, i64 0), i32 %37)
%39 = call i64 @mq_close(i32 %7)
%40 = icmp slt i64 %39, 0
br i1 %40, label %41, label %44
41: ; preds = %36
%42 = call i32 (...) @pthread_self()
%43 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @.str.7, i64 0, i64 0), i32 %42)
br label %44
44: ; preds = %41, %36
%45 = call i32 (...) @pthread_self()
%46 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.8, i64 0, i64 0), i32 %45)
br label %47
47: ; preds = %44, %9
ret i8* null
}