#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 main(void) {
  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]);
  }
  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):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2c7a(%rip),%rax
mov    %rax,-0x70(%rbp)
mov    0x2c77(%rip),%rax
mov    %rax,-0x68(%rbp)
mov    0x2c74(%rip),%rax
mov    %rax,-0x60(%rbp)
mov    0x2e31(%rip),%rax
mov    (%rax),%esi
mov    0x2e58(%rip),%rax
or     (%rax),%esi
mov    0x2e47(%rip),%rax
or     (%rax),%esi
lea    0xee2(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x48(%rbp)
cmpl   $0xffffffff,-0x48(%rbp)
jne    11dc <main+0x7c>
lea    0xed7(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12ae <main+0x14e>
movl   $0x0,-0x4c(%rbp)
cmpl   $0x3,-0x4c(%rbp)
jge    1238 <main+0xd8>
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
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    11e3 <main+0x83>
mov    -0x48(%rbp),%edi
lea    -0x40(%rbp),%rsi
mov    $0x3,%edx
call   1030 <writev@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0xffffffff,-0x44(%rbp)
jne    126e <main+0x10e>
lea    0xe4a(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12ae <main+0x14e>
mov    -0x44(%rbp),%esi
lea    0xe36(%rip),%rdi
call   1050 <printf@plt>
mov    -0x48(%rbp),%edi
call   1060 <close@plt>
cmp    $0x0,%rax
je     12a7 <main+0x147>
lea    0xe29(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12ae <main+0x14e>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
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     120a <main+0xaa>
mov    %eax,%ebx
lea    0xe66(%rip),%rdi
mov    %rdi,(%rsp)
call   1040 <strlen@plt>
mov    %eax,0x8(%rsp)
lea    0xe82(%rip),%rdi
mov    %rdi,0x10(%rsp)
call   1040 <strlen@plt>
mov    %eax,0x18(%rsp)
lea    0xea0(%rip),%rdi
mov    %rdi,0x20(%rsp)
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     1213 <main+0xb3>
lea    0xec2(%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    121c <main+0xbc>
add    $0x30,%rsp
pop    %rbx
ret
lea    0xe91(%rip),%rdi
jmp    1223 <main+0xc3>
lea    0xe8d(%rip),%rdi
jmp    1223 <main+0xc3>
lea    0xe9c(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%eax
jmp    1204 <main+0xa4>

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.main.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 @main() {
%1 = alloca i32
%2 = alloca [3 x %struct.iovec]
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca [3 x i8*]
store i32 0, i32* %1
%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 %60
18: ; preds = %0
store i32 0, i32* %5
br label %19
19: ; preds = %40, %18
%20 = load i32, i32* %5
%21 = icmp slt i32 %20, 3
br i1 %21, label %22, label %43
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 = call i32 @strlen(i8* %34)
%36 = load i32, i32* %5
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds [3 x %struct.iovec], [3 x %struct.iovec]* %2, i64 0, i64 %37
%39 = getelementptr inbounds %struct.iovec, %struct.iovec* %38, i32 0, i32 1
store i32 %35, i32* %39
br label %40
40: ; preds = %22
%41 = load i32, i32* %5
%42 = add nsw i32 %41, 1
store i32 %42, i32* %5
br label %19
43: ; preds = %19
%44 = load i32, i32* %4
%45 = getelementptr inbounds [3 x %struct.iovec], [3 x %struct.iovec]* %2, i64 0, i64 0
%46 = call i32 @writev(i32 %44, %struct.iovec* %45, i32 3)
store i32 %46, i32* %3
%47 = load i32, i32* %3
%48 = icmp eq i32 %47, -1
br i1 %48, label %49, label %51
49: ; preds = %43
%50 = 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 %60
51: ; preds = %43
%52 = load i32, i32* %3
%53 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.6, i64 0, i64 0), i32 %52)
%54 = load i32, i32* %4
%55 = call i64 @close(i32 %54)
%56 = icmp ne i64 %55, 0
br i1 %56, label %57, label %59
57: ; preds = %51
%58 = 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 %60
59: ; preds = %51
store i32 0, i32* %1
br label %60
60: ; preds = %59, %57, %49, %16
%61 = load i32, i32* %1
ret i32 %61
}

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 @main() {
%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 0))
%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 0))
%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 0))
%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
}