#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;
  struct iovec *msg_iov;
  int /*<<< orphan*/ member_0;
};
struct iovec {
  char *iov_base;
  size_t iov_len;
};
typedef size_t ssize_t;

/* Variables and functions */
size_t check(int /*<<< orphan*/, char *);
int /*<<< orphan*/ recvmsg(int, struct msghdr *, int /*<<< orphan*/);
size_t recv_str(int chan, char *buf, size_t len) {
  buf[0] = 0;
  struct iovec data = {.iov_base = buf, .iov_len = len - 1};
  struct msghdr msg = {0};
  msg.msg_iov = &data;
  msg.msg_iovlen = 1;
  ssize_t recv_len = check(recvmsg(chan, &msg, 0), "recvmsg(str)");
  buf[recv_len] = 0;
  return recv_len;
}


x86 assembly (clang, -O0):
<recv_str>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rax
sub    $0x1,%rax
mov    %rax,-0x20(%rbp)
lea    -0x40(%rbp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1030 <memset@plt>
lea    -0x28(%rbp),%rax
mov    %rax,-0x38(%rbp)
movl   $0x1,-0x40(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x40(%rbp),%rsi
xor    %edx,%edx
call   1040 <recvmsg@plt>
mov    %eax,%edi
lea    0xe6c(%rip),%rsi
call   1050 <check@plt>
mov    %rax,-0x48(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x48(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x48(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<recv_str>:
push   %rbx
sub    $0x30,%rsp
mov    %rsi,%rbx
movb   $0x0,(%rsi)
mov    %rsi,0x20(%rsp)
add    $0xffffffffffffffff,%rdx
mov    %rdx,0x28(%rsp)
xorps  %xmm0,%xmm0
movaps %xmm0,(%rsp)
movq   $0x0,0x10(%rsp)
lea    0x20(%rsp),%rax
mov    %rax,0x8(%rsp)
movl   $0x1,(%rsp)
mov    %rsp,%rsi
xor    %edx,%edx
call   1030 <recvmsg@plt>
lea    0xe95(%rip),%rsi
mov    %eax,%edi
call   1040 <check@plt>
movb   $0x0,(%rbx,%rax,1)
add    $0x30,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.iovec = type { i8*, i64 }
%struct.msghdr = type { i32, %struct.iovec*, i32 }
@.str = [13 x i8] c"recvmsg(str)\00"
define i64 @recv_str(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca %struct.iovec
%8 = alloca %struct.msghdr
%9 = alloca i64
store i32 %0, i32* %4
store i8* %1, i8** %5
store i64 %2, i64* %6
%10 = load i8*, i8** %5
%11 = getelementptr inbounds i8, i8* %10, i64 0
store i8 0, i8* %11
%12 = getelementptr inbounds %struct.iovec, %struct.iovec* %7, i32 0, i32 0
%13 = load i8*, i8** %5
store i8* %13, i8** %12
%14 = getelementptr inbounds %struct.iovec, %struct.iovec* %7, i32 0, i32 1
%15 = load i64, i64* %6
%16 = sub i64 %15, 1
store i64 %16, i64* %14
%17 = bitcast %struct.msghdr* %8 to i8*
%18 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %8, i32 0, i32 1
store %struct.iovec* %7, %struct.iovec** %18
%19 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %8, i32 0, i32 0
store i32 1, i32* %19
%20 = load i32, i32* %4
%21 = call i32 @recvmsg(i32 %20, %struct.msghdr* %8, i32 0)
%22 = call i64 @check(i32 %21, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
store i64 %22, i64* %9
%23 = load i8*, i8** %5
%24 = load i64, i64* %9
%25 = getelementptr inbounds i8, i8* %23, i64 %24
store i8 0, i8* %25
%26 = load i64, i64* %9
ret i64 %26
}

LLVM IR (-O2):
%struct.iovec = type { i8*, i64 }
%struct.msghdr = type { i32, %struct.iovec*, i32 }
@.str = [13 x i8] c"recvmsg(str)\00"
define i64 @recv_str(i32 %0, i8* %1, i64 %2) {
%4 = alloca %struct.iovec
%5 = alloca %struct.msghdr
store i8 0, i8* %1
%6 = bitcast %struct.iovec* %4 to i8*
%7 = getelementptr inbounds %struct.iovec, %struct.iovec* %4, i64 0, i32 0
store i8* %1, i8** %7
%8 = getelementptr inbounds %struct.iovec, %struct.iovec* %4, i64 0, i32 1
%9 = add i64 %2, -1
store i64 %9, i64* %8
%10 = bitcast %struct.msghdr* %5 to i8*
%11 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %5, i64 0, i32 1
store %struct.iovec* %4, %struct.iovec** %11
%12 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %5, i64 0, i32 0
store i32 1, i32* %12
%13 = call i32 @recvmsg(i32 %0, %struct.msghdr* %5, i32 0)
%14 = call i64 @check(i32 %13, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%15 = getelementptr inbounds i8, i8* %1, i64 %14
store i8 0, i8* %15
ret i64 %14
}