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

/* Variables and functions */
int /*<<< orphan*/ assert(int);
unsigned char *malloc(int);
int /*<<< orphan*/ mq_getattr(int /*<<< orphan*/, struct mq_attr *);
int my_mq_receive(int /*<<< orphan*/, unsigned char *);
int send(int, unsigned char *, int, int /*<<< orphan*/);
void text_mq2sock(mqd_t mq, int socket) {
  int len;
  unsigned char *buffer;
  struct mq_attr attr;
  mq_getattr(mq, &attr);
  buffer = malloc(attr.mq_msgsize);
  do {
    int tmp;
    tmp = my_mq_receive(mq, buffer);
    len = send(socket, buffer, tmp, 0);
    assert(len == tmp);
  } while (len == attr.mq_msgsize);
}


x86 assembly (clang, -O0):
<text_mq2sock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   1070 <mq_getattr@plt>
mov    -0x20(%rbp),%edi
call   1060 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x18(%rbp),%rsi
call   1030 <my_mq_receive@plt>
mov    %eax,-0x24(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x24(%rbp),%edx
xor    %ecx,%ecx
call   1040 <send@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%eax
cmp    -0x24(%rbp),%eax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    -0xc(%rbp),%eax
cmp    -0x20(%rbp),%eax
je     1176 <text_mq2sock+0x26>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<text_mq2sock>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %esi,%r14d
mov    %edi,%r15d
lea    0x8(%rsp),%rsi
call   1070 <mq_getattr@plt>
mov    0x8(%rsp),%edi
call   1060 <malloc@plt>
mov    %rax,%r12
nopl   0x0(%rax,%rax,1)
mov    %r15d,%edi
mov    %r12,%rsi
call   1030 <my_mq_receive@plt>
mov    %eax,%ebp
mov    %r14d,%edi
mov    %r12,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1040 <send@plt>
mov    %eax,%ebx
xor    %edi,%edi
cmp    %ebp,%eax
sete   %dil
call   1050 <assert@plt>
cmp    0x8(%rsp),%ebx
je     1180 <text_mq2sock+0x30>
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.mq_attr = type { i32 }
define void @text_mq2sock(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca %struct.mq_attr
%8 = alloca i32
store i32 %0, i32* %3
store i32 %1, i32* %4
%9 = load i32, i32* %3
%10 = call i32 @mq_getattr(i32 %9, %struct.mq_attr* %7)
%11 = getelementptr inbounds %struct.mq_attr, %struct.mq_attr* %7, i32 0, i32 0
%12 = load i32, i32* %11
%13 = call i8* @malloc(i32 %12)
store i8* %13, i8** %6
br label %14
14: ; preds = %27, %2
%15 = load i32, i32* %3
%16 = load i8*, i8** %6
%17 = call i32 @my_mq_receive(i32 %15, i8* %16)
store i32 %17, i32* %8
%18 = load i32, i32* %4
%19 = load i8*, i8** %6
%20 = load i32, i32* %8
%21 = call i32 @send(i32 %18, i8* %19, i32 %20, i32 0)
store i32 %21, i32* %5
%22 = load i32, i32* %5
%23 = load i32, i32* %8
%24 = icmp eq i32 %22, %23
%25 = zext i1 %24 to i32
%26 = call i32 @assert(i32 %25)
br label %27
27: ; preds = %14
%28 = load i32, i32* %5
%29 = getelementptr inbounds %struct.mq_attr, %struct.mq_attr* %7, i32 0, i32 0
%30 = load i32, i32* %29
%31 = icmp eq i32 %28, %30
br i1 %31, label %14, label %32
32: ; preds = %27
ret void
}

LLVM IR (-O2):
%struct.mq_attr = type { i32 }
define void @text_mq2sock(i32 %0, i32 %1) {
%3 = alloca %struct.mq_attr
%4 = bitcast %struct.mq_attr* %3 to i8*
%5 = call i32 @mq_getattr(i32 %0, %struct.mq_attr* %3)
%6 = getelementptr inbounds %struct.mq_attr, %struct.mq_attr* %3, i64 0, i32 0
%7 = load i32, i32* %6
%8 = call i8* @malloc(i32 %7)
br label %9
9: ; preds = %9, %2
%10 = call i32 @my_mq_receive(i32 %0, i8* %8)
%11 = call i32 @send(i32 %1, i8* %8, i32 %10, i32 0)
%12 = icmp eq i32 %11, %10
%13 = zext i1 %12 to i32
%14 = call i32 @assert(i32 %13)
%15 = load i32, i32* %6
%16 = icmp eq i32 %11, %15
br i1 %16, label %9, label %17
17: ; preds = %9
ret void
}