#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*/, ...);
int send(int, ...);
void code_mq2sock(mqd_t mq, int socket) {
  int symcount;
  unsigned char codelen[128];
  unsigned char *buffer;
  struct mq_attr attr;
  int len;
  mq_getattr(mq, &attr);
  buffer = malloc(attr.mq_msgsize);
  len = my_mq_receive(mq, &symcount);
  assert(len == 4);
  len = send(socket, &symcount, 4, 0);
  assert(len == 4);
  len = my_mq_receive(mq, codelen);
  assert(len == 128);
  len = send(socket, codelen, 128, 0);
  assert(len == 128);
  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):
<code_mq2sock>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    -0x4(%rbp),%edi
lea    -0xa0(%rbp),%rsi
call   1070 <mq_getattr@plt>
mov    -0xa0(%rbp),%edi
call   1060 <malloc@plt>
mov    %rax,-0x98(%rbp)
mov    -0x4(%rbp),%edi
lea    -0xc(%rbp),%rsi
mov    $0x0,%al
call   1030 <my_mq_receive@plt>
mov    %eax,-0xa4(%rbp)
cmpl   $0x4,-0xa4(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    -0x8(%rbp),%edi
lea    -0xc(%rbp),%rsi
mov    $0x4,%edx
xor    %ecx,%ecx
mov    $0x0,%al
call   1040 <send@plt>
mov    %eax,-0xa4(%rbp)
cmpl   $0x4,-0xa4(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    -0x4(%rbp),%edi
lea    -0x90(%rbp),%rsi
mov    $0x0,%al
call   1030 <my_mq_receive@plt>
mov    %eax,-0xa4(%rbp)
cmpl   $0x80,-0xa4(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    -0x8(%rbp),%edi
lea    -0x90(%rbp),%rsi
mov    $0x80,%edx
xor    %ecx,%ecx
mov    $0x0,%al
call   1040 <send@plt>
mov    %eax,-0xa4(%rbp)
cmpl   $0x80,-0xa4(%rbp)
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    -0x4(%rbp),%edi
mov    -0x98(%rbp),%rsi
mov    $0x0,%al
call   1030 <my_mq_receive@plt>
mov    %eax,-0xa8(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x98(%rbp),%rsi
mov    -0xa8(%rbp),%edx
xor    %ecx,%ecx
mov    $0x0,%al
call   1040 <send@plt>
mov    %eax,-0xa4(%rbp)
mov    -0xa4(%rbp),%eax
cmp    -0xa8(%rbp),%eax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    -0xa4(%rbp),%eax
cmp    -0xa0(%rbp),%eax
je     123c <code_mq2sock+0xec>
add    $0xb0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<code_mq2sock>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x98,%rsp
mov    %esi,%r14d
mov    %edi,%r12d
lea    0x8(%rsp),%rsi
call   1070 <mq_getattr@plt>
mov    0x8(%rsp),%edi
call   1060 <malloc@plt>
mov    %rax,%r13
lea    0xc(%rsp),%r15
mov    %r12d,%edi
mov    %r15,%rsi
xor    %eax,%eax
call   1030 <my_mq_receive@plt>
xor    %edi,%edi
cmp    $0x4,%eax
sete   %dil
call   1050 <assert@plt>
mov    %r14d,%edi
mov    %r15,%rsi
mov    $0x4,%edx
xor    %ecx,%ecx
xor    %eax,%eax
call   1040 <send@plt>
xor    %edi,%edi
cmp    $0x4,%eax
sete   %dil
call   1050 <assert@plt>
lea    0x10(%rsp),%r15
mov    %r12d,%edi
mov    %r15,%rsi
xor    %eax,%eax
call   1030 <my_mq_receive@plt>
xor    %edi,%edi
cmp    $0x80,%eax
sete   %dil
call   1050 <assert@plt>
mov    %r14d,%edi
mov    %r15,%rsi
mov    $0x80,%edx
xor    %ecx,%ecx
xor    %eax,%eax
call   1040 <send@plt>
xor    %edi,%edi
cmp    $0x80,%eax
sete   %dil
call   1050 <assert@plt>
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r12d,%edi
mov    %r13,%rsi
xor    %eax,%eax
call   1030 <my_mq_receive@plt>
mov    %eax,%ebp
mov    %r14d,%edi
mov    %r13,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
xor    %eax,%eax
call   1040 <send@plt>
mov    %eax,%ebx
xor    %edi,%edi
cmp    %ebp,%eax
sete   %dil
call   1050 <assert@plt>
cmp    0x8(%rsp),%ebx
je     1210 <code_mq2sock+0xc0>
add    $0x98,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.mq_attr = type { i32 }
define void @code_mq2sock(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca [128 x i8]
%7 = alloca i8*
%8 = alloca %struct.mq_attr
%9 = alloca i32
%10 = alloca i32
store i32 %0, i32* %3
store i32 %1, i32* %4
%11 = load i32, i32* %3
%12 = call i32 @mq_getattr(i32 %11, %struct.mq_attr* %8)
%13 = getelementptr inbounds %struct.mq_attr, %struct.mq_attr* %8, i32 0, i32 0
%14 = load i32, i32* %13
%15 = call i8* @malloc(i32 %14)
store i8* %15, i8** %7
%16 = load i32, i32* %3
%17 = call i32 (i32, ...) @my_mq_receive(i32 %16, i32* %5)
store i32 %17, i32* %9
%18 = load i32, i32* %9
%19 = icmp eq i32 %18, 4
%20 = zext i1 %19 to i32
%21 = call i32 @assert(i32 %20)
%22 = load i32, i32* %4
%23 = call i32 (i32, ...) @send(i32 %22, i32* %5, i32 4, i32 0)
store i32 %23, i32* %9
%24 = load i32, i32* %9
%25 = icmp eq i32 %24, 4
%26 = zext i1 %25 to i32
%27 = call i32 @assert(i32 %26)
%28 = load i32, i32* %3
%29 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%30 = call i32 (i32, ...) @my_mq_receive(i32 %28, i8* %29)
store i32 %30, i32* %9
%31 = load i32, i32* %9
%32 = icmp eq i32 %31, 128
%33 = zext i1 %32 to i32
%34 = call i32 @assert(i32 %33)
%35 = load i32, i32* %4
%36 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%37 = call i32 (i32, ...) @send(i32 %35, i8* %36, i32 128, i32 0)
store i32 %37, i32* %9
%38 = load i32, i32* %9
%39 = icmp eq i32 %38, 128
%40 = zext i1 %39 to i32
%41 = call i32 @assert(i32 %40)
br label %42
42: ; preds = %55, %2
%43 = load i32, i32* %3
%44 = load i8*, i8** %7
%45 = call i32 (i32, ...) @my_mq_receive(i32 %43, i8* %44)
store i32 %45, i32* %10
%46 = load i32, i32* %4
%47 = load i8*, i8** %7
%48 = load i32, i32* %10
%49 = call i32 (i32, ...) @send(i32 %46, i8* %47, i32 %48, i32 0)
store i32 %49, i32* %9
%50 = load i32, i32* %9
%51 = load i32, i32* %10
%52 = icmp eq i32 %50, %51
%53 = zext i1 %52 to i32
%54 = call i32 @assert(i32 %53)
br label %55
55: ; preds = %42
%56 = load i32, i32* %9
%57 = getelementptr inbounds %struct.mq_attr, %struct.mq_attr* %8, i32 0, i32 0
%58 = load i32, i32* %57
%59 = icmp eq i32 %56, %58
br i1 %59, label %42, label %60
60: ; preds = %55
ret void
}

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