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

/* Variables and functions */
int O_CREAT;
int O_TRUNC;
int O_WRONLY;
scalar_t__ close(int);
int open(char *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ strlen(char *);
int writev(int, struct iovec *, int);
int writev1() {
  struct iovec iov[3];
  ssize_t nr;
  int fd, i;
  char *buf[] = {"The term buccaneer comes from the word boucan.\n",
                 "A boucan is a wooden frame used for cooking meat.\n",
                 "Buccaneer is the West Indies name for a pirate.\n"};
  fd = open("buccaneer.txt", O_WRONLY | O_CREAT | O_TRUNC);
  if (fd == -1) {
    perror("open");
    return 1;
  }
  for (i = 0; i < 3; i++) {
    iov[i].iov_base = buf[i];
    iov[i].iov_len = strlen(buf[i] + 1);
  }
  nr = writev(fd, iov, 3);
  if (nr == -1) {
    perror("writev");
    return 1;
  }
  printf("wrote %zd bytes\n", nr);
  if (close(fd)) {
    perror("close");
    return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<writev1>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    0x2c81(%rip),%rax
mov    %rax,-0x70(%rbp)
mov    0x2c7e(%rip),%rax
mov    %rax,-0x68(%rbp)
mov    0x2c7b(%rip),%rax
mov    %rax,-0x60(%rbp)
mov    0x2e38(%rip),%rax
mov    (%rax),%esi
mov    0x2e5f(%rip),%rax
or     (%rax),%esi
mov    0x2e4e(%rip),%rax
or     (%rax),%esi
lea    0xee9(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x48(%rbp)
cmpl   $0xffffffff,-0x48(%rbp)
jne    11d5 <writev1+0x75>
lea    0xede(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12ab <writev1+0x14b>
movl   $0x0,-0x4c(%rbp)
cmpl   $0x3,-0x4c(%rbp)
jge    1235 <writev1+0xd5>
movslq -0x4c(%rbp),%rax
mov    -0x70(%rbp,%rax,8),%rcx
movslq -0x4c(%rbp),%rdx
lea    -0x40(%rbp),%rax
shl    $0x4,%rdx
add    %rdx,%rax
mov    %rcx,(%rax)
movslq -0x4c(%rbp),%rax
mov    -0x70(%rbp,%rax,8),%rdi
add    $0x1,%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
movslq -0x4c(%rbp),%rdx
lea    -0x40(%rbp),%rax
shl    $0x4,%rdx
add    %rdx,%rax
mov    %ecx,0x8(%rax)
mov    -0x4c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4c(%rbp)
jmp    11dc <writev1+0x7c>
mov    -0x48(%rbp),%edi
lea    -0x40(%rbp),%rsi
mov    $0x3,%edx
call   1030 <writev@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0xffffffff,-0x44(%rbp)
jne    126b <writev1+0x10b>
lea    0xe4d(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12ab <writev1+0x14b>
mov    -0x44(%rbp),%esi
lea    0xe39(%rip),%rdi
call   1050 <printf@plt>
mov    -0x48(%rbp),%edi
call   1060 <close@plt>
cmp    $0x0,%rax
je     12a4 <writev1+0x144>
lea    0xe2c(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12ab <writev1+0x14b>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<writev1>:
push   %rbx
sub    $0x30,%rsp
mov    0x2e5c(%rip),%rax
mov    0x2e85(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e72(%rip),%rax
or     (%rax),%esi
lea    0xf0d(%rip),%rdi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     1223 <writev1+0xc3>
mov    %eax,%ebx
lea    0xe62(%rip),%rax
mov    %rax,(%rsp)
lea    0xe58(%rip),%rdi
call   1040 <strlen@plt>
mov    %eax,0x8(%rsp)
lea    0xe77(%rip),%rax
mov    %rax,0x10(%rsp)
lea    0xe6c(%rip),%rdi
call   1040 <strlen@plt>
mov    %eax,0x18(%rsp)
lea    0xe8e(%rip),%rax
mov    %rax,0x20(%rsp)
lea    0xe83(%rip),%rdi
call   1040 <strlen@plt>
mov    %eax,0x28(%rsp)
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0x3,%edx
call   1030 <writev@plt>
cmp    $0xffffffff,%eax
je     122c <writev1+0xcc>
lea    0xea9(%rip),%rdi
mov    %eax,%esi
call   1050 <printf@plt>
mov    %ebx,%edi
call   1060 <close@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
jne    1235 <writev1+0xd5>
add    $0x30,%rsp
pop    %rbx
ret
lea    0xe78(%rip),%rdi
jmp    123c <writev1+0xdc>
lea    0xe74(%rip),%rdi
jmp    123c <writev1+0xdc>
lea    0xe83(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%eax
jmp    121d <writev1+0xbd>

LLVM IR (-O0):
%struct.iovec = type { i8*, i32 }
@.str = [48 x i8] c"The term buccaneer comes from the word boucan.\0A\00"
@.str.1 = [51 x i8] c"A boucan is a wooden frame used for cooking meat.\0A\00"
@.str.2 = [49 x i8] c"Buccaneer is the West Indies name for a pirate.\0A\00"
@__const.writev1.buf = [3 x i8*] [i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str, i32 0, i32 0), i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.1, i32 0, i32 0), i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.2, i32 0, i32 0)]
@.str.3 = [14 x i8] c"buccaneer.txt\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@.str.4 = [5 x i8] c"open\00"
@.str.5 = [7 x i8] c"writev\00"
@.str.6 = [17 x i8] c"wrote %zd bytes\0A\00"
@.str.7 = [6 x i8] c"close\00"
define i32 @writev1() {
%1 = alloca i32
%2 = alloca [3 x %struct.iovec]
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca [3 x i8*]
%7 = bitcast [3 x i8*]* %6 to i8*
%8 = load i32, i32* @O_WRONLY
%9 = load i32, i32* @O_CREAT
%10 = or i32 %8, %9
%11 = load i32, i32* @O_TRUNC
%12 = or i32 %10, %11
%13 = call i32 @open(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), i32 %12)
store i32 %13, i32* %4
%14 = load i32, i32* %4
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %18
16: ; preds = %0
%17 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
store i32 1, i32* %1
br label %61
18: ; preds = %0
store i32 0, i32* %5
br label %19
19: ; preds = %41, %18
%20 = load i32, i32* %5
%21 = icmp slt i32 %20, 3
br i1 %21, label %22, label %44
22: ; preds = %19
%23 = load i32, i32* %5
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds [3 x i8*], [3 x i8*]* %6, i64 0, i64 %24
%26 = load i8*, i8** %25
%27 = load i32, i32* %5
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds [3 x %struct.iovec], [3 x %struct.iovec]* %2, i64 0, i64 %28
%30 = getelementptr inbounds %struct.iovec, %struct.iovec* %29, i32 0, i32 0
store i8* %26, i8** %30
%31 = load i32, i32* %5
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds [3 x i8*], [3 x i8*]* %6, i64 0, i64 %32
%34 = load i8*, i8** %33
%35 = getelementptr inbounds i8, i8* %34, i64 1
%36 = call i32 @strlen(i8* %35)
%37 = load i32, i32* %5
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds [3 x %struct.iovec], [3 x %struct.iovec]* %2, i64 0, i64 %38
%40 = getelementptr inbounds %struct.iovec, %struct.iovec* %39, i32 0, i32 1
store i32 %36, i32* %40
br label %41
41: ; preds = %22
%42 = load i32, i32* %5
%43 = add nsw i32 %42, 1
store i32 %43, i32* %5
br label %19
44: ; preds = %19
%45 = load i32, i32* %4
%46 = getelementptr inbounds [3 x %struct.iovec], [3 x %struct.iovec]* %2, i64 0, i64 0
%47 = call i32 @writev(i32 %45, %struct.iovec* %46, i32 3)
store i32 %47, i32* %3
%48 = load i32, i32* %3
%49 = icmp eq i32 %48, -1
br i1 %49, label %50, label %52
50: ; preds = %44
%51 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0))
store i32 1, i32* %1
br label %61
52: ; preds = %44
%53 = load i32, i32* %3
%54 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.6, i64 0, i64 0), i32 %53)
%55 = load i32, i32* %4
%56 = call i64 @close(i32 %55)
%57 = icmp ne i64 %56, 0
br i1 %57, label %58, label %60
58: ; preds = %52
%59 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.7, i64 0, i64 0))
store i32 1, i32* %1
br label %61
60: ; preds = %52
store i32 0, i32* %1
br label %61
61: ; preds = %60, %58, %50, %16
%62 = load i32, i32* %1
ret i32 %62
}

