#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;
typedef scalar_t__ caddr_t;

/* Variables and functions */
scalar_t__ EAGAIN;
scalar_t__ EINTR;
scalar_t__ EWOULDBLOCK;
scalar_t__ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, char *, char *,
                           int);
size_t iovlen(struct iovec *, size_t);
int /*<<< orphan*/ stderr;
char *strerror(scalar_t__);
int writev(int, struct iovec *, size_t);
ssize_t writevn(int fd, struct iovec *iov, size_t n) {
  size_t total = 0, done = 0, i;
  ssize_t ret;
  total = iovlen(iov, n);
  while (done < total) {
    ret = writev(fd, iov, n);
    if (ret == -1) {
      if (errno == EINTR) {
        continue;
      }
      if (errno == EAGAIN || errno == EWOULDBLOCK) {
        return (ssize_t)done;
      }
      fprintf(
          stderr,
          "%s: "
          "writev: %s"
          " (%s:%d)\n",
          "warning", strerror(errno),
          "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/"
          "2022-01-22/02-19-57/repos/hnw/rlogd/refs/heads/master/src/common.c",
          437);
      ;
      ;
      return -1;
    }
    done += ret;
    for (i = 0; i < n; i++) {
      if (iov[i].iov_len > (size_t)ret) {
        iov[i].iov_len -= ret;
        iov[i].iov_base = (caddr_t)iov[i].iov_base + ret;
        break;
      }
      ret -= iov[i].iov_len;
      iov[i].iov_len = 0;
    }
  }
  return (ssize_t)done;
}


