#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);
int __read(int, char *, size_t);
int /*<<< orphan*/ __set_errno(int /*<<< orphan*/);
int /*<<< orphan*/ free(char *);
scalar_t__ malloc(size_t);
int /*<<< orphan*/ memcpy(void *, void *, size_t);
ssize_t __libc_readv(int fd, const struct iovec *vector, int count) {
  char *buffer;
  char *buffer_start;
  size_t bytes;
  ssize_t bytes_read;
  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;
  }
  bytes_read = __read(fd, buffer, bytes);
  if (bytes_read <= 0)
    return -1;
  bytes = bytes_read;
  buffer_start = buffer;
  for (i = 0; i < count; ++i) {
    size_t copy = MIN(vector[i].iov_len, bytes);
    (void)memcpy((void *)vector[i].iov_base, (void *)buffer, copy);
    buffer += copy;
    bytes -= copy;
    if (bytes == 0)
      break;
  }
  if (use_malloc)
    free(buffer_start);
  return bytes_read;
}


x86 assembly (clang, -O0):
<__libc_readv>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x3c(%rbp)
movq   $0x0,-0x30(%rbp)
movl   $0x0,-0x38(%rbp)
mov    -0x38(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    121c <__libc_readv+0x9c>
mov    0x2e2d(%rip),%rax
mov    (%rax),%rax
sub    -0x30(%rbp),%rax
mov    -0x10(%rbp),%rcx
movslq -0x38(%rbp),%rdx
shl    $0x4,%rdx
add    %rdx,%rcx
cmp    (%rcx),%rax
jae    11f4 <__libc_readv+0x74>
mov    0x2df7(%rip),%rax
mov    (%rax),%edi
call   1030 <__set_errno@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    134e <__libc_readv+0x1ce>
mov    -0x10(%rbp),%rax
movslq -0x38(%rbp),%rcx
shl    $0x4,%rcx
add    %rcx,%rax
mov    (%rax),%rax
add    -0x30(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x38(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x38(%rbp)
jmp    11a8 <__libc_readv+0x28>
mov    -0x30(%rbp),%rdi
call   1090 <__libc_use_alloca@plt>
cmp    $0x0,%rax
je     1241 <__libc_readv+0xc1>
mov    -0x30(%rbp),%rdi
call   10a0 <__alloca@plt>
mov    %rax,-0x20(%rbp)
jmp    126c <__libc_readv+0xec>
mov    -0x30(%rbp),%rdi
call   1080 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1265 <__libc_readv+0xe5>
movl   $0xffffffff,-0x4(%rbp)
jmp    134e <__libc_readv+0x1ce>
movl   $0x1,-0x3c(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x30(%rbp),%rdx
call   1060 <__read@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jg     1295 <__libc_readv+0x115>
movl   $0xffffffff,-0x4(%rbp)
jmp    134e <__libc_readv+0x1ce>
movslq -0x34(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x38(%rbp)
mov    -0x38(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    1335 <__libc_readv+0x1b5>
mov    -0x10(%rbp),%rax
movslq -0x38(%rbp),%rcx
shl    $0x4,%rcx
add    %rcx,%rax
mov    (%rax),%rdi
mov    -0x30(%rbp),%rsi
call   1050 <MIN@plt>
mov    %rax,-0x48(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x38(%rbp),%rcx
shl    $0x4,%rcx
add    %rcx,%rax
mov    0x8(%rax),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x48(%rbp),%rdx
call   1070 <memcpy@plt>
mov    -0x48(%rbp),%rax
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x48(%rbp),%rcx
mov    -0x30(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1322 <__libc_readv+0x1a2>
jmp    1335 <__libc_readv+0x1b5>
jmp    1327 <__libc_readv+0x1a7>
mov    -0x38(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x38(%rbp)
jmp    12ac <__libc_readv+0x12c>
cmpl   $0x0,-0x3c(%rbp)
je     1348 <__libc_readv+0x1c8>
mov    -0x28(%rbp),%rdi
call   1040 <free@plt>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<__libc_readv>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edx,%r15d
mov    %rsi,%r14
mov    %edi,%r13d
test   %edx,%edx
jle    11cd <__libc_readv+0x4d>
mov    0x2e46(%rip),%rax
mov    (%rax),%rax
mov    %r15d,%ecx
shl    $0x4,%rcx
xor    %edx,%edx
xor    %ebp,%ebp
mov    %rax,%rdi
sub    %rbp,%rdi
mov    (%r14,%rdx,1),%rsi
cmp    %rsi,%rdi
jb     11ec <__libc_readv+0x6c>
add    %rsi,%rbp
add    $0x10,%rdx
cmp    %rdx,%rcx
jne    11b0 <__libc_readv+0x30>
jmp    11cf <__libc_readv+0x4f>
xor    %ebp,%ebp
mov    %rbp,%rdi
call   1090 <__libc_use_alloca@plt>
mov    %rax,%r12
mov    %rbp,%rdi
test   %rax,%rax
je     11ff <__libc_readv+0x7f>
call   10a0 <__alloca@plt>
mov    %rax,%rbx
jmp    1210 <__libc_readv+0x90>
mov    0x2de5(%rip),%rax
mov    (%rax),%edi
call   1030 <__set_errno@plt>
jmp    129d <__libc_readv+0x11d>
call   1080 <malloc@plt>
mov    %rax,%rbx
test   %rax,%rax
je     129d <__libc_readv+0x11d>
mov    %r13d,%edi
mov    %rbx,%rsi
mov    %rbp,%rdx
call   1060 <__read@plt>
test   %eax,%eax
jle    129d <__libc_readv+0x11d>
mov    %rbx,0x8(%rsp)
mov    %eax,0x4(%rsp)
mov    %r12,0x10(%rsp)
test   %r15d,%r15d
jle    1285 <__libc_readv+0x105>
mov    0x4(%rsp),%r12d
mov    %r15d,%r15d
add    $0x8,%r14
mov    $0x1,%r13d
mov    0x8(%rsp),%rbx
mov    -0x8(%r14),%rdi
mov    %r12,%rsi
call   1050 <MIN@plt>
mov    %rax,%rbp
mov    (%r14),%rdi
mov    %rbx,%rsi
mov    %rax,%rdx
call   1070 <memcpy@plt>
sub    %rbp,%r12
je     1285 <__libc_readv+0x105>
add    %rbp,%rbx
add    $0x10,%r14
lea    0x1(%r13),%rax
cmp    %r15,%r13
mov    %rax,%r13
jb     1250 <__libc_readv+0xd0>
cmpq   $0x0,0x10(%rsp)
jne    1297 <__libc_readv+0x117>
mov    0x8(%rsp),%rdi
call   1040 <free@plt>
mov    0x4(%rsp),%eax
jmp    12a2 <__libc_readv+0x122>
mov    $0xffffffff,%eax
add    $0x18,%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_readv(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 i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i64
store i32 %0, i32* %5
store %struct.iovec* %1, %struct.iovec** %6
store i32 %2, i32* %7
store i32 0, i32* %13
store i64 0, i64* %10
store i32 0, i32* %12
br label %15
15: ; preds = %42, %3
%16 = load i32, i32* %12
%17 = load i32, i32* %7
%18 = icmp slt i32 %16, %17
br i1 %18, label %19, label %45
19: ; preds = %15
%20 = load i64, i64* @SSIZE_MAX
%21 = load i64, i64* %10
%22 = sub i64 %20, %21
%23 = load %struct.iovec*, %struct.iovec** %6
%24 = load i32, i32* %12
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds %struct.iovec, %struct.iovec* %23, i64 %25
%27 = getelementptr inbounds %struct.iovec, %struct.iovec* %26, i32 0, i32 0
%28 = load i64, i64* %27
%29 = icmp ult i64 %22, %28
br i1 %29, label %30, label %33
30: ; preds = %19
%31 = load i32, i32* @EINVAL
%32 = call i32 @__set_errno(i32 %31)
store i32 -1, i32* %4
br label %117
33: ; preds = %19
%34 = load %struct.iovec*, %struct.iovec** %6
%35 = load i32, i32* %12
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds %struct.iovec, %struct.iovec* %34, i64 %36
%38 = getelementptr inbounds %struct.iovec, %struct.iovec* %37, i32 0, i32 0
%39 = load i64, i64* %38
%40 = load i64, i64* %10
%41 = add i64 %40, %39
store i64 %41, i64* %10
br label %42
42: ; preds = %33
%43 = load i32, i32* %12
%44 = add nsw i32 %43, 1
store i32 %44, i32* %12
br label %15
45: ; preds = %15
%46 = load i64, i64* %10
%47 = call i64 @__libc_use_alloca(i64 %46)
%48 = icmp ne i64 %47, 0
br i1 %48, label %49, label %53
49: ; preds = %45
%50 = load i64, i64* %10
%51 = call i64 @__alloca(i64 %50)
%52 = inttoptr i64 %51 to i8*
store i8* %52, i8** %8
br label %61
53: ; preds = %45
%54 = load i64, i64* %10
%55 = call i64 @malloc(i64 %54)
%56 = inttoptr i64 %55 to i8*
store i8* %56, i8** %8
%57 = load i8*, i8** %8
%58 = icmp eq i8* %57, null
br i1 %58, label %59, label %60
59: ; preds = %53
store i32 -1, i32* %4
br label %117
60: ; preds = %53
store i32 1, i32* %13
br label %61
61: ; preds = %60, %49
%62 = load i32, i32* %5
%63 = load i8*, i8** %8
%64 = load i64, i64* %10
%65 = call i32 @__read(i32 %62, i8* %63, i64 %64)
store i32 %65, i32* %11
%66 = load i32, i32* %11
%67 = icmp sle i32 %66, 0
br i1 %67, label %68, label %69
68: ; preds = %61
store i32 -1, i32* %4
br label %117
69: ; preds = %61
%70 = load i32, i32* %11
%71 = sext i32 %70 to i64
store i64 %71, i64* %10
%72 = load i8*, i8** %8
store i8* %72, i8** %9
store i32 0, i32* %12
br label %73
73: ; preds = %106, %69
%74 = load i32, i32* %12
%75 = load i32, i32* %7
%76 = icmp slt i32 %74, %75
br i1 %76, label %77, label %109
77: ; preds = %73
%78 = load %struct.iovec*, %struct.iovec** %6
%79 = load i32, i32* %12
%80 = sext i32 %79 to i64
%81 = getelementptr inbounds %struct.iovec, %struct.iovec* %78, i64 %80
%82 = getelementptr inbounds %struct.iovec, %struct.iovec* %81, i32 0, i32 0
%83 = load i64, i64* %82
%84 = load i64, i64* %10
%85 = call i64 @MIN(i64 %83, i64 %84)
store i64 %85, i64* %14
%86 = load %struct.iovec*, %struct.iovec** %6
%87 = load i32, i32* %12
%88 = sext i32 %87 to i64
%89 = getelementptr inbounds %struct.iovec, %struct.iovec* %86, i64 %88
%90 = getelementptr inbounds %struct.iovec, %struct.iovec* %89, i32 0, i32 1
%91 = load i64, i64* %90
%92 = inttoptr i64 %91 to i8*
%93 = load i8*, i8** %8
%94 = load i64, i64* %14
%95 = call i32 @memcpy(i8* %92, i8* %93, i64 %94)
%96 = load i64, i64* %14
%97 = load i8*, i8** %8
%98 = getelementptr inbounds i8, i8* %97, i64 %96
store i8* %98, i8** %8
%99 = load i64, i64* %14
%100 = load i64, i64* %10
%101 = sub i64 %100, %99
store i64 %101, i64* %10
%102 = load i64, i64* %10
%103 = icmp eq i64 %102, 0
br i1 %103, label %104, label %105
104: ; preds = %77
br label %109
105: ; preds = %77
br label %106
106: ; preds = %105
%107 = load i32, i32* %12
%108 = add nsw i32 %107, 1
store i32 %108, i32* %12
br label %73
109: ; preds = %104, %73
%110 = load i32, i32* %13
%111 = icmp ne i32 %110, 0
br i1 %111, label %112, label %115
112: ; preds = %109
%113 = load i8*, i8** %9
%114 = call i32 @free(i8* %113)
br label %115
115: ; preds = %112, %109
%116 = load i32, i32* %11
store i32 %116, i32* %4
br label %117
117: ; preds = %115, %68, %59, %30
%118 = load i32, i32* %4
ret i32 %118
}

LLVM IR (-O2):
%struct.iovec = type { i64, i64 }
@SSIZE_MAX = global i64 0
@EINVAL = global i32 0
define i32 @__libc_readv(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 %60
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 %60, label %31
31: ; preds = %28, %26
%32 = phi i64 [ %27, %26 ], [ %29, %28 ]
%33 = inttoptr i64 %32 to i8*
%34 = tail call i32 @__read(i32 %0, i8* %33, i64 %23)
%35 = icmp slt i32 %34, 1
br i1 %35, label %60, label %36
36: ; preds = %31
br i1 %4, label %37, label %57
37: ; preds = %36
%38 = zext i32 %34 to i64
%39 = zext i32 %2 to i64
br label %40
40: ; preds = %37, %40
%41 = phi i64 [ 0, %37 ], [ %54, %40 ]
%42 = phi i8* [ %33, %37 ], [ %53, %40 ]
%43 = phi i64 [ %38, %37 ], [ %51, %40 ]
%44 = getelementptr inbounds %struct.iovec, %struct.iovec* %1, i64 %41, i32 0
%45 = load i64, i64* %44
%46 = tail call i64 @MIN(i64 %45, i64 %43)
%47 = getelementptr inbounds %struct.iovec, %struct.iovec* %1, i64 %41, i32 1
%48 = load i64, i64* %47
%49 = inttoptr i64 %48 to i8*
%50 = tail call i32 @memcpy(i8* %49, i8* %42, i64 %46)
%51 = sub i64 %43, %46
%52 = icmp ne i64 %51, 0
%53 = getelementptr inbounds i8, i8* %42, i64 %46
%54 = add nuw nsw i64 %41, 1
%55 = icmp ult i64 %54, %39
%56 = select i1 %52, i1 %55, i1 false
br i1 %56, label %40, label %57
57: ; preds = %40, %36
br i1 %25, label %58, label %60
58: ; preds = %57
%59 = tail call i32 @free(i8* %33)
br label %60
60: ; preds = %57, %58, %31, %28, %15
%61 = phi i32 [ -1, %15 ], [ -1, %28 ], [ -1, %31 ], [ %34, %58 ], [ %34, %57 ]
ret i32 %61
}