#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 stat {
  int dummy;
};
struct iovec {
  char *iov_base;
  int /*<<< orphan*/ iov_len;
};

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int EXIT_SUCCESS;
int O_CREAT;
int O_RDWR;
int /*<<< orphan*/ assert(int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
char *malloc(int);
int open(char *, int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int /*<<< orphan*/ strlen(char *);
int writev(int, struct iovec *, int);
int main(int argc, char **argv) {
  int fd;
  struct iovec ios[3];
  char *buffs[3];
  struct stat st;
  int rc;
  int i;
  int total_lines = 0;
  fd = open("writev_testfile.txt", O_CREAT | O_RDWR, 0666);
  if (fd < 0) {
    perror("open");
    exit(EXIT_FAILURE);
  }
  for (i = 0; i < 3; i++) {
    buffs[i] = malloc(0x100);
    assert(buffs[i] != NULL);
  }
  for (i = 0; i < 3; i++) {
    int j;
    for (j = 0; j < 3; j++, total_lines++) {
      snprintf(buffs[j], 0x100, "TestLine: %d\n", total_lines);
      ios[j].iov_base = buffs[j];
      ios[j].iov_len = strlen(buffs[j]);
    }
    rc = writev(fd, ios, 3);
    if (rc <= 0) {
      perror("writev");
      exit(EXIT_FAILURE);
    }
  }
  close(fd);
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x84(%rbp)
mov    0x2e3e(%rip),%rax
mov    (%rax),%esi
mov    0x2e1d(%rip),%rax
or     (%rax),%esi
lea    0xe34(%rip),%rdi
mov    $0x1b6,%edx
call   1090 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11fd <main+0x6d>
lea    0xe2a(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2dda(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
movl   $0x0,-0x80(%rbp)
cmpl   $0x3,-0x80(%rbp)
jge    1249 <main+0xb9>
mov    $0x100,%edi
call   1080 <malloc@plt>
mov    %rax,%rcx
movslq -0x80(%rbp),%rax
mov    %rcx,-0x70(%rbp,%rax,8)
movslq -0x80(%rbp),%rax
cmpq   $0x0,-0x70(%rbp,%rax,8)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
mov    -0x80(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x80(%rbp)
jmp    1204 <main+0x74>
movl   $0x0,-0x80(%rbp)
cmpl   $0x3,-0x80(%rbp)
jge    134b <main+0x1bb>
movl   $0x0,-0x88(%rbp)
cmpl   $0x3,-0x88(%rbp)
jge    1300 <main+0x170>
movslq -0x88(%rbp),%rax
mov    -0x70(%rbp,%rax,8),%rdi
mov    -0x84(%rbp),%ecx
mov    $0x100,%esi
lea    0xd8a(%rip),%rdx
call   1050 <snprintf@plt>
movslq -0x88(%rbp),%rax
mov    -0x70(%rbp,%rax,8),%rcx
movslq -0x88(%rbp),%rdx
lea    -0x50(%rbp),%rax
shl    $0x4,%rdx
add    %rdx,%rax
mov    %rcx,(%rax)
movslq -0x88(%rbp),%rax
mov    -0x70(%rbp,%rax,8),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
movslq -0x88(%rbp),%rdx
lea    -0x50(%rbp),%rax
shl    $0x4,%rdx
add    %rdx,%rax
mov    %ecx,0x8(%rax)
mov    -0x88(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x88(%rbp)
mov    -0x84(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x84(%rbp)
jmp    1264 <main+0xd4>
mov    -0x14(%rbp),%edi
lea    -0x50(%rbp),%rsi
mov    $0x3,%edx
call   1030 <writev@plt>
mov    %eax,-0x7c(%rbp)
cmpl   $0x0,-0x7c(%rbp)
jg     1338 <main+0x1a8>
lea    0xd02(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2c9f(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
jmp    133d <main+0x1ad>
mov    -0x80(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x80(%rbp)
jmp    1250 <main+0xc0>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
mov    0x2c6e(%rip),%rax
mov    (%rax),%eax
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x30,%rsp
mov    0x2e55(%rip),%rax
mov    0x2e36(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe4b(%rip),%rdi
mov    $0x1b6,%edx
call   1090 <open@plt>
test   %eax,%eax
js     13eb <main+0x25b>
mov    %eax,%r14d
mov    $0x100,%edi
call   1080 <malloc@plt>
mov    %rax,%rbx
xor    %edi,%edi
test   %rax,%rax
setne  %dil
call   1070 <assert@plt>
mov    $0x100,%edi
call   1080 <malloc@plt>
mov    %rax,%rbp
xor    %edi,%edi
test   %rax,%rax
setne  %dil
call   1070 <assert@plt>
mov    $0x100,%edi
call   1080 <malloc@plt>
mov    %rax,%r15
xor    %edi,%edi
test   %rax,%rax
setne  %dil
call   1070 <assert@plt>
lea    0xdf7(%rip),%r12
mov    %rbx,%rdi
mov    $0x100,%esi
mov    %r12,%rdx
xor    %ecx,%ecx
call   1050 <snprintf@plt>
mov    %rbx,(%rsp)
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %eax,0x8(%rsp)
mov    %rbp,%rdi
mov    $0x100,%esi
mov    %r12,%rdx
mov    $0x1,%ecx
call   1050 <snprintf@plt>
mov    %rbp,0x10(%rsp)
mov    %rbp,%rdi
call   1040 <strlen@plt>
mov    %eax,0x18(%rsp)
mov    %r15,%rdi
mov    $0x100,%esi
mov    %r12,%rdx
mov    $0x2,%ecx
call   1050 <snprintf@plt>
mov    %r15,0x20(%rsp)
mov    %r15,%rdi
call   1040 <strlen@plt>
mov    %eax,0x28(%rsp)
mov    %rsp,%rsi
mov    %r14d,%edi
mov    $0x3,%edx
call   1030 <writev@plt>
test   %eax,%eax
jle    13e2 <main+0x252>
lea    0xd6a(%rip),%r12
mov    %rbx,%rdi
mov    $0x100,%esi
mov    %r12,%rdx
mov    $0x3,%ecx
call   1050 <snprintf@plt>
mov    %rbx,(%rsp)
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %eax,0x8(%rsp)
mov    %rbp,%rdi
mov    $0x100,%esi
mov    %r12,%rdx
mov    $0x4,%ecx
call   1050 <snprintf@plt>
mov    %rbp,0x10(%rsp)
mov    %rbp,%rdi
call   1040 <strlen@plt>
mov    %eax,0x18(%rsp)
mov    %r15,%rdi
mov    $0x100,%esi
mov    %r12,%rdx
mov    $0x5,%ecx
call   1050 <snprintf@plt>
mov    %r15,0x20(%rsp)
mov    %r15,%rdi
call   1040 <strlen@plt>
mov    %eax,0x28(%rsp)
mov    %rsp,%rsi
mov    %r14d,%edi
mov    $0x3,%edx
call   1030 <writev@plt>
test   %eax,%eax
jle    13e2 <main+0x252>
lea    0xcda(%rip),%r12
mov    %rbx,%rdi
mov    $0x100,%esi
mov    %r12,%rdx
mov    $0x6,%ecx
call   1050 <snprintf@plt>
mov    %rbx,(%rsp)
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %eax,0x8(%rsp)
mov    %rbp,%rdi
mov    $0x100,%esi
mov    %r12,%rdx
mov    $0x7,%ecx
call   1050 <snprintf@plt>
mov    %rbp,0x10(%rsp)
mov    %rbp,%rdi
call   1040 <strlen@plt>
mov    %eax,0x18(%rsp)
mov    %r15,%rdi
mov    $0x100,%esi
mov    %r12,%rdx
mov    $0x8,%ecx
call   1050 <snprintf@plt>
mov    %r15,0x20(%rsp)
mov    %r15,%rdi
call   1040 <strlen@plt>
mov    %eax,0x28(%rsp)
mov    %rsp,%rsi
mov    %r14d,%edi
mov    $0x3,%edx
call   1030 <writev@plt>
test   %eax,%eax
jle    13e2 <main+0x252>
mov    %r14d,%edi
call   1060 <close@plt>
mov    0x2bf5(%rip),%rax
mov    (%rax),%eax
add    $0x30,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xc3e(%rip),%rdi
jmp    13f2 <main+0x262>
lea    0xc22(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2bd2(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
%struct.iovec = type { i8*, i32 }
%struct.stat = type { i32 }
@.str = [20 x i8] c"writev_testfile.txt\00"
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@.str.1 = [5 x i8] c"open\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [14 x i8] c"TestLine: %d\0A\00"
@.str.3 = [7 x i8] c"writev\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca [3 x %struct.iovec]
%8 = alloca [3 x i8*]
%9 = alloca %struct.stat
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %12
%14 = load i32, i32* @O_CREAT
%15 = load i32, i32* @O_RDWR
%16 = or i32 %14, %15
%17 = call i32 @open(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i32 %16, i32 438)
store i32 %17, i32* %6
%18 = load i32, i32* %6
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %24
20: ; preds = %2
%21 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%22 = load i32, i32* @EXIT_FAILURE
%23 = call i32 @exit(i32 %22)
unreachable
24: ; preds = %2
store i32 0, i32* %11
br label %25
25: ; preds = %40, %24
%26 = load i32, i32* %11
%27 = icmp slt i32 %26, 3
br i1 %27, label %28, label %43
28: ; preds = %25
%29 = call i8* @malloc(i32 256)
%30 = load i32, i32* %11
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds [3 x i8*], [3 x i8*]* %8, i64 0, i64 %31
store i8* %29, i8** %32
%33 = load i32, i32* %11
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds [3 x i8*], [3 x i8*]* %8, i64 0, i64 %34
%36 = load i8*, i8** %35
%37 = icmp ne i8* %36, null
%38 = zext i1 %37 to i32
%39 = call i32 @assert(i32 %38)
br label %40
40: ; preds = %28
%41 = load i32, i32* %11
%42 = add nsw i32 %41, 1
store i32 %42, i32* %11
br label %25
43: ; preds = %25
store i32 0, i32* %11
br label %44
44: ; preds = %91, %43
%45 = load i32, i32* %11
%46 = icmp slt i32 %45, 3
br i1 %46, label %47, label %94
47: ; preds = %44
store i32 0, i32* %13
br label %48
48: ; preds = %75, %47
%49 = load i32, i32* %13
%50 = icmp slt i32 %49, 3
br i1 %50, label %51, label %80
51: ; preds = %48
%52 = load i32, i32* %13
%53 = sext i32 %52 to i64
%54 = getelementptr inbounds [3 x i8*], [3 x i8*]* %8, i64 0, i64 %53
%55 = load i8*, i8** %54
%56 = load i32, i32* %12
%57 = call i32 @snprintf(i8* %55, i32 256, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32 %56)
%58 = load i32, i32* %13
%59 = sext i32 %58 to i64
%60 = getelementptr inbounds [3 x i8*], [3 x i8*]* %8, i64 0, i64 %59
%61 = load i8*, i8** %60
%62 = load i32, i32* %13
%63 = sext i32 %62 to i64
%64 = getelementptr inbounds [3 x %struct.iovec], [3 x %struct.iovec]* %7, i64 0, i64 %63
%65 = getelementptr inbounds %struct.iovec, %struct.iovec* %64, i32 0, i32 0
store i8* %61, i8** %65
%66 = load i32, i32* %13
%67 = sext i32 %66 to i64
%68 = getelementptr inbounds [3 x i8*], [3 x i8*]* %8, i64 0, i64 %67
%69 = load i8*, i8** %68
%70 = call i32 @strlen(i8* %69)
%71 = load i32, i32* %13
%72 = sext i32 %71 to i64
%73 = getelementptr inbounds [3 x %struct.iovec], [3 x %struct.iovec]* %7, i64 0, i64 %72
%74 = getelementptr inbounds %struct.iovec, %struct.iovec* %73, i32 0, i32 1
store i32 %70, i32* %74
br label %75
75: ; preds = %51
%76 = load i32, i32* %13
%77 = add nsw i32 %76, 1
store i32 %77, i32* %13
%78 = load i32, i32* %12
%79 = add nsw i32 %78, 1
store i32 %79, i32* %12
br label %48
80: ; preds = %48
%81 = load i32, i32* %6
%82 = getelementptr inbounds [3 x %struct.iovec], [3 x %struct.iovec]* %7, i64 0, i64 0
%83 = call i32 @writev(i32 %81, %struct.iovec* %82, i32 3)
store i32 %83, i32* %10
%84 = load i32, i32* %10
%85 = icmp sle i32 %84, 0
br i1 %85, label %86, label %90
86: ; preds = %80
%87 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%88 = load i32, i32* @EXIT_FAILURE
%89 = call i32 @exit(i32 %88)
unreachable
90: ; preds = %80
br label %91
91: ; preds = %90
%92 = load i32, i32* %11
%93 = add nsw i32 %92, 1
store i32 %93, i32* %11
br label %44
94: ; preds = %44
%95 = load i32, i32* %6
%96 = call i32 @close(i32 %95)
%97 = load i32, i32* @EXIT_SUCCESS
ret i32 %97
}

LLVM IR (-O2):
%struct.iovec = type { i8*, i32 }
@.str = [20 x i8] c"writev_testfile.txt\00"
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@.str.1 = [5 x i8] c"open\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [14 x i8] c"TestLine: %d\0A\00"
@.str.3 = [7 x i8] c"writev\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [3 x %struct.iovec]
%4 = bitcast [3 x %struct.iovec]* %3 to i8*
%5 = load i32, i32* @O_CREAT
%6 = load i32, i32* @O_RDWR
%7 = or i32 %6, %5
%8 = tail call i32 @open(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i32 %7, i32 438)
%9 = icmp slt i32 %8, 0
br i1 %9, label %38, label %10
10: ; preds = %2
%11 = tail call dereferenceable_or_null(256) i8* @malloc(i32 256)
%12 = icmp ne i8* %11, null
%13 = zext i1 %12 to i32
%14 = tail call i32 @assert(i32 %13)
%15 = tail call dereferenceable_or_null(256) i8* @malloc(i32 256)
%16 = icmp ne i8* %15, null
%17 = zext i1 %16 to i32
%18 = tail call i32 @assert(i32 %17)
%19 = tail call dereferenceable_or_null(256) i8* @malloc(i32 256)
%20 = icmp ne i8* %19, null
%21 = zext i1 %20 to i32
%22 = tail call i32 @assert(i32 %21)
%23 = getelementptr inbounds [3 x %struct.iovec], [3 x %struct.iovec]* %3, i64 0, i64 0
%24 = call i32 @snprintf(i8* %11, i32 256, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32 0)
%25 = getelementptr inbounds [3 x %struct.iovec], [3 x %struct.iovec]* %3, i64 0, i64 0, i32 0
store i8* %11, i8** %25
%26 = call i32 @strlen(i8* %11)
%27 = getelementptr inbounds [3 x %struct.iovec], [3 x %struct.iovec]* %3, i64 0, i64 0, i32 1
store i32 %26, i32* %27
%28 = call i32 @snprintf(i8* %15, i32 256, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32 1)
%29 = getelementptr inbounds [3 x %struct.iovec], [3 x %struct.iovec]* %3, i64 0, i64 1, i32 0
store i8* %15, i8** %29
%30 = call i32 @strlen(i8* %15)
%31 = getelementptr inbounds [3 x %struct.iovec], [3 x %struct.iovec]* %3, i64 0, i64 1, i32 1
store i32 %30, i32* %31
%32 = call i32 @snprintf(i8* %19, i32 256, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32 2)
%33 = getelementptr inbounds [3 x %struct.iovec], [3 x %struct.iovec]* %3, i64 0, i64 2, i32 0
store i8* %19, i8** %33
%34 = call i32 @strlen(i8* %19)
%35 = getelementptr inbounds [3 x %struct.iovec], [3 x %struct.iovec]* %3, i64 0, i64 2, i32 1
store i32 %34, i32* %35
%36 = call i32 @writev(i32 %8, %struct.iovec* %23, i32 3)
%37 = icmp slt i32 %36, 1
br i1 %37, label %63, label %42
38: ; preds = %2
%39 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%40 = load i32, i32* @EXIT_FAILURE
%41 = tail call i32 @exit(i32 %40)
unreachable
42: ; preds = %10
%43 = call i32 @snprintf(i8* %11, i32 256, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32 3)
store i8* %11, i8** %25
%44 = call i32 @strlen(i8* %11)
store i32 %44, i32* %27
%45 = call i32 @snprintf(i8* %15, i32 256, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32 4)
store i8* %15, i8** %29
%46 = call i32 @strlen(i8* %15)
store i32 %46, i32* %31
%47 = call i32 @snprintf(i8* %19, i32 256, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32 5)
store i8* %19, i8** %33
%48 = call i32 @strlen(i8* %19)
store i32 %48, i32* %35
%49 = call i32 @writev(i32 %8, %struct.iovec* %23, i32 3)
%50 = icmp slt i32 %49, 1
br i1 %50, label %63, label %51
51: ; preds = %42
%52 = call i32 @snprintf(i8* %11, i32 256, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32 6)
store i8* %11, i8** %25
%53 = call i32 @strlen(i8* %11)
store i32 %53, i32* %27
%54 = call i32 @snprintf(i8* %15, i32 256, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32 7)
store i8* %15, i8** %29
%55 = call i32 @strlen(i8* %15)
store i32 %55, i32* %31
%56 = call i32 @snprintf(i8* %19, i32 256, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32 8)
store i8* %19, i8** %33
%57 = call i32 @strlen(i8* %19)
store i32 %57, i32* %35
%58 = call i32 @writev(i32 %8, %struct.iovec* %23, i32 3)
%59 = icmp slt i32 %58, 1
br i1 %59, label %63, label %60
60: ; preds = %51
%61 = call i32 @close(i32 %8)
%62 = load i32, i32* @EXIT_SUCCESS
ret i32 %62
63: ; preds = %51, %42, %10
%64 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%65 = load i32, i32* @EXIT_FAILURE
%66 = call i32 @exit(i32 %65)
unreachable
}