LLVM IR (-O2):
%struct.iovec = type { i8*, i32 }
@.str = [48 x i8] c"The term buccaneer comes from the word boucan.\0A\00"
@.str.1 = [51 x i8] c"A boucan is a wooden frame used for cooking meat.\0A\00"
@.str.2 = [49 x i8] c"Buccaneer is the West Indies name for a pirate.\0A\00"
@.str.3 = [14 x i8] c"buccaneer.txt\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@.str.4 = [5 x i8] c"open\00"
@.str.5 = [7 x i8] c"writev\00"
@.str.6 = [17 x i8] c"wrote %zd bytes\0A\00"
@.str.7 = [6 x i8] c"close\00"
define i32 @writev1() {
%1 = alloca [3 x %struct.iovec]
%2 = bitcast [3 x %struct.iovec]* %1 to i8*
%3 = load i32, i32* @O_WRONLY
%4 = load i32, i32* @O_CREAT
%5 = or i32 %4, %3
%6 = load i32, i32* @O_TRUNC
%7 = or i32 %5, %6
%8 = tail call i32 @open(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), i32 %7)
%9 = icmp eq i32 %8, -1
br i1 %9, label %23, label %10
10: ; preds = %0
%11 = getelementptr inbounds [3 x %struct.iovec], [3 x %struct.iovec]* %1, i64 0, i64 0, i32 0
store i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str, i64 0, i64 0), i8** %11
%12 = tail call i32 @strlen(i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str, i64 0, i64 1))
%13 = getelementptr inbounds [3 x %struct.iovec], [3 x %struct.iovec]* %1, i64 0, i64 0, i32 1
store i32 %12, i32* %13
%14 = getelementptr inbounds [3 x %struct.iovec], [3 x %struct.iovec]* %1, i64 0, i64 1, i32 0
store i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.1, i64 0, i64 0), i8** %14
%15 = tail call i32 @strlen(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.1, i64 0, i64 1))
%16 = getelementptr inbounds [3 x %struct.iovec], [3 x %struct.iovec]* %1, i64 0, i64 1, i32 1
store i32 %15, i32* %16
%17 = getelementptr inbounds [3 x %struct.iovec], [3 x %struct.iovec]* %1, i64 0, i64 2, i32 0
store i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.2, i64 0, i64 0), i8** %17
%18 = tail call i32 @strlen(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.2, i64 0, i64 1))
%19 = getelementptr inbounds [3 x %struct.iovec], [3 x %struct.iovec]* %1, i64 0, i64 2, i32 1
store i32 %18, i32* %19
%20 = getelementptr inbounds [3 x %struct.iovec], [3 x %struct.iovec]* %1, i64 0, i64 0
%21 = call i32 @writev(i32 %8, %struct.iovec* %20, i32 3)
%22 = icmp eq i32 %21, -1
br i1 %22, label %25, label %27
23: ; preds = %0
%24 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
br label %33
25: ; preds = %10
%26 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0))
br label %33
27: ; preds = %10
%28 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.6, i64 0, i64 0), i32 %21)
%29 = call i64 @close(i32 %8)
%30 = icmp eq i64 %29, 0
br i1 %30, label %33, label %31
31: ; preds = %27
%32 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.7, i64 0, i64 0))
br label %33
33: ; preds = %27, %31, %25, %23
%34 = phi i32 [ 1, %23 ], [ 1, %25 ], [ 1, %31 ], [ 0, %27 ]
ret i32 %34
}