#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 */
typedef int /*<<< orphan*/ header;

/* Variables and functions */
void *malloc(int);
int read(int, void *, int);
int ipc_rcvmsga(int socket, void **buf, int *buf_size) {
  int n, header[2];
  void *buffer;
  if (!buf)
    return -1;
  n = read(socket, header, sizeof(header));
  if (n != sizeof(header))
    return -2;
  if (header[0] != 0xbabecafe)
    return -3;
  buffer = malloc(header[1]);
  *buf = buffer;
  if (buf_size)
    *buf_size = header[1];
  if (!buffer)
    return -4;
  n = read(socket, buffer, header[1]);
  if (n != header[1])
    return -5;
  return header[1];
}


x86 assembly (clang, -O0):
<ipc_rcvmsga>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    114a <ipc_rcvmsga+0x2a>
movl   $0xffffffff,-0x4(%rbp)
jmp    1203 <ipc_rcvmsga+0xe3>
mov    -0x8(%rbp),%edi
lea    -0x24(%rbp),%rsi
mov    $0x8,%edx
call   1030 <read@plt>
mov    %eax,-0x1c(%rbp)
movslq -0x1c(%rbp),%rax
cmp    $0x8,%rax
je     1178 <ipc_rcvmsga+0x58>
movl   $0xfffffffe,-0x4(%rbp)
jmp    1203 <ipc_rcvmsga+0xe3>
cmpl   $0xbabecafe,-0x24(%rbp)
je     1191 <ipc_rcvmsga+0x71>
movl   $0xfffffffd,-0x4(%rbp)
jmp    1203 <ipc_rcvmsga+0xe3>
mov    -0x20(%rbp),%edi
call   1040 <malloc@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
cmpq   $0x0,-0x18(%rbp)
je     11bc <ipc_rcvmsga+0x9c>
mov    -0x20(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
cmpq   $0x0,-0x30(%rbp)
jne    11d3 <ipc_rcvmsga+0xb3>
movl   $0xfffffffc,-0x4(%rbp)
jmp    1203 <ipc_rcvmsga+0xe3>
mov    -0x8(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    -0x20(%rbp),%edx
call   1030 <read@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x20(%rbp),%eax
je     11fd <ipc_rcvmsga+0xdd>
movl   $0xfffffffb,-0x4(%rbp)
jmp    1203 <ipc_rcvmsga+0xe3>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ipc_rcvmsga>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
test   %rsi,%rsi
je     1193 <ipc_rcvmsga+0x73>
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %edi,%ebp
mov    %rsp,%rsi
mov    $0x8,%edx
call   1030 <read@plt>
mov    %eax,%ecx
mov    $0xfffffffe,%eax
cmp    $0x8,%ecx
jne    1198 <ipc_rcvmsga+0x78>
mov    $0xfffffffd,%eax
cmpl   $0xbabecafe,(%rsp)
jne    1198 <ipc_rcvmsga+0x78>
mov    0x4(%rsp),%r15d
mov    %r15d,%edi
call   1040 <malloc@plt>
mov    %rax,(%rbx)
test   %r14,%r14
je     1173 <ipc_rcvmsga+0x53>
mov    %r15d,(%r14)
test   %rax,%rax
je     11a3 <ipc_rcvmsga+0x83>
mov    %ebp,%edi
mov    %rax,%rsi
mov    %r15d,%edx
call   1030 <read@plt>
cmp    %r15d,%eax
mov    $0xfffffffb,%eax
cmove  %r15d,%eax
jmp    1198 <ipc_rcvmsga+0x78>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0xfffffffc,%eax
jmp    1198 <ipc_rcvmsga+0x78>

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

LLVM IR (-O2):
define i32 @ipc_rcvmsga(i32 %0, i8** writeonly %1, i32* writeonly %2) {
%4 = alloca [2 x i32]
%5 = bitcast [2 x i32]* %4 to i8*
%6 = icmp eq i8** %1, null
br i1 %6, label %26, label %7
7: ; preds = %3
%8 = call i32 @read(i32 %0, i8* %5, i32 8)
%9 = icmp eq i32 %8, 8
br i1 %9, label %10, label %26
10: ; preds = %7
%11 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
%12 = load i32, i32* %11
%13 = icmp eq i32 %12, -1161901314
br i1 %13, label %14, label %26
14: ; preds = %10
%15 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%16 = load i32, i32* %15
%17 = tail call i8* @malloc(i32 %16)
store i8* %17, i8** %1
%18 = icmp eq i32* %2, null
br i1 %18, label %20, label %19
19: ; preds = %14
store i32 %16, i32* %2
br label %20
20: ; preds = %19, %14
%21 = icmp eq i8* %17, null
br i1 %21, label %26, label %22
22: ; preds = %20
%23 = tail call i32 @read(i32 %0, i8* %17, i32 %16)
%24 = icmp eq i32 %23, %16
%25 = select i1 %24, i32 %16, i32 -5
br label %26
26: ; preds = %22, %20, %10, %7, %3
%27 = phi i32 [ -1, %3 ], [ -2, %7 ], [ -3, %10 ], [ -4, %20 ], [ %25, %22 ]
ret i32 %27
}