#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*/ uint8_t;
struct msghdr {
  int msg_iovlen;
  struct iovec *msg_iov;
};
struct iovec {
  int iov_len;
  int /*<<< orphan*/ *iov_base;
};
typedef scalar_t__ ssize_t;
typedef int /*<<< orphan*/ msg;
typedef int /*<<< orphan*/ cmd;

/* Variables and functions */
int /*<<< orphan*/ memset(struct msghdr *, int /*<<< orphan*/, int);
scalar_t__ sendmsg(int, struct msghdr *, int /*<<< orphan*/);
int vrf_sendcmd(int ctlfd, uint8_t cmd, const void *arg, size_t arglen) {
  struct msghdr msg;
  struct iovec iov[2];
  ssize_t sent;
  iov[0].iov_base = &cmd;
  iov[0].iov_len = sizeof(cmd);
  iov[1].iov_base = (void *)arg;
  iov[1].iov_len = arglen;
  memset(&msg, 0, sizeof(msg));
  msg.msg_iov = iov;
  msg.msg_iovlen = arglen ? 2 : 1;
  sent = sendmsg(ctlfd, &msg, 0);
  if (sent < 0)
    return -1;
  return ((size_t)sent == sizeof(cmd) + arglen) ? 0 : -1;
}


x86 assembly (clang, -O0):
<vrf_sendcmd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
lea    -0xc(%rbp),%rax
mov    %rax,-0x48(%rbp)
movl   $0x4,-0x50(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x20(%rbp),%rax
mov    %eax,-0x40(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1040 <memset@plt>
lea    -0x50(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rdx
mov    $0x1,%eax
mov    $0x2,%ecx
cmp    $0x0,%rdx
cmovne %ecx,%eax
mov    %eax,-0x30(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x30(%rbp),%rsi
xor    %edx,%edx
call   1030 <sendmsg@plt>
mov    %rax,-0x58(%rbp)
cmpq   $0x0,-0x58(%rbp)
jge    11ad <vrf_sendcmd+0x8d>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c9 <vrf_sendcmd+0xa9>
mov    -0x58(%rbp),%rdx
mov    -0x20(%rbp),%rsi
add    $0x4,%rsi
mov    $0xffffffff,%eax
xor    %ecx,%ecx
cmp    %rsi,%rdx
cmove  %ecx,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<vrf_sendcmd>:
push   %rbp
push   %r14
push   %rbx
sub    $0x40,%rsp
mov    %rcx,%rbx
mov    %edi,%ebp
mov    %esi,0xc(%rsp)
lea    0xc(%rsp),%rax
mov    %rax,0x28(%rsp)
movl   $0x4,0x20(%rsp)
mov    %rdx,0x38(%rsp)
mov    %ebx,0x30(%rsp)
lea    0x10(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1040 <memset@plt>
lea    0x20(%rsp),%rax
mov    %rax,0x18(%rsp)
cmp    $0x1,%rbx
mov    $0x2,%eax
sbb    $0x0,%eax
mov    %eax,0x10(%rsp)
mov    %ebp,%edi
mov    %r14,%rsi
xor    %edx,%edx
call   1030 <sendmsg@plt>
test   %rax,%rax
sets   %cl
add    $0x4,%rbx
cmp    %rbx,%rax
setne  %al
or     %cl,%al
movzbl %al,%eax
neg    %eax
add    $0x40,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.msghdr = type { i32, %struct.iovec* }
%struct.iovec = type { i32, i32* }
define i32 @vrf_sendcmd(i32 %0, i32 %1, i8* %2, i64 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i64
%10 = alloca %struct.msghdr
%11 = alloca [2 x %struct.iovec]
%12 = alloca i64
store i32 %0, i32* %6
store i32 %1, i32* %7
store i8* %2, i8** %8
store i64 %3, i64* %9
%13 = getelementptr inbounds [2 x %struct.iovec], [2 x %struct.iovec]* %11, i64 0, i64 0
%14 = getelementptr inbounds %struct.iovec, %struct.iovec* %13, i32 0, i32 1
store i32* %7, i32** %14
%15 = getelementptr inbounds [2 x %struct.iovec], [2 x %struct.iovec]* %11, i64 0, i64 0
%16 = getelementptr inbounds %struct.iovec, %struct.iovec* %15, i32 0, i32 0
store i32 4, i32* %16
%17 = load i8*, i8** %8
%18 = bitcast i8* %17 to i32*
%19 = getelementptr inbounds [2 x %struct.iovec], [2 x %struct.iovec]* %11, i64 0, i64 1
%20 = getelementptr inbounds %struct.iovec, %struct.iovec* %19, i32 0, i32 1
store i32* %18, i32** %20
%21 = load i64, i64* %9
%22 = trunc i64 %21 to i32
%23 = getelementptr inbounds [2 x %struct.iovec], [2 x %struct.iovec]* %11, i64 0, i64 1
%24 = getelementptr inbounds %struct.iovec, %struct.iovec* %23, i32 0, i32 0
store i32 %22, i32* %24
%25 = call i32 @memset(%struct.msghdr* %10, i32 0, i32 16)
%26 = getelementptr inbounds [2 x %struct.iovec], [2 x %struct.iovec]* %11, i64 0, i64 0
%27 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %10, i32 0, i32 1
store %struct.iovec* %26, %struct.iovec** %27
%28 = load i64, i64* %9
%29 = icmp ne i64 %28, 0
%30 = zext i1 %29 to i64
%31 = select i1 %29, i32 2, i32 1
%32 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %10, i32 0, i32 0
store i32 %31, i32* %32
%33 = load i32, i32* %6
%34 = call i64 @sendmsg(i32 %33, %struct.msghdr* %10, i32 0)
store i64 %34, i64* %12
%35 = load i64, i64* %12
%36 = icmp slt i64 %35, 0
br i1 %36, label %37, label %38
37: ; preds = %4
store i32 -1, i32* %5
br label %45
38: ; preds = %4
%39 = load i64, i64* %12
%40 = load i64, i64* %9
%41 = add i64 4, %40
%42 = icmp eq i64 %39, %41
%43 = zext i1 %42 to i64
%44 = select i1 %42, i32 0, i32 -1
store i32 %44, i32* %5
br label %45
45: ; preds = %38, %37
%46 = load i32, i32* %5
ret i32 %46
}

LLVM IR (-O2):
%struct.msghdr = type { i32, %struct.iovec* }
%struct.iovec = type { i32, i32* }
define i32 @vrf_sendcmd(i32 %0, i32 %1, i8* %2, i64 %3) {
%5 = alloca i32
%6 = alloca %struct.msghdr
%7 = alloca [2 x %struct.iovec]
store i32 %1, i32* %5
%8 = bitcast %struct.msghdr* %6 to i8*
%9 = bitcast [2 x %struct.iovec]* %7 to i8*
%10 = getelementptr inbounds [2 x %struct.iovec], [2 x %struct.iovec]* %7, i64 0, i64 0
%11 = getelementptr inbounds [2 x %struct.iovec], [2 x %struct.iovec]* %7, i64 0, i64 0, i32 1
store i32* %5, i32** %11
%12 = getelementptr inbounds [2 x %struct.iovec], [2 x %struct.iovec]* %7, i64 0, i64 0, i32 0
store i32 4, i32* %12
%13 = getelementptr inbounds [2 x %struct.iovec], [2 x %struct.iovec]* %7, i64 0, i64 1, i32 1
%14 = bitcast i32** %13 to i8**
store i8* %2, i8** %14
%15 = trunc i64 %3 to i32
%16 = getelementptr inbounds [2 x %struct.iovec], [2 x %struct.iovec]* %7, i64 0, i64 1, i32 0
store i32 %15, i32* %16
%17 = call i32 @memset(%struct.msghdr* %6, i32 0, i32 16)
%18 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %6, i64 0, i32 1
store %struct.iovec* %10, %struct.iovec** %18
%19 = icmp eq i64 %3, 0
%20 = select i1 %19, i32 1, i32 2
%21 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %6, i64 0, i32 0
store i32 %20, i32* %21
%22 = call i64 @sendmsg(i32 %0, %struct.msghdr* %6, i32 0)
%23 = icmp slt i64 %22, 0
%24 = add i64 %3, 4
%25 = icmp ne i64 %22, %24
%26 = or i1 %23, %25
%27 = sext i1 %26 to i32
ret i32 %27
}