#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 */
union sigval {
  int /*<<< orphan*/ *sival_ptr;
};
struct mq_attr {
  scalar_t__ mq_msgsize;
};
typedef int /*<<< orphan*/ mqd_t;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int mq_close(int /*<<< orphan*/);
int mq_getattr(int /*<<< orphan*/, struct mq_attr *);
scalar_t__ mq_receive(int /*<<< orphan*/, char *, size_t, unsigned int *);
int mq_unlink(char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ pthread_exit(int /*<<< orphan*/ *);
int /*<<< orphan*/ setup_notify(int /*<<< orphan*/ *);
void notify_thread(union sigval sv) {
  char buf[64] = {0};
  unsigned int prio = 0;
  mqd_t *mqd = sv.sival_ptr;
  struct mq_attr attr;
  if (!mqd) {
    printf("mqd is NULL\n");
    exit(EXIT_FAILURE);
  }
  if (mq_getattr(*mqd, &attr) == -1)
    do {
      perror("Get attribute failed");
      exit(EXIT_FAILURE);
    } while (0);
  while (1) {
    setup_notify(mqd);
    memset(buf, 0, sizeof(buf));
    while (mq_receive(*mqd, buf, (size_t)attr.mq_msgsize, &prio) >= 0)
      printf("Received message: %s\n", buf);
    break;
  }
  if (mq_close(*mqd) == -1)
    do {
      perror("Message queue close failed");
      exit(EXIT_FAILURE);
    } while (0);
  printf("Message queue closed\n");
  if (mq_unlink("/mq") == -1)
    do {
      perror("Message queue unlink failed");
      exit(EXIT_FAILURE);
    } while (0);
  printf("Message queue unlinked\n");
  pthread_exit(NULL);
}


x86 assembly (clang, -O0):
<notify_thread>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x8(%rbp)
lea    -0x50(%rbp),%rdi
xor    %esi,%esi
mov    $0x40,%edx
call   1040 <memset@plt>
movl   $0x0,-0x54(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x60(%rbp)
cmpq   $0x0,-0x60(%rbp)
jne    11f2 <notify_thread+0x52>
lea    0xe23(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    0x2df5(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    -0x60(%rbp),%rax
mov    (%rax),%edi
lea    -0x68(%rbp),%rsi
call   10c0 <mq_getattr@plt>
cmp    $0xffffffff,%eax
jne    1229 <notify_thread+0x89>
jmp    120f <notify_thread+0x6f>
lea    0xdf7(%rip),%rdi
call   1090 <perror@plt>
mov    0x2dbe(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
jmp    122e <notify_thread+0x8e>
mov    -0x60(%rbp),%rdi
call   1070 <setup_notify@plt>
lea    -0x50(%rbp),%rdi
xor    %esi,%esi
mov    $0x40,%edx
call   1040 <memset@plt>
mov    -0x60(%rbp),%rax
mov    (%rax),%edi
lea    -0x50(%rbp),%rsi
mov    -0x68(%rbp),%rdx
lea    -0x54(%rbp),%rcx
call   1080 <mq_receive@plt>
cmp    $0x0,%rax
jl     127f <notify_thread+0xdf>
lea    -0x50(%rbp),%rsi
lea    0xdaf(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1247 <notify_thread+0xa7>
jmp    1284 <notify_thread+0xe4>
mov    -0x60(%rbp),%rax
mov    (%rax),%edi
call   1050 <mq_close@plt>
cmp    $0xffffffff,%eax
jne    12b7 <notify_thread+0x117>
jmp    129d <notify_thread+0xfd>
lea    0xd94(%rip),%rdi
call   1090 <perror@plt>
mov    0x2d30(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
lea    0xd95(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xd9d(%rip),%rdi
call   10b0 <mq_unlink@plt>
cmp    $0xffffffff,%eax
jne    12f9 <notify_thread+0x159>
jmp    12df <notify_thread+0x13f>
lea    0xd87(%rip),%rdi
call   1090 <perror@plt>
mov    0x2cee(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
lea    0xd89(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1060 <pthread_exit@plt>
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<notify_thread>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x58,%rsp
xorps  %xmm0,%xmm0
movaps %xmm0,0x40(%rsp)
movaps %xmm0,0x30(%rsp)
movaps %xmm0,0x20(%rsp)
movaps %xmm0,0x10(%rsp)
movl   $0x0,0x4(%rsp)
test   %rdi,%rdi
je     12be <notify_thread+0x10e>
mov    %rdi,%r15
mov    (%rdi),%edi
lea    0x8(%rsp),%rsi
call   10d0 <mq_getattr@plt>
cmp    $0xffffffff,%eax
je     12cc <notify_thread+0x11c>
mov    %r15,%rdi
call   1080 <setup_notify@plt>
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x40,%edx
call   1050 <memset@plt>
mov    (%r15),%edi
mov    0x8(%rsp),%rdx
lea    0x4(%rsp),%rcx
mov    %rbx,%rsi
call   1090 <mq_receive@plt>
test   %rax,%rax
js     1275 <notify_thread+0xc5>
lea    0xddd(%rip),%r14
lea    0x10(%rsp),%rbx
lea    0x4(%rsp),%r12
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r14,%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    (%r15),%edi
mov    0x8(%rsp),%rdx
mov    %rbx,%rsi
mov    %r12,%rcx
call   1090 <mq_receive@plt>
test   %rax,%rax
jns    1250 <notify_thread+0xa0>
mov    (%r15),%edi
call   1060 <mq_close@plt>
cmp    $0xffffffff,%eax
je     12d5 <notify_thread+0x125>
lea    0xde9(%rip),%rdi
call   1030 <puts@plt>
lea    0xdb1(%rip),%rdi
call   10c0 <mq_unlink@plt>
cmp    $0xffffffff,%eax
je     12de <notify_thread+0x12e>
lea    0xde1(%rip),%rdi
call   1030 <puts@plt>
xor    %edi,%edi
call   1070 <pthread_exit@plt>
add    $0x58,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
lea    0xda1(%rip),%rdi
call   1030 <puts@plt>
jmp    12ea <notify_thread+0x13a>
lea    0xd2d(%rip),%rdi
jmp    12e5 <notify_thread+0x135>
lea    0xd4f(%rip),%rdi
jmp    12e5 <notify_thread+0x135>
lea    0xd65(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2cef(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
%union.sigval = type { i32* }
%struct.mq_attr = type { i64 }
@.str = [13 x i8] c"mqd is NULL\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [21 x i8] c"Get attribute failed\00"
@.str.2 = [22 x i8] c"Received message: %s\0A\00"
@.str.3 = [27 x i8] c"Message queue close failed\00"
@.str.4 = [22 x i8] c"Message queue closed\0A\00"
@.str.5 = [4 x i8] c"/mq\00"
@.str.6 = [28 x i8] c"Message queue unlink failed\00"
@.str.7 = [24 x i8] c"Message queue unlinked\0A\00"
define void @notify_thread(i32* %0) {
%2 = alloca %union.sigval
%3 = alloca [64 x i8]
%4 = alloca i32
%5 = alloca i32*
%6 = alloca %struct.mq_attr
%7 = getelementptr inbounds %union.sigval, %union.sigval* %2, i32 0, i32 0
store i32* %0, i32** %7
%8 = bitcast [64 x i8]* %3 to i8*
store i32 0, i32* %4
%9 = bitcast %union.sigval* %2 to i32**
%10 = load i32*, i32** %9
store i32* %10, i32** %5
%11 = load i32*, i32** %5
%12 = icmp ne i32* %11, null
br i1 %12, label %17, label %13
13: ; preds = %1
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%15 = load i32, i32* @EXIT_FAILURE
%16 = call i32 @exit(i32 %15)
unreachable
17: ; preds = %1
%18 = load i32*, i32** %5
%19 = load i32, i32* %18
%20 = call i32 @mq_getattr(i32 %19, %struct.mq_attr* %6)
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %28
22: ; preds = %17
br label %23
23: ; preds = %22
%24 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
%25 = load i32, i32* @EXIT_FAILURE
%26 = call i32 @exit(i32 %25)
unreachable
27: ; No predecessors!
br label %28
28: ; preds = %27, %17
br label %29
29: ; preds = %28
%30 = load i32*, i32** %5
%31 = call i32 @setup_notify(i32* %30)
%32 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 0
%33 = call i32 @memset(i8* %32, i32 0, i32 64)
br label %34
34: ; preds = %42, %29
%35 = load i32*, i32** %5
%36 = load i32, i32* %35
%37 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 0
%38 = getelementptr inbounds %struct.mq_attr, %struct.mq_attr* %6, i32 0, i32 0
%39 = load i64, i64* %38
%40 = call i64 @mq_receive(i32 %36, i8* %37, i64 %39, i32* %4)
%41 = icmp sge i64 %40, 0
br i1 %41, label %42, label %45
42: ; preds = %34
%43 = getelementptr inbounds [64 x i8], [64 x i8]* %3, i64 0, i64 0
%44 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i8* %43)
br label %34
45: ; preds = %34
br label %46
46: ; preds = %45
%47 = load i32*, i32** %5
%48 = load i32, i32* %47
%49 = call i32 @mq_close(i32 %48)
%50 = icmp eq i32 %49, -1
br i1 %50, label %51, label %57
51: ; preds = %46
br label %52
52: ; preds = %51
%53 = call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0))
%54 = load i32, i32* @EXIT_FAILURE
%55 = call i32 @exit(i32 %54)
unreachable
56: ; No predecessors!
br label %57
57: ; preds = %56, %46
%58 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0))
%59 = call i32 @mq_unlink(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0))
%60 = icmp eq i32 %59, -1
br i1 %60, label %61, label %67
61: ; preds = %57
br label %62
62: ; preds = %61
%63 = call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.6, i64 0, i64 0))
%64 = load i32, i32* @EXIT_FAILURE
%65 = call i32 @exit(i32 %64)
unreachable
66: ; No predecessors!
br label %67
67: ; preds = %66, %57
%68 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.7, i64 0, i64 0))
%69 = call i32 @pthread_exit(i32* null)
ret void
}

LLVM IR (-O2):
%struct.mq_attr = type { i64 }
@EXIT_FAILURE = global i32 0
@.str.1 = [21 x i8] c"Get attribute failed\00"
@.str.2 = [22 x i8] c"Received message: %s\0A\00"
@.str.3 = [27 x i8] c"Message queue close failed\00"
@.str.5 = [4 x i8] c"/mq\00"
@.str.6 = [28 x i8] c"Message queue unlink failed\00"
@str = [12 x i8] c"mqd is NULL\00"
@str.8 = [21 x i8] c"Message queue closed\00"
@str.9 = [23 x i8] c"Message queue unlinked\00"
define void @notify_thread(i32* %0) {
%2 = alloca [64 x i8]
%3 = alloca i32
%4 = alloca %struct.mq_attr
%5 = getelementptr inbounds [64 x i8], [64 x i8]* %2, i64 0, i64 0
%6 = bitcast i32* %3 to i8*
store i32 0, i32* %3
%7 = bitcast %struct.mq_attr* %4 to i8*
%8 = icmp eq i32* %0, null
br i1 %8, label %9, label %13
9: ; preds = %1
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str, i64 0, i64 0))
%11 = load i32, i32* @EXIT_FAILURE
%12 = tail call i32 @exit(i32 %11)
unreachable
13: ; preds = %1
%14 = load i32, i32* %0
%15 = call i32 @mq_getattr(i32 %14, %struct.mq_attr* %4)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %21
17: ; preds = %13
%18 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
%19 = load i32, i32* @EXIT_FAILURE
%20 = call i32 @exit(i32 %19)
unreachable
21: ; preds = %13
%22 = call i32 @setup_notify(i32* %0)
%23 = call i32 @memset(i8* %5, i32 0, i32 64)
%24 = getelementptr inbounds %struct.mq_attr, %struct.mq_attr* %4, i64 0, i32 0
%25 = load i32, i32* %0
%26 = load i64, i64* %24
%27 = call i64 @mq_receive(i32 %25, i8* %5, i64 %26, i32* %3)
%28 = icmp sgt i64 %27, -1
br i1 %28, label %29, label %35
29: ; preds = %21, %29
%30 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i8* %5)
%31 = load i32, i32* %0
%32 = load i64, i64* %24
%33 = call i64 @mq_receive(i32 %31, i8* %5, i64 %32, i32* %3)
%34 = icmp sgt i64 %33, -1
br i1 %34, label %29, label %35
35: ; preds = %29, %21
%36 = load i32, i32* %0
%37 = call i32 @mq_close(i32 %36)
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %43
39: ; preds = %35
%40 = call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0))
%41 = load i32, i32* @EXIT_FAILURE
%42 = call i32 @exit(i32 %41)
unreachable
43: ; preds = %35
%44 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str.8, i64 0, i64 0))
%45 = call i32 @mq_unlink(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0))
%46 = icmp eq i32 %45, -1
br i1 %46, label %47, label %51
47: ; preds = %43
%48 = call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.6, i64 0, i64 0))
%49 = load i32, i32* @EXIT_FAILURE
%50 = call i32 @exit(i32 %49)
unreachable
51: ; preds = %43
%52 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str.9, i64 0, i64 0))
%53 = call i32 @pthread_exit(i32* null)
ret void
}