x86 assembly (clang, -O0):
<writevn>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1050 <iovlen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x20(%rbp),%rax
jae    1322 <writevn+0x1e2>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <writev@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0xffffffff,-0x34(%rbp)
jne    124b <writevn+0x10b>
mov    0x2e32(%rip),%rax
mov    (%rax),%rax
mov    0x2e18(%rip),%rcx
cmp    (%rcx),%rax
jne    11be <writevn+0x7e>
jmp    1174 <writevn+0x34>
mov    0x2e13(%rip),%rax
mov    (%rax),%rax
mov    0x2de9(%rip),%rcx
cmp    (%rcx),%rax
je     11f2 <writevn+0xb2>
mov    0x2df9(%rip),%rax
mov    (%rax),%rax
mov    0x2de7(%rip),%rcx
cmp    (%rcx),%rax
jne    11fe <writevn+0xbe>
mov    -0x28(%rbp),%rax
mov    %eax,-0x4(%rbp)
jmp    1329 <writevn+0x1e9>
mov    0x2ddb(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
mov    0x2dc7(%rip),%rax
mov    (%rax),%rdi
call   1060 <strerror@plt>
mov    -0x38(%rbp),%edi
mov    %rax,%rcx
lea    0xdda(%rip),%rsi
lea    0xdeb(%rip),%rdx
lea    0xdec(%rip),%r8
mov    $0x1b5,%r9d
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1329 <writevn+0x1e9>
movslq -0x34(%rbp),%rax
add    -0x28(%rbp),%rax
mov    %rax,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x30(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    131d <writevn+0x1dd>
mov    -0x10(%rbp),%rax
mov    -0x30(%rbp),%rcx
shl    $0x4,%rcx
add    %rcx,%rax
mov    (%rax),%rax
movslq -0x34(%rbp),%rcx
cmp    %rcx,%rax
jbe    12da <writevn+0x19a>
movslq -0x34(%rbp),%rdx
mov    -0x10(%rbp),%rax
mov    -0x30(%rbp),%rcx
shl    $0x4,%rcx
add    %rcx,%rax
mov    (%rax),%rcx
sub    %rdx,%rcx
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
mov    -0x30(%rbp),%rcx
shl    $0x4,%rcx
add    %rcx,%rax
mov    0x8(%rax),%rcx
movslq -0x34(%rbp),%rax
add    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    -0x30(%rbp),%rdx
shl    $0x4,%rdx
add    %rdx,%rax
mov    %rcx,0x8(%rax)
jmp    131d <writevn+0x1dd>
mov    -0x10(%rbp),%rax
mov    -0x30(%rbp),%rcx
shl    $0x4,%rcx
add    %rcx,%rax
mov    (%rax),%rcx
movslq -0x34(%rbp),%rax
sub    %rcx,%rax
mov    %eax,-0x34(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x30(%rbp),%rcx
shl    $0x4,%rcx
add    %rcx,%rax
movq   $0x0,(%rax)
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
jmp    125f <writevn+0x11f>
jmp    1174 <writevn+0x34>
mov    -0x28(%rbp),%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<writevn>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%rbx
mov    %rsi,%r15
mov    %edi,%r14d
mov    %rsi,%rdi
mov    %rdx,%rsi
call   1050 <iovlen@plt>
mov    %rax,(%rsp)
xor    %ebp,%ebp
test   %rax,%rax
je     1245 <writevn+0x105>
mov    0x2e63(%rip),%r13
mov    0x2e4c(%rip),%r12
nopl   0x0(%rax)
mov    %r14d,%edi
mov    %r15,%rsi
mov    %rbx,%rdx
call   1030 <writev@plt>
cmp    $0xffffffff,%eax
jne    119f <writevn+0x5f>
mov    0x0(%r13),%rdi
cmp    (%r12),%rdi
je     1180 <writevn+0x40>
jmp    11f5 <writevn+0xb5>
movslq %eax,%rcx
add    %rcx,%rbp
mov    %rbp,%r8
test   %rbx,%rbx
je     11ea <writevn+0xaa>
mov    %rbx,%rdx
mov    %r15,%rcx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    (%rcx),%rbp
movslq %eax,%rdi
mov    %rbp,%rsi
sub    %rdi,%rsi
ja     11e3 <writevn+0xa3>
sub    %ebp,%eax
movq   $0x0,(%rcx)
add    $0x10,%rcx
add    $0xffffffffffffffff,%rdx
jne    11c0 <writevn+0x80>
jmp    11ea <writevn+0xaa>
mov    %rsi,(%rcx)
add    %rdi,0x8(%rcx)
mov    %r8,%rbp
cmp    (%rsp),%r8
jb     1180 <writevn+0x40>
jmp    1245 <writevn+0x105>
mov    0x2dbc(%rip),%rax
cmp    (%rax),%rdi
je     1245 <writevn+0x105>
mov    0x2dc8(%rip),%rax
cmp    (%rax),%rdi
je     1245 <writevn+0x105>
mov    0x2dcc(%rip),%rax
mov    (%rax),%ebx
call   1060 <strerror@plt>
lea    0xdde(%rip),%rsi
lea    0xdef(%rip),%rdx
lea    0xdf0(%rip),%r8
mov    %ebx,%edi
mov    %rax,%rcx
mov    $0x1b5,%r9d
call   1040 <fprintf@plt>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.iovec = type { i64, i64 }
@errno = global i64 0
@EINTR = global i64 0
@EAGAIN = global i64 0
@EWOULDBLOCK = global i64 0
@stderr = global i32 0
@.str = [24 x i8] c"%s: writev: %s (%s:%d)\0A\00"
@.str.1 = [8 x i8] c"warning\00"
@.str.2 = [131 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/hnw/rlogd/refs/heads/master/src/common.c\00"
define i32 @writevn(i32 %0, %struct.iovec* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.iovec*
%7 = alloca i64
%8 = alloca i64
%9 = alloca i64
%10 = alloca i64
%11 = alloca i32
store i32 %0, i32* %5
store %struct.iovec* %1, %struct.iovec** %6
store i64 %2, i64* %7
store i64 0, i64* %8
store i64 0, i64* %9
%12 = load %struct.iovec*, %struct.iovec** %6
%13 = load i64, i64* %7
%14 = call i64 @iovlen(%struct.iovec* %12, i64 %13)
store i64 %14, i64* %8
br label %15
15: ; preds = %103, %30, %3
%16 = load i64, i64* %9
%17 = load i64, i64* %8
%18 = icmp ult i64 %16, %17
br i1 %18, label %19, label %104
19: ; preds = %15
%20 = load i32, i32* %5
%21 = load %struct.iovec*, %struct.iovec** %6
%22 = load i64, i64* %7
%23 = call i32 @writev(i32 %20, %struct.iovec* %21, i64 %22)
store i32 %23, i32* %11
%24 = load i32, i32* %11
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %47
26: ; preds = %19
%27 = load i64, i64* @errno
%28 = load i64, i64* @EINTR
%29 = icmp eq i64 %27, %28
br i1 %29, label %30, label %31
30: ; preds = %26
br label %15
31: ; preds = %26
%32 = load i64, i64* @errno
%33 = load i64, i64* @EAGAIN
%34 = icmp eq i64 %32, %33
br i1 %34, label %39, label %35
35: ; preds = %31
%36 = load i64, i64* @errno
%37 = load i64, i64* @EWOULDBLOCK
%38 = icmp eq i64 %36, %37
br i1 %38, label %39, label %42
39: ; preds = %35, %31
%40 = load i64, i64* %9
%41 = trunc i64 %40 to i32
store i32 %41, i32* %4
br label %107
42: ; preds = %35
%43 = load i32, i32* @stderr
%44 = load i64, i64* @errno
%45 = call i8* @strerror(i64 %44)
%46 = call i32 @fprintf(i32 %43, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8* %45, i8* getelementptr inbounds ([131 x i8], [131 x i8]* @.str.2, i64 0, i64 0), i32 437)
store i32 -1, i32* %4
br label %107
47: ; preds = %19
%48 = load i32, i32* %11
%49 = sext i32 %48 to i64
%50 = load i64, i64* %9
%51 = add i64 %50, %49
store i64 %51, i64* %9
store i64 0, i64* %10
br label %52
52: ; preds = %100, %47
%53 = load i64, i64* %10
%54 = load i64, i64* %7
%55 = icmp ult i64 %53, %54
br i1 %55, label %56, label %103
56: ; preds = %52
%57 = load %struct.iovec*, %struct.iovec** %6
%58 = load i64, i64* %10
%59 = getelementptr inbounds %struct.iovec, %struct.iovec* %57, i64 %58
%60 = getelementptr inbounds %struct.iovec, %struct.iovec* %59, i32 0, i32 0
%61 = load i64, i64* %60
%62 = load i32, i32* %11
%63 = sext i32 %62 to i64
%64 = icmp ugt i64 %61, %63
br i1 %64, label %65, label %86
65: ; preds = %56
%66 = load i32, i32* %11
%67 = sext i32 %66 to i64
%68 = load %struct.iovec*, %struct.iovec** %6
%69 = load i64, i64* %10
%70 = getelementptr inbounds %struct.iovec, %struct.iovec* %68, i64 %69
%71 = getelementptr inbounds %struct.iovec, %struct.iovec* %70, i32 0, i32 0
%72 = load i64, i64* %71
%73 = sub i64 %72, %67
store i64 %73, i64* %71
%74 = load %struct.iovec*, %struct.iovec** %6
%75 = load i64, i64* %10
%76 = getelementptr inbounds %struct.iovec, %struct.iovec* %74, i64 %75
%77 = getelementptr inbounds %struct.iovec, %struct.iovec* %76, i32 0, i32 1
%78 = load i64, i64* %77
%79 = load i32, i32* %11
%80 = sext i32 %79 to i64
%81 = add nsw i64 %78, %80
%82 = load %struct.iovec*, %struct.iovec** %6
%83 = load i64, i64* %10
%84 = getelementptr inbounds %struct.iovec, %struct.iovec* %82, i64 %83
%85 = getelementptr inbounds %struct.iovec, %struct.iovec* %84, i32 0, i32 1
store i64 %81, i64* %85
br label %103
86: ; preds = %56
%87 = load %struct.iovec*, %struct.iovec** %6
%88 = load i64, i64* %10
%89 = getelementptr inbounds %struct.iovec, %struct.iovec* %87, i64 %88
%90 = getelementptr inbounds %struct.iovec, %struct.iovec* %89, i32 0, i32 0
%91 = load i64, i64* %90
%92 = load i32, i32* %11
%93 = sext i32 %92 to i64
%94 = sub i64 %93, %91
%95 = trunc i64 %94 to i32
store i32 %95, i32* %11
%96 = load %struct.iovec*, %struct.iovec** %6
%97 = load i64, i64* %10
%98 = getelementptr inbounds %struct.iovec, %struct.iovec* %96, i64 %97
%99 = getelementptr inbounds %struct.iovec, %struct.iovec* %98, i32 0, i32 0
store i64 0, i64* %99
br label %100
100: ; preds = %86
%101 = load i64, i64* %10
%102 = add i64 %101, 1
store i64 %102, i64* %10
br label %52
103: ; preds = %65, %52
br label %15
104: ; preds = %15
%105 = load i64, i64* %9
%106 = trunc i64 %105 to i32
store i32 %106, i32* %4
br label %107
107: ; preds = %104, %42, %39
%108 = load i32, i32* %4
ret i32 %108
}

LLVM IR (-O2):
%struct.iovec = type { i64, i64 }
@errno = global i64 0
@EINTR = global i64 0
@EAGAIN = global i64 0
@EWOULDBLOCK = global i64 0
@stderr = global i32 0
@.str = [24 x i8] c"%s: writev: %s (%s:%d)\0A\00"
@.str.1 = [8 x i8] c"warning\00"
@.str.2 = [131 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/hnw/rlogd/refs/heads/master/src/common.c\00"
define i32 @writevn(i32 %0, %struct.iovec* %1, i64 %2) {
%4 = tail call i64 @iovlen(%struct.iovec* %1, i64 %2)
%5 = icmp eq i64 %4, 0
br i1 %5, label %53, label %6
6: ; preds = %3
%7 = icmp eq i64 %2, 0
br label %8
8: ; preds = %6, %49
%9 = phi i64 [ 0, %6 ], [ %31, %49 ]
br label %10
10: ; preds = %8, %13
%11 = tail call i32 @writev(i32 %0, %struct.iovec* %1, i64 %2)
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %29
13: ; preds = %10
%14 = load i64, i64* @errno
%15 = load i64, i64* @EINTR
%16 = icmp eq i64 %14, %15
br i1 %16, label %10, label %17
17: ; preds = %13
%18 = load i64, i64* @EAGAIN
%19 = icmp eq i64 %14, %18
%20 = load i64, i64* @EWOULDBLOCK
%21 = icmp eq i64 %14, %20
%22 = select i1 %19, i1 true, i1 %21
br i1 %22, label %23, label %25
23: ; preds = %17
%24 = trunc i64 %9 to i32
br label %53
25: ; preds = %17
%26 = load i32, i32* @stderr
%27 = tail call i8* @strerror(i64 %14)
%28 = tail call i32 @fprintf(i32 %26, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8* %27, i8* getelementptr inbounds ([131 x i8], [131 x i8]* @.str.2, i64 0, i64 0), i32 437)
br label %53
29: ; preds = %10
%30 = sext i32 %11 to i64
%31 = add i64 %9, %30
br i1 %7, label %49, label %32
32: ; preds = %29, %44
%33 = phi i32 [ %46, %44 ], [ %11, %29 ]
%34 = phi i64 [ %47, %44 ], [ 0, %29 ]
%35 = getelementptr inbounds %struct.iovec, %struct.iovec* %1, i64 %34, i32 0
%36 = load i64, i64* %35
%37 = sext i32 %33 to i64
%38 = icmp ugt i64 %36, %37
br i1 %38, label %39, label %44
39: ; preds = %32
%40 = sub i64 %36, %37
store i64 %40, i64* %35
%41 = getelementptr inbounds %struct.iovec, %struct.iovec* %1, i64 %34, i32 1
%42 = load i64, i64* %41
%43 = add nsw i64 %42, %37
store i64 %43, i64* %41
br label %49
44: ; preds = %32
%45 = trunc i64 %36 to i32
%46 = sub i32 %33, %45
store i64 0, i64* %35
%47 = add nuw i64 %34, 1
%48 = icmp eq i64 %47, %2
br i1 %48, label %49, label %32
49: ; preds = %44, %29, %39
%50 = icmp ult i64 %31, %4
br i1 %50, label %8, label %51
51: ; preds = %49
%52 = trunc i64 %31 to i32
br label %53
53: ; preds = %3, %51, %25, %23
%54 = phi i32 [ %24, %23 ], [ -1, %25 ], [ 0, %3 ], [ %52, %51 ]
ret i32 %54
}