#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*/ MSG_WAITALL;
int /*<<< orphan*/ assert(int);
unsigned char *malloc(int);
int /*<<< orphan*/ mq_getattr(int /*<<< orphan*/, struct mq_attr *);
int /*<<< orphan*/ my_mq_send(int /*<<< orphan*/, ...);
int /*<<< orphan*/ perror(char *);
int recv(int, ...);
void code_sock2mq(int socket, mqd_t mq) {
  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 = recv(socket, &symcount, 4, MSG_WAITALL);
  assert(len == 4);
  my_mq_send(mq, &symcount, 4);
  len = recv(socket, codelen, 128, MSG_WAITALL);
  assert(len == 128);
  my_mq_send(mq, codelen, 128);
  do {
    len = recv(socket, buffer, attr.mq_msgsize, MSG_WAITALL);
    if (len == -1)
      perror("recv");
    my_mq_send(mq, buffer, len);
  } while (len == attr.mq_msgsize);
}


x86 assembly (clang, -O0):
<code_sock2mq>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    -0x8(%rbp),%edi
lea    -0xa0(%rbp),%rsi
call   1080 <mq_getattr@plt>
mov    -0xa0(%rbp),%edi
call   1060 <malloc@plt>
mov    %rax,-0x98(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2e3c(%rip),%rax
mov    (%rax),%ecx
lea    -0xc(%rbp),%rsi
mov    $0x4,%edx
mov    $0x0,%al
call   1040 <recv@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
mov    $0x0,%al
call   1030 <my_mq_send@plt>
mov    -0x4(%rbp),%edi
lea    -0x90(%rbp),%rsi
mov    0x2dec(%rip),%rax
mov    (%rax),%ecx
mov    $0x80,%edx
mov    $0x0,%al
call   1040 <recv@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
mov    $0x0,%al
call   1030 <my_mq_send@plt>
mov    -0x4(%rbp),%edi
mov    -0x98(%rbp),%rsi
mov    -0xa0(%rbp),%edx
mov    0x2d94(%rip),%rax
mov    (%rax),%ecx
mov    $0x0,%al
call   1040 <recv@plt>
mov    %eax,-0xa4(%rbp)
cmpl   $0xffffffff,-0xa4(%rbp)
jne    126c <code_sock2mq+0x10c>
lea    0xd99(%rip),%rdi
call   1070 <perror@plt>
mov    -0x8(%rbp),%edi
mov    -0x98(%rbp),%rsi
mov    -0xa4(%rbp),%edx
mov    $0x0,%al
call   1030 <my_mq_send@plt>
mov    -0xa4(%rbp),%eax
cmp    -0xa0(%rbp),%eax
je     122d <code_sock2mq+0xcd>
add    $0xb0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<code_sock2mq>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x98,%rsp
mov    %esi,%r12d
mov    %edi,%r14d
lea    0x8(%rsp),%rsi
mov    %r12d,%edi
call   1080 <mq_getattr@plt>
mov    0x8(%rsp),%edi
call   1060 <malloc@plt>
mov    %rax,%rbp
mov    0x2e41(%rip),%r13
mov    0x0(%r13),%ecx
lea    0xc(%rsp),%rbx
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0x4,%edx
xor    %eax,%eax
call   1040 <recv@plt>
xor    %edi,%edi
cmp    $0x4,%eax
sete   %dil
call   1050 <assert@plt>
mov    %r12d,%edi
mov    %rbx,%rsi
mov    $0x4,%edx
xor    %eax,%eax
call   1030 <my_mq_send@plt>
mov    0x0(%r13),%ecx
lea    0x10(%rsp),%rbx
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0x80,%edx
xor    %eax,%eax
call   1040 <recv@plt>
xor    %edi,%edi
cmp    $0x80,%eax
sete   %dil
call   1050 <assert@plt>
mov    %r12d,%edi
mov    %rbx,%rsi
mov    $0x80,%edx
xor    %eax,%eax
call   1030 <my_mq_send@plt>
mov    0x8(%rsp),%ebx
lea    0xde6(%rip),%r15
jmp    1235 <code_sock2mq+0xd5>
nopl   0x0(%rax)
mov    %r12d,%edi
mov    %rbp,%rsi
mov    %ebx,%edx
xor    %eax,%eax
call   1030 <my_mq_send@plt>
cmp    0x8(%rsp),%ebx
jne    1259 <code_sock2mq+0xf9>
mov    0x0(%r13),%ecx
mov    %r14d,%edi
mov    %rbp,%rsi
mov    %ebx,%edx
xor    %eax,%eax
call   1040 <recv@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
jne    1220 <code_sock2mq+0xc0>
mov    %r15,%rdi
call   1070 <perror@plt>
jmp    1220 <code_sock2mq+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 }
@MSG_WAITALL = global i32 0
@.str = [5 x i8] c"recv\00"
define void @code_sock2mq(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
store i32 %0, i32* %3
store i32 %1, i32* %4
%10 = load i32, i32* %4
%11 = call i32 @mq_getattr(i32 %10, %struct.mq_attr* %8)
%12 = getelementptr inbounds %struct.mq_attr, %struct.mq_attr* %8, i32 0, i32 0
%13 = load i32, i32* %12
%14 = call i8* @malloc(i32 %13)
store i8* %14, i8** %7
%15 = load i32, i32* %3
%16 = load i32, i32* @MSG_WAITALL
%17 = call i32 (i32, ...) @recv(i32 %15, i32* %5, i32 4, i32 %16)
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, ...) @my_mq_send(i32 %22, i32* %5, i32 4)
%24 = load i32, i32* %3
%25 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%26 = load i32, i32* @MSG_WAITALL
%27 = call i32 (i32, ...) @recv(i32 %24, i8* %25, i32 128, i32 %26)
store i32 %27, i32* %9
%28 = load i32, i32* %9
%29 = icmp eq i32 %28, 128
%30 = zext i1 %29 to i32
%31 = call i32 @assert(i32 %30)
%32 = load i32, i32* %4
%33 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%34 = call i32 (i32, ...) @my_mq_send(i32 %32, i8* %33, i32 128)
br label %35
35: ; preds = %51, %2
%36 = load i32, i32* %3
%37 = load i8*, i8** %7
%38 = getelementptr inbounds %struct.mq_attr, %struct.mq_attr* %8, i32 0, i32 0
%39 = load i32, i32* %38
%40 = load i32, i32* @MSG_WAITALL
%41 = call i32 (i32, ...) @recv(i32 %36, i8* %37, i32 %39, i32 %40)
store i32 %41, i32* %9
%42 = load i32, i32* %9
%43 = icmp eq i32 %42, -1
br i1 %43, label %44, label %46
44: ; preds = %35
%45 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %46
46: ; preds = %44, %35
%47 = load i32, i32* %4
%48 = load i8*, i8** %7
%49 = load i32, i32* %9
%50 = call i32 (i32, ...) @my_mq_send(i32 %47, i8* %48, i32 %49)
br label %51
51: ; preds = %46
%52 = load i32, i32* %9
%53 = getelementptr inbounds %struct.mq_attr, %struct.mq_attr* %8, i32 0, i32 0
%54 = load i32, i32* %53
%55 = icmp eq i32 %52, %54
br i1 %55, label %35, label %56
56: ; preds = %51
ret void
}

