#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 */
int read(int, void *, int);
int ipc_rcvmsg(int socket, void *buf, int buf_size) {
  int n, header[2];
  n = read(socket, header, sizeof(header));
  if (n != sizeof(header))
    return -2;
  if (header[0] != 0xbabecafe)
    return -3;
  if (header[1] > buf_size)
    return -4;
  n = read(socket, buf, header[1]);
  if (n != header[1])
    return -5;
  return header[1];
}


x86 assembly (clang, -O0):
<ipc_rcvmsg>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x8,%edx
call   1030 <read@plt>
mov    %eax,-0x18(%rbp)
movslq -0x18(%rbp),%rax
cmp    $0x8,%rax
je     1150 <ipc_rcvmsg+0x40>
movl   $0xfffffffe,-0x4(%rbp)
jmp    11b1 <ipc_rcvmsg+0xa1>
cmpl   $0xbabecafe,-0x20(%rbp)
je     1169 <ipc_rcvmsg+0x59>
movl   $0xfffffffd,-0x4(%rbp)
jmp    11b1 <ipc_rcvmsg+0xa1>
mov    -0x1c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jle    1181 <ipc_rcvmsg+0x71>
movl   $0xfffffffc,-0x4(%rbp)
jmp    11b1 <ipc_rcvmsg+0xa1>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1030 <read@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x1c(%rbp),%eax
je     11ab <ipc_rcvmsg+0x9b>
movl   $0xfffffffb,-0x4(%rbp)
jmp    11b1 <ipc_rcvmsg+0xa1>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ipc_rcvmsg>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%ebx
mov    %rsi,%r14
mov    %edi,%ebp
mov    %rsp,%rsi
mov    $0x8,%edx
call   1030 <read@plt>
mov    %eax,%ecx
mov    $0xfffffffe,%eax
cmp    $0x8,%ecx
jne    116d <ipc_rcvmsg+0x5d>
mov    $0xfffffffd,%eax
cmpl   $0xbabecafe,(%rsp)
jne    116d <ipc_rcvmsg+0x5d>
mov    0x4(%rsp),%r15d
mov    $0xfffffffc,%eax
cmp    %ebx,%r15d
jg     116d <ipc_rcvmsg+0x5d>
mov    %ebp,%edi
mov    %r14,%rsi
mov    %r15d,%edx
call   1030 <read@plt>
cmp    %r15d,%eax
mov    $0xfffffffb,%eax
cmove  %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @ipc_rcvmsg(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]
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%10 = load i32, i32* %5
%11 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 0
%12 = bitcast i32* %11 to i8*
%13 = call i32 @read(i32 %10, i8* %12, i32 8)
store i32 %13, i32* %8
%14 = load i32, i32* %8
%15 = sext i32 %14 to i64
%16 = icmp ne i64 %15, 8
br i1 %16, label %17, label %18
17: ; preds = %3
store i32 -2, i32* %4
br label %43
18: ; preds = %3
%19 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 0
%20 = load i32, i32* %19
%21 = icmp ne i32 %20, -1161901314
br i1 %21, label %22, label %23
22: ; preds = %18
store i32 -3, i32* %4
br label %43
23: ; preds = %18
%24 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 1
%25 = load i32, i32* %24
%26 = load i32, i32* %7
%27 = icmp sgt i32 %25, %26
br i1 %27, label %28, label %29
28: ; preds = %23
store i32 -4, i32* %4
br label %43
29: ; preds = %23
%30 = load i32, i32* %5
%31 = load i8*, i8** %6
%32 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 1
%33 = load i32, i32* %32
%34 = call i32 @read(i32 %30, i8* %31, i32 %33)
store i32 %34, i32* %8
%35 = load i32, i32* %8
%36 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 1
%37 = load i32, i32* %36
%38 = icmp ne i32 %35, %37
br i1 %38, label %39, label %40
39: ; preds = %29
store i32 -5, i32* %4
br label %43
40: ; preds = %29
%41 = getelementptr inbounds [2 x i32], [2 x i32]* %9, i64 0, i64 1
%42 = load i32, i32* %41
store i32 %42, i32* %4
br label %43
43: ; preds = %40, %39, %28, %22, %17
%44 = load i32, i32* %4
ret i32 %44
}

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