#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 msghdr {
  int msg_iovlen;
  int msg_flags;
  scalar_t__ msg_namelen;
  int /*<<< orphan*/ *msg_name;
  struct iovec *msg_iov;
};
struct iovec {
  size_t iov_len;
  void *iov_base;
};

/* Variables and functions */
scalar_t__ EINTR;
int MSG_CTRUNC;
int __recvmsg(int, struct msghdr *, int /*<<< orphan*/);
scalar_t__ errno;
int __msgread(int sock, void *data, size_t cnt) {
  struct iovec iov;
  struct msghdr msg;
  int len;
  iov.iov_base = data;
  iov.iov_len = cnt;
  msg.msg_iov = &iov;
  msg.msg_iovlen = 1;
  msg.msg_name = NULL;
  msg.msg_namelen = 0;
  msg.msg_flags = 0;
restart:
  len = __recvmsg(sock, &msg, 0);
  if (len >= 0) {
    if (msg.msg_flags & MSG_CTRUNC || len == 0)
      return 0;
    else
      return len;
  }
  if (errno == EINTR)
    goto restart;
  return -1;
}


x86 assembly (clang, -O0):
<__msgread>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
lea    -0x28(%rbp),%rax
mov    %rax,-0x30(%rbp)
movl   $0x1,-0x48(%rbp)
movq   $0x0,-0x38(%rbp)
movq   $0x0,-0x40(%rbp)
movl   $0x0,-0x44(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x48(%rbp),%rsi
xor    %edx,%edx
call   1030 <__recvmsg@plt>
mov    %eax,-0x4c(%rbp)
cmpl   $0x0,-0x4c(%rbp)
jl     11aa <__msgread+0x9a>
mov    -0x44(%rbp),%eax
mov    0x2e62(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
jne    1193 <__msgread+0x83>
cmpl   $0x0,-0x4c(%rbp)
jne    119f <__msgread+0x8f>
movl   $0x0,-0x4(%rbp)
jmp    11d0 <__msgread+0xc0>
mov    -0x4c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11d0 <__msgread+0xc0>
mov    0x2e27(%rip),%rax
mov    (%rax),%rax
mov    0x2e15(%rip),%rcx
cmp    (%rcx),%rax
jne    11c9 <__msgread+0xb9>
jmp    1159 <__msgread+0x49>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<__msgread>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %edi,%ebx
mov    %rsi,0x30(%rsp)
mov    %rdx,0x28(%rsp)
lea    0x28(%rsp),%rax
mov    %rax,0x20(%rsp)
movl   $0x1,0x8(%rsp)
xorps  %xmm0,%xmm0
movups %xmm0,0xc(%rsp)
movl   $0x0,0x1c(%rsp)
lea    0x8(%rsp),%r14
mov    0x2e83(%rip),%r15
mov    0x2e74(%rip),%r12
nopl   0x0(%rax)
mov    %ebx,%edi
mov    %r14,%rsi
xor    %edx,%edx
call   1030 <__recvmsg@plt>
test   %eax,%eax
jns    1180 <__msgread+0x70>
mov    (%r15),%rax
cmp    (%r12),%rax
je     1160 <__msgread+0x50>
mov    $0xffffffff,%eax
jmp    1192 <__msgread+0x82>
mov    0x2e59(%rip),%rcx
mov    (%rcx),%ecx
xor    %edx,%edx
test   %ecx,0xc(%rsp)
cmovne %edx,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.iovec = type { i64, i8* }
%struct.msghdr = type { i32, i32, i64, i32*, %struct.iovec* }
@MSG_CTRUNC = global i32 0
@errno = global i64 0
@EINTR = global i64 0
define i32 @__msgread(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca %struct.iovec
%9 = alloca %struct.msghdr
%10 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%11 = load i8*, i8** %6
%12 = getelementptr inbounds %struct.iovec, %struct.iovec* %8, i32 0, i32 1
store i8* %11, i8** %12
%13 = load i64, i64* %7
%14 = getelementptr inbounds %struct.iovec, %struct.iovec* %8, i32 0, i32 0
store i64 %13, i64* %14
%15 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %9, i32 0, i32 4
store %struct.iovec* %8, %struct.iovec** %15
%16 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %9, i32 0, i32 0
store i32 1, i32* %16
%17 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %9, i32 0, i32 3
store i32* null, i32** %17
%18 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %9, i32 0, i32 2
store i64 0, i64* %18
%19 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %9, i32 0, i32 1
store i32 0, i32* %19
br label %20
20: ; preds = %41, %3
%21 = load i32, i32* %5
%22 = call i32 @__recvmsg(i32 %21, %struct.msghdr* %9, i32 0)
store i32 %22, i32* %10
%23 = load i32, i32* %10
%24 = icmp sge i32 %23, 0
br i1 %24, label %25, label %37
25: ; preds = %20
%26 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %9, i32 0, i32 1
%27 = load i32, i32* %26
%28 = load i32, i32* @MSG_CTRUNC
%29 = and i32 %27, %28
%30 = icmp ne i32 %29, 0
br i1 %30, label %34, label %31
31: ; preds = %25
%32 = load i32, i32* %10
%33 = icmp eq i32 %32, 0
br i1 %33, label %34, label %35
34: ; preds = %31, %25
store i32 0, i32* %4
br label %43
35: ; preds = %31
%36 = load i32, i32* %10
store i32 %36, i32* %4
br label %43
37: ; preds = %20
%38 = load i64, i64* @errno
%39 = load i64, i64* @EINTR
%40 = icmp eq i64 %38, %39
br i1 %40, label %41, label %42
41: ; preds = %37
br label %20
42: ; preds = %37
store i32 -1, i32* %4
br label %43
43: ; preds = %42, %35, %34
%44 = load i32, i32* %4
ret i32 %44
}

LLVM IR (-O2):
%struct.iovec = type { i64, i8* }
%struct.msghdr = type { i32, i32, i64, i32*, %struct.iovec* }
@MSG_CTRUNC = global i32 0
@errno = global i64 0
@EINTR = global i64 0
define i32 @__msgread(i32 %0, i8* %1, i64 %2) {
%4 = alloca %struct.iovec
%5 = alloca %struct.msghdr
%6 = bitcast %struct.iovec* %4 to i8*
%7 = bitcast %struct.msghdr* %5 to i8*
%8 = getelementptr inbounds %struct.iovec, %struct.iovec* %4, i64 0, i32 1
store i8* %1, i8** %8
%9 = getelementptr inbounds %struct.iovec, %struct.iovec* %4, i64 0, i32 0
store i64 %2, i64* %9
%10 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %5, i64 0, i32 4
store %struct.iovec* %4, %struct.iovec** %10
%11 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %5, i64 0, i32 0
store i32 1, i32* %11
%12 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %5, i64 0, i32 1
%13 = bitcast i32* %12 to i8*
br label %14
14: ; preds = %25, %3
%15 = call i32 @__recvmsg(i32 %0, %struct.msghdr* %5, i32 0)
%16 = icmp sgt i32 %15, -1
br i1 %16, label %17, label %25
17: ; preds = %14
%18 = load i32, i32* %12
%19 = load i32, i32* @MSG_CTRUNC
%20 = and i32 %19, %18
%21 = icmp ne i32 %20, 0
%22 = icmp eq i32 %15, 0
%23 = select i1 %21, i1 true, i1 %22
%24 = select i1 %23, i32 0, i32 %15
br label %29
25: ; preds = %14
%26 = load i64, i64* @errno
%27 = load i64, i64* @EINTR
%28 = icmp eq i64 %26, %27
br i1 %28, label %14, label %29
29: ; preds = %25, %17
%30 = phi i32 [ %24, %17 ], [ -1, %25 ]
ret i32 %30
}