#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 {
  size_t iov_len;
  scalar_t__ iov_base;
};
typedef int ssize_t;

/* Variables and functions */
int /*<<< orphan*/ EINVAL;
size_t MIN(size_t, size_t);
size_t SSIZE_MAX;
scalar_t__ __alloca(size_t);
scalar_t__ __libc_use_alloca(size_t);
char *__mempcpy(void *, void *, size_t);
int /*<<< orphan*/ __set_errno(int /*<<< orphan*/);
int __write(int, char *, size_t);
int /*<<< orphan*/ free(char *);
scalar_t__ malloc(size_t);
ssize_t __libc_writev(int fd, const struct iovec *vector, int count) {
  char *buffer;
  register char *bp;
  size_t bytes, to_copy;
  ssize_t bytes_written;
  int i;
  bool use_malloc = false;
  bytes = 0;
  for (i = 0; i < count; ++i) {
    if (SSIZE_MAX - bytes < vector[i].iov_len) {
      __set_errno(EINVAL);
      return -1;
    }
    bytes += vector[i].iov_len;
  }
  if (__libc_use_alloca(bytes))
    buffer = (char *)__alloca(bytes);
  else {
    buffer = (char *)malloc(bytes);
    if (buffer == NULL)
      return -1;
    use_malloc = true;
  }
  to_copy = bytes;
  bp = buffer;
  for (i = 0; i < count; ++i) {
    size_t copy = MIN(vector[i].iov_len, to_copy);
    bp = __mempcpy((void *)bp, (void *)vector[i].iov_base, copy);
    to_copy -= copy;
    if (to_copy == 0)
      break;
  }
  bytes_written = __write(fd, buffer, bytes);
  if (use_malloc)
    free(buffer);
  return bytes_written;
}


x86 assembly (clang, -O0):
<__libc_writev>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x44(%rbp)
movq   $0x0,-0x30(%rbp)
movl   $0x0,-0x40(%rbp)
mov    -0x40(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    121c <__libc_writev+0x9c>
mov    0x2e2d(%rip),%rax
mov    (%rax),%rax
sub    -0x30(%rbp),%rax
mov    -0x10(%rbp),%rcx
movslq -0x40(%rbp),%rdx
shl    $0x4,%rdx
add    %rdx,%rcx
cmp    (%rcx),%rax
jae    11f4 <__libc_writev+0x74>
mov    0x2df7(%rip),%rax
mov    (%rax),%edi
call   1030 <__set_errno@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1330 <__libc_writev+0x1b0>
mov    -0x10(%rbp),%rax
movslq -0x40(%rbp),%rcx
shl    $0x4,%rcx
add    %rcx,%rax
mov    (%rax),%rax
add    -0x30(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x40(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x40(%rbp)
jmp    11a8 <__libc_writev+0x28>
mov    -0x30(%rbp),%rdi
call   1090 <__libc_use_alloca@plt>
cmp    $0x0,%rax
je     1241 <__libc_writev+0xc1>
mov    -0x30(%rbp),%rdi
call   10a0 <__alloca@plt>
mov    %rax,-0x20(%rbp)
jmp    126c <__libc_writev+0xec>
mov    -0x30(%rbp),%rdi
call   1080 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1265 <__libc_writev+0xe5>
movl   $0xffffffff,-0x4(%rbp)
jmp    1330 <__libc_writev+0x1b0>
movl   $0x1,-0x44(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x40(%rbp)
mov    -0x40(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    1304 <__libc_writev+0x184>
mov    -0x10(%rbp),%rax
movslq -0x40(%rbp),%rcx
shl    $0x4,%rcx
add    %rcx,%rax
mov    (%rax),%rdi
mov    -0x38(%rbp),%rsi
call   1050 <MIN@plt>
mov    %rax,-0x50(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rax
movslq -0x40(%rbp),%rcx
shl    $0x4,%rcx
add    %rcx,%rax
mov    0x8(%rax),%rsi
mov    -0x50(%rbp),%rdx
call   1060 <__mempcpy@plt>
mov    %rax,-0x28(%rbp)
mov    -0x50(%rbp),%rcx
mov    -0x38(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    12f1 <__libc_writev+0x171>
jmp    1304 <__libc_writev+0x184>
jmp    12f6 <__libc_writev+0x176>
mov    -0x40(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x40(%rbp)
jmp    1283 <__libc_writev+0x103>
mov    -0x8(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x30(%rbp),%rdx
call   1070 <__write@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x44(%rbp)
je     132a <__libc_writev+0x1aa>
mov    -0x20(%rbp),%rdi
call   1040 <free@plt>
mov    -0x3c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<__libc_writev>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %edx,%r12d
mov    %rsi,%r15
test   %edx,%edx
jle    11cd <__libc_writev+0x4d>
mov    0x2e49(%rip),%rax
mov    (%rax),%rbp
mov    %r12d,%ecx
shl    $0x4,%rcx
xor    %edx,%edx
xor    %r14d,%r14d
xchg   %ax,%ax
mov    %rbp,%rax
sub    %r14,%rax
mov    (%r15,%rdx,1),%rsi
cmp    %rsi,%rax
jb     11ee <__libc_writev+0x6e>
add    %rsi,%r14
add    $0x10,%rdx
cmp    %rdx,%rcx
jne    11b0 <__libc_writev+0x30>
jmp    11d0 <__libc_writev+0x50>
xor    %r14d,%r14d
mov    %edi,0xc(%rsp)
mov    %r14,%rdi
call   1090 <__libc_use_alloca@plt>
mov    %rax,%rbx
mov    %r14,%rdi
test   %rax,%rax
je     1201 <__libc_writev+0x81>
call   10a0 <__alloca@plt>
jmp    120f <__libc_writev+0x8f>
mov    0x2de3(%rip),%rax
mov    (%rax),%edi
call   1030 <__set_errno@plt>
jmp    129f <__libc_writev+0x11f>
call   1080 <malloc@plt>
test   %rax,%rax
je     129f <__libc_writev+0x11f>
mov    %rax,0x10(%rsp)
mov    %rbx,0x18(%rsp)
test   %r12d,%r12d
jle    1277 <__libc_writev+0xf7>
mov    %r12d,%eax
mov    %rax,0x20(%rsp)
add    $0x8,%r15
mov    $0x1,%r13d
mov    %r14,%rbp
mov    0x10(%rsp),%r12
nopl   0x0(%rax,%rax,1)
mov    -0x8(%r15),%rdi
mov    %rbp,%rsi
call   1050 <MIN@plt>
mov    %rax,%rbx
mov    (%r15),%rsi
mov    %r12,%rdi
mov    %rax,%rdx
call   1060 <__mempcpy@plt>
sub    %rbx,%rbp
je     1277 <__libc_writev+0xf7>
mov    %rax,%r12
add    $0x10,%r15
lea    0x1(%r13),%rax
cmp    0x20(%rsp),%r13
mov    %rax,%r13
jb     1240 <__libc_writev+0xc0>
mov    0xc(%rsp),%edi
mov    0x10(%rsp),%rbp
mov    %rbp,%rsi
mov    %r14,%rdx
call   1070 <__write@plt>
mov    %eax,%ebx
cmpq   $0x0,0x18(%rsp)
jne    12a4 <__libc_writev+0x124>
mov    %rbp,%rdi
call   1040 <free@plt>
jmp    12a4 <__libc_writev+0x124>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.iovec = type { i64, i64 }
@SSIZE_MAX = global i64 0
@EINVAL = global i32 0
define i32 @__libc_writev(i32 %0, %struct.iovec* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.iovec*
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i64
%11 = alloca i64
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca i64
store i32 %0, i32* %5
store %struct.iovec* %1, %struct.iovec** %6
store i32 %2, i32* %7
store i32 0, i32* %14
store i64 0, i64* %10
store i32 0, i32* %13
br label %16
16: ; preds = %43, %3
%17 = load i32, i32* %13
%18 = load i32, i32* %7
%19 = icmp slt i32 %17, %18
br i1 %19, label %20, label %46
20: ; preds = %16
%21 = load i64, i64* @SSIZE_MAX
%22 = load i64, i64* %10
%23 = sub i64 %21, %22
%24 = load %struct.iovec*, %struct.iovec** %6
%25 = load i32, i32* %13
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds %struct.iovec, %struct.iovec* %24, i64 %26
%28 = getelementptr inbounds %struct.iovec, %struct.iovec* %27, i32 0, i32 0
%29 = load i64, i64* %28
%30 = icmp ult i64 %23, %29
br i1 %30, label %31, label %34
31: ; preds = %20
%32 = load i32, i32* @EINVAL
%33 = call i32 @__set_errno(i32 %32)
store i32 -1, i32* %4
br label %110
34: ; preds = %20
%35 = load %struct.iovec*, %struct.iovec** %6
%36 = load i32, i32* %13
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds %struct.iovec, %struct.iovec* %35, i64 %37
%39 = getelementptr inbounds %struct.iovec, %struct.iovec* %38, i32 0, i32 0
%40 = load i64, i64* %39
%41 = load i64, i64* %10
%42 = add i64 %41, %40
store i64 %42, i64* %10
br label %43
43: ; preds = %34
%44 = load i32, i32* %13
%45 = add nsw i32 %44, 1
store i32 %45, i32* %13
br label %16
46: ; preds = %16
%47 = load i64, i64* %10
%48 = call i64 @__libc_use_alloca(i64 %47)
%49 = icmp ne i64 %48, 0
br i1 %49, label %50, label %54
50: ; preds = %46
%51 = load i64, i64* %10
%52 = call i64 @__alloca(i64 %51)
%53 = inttoptr i64 %52 to i8*
store i8* %53, i8** %8
br label %62
54: ; preds = %46
%55 = load i64, i64* %10
%56 = call i64 @malloc(i64 %55)
%57 = inttoptr i64 %56 to i8*
store i8* %57, i8** %8
%58 = load i8*, i8** %8
%59 = icmp eq i8* %58, null
br i1 %59, label %60, label %61
60: ; preds = %54
store i32 -1, i32* %4
br label %110
61: ; preds = %54
store i32 1, i32* %14
br label %62
62: ; preds = %61, %50
%63 = load i64, i64* %10
store i64 %63, i64* %11
%64 = load i8*, i8** %8
store i8* %64, i8** %9
store i32 0, i32* %13
br label %65
65: ; preds = %95, %62
%66 = load i32, i32* %13
%67 = load i32, i32* %7
%68 = icmp slt i32 %66, %67
br i1 %68, label %69, label %98
69: ; preds = %65
%70 = load %struct.iovec*, %struct.iovec** %6
%71 = load i32, i32* %13
%72 = sext i32 %71 to i64
%73 = getelementptr inbounds %struct.iovec, %struct.iovec* %70, i64 %72
%74 = getelementptr inbounds %struct.iovec, %struct.iovec* %73, i32 0, i32 0
%75 = load i64, i64* %74
%76 = load i64, i64* %11
%77 = call i64 @MIN(i64 %75, i64 %76)
store i64 %77, i64* %15
%78 = load i8*, i8** %9
%79 = load %struct.iovec*, %struct.iovec** %6
%80 = load i32, i32* %13
%81 = sext i32 %80 to i64
%82 = getelementptr inbounds %struct.iovec, %struct.iovec* %79, i64 %81
%83 = getelementptr inbounds %struct.iovec, %struct.iovec* %82, i32 0, i32 1
%84 = load i64, i64* %83
%85 = inttoptr i64 %84 to i8*
%86 = load i64, i64* %15
%87 = call i8* @__mempcpy(i8* %78, i8* %85, i64 %86)
store i8* %87, i8** %9
%88 = load i64, i64* %15
%89 = load i64, i64* %11
%90 = sub i64 %89, %88
store i64 %90, i64* %11
%91 = load i64, i64* %11
%92 = icmp eq i64 %91, 0
br i1 %92, label %93, label %94
93: ; preds = %69
br label %98
94: ; preds = %69
br label %95
95: ; preds = %94
%96 = load i32, i32* %13
%97 = add nsw i32 %96, 1
store i32 %97, i32* %13
br label %65
98: ; preds = %93, %65
%99 = load i32, i32* %5
%100 = load i8*, i8** %8
%101 = load i64, i64* %10
%102 = call i32 @__write(i32 %99, i8* %100, i64 %101)
store i32 %102, i32* %12
%103 = load i32, i32* %14
%104 = icmp ne i32 %103, 0
br i1 %104, label %105, label %108
105: ; preds = %98
%106 = load i8*, i8** %8
%107 = call i32 @free(i8* %106)
br label %108
108: ; preds = %105, %98
%109 = load i32, i32* %12
store i32 %109, i32* %4
br label %110
110: ; preds = %108, %60, %31
%111 = load i32, i32* %4
ret i32 %111
}

LLVM IR (-O2):
%struct.iovec = type { i64, i64 }
@SSIZE_MAX = global i64 0
@EINVAL = global i32 0
define i32 @__libc_writev(i32 %0, %struct.iovec* nocapture %1, i32 %2) {
%4 = icmp sgt i32 %2, 0
br i1 %4, label %5, label %22
5: ; preds = %3
%6 = load i64, i64* @SSIZE_MAX
%7 = zext i32 %2 to i64
br label %8
8: ; preds = %5, %18
%9 = phi i64 [ 0, %5 ], [ %20, %18 ]
%10 = phi i64 [ 0, %5 ], [ %19, %18 ]
%11 = sub i64 %6, %10
%12 = getelementptr inbounds %struct.iovec, %struct.iovec* %1, i64 %9, i32 0
%13 = load i64, i64* %12
%14 = icmp ult i64 %11, %13
br i1 %14, label %15, label %18
15: ; preds = %8
%16 = load i32, i32* @EINVAL
%17 = tail call i32 @__set_errno(i32 %16)
br label %56
18: ; preds = %8
%19 = add i64 %13, %10
%20 = add nuw nsw i64 %9, 1
%21 = icmp eq i64 %20, %7
br i1 %21, label %22, label %8
22: ; preds = %18, %3
%23 = phi i64 [ 0, %3 ], [ %19, %18 ]
%24 = tail call i64 @__libc_use_alloca(i64 %23)
%25 = icmp eq i64 %24, 0
br i1 %25, label %28, label %26
26: ; preds = %22
%27 = tail call i64 @__alloca(i64 %23)
br label %31
28: ; preds = %22
%29 = tail call i64 @malloc(i64 %23)
%30 = icmp eq i64 %29, 0
br i1 %30, label %56, label %31
31: ; preds = %28, %26
%32 = phi i64 [ %27, %26 ], [ %29, %28 ]
%33 = inttoptr i64 %32 to i8*
br i1 %4, label %34, label %52
34: ; preds = %31
%35 = zext i32 %2 to i64
br label %36
36: ; preds = %36, %34
%37 = phi i64 [ %49, %36 ], [ 0, %34 ]
%38 = phi i64 [ %47, %36 ], [ %23, %34 ]
%39 = phi i8* [ %46, %36 ], [ %33, %34 ]
%40 = getelementptr inbounds %struct.iovec, %struct.iovec* %1, i64 %37, i32 0
%41 = load i64, i64* %40
%42 = tail call i64 @MIN(i64 %41, i64 %38)
%43 = getelementptr inbounds %struct.iovec, %struct.iovec* %1, i64 %37, i32 1
%44 = load i64, i64* %43
%45 = inttoptr i64 %44 to i8*
%46 = tail call i8* @__mempcpy(i8* %39, i8* %45, i64 %42)
%47 = sub i64 %38, %42
%48 = icmp ne i64 %47, 0
%49 = add nuw nsw i64 %37, 1
%50 = icmp ult i64 %49, %35
%51 = select i1 %48, i1 %50, i1 false
br i1 %51, label %36, label %52
52: ; preds = %36, %31
%53 = tail call i32 @__write(i32 %0, i8* %33, i64 %23)
br i1 %25, label %54, label %56
54: ; preds = %52
%55 = tail call i32 @free(i8* %33)
br label %56
56: ; preds = %52, %54, %28, %15
%57 = phi i32 [ -1, %15 ], [ -1, %28 ], [ %53, %54 ], [ %53, %52 ]
ret i32 %57
}