LLVM IR (-O2):
%struct.mq_attr = type { i32 }
@MSG_WAITALL = global i32 0
@.str = [5 x i8] c"recv\00"
define void @code_sock2mq(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 %1, %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 = load i32, i32* @MSG_WAITALL
%14 = call i32 (i32, ...) @recv(i32 %0, i32* %3, i32 4, i32 %13)
%15 = icmp eq i32 %14, 4
%16 = zext i1 %15 to i32
%17 = call i32 @assert(i32 %16)
%18 = call i32 (i32, ...) @my_mq_send(i32 %1, i32* %3, i32 4)
%19 = load i32, i32* @MSG_WAITALL
%20 = call i32 (i32, ...) @recv(i32 %0, i8* %7, i32 128, i32 %19)
%21 = icmp eq i32 %20, 128
%22 = zext i1 %21 to i32
%23 = call i32 @assert(i32 %22)
%24 = call i32 (i32, ...) @my_mq_send(i32 %1, i8* %7, i32 128)
%25 = load i32, i32* %10
br label %26
26: ; preds = %33, %2
%27 = phi i32 [ %29, %33 ], [ %25, %2 ]
%28 = load i32, i32* @MSG_WAITALL
%29 = call i32 (i32, ...) @recv(i32 %0, i8* %12, i32 %27, i32 %28)
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %33
31: ; preds = %26
%32 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %33
33: ; preds = %31, %26
%34 = call i32 (i32, ...) @my_mq_send(i32 %1, i8* %12, i32 %29)
%35 = load i32, i32* %10
%36 = icmp eq i32 %29, %35
br i1 %36, label %26, label %37
37: ; preds = %33
ret void
}