#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 iovec {
  int iov_len;
  void *iov_base;
};
typedef int ssize_t;
typedef int /*<<< orphan*/ count;

/* Variables and functions */
scalar_t__ EINTR;
scalar_t__ errno;
unsigned short htons(unsigned short);
int writev(int, struct iovec *, int);
ssize_t write_string(int fd, const char *s, unsigned short len) {
  unsigned short count;
  struct iovec iov[2];
  int iovcnt;
  ssize_t n;
  iovcnt = (s != NULL) ? 2 : 1;
  count = htons(len);
  iov[0].iov_base = (void *)&count;
  iov[0].iov_len = sizeof(count);
  iov[1].iov_base = (void *)s;
  iov[1].iov_len = len;
  while ((n = writev(fd, iov, iovcnt)) == -1 && errno == EINTR)
    ;
  return n;
}


x86 assembly (clang, -O0):
<write_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %dx,%ax
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %ax,-0x12(%rbp)
mov    -0x10(%rbp),%rdx
mov    $0x1,%eax
mov    $0x2,%ecx
cmp    $0x0,%rdx
cmovne %ecx,%eax
mov    %eax,-0x44(%rbp)
movzwl -0x12(%rbp),%edi
call   1040 <htons@plt>
mov    %ax,-0x14(%rbp)
lea    -0x14(%rbp),%rax
mov    %rax,-0x38(%rbp)
movl   $0x2,-0x40(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
movzwl -0x12(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x40(%rbp),%rsi
mov    -0x44(%rbp),%edx
call   1030 <writev@plt>
mov    %eax,%ecx
mov    %ecx,-0x48(%rbp)
xor    %eax,%eax
cmp    $0xffffffff,%ecx
mov    %al,-0x49(%rbp)
jne    11b5 <write_string+0x95>
mov    0x2e3e(%rip),%rax
mov    (%rax),%rax
mov    0x2e2c(%rip),%rcx
cmp    (%rcx),%rax
sete   %al
mov    %al,-0x49(%rbp)
mov    -0x49(%rbp),%al
test   $0x1,%al
jne    11c5 <write_string+0xa5>
jmp    11ca <write_string+0xaa>
jmp    1179 <write_string+0x59>
mov    -0x48(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_string>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x30,%rsp
mov    %edx,%r14d
mov    %rsi,%r15
mov    %edi,%ebx
cmp    $0x1,%rsi
mov    $0x2,%ebp
sbb    $0x0,%ebp
mov    %edx,%edi
call   1040 <htons@plt>
mov    %ax,0xe(%rsp)
lea    0xe(%rsp),%rax
mov    %rax,0x18(%rsp)
movl   $0x2,0x10(%rsp)
mov    %r15,0x28(%rsp)
mov    %r14d,0x20(%rsp)
lea    0x10(%rsp),%r14
mov    0x2e6c(%rip),%r15
mov    0x2e5d(%rip),%r12
nopl   0x0(%rax,%rax,1)
mov    %ebx,%edi
mov    %r14,%rsi
mov    %ebp,%edx
call   1030 <writev@plt>
cmp    $0xffffffff,%eax
jne    119a <write_string+0x7a>
mov    (%r12),%rcx
cmp    %rcx,(%r15)
je     1180 <write_string+0x60>
add    $0x30,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.iovec = type { i32, i8* }
@errno = global i64 0
@EINTR = global i64 0
define i32 @write_string(i32 %0, i8* %1, i16 zeroext %2) {
%4 = alloca i16
%5 = alloca [2 x %struct.iovec]
%6 = bitcast i16* %4 to i8*
%7 = bitcast [2 x %struct.iovec]* %5 to i8*
%8 = icmp eq i8* %1, null
%9 = select i1 %8, i32 1, i32 2
%10 = tail call zeroext i16 @htons(i16 zeroext %2)
store i16 %10, i16* %4
%11 = getelementptr inbounds [2 x %struct.iovec], [2 x %struct.iovec]* %5, i64 0, i64 0
%12 = getelementptr inbounds [2 x %struct.iovec], [2 x %struct.iovec]* %5, i64 0, i64 0, i32 1
%13 = bitcast i8** %12 to i16**
store i16* %4, i16** %13
%14 = getelementptr inbounds [2 x %struct.iovec], [2 x %struct.iovec]* %5, i64 0, i64 0, i32 0
store i32 2, i32* %14
%15 = getelementptr inbounds [2 x %struct.iovec], [2 x %struct.iovec]* %5, i64 0, i64 1, i32 1
store i8* %1, i8** %15
%16 = zext i16 %2 to i32
%17 = getelementptr inbounds [2 x %struct.iovec], [2 x %struct.iovec]* %5, i64 0, i64 1, i32 0
store i32 %16, i32* %17
br label %18
18: ; preds = %18, %3
%19 = call i32 @writev(i32 %0, %struct.iovec* %11, i32 %9)
%20 = icmp eq i32 %19, -1
%21 = load i64, i64* @errno
%22 = load i64, i64* @EINTR
%23 = icmp eq i64 %21, %22
%24 = select i1 %20, i1 %23, i1 false
br i1 %24, label %18, label %25
25: ; preds = %18
ret i32 %19
}