#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*/ err(int, char *);
int /*<<< orphan*/ sendmsg(int, struct msghdr *, int /*<<< orphan*/);
int strlen(char *);
void send_str(int chan, char *s) {
  size_t len = strlen(s) + 1;
  struct iovec data = {.iov_base = s, .iov_len = len};
  struct msghdr msg = {0};
  msg.msg_iov = &data;
  msg.msg_iovlen = 1;
  ssize_t send_len = check(sendmsg(chan, &msg, 0), "sendmsg(str)");
  if (send_len != len) {
    err(1, "sendmsg(len)");
  }
}


x86 assembly (clang, -O0):
<send_str>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
add    $0x1,%eax
cltq
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
lea    -0x40(%rbp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1050 <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   1030 <sendmsg@plt>
mov    %eax,%edi
lea    0xe49(%rip),%rsi
call   1070 <check@plt>
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rax
cmp    -0x18(%rbp),%rax
je     11df <send_str+0x8f>
mov    $0x1,%edi
lea    0xe33(%rip),%rsi
call   1060 <err@plt>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<send_str>:
push   %rbp
push   %r14
push   %rbx
sub    $0x30,%rsp
mov    %rsi,%rbx
mov    %edi,%r14d
mov    %rsi,%rdi
call   1040 <strlen@plt>
add    $0x1,%eax
movslq %eax,%rbp
mov    %rbx,0x20(%rsp)
mov    %rbp,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
mov    %r14d,%edi
xor    %edx,%edx
call   1030 <sendmsg@plt>
lea    0xe65(%rip),%rsi
mov    %eax,%edi
call   1060 <check@plt>
cmp    %rbp,%rax
je     11b8 <send_str+0x78>
lea    0xe5f(%rip),%rsi
mov    $0x1,%edi
call   1050 <err@plt>
add    $0x30,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.iovec = type { i8*, i64 }
%struct.msghdr = type { i32, %struct.iovec*, i32 }
@.str = [13 x i8] c"sendmsg(str)\00"
@.str.1 = [13 x i8] c"sendmsg(len)\00"
define void @send_str(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i64
%6 = alloca %struct.iovec
%7 = alloca %struct.msghdr
%8 = alloca i64
store i32 %0, i32* %3
store i8* %1, i8** %4
%9 = load i8*, i8** %4
%10 = call i32 @strlen(i8* %9)
%11 = add nsw i32 %10, 1
%12 = sext i32 %11 to i64
store i64 %12, i64* %5
%13 = getelementptr inbounds %struct.iovec, %struct.iovec* %6, i32 0, i32 0
%14 = load i8*, i8** %4
store i8* %14, i8** %13
%15 = getelementptr inbounds %struct.iovec, %struct.iovec* %6, i32 0, i32 1
%16 = load i64, i64* %5
store i64 %16, i64* %15
%17 = bitcast %struct.msghdr* %7 to i8*
%18 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %7, i32 0, i32 1
store %struct.iovec* %6, %struct.iovec** %18
%19 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %7, i32 0, i32 0
store i32 1, i32* %19
%20 = load i32, i32* %3
%21 = call i32 @sendmsg(i32 %20, %struct.msghdr* %7, 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* %8
%23 = load i64, i64* %8
%24 = load i64, i64* %5
%25 = icmp ne i64 %23, %24
br i1 %25, label %26, label %28
26: ; preds = %2
%27 = call i32 @err(i32 1, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
br label %28
28: ; preds = %26, %2
ret void
}

LLVM IR (-O2):
%struct.iovec = type { i8*, i64 }
%struct.msghdr = type { i32, %struct.iovec*, i32 }
@.str = [13 x i8] c"sendmsg(str)\00"
@.str.1 = [13 x i8] c"sendmsg(len)\00"
define void @send_str(i32 %0, i8* %1) {
%3 = alloca %struct.iovec
%4 = alloca %struct.msghdr
%5 = tail call i32 @strlen(i8* %1)
%6 = add nsw i32 %5, 1
%7 = sext i32 %6 to i64
%8 = bitcast %struct.iovec* %3 to i8*
%9 = getelementptr inbounds %struct.iovec, %struct.iovec* %3, i64 0, i32 0
store i8* %1, i8** %9
%10 = getelementptr inbounds %struct.iovec, %struct.iovec* %3, i64 0, i32 1
store i64 %7, i64* %10
%11 = bitcast %struct.msghdr* %4 to i8*
%12 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %4, i64 0, i32 1
store %struct.iovec* %3, %struct.iovec** %12
%13 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %4, i64 0, i32 0
store i32 1, i32* %13
%14 = call i32 @sendmsg(i32 %0, %struct.msghdr* %4, i32 0)
%15 = call i64 @check(i32 %14, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%16 = icmp eq i64 %15, %7
br i1 %16, label %19, label %17
17: ; preds = %2
%18 = call i32 @err(i32 1, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
br label %19
19: ; preds = %17, %2
ret void
}