#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 iov_len;
};
typedef int ssize_t;
typedef int /*<<< orphan*/ foo;
typedef int /*<<< orphan*/ baz;
typedef int /*<<< orphan*/ bar;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ STDOUT_FILENO;
scalar_t__ close(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int readv(int, struct iovec *, int);
int /*<<< orphan*/ write(int /*<<< orphan*/, char *, int);
int main(void) {
  char foo[48] = {0}, bar[51] = {0}, baz[49] = {0};
  struct iovec iov[3];
  ssize_t nr;
  int fd, i;
  fd = open("buccaneer.txt", O_RDONLY);
  if (fd == -1) {
    perror("open");
    return 1;
  }
  iov[0].iov_base = foo;
  iov[0].iov_len = sizeof(foo);
  iov[1].iov_base = bar;
  iov[1].iov_len = sizeof(bar);
  iov[2].iov_base = baz;
  iov[2].iov_len = sizeof(baz);
  nr = readv(fd, iov, 3);
  if (nr == -1) {
    perror("readv");
    return 1;
  }
  for (i = 0; i < 3; i++) {
    write(STDOUT_FILENO, iov[i].iov_base, iov[i].iov_len);
  }
  if (close(fd)) {
    perror("close");
    return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x100,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x40(%rbp),%rdi
xor    %esi,%esi
mov    $0x30,%edx
call   1040 <memset@plt>
lea    -0x80(%rbp),%rdi
xor    %esi,%esi
mov    $0x33,%edx
call   1040 <memset@plt>
lea    -0xc0(%rbp),%rdi
xor    %esi,%esi
mov    $0x31,%edx
call   1040 <memset@plt>
mov    0x2e34(%rip),%rax
mov    (%rax),%esi
lea    0xe4b(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0xf8(%rbp)
cmpl   $0xffffffff,-0xf8(%rbp)
jne    11e5 <main+0x85>
lea    0xe3a(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1306 <main+0x1a6>
lea    -0x40(%rbp),%rax
mov    %rax,-0xf0(%rbp)
movl   $0x30,-0xe8(%rbp)
lea    -0x80(%rbp),%rax
mov    %rax,-0xe0(%rbp)
movl   $0x33,-0xd8(%rbp)
lea    -0xc0(%rbp),%rax
mov    %rax,-0xd0(%rbp)
movl   $0x31,-0xc8(%rbp)
mov    -0xf8(%rbp),%edi
lea    -0xf0(%rbp),%rsi
mov    $0x3,%edx
call   1060 <readv@plt>
mov    %eax,-0xf4(%rbp)
cmpl   $0xffffffff,-0xf4(%rbp)
jne    1269 <main+0x109>
lea    0xdbb(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1306 <main+0x1a6>
movl   $0x0,-0xfc(%rbp)
cmpl   $0x3,-0xfc(%rbp)
jge    12d2 <main+0x172>
mov    0x2d69(%rip),%rax
mov    (%rax),%edi
movslq -0xfc(%rbp),%rcx
lea    -0xf0(%rbp),%rax
shl    $0x4,%rcx
add    %rcx,%rax
mov    (%rax),%rsi
movslq -0xfc(%rbp),%rcx
lea    -0xf0(%rbp),%rax
shl    $0x4,%rcx
add    %rcx,%rax
mov    0x8(%rax),%edx
call   1030 <write@plt>
mov    -0xfc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xfc(%rbp)
jmp    1273 <main+0x113>
mov    -0xf8(%rbp),%edi
call   1050 <close@plt>
cmp    $0x0,%rax
je     12ff <main+0x19f>
lea    0xd2b(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1306 <main+0x1a6>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x100,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0xe8,%rsp
xorps  %xmm0,%xmm0
movaps %xmm0,0xd0(%rsp)
movaps %xmm0,0xc0(%rsp)
movaps %xmm0,0xb0(%rsp)
movaps %xmm0,0x90(%rsp)
movaps %xmm0,0x80(%rsp)
movaps %xmm0,0x70(%rsp)
movl   $0x0,0x9f(%rsp)
movaps %xmm0,0x50(%rsp)
movaps %xmm0,0x40(%rsp)
movaps %xmm0,0x30(%rsp)
movb   $0x0,0x60(%rsp)
mov    0x2e30(%rip),%rax
mov    (%rax),%esi
lea    0xe47(%rip),%rdi
call   1060 <open@plt>
cmp    $0xffffffff,%eax
je     126a <main+0x11a>
mov    %eax,%ebx
lea    0xb0(%rsp),%rax
mov    %rax,(%rsp)
movl   $0x30,0x8(%rsp)
lea    0x70(%rsp),%rax
mov    %rax,0x10(%rsp)
movl   $0x33,0x18(%rsp)
lea    0x30(%rsp),%rax
mov    %rax,0x20(%rsp)
movl   $0x31,0x28(%rsp)
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0x3,%edx
call   1050 <readv@plt>
cmp    $0xffffffff,%eax
je     1273 <main+0x123>
mov    0x2dd4(%rip),%r14
mov    (%r14),%edi
mov    (%rsp),%rsi
mov    0x8(%rsp),%edx
call   1030 <write@plt>
mov    (%r14),%edi
mov    0x10(%rsp),%rsi
mov    0x18(%rsp),%edx
call   1030 <write@plt>
mov    (%r14),%edi
mov    0x20(%rsp),%rsi
mov    0x28(%rsp),%edx
call   1030 <write@plt>
mov    %ebx,%edi
call   1040 <close@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
jne    127c <main+0x12c>
add    $0xe8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xd9d(%rip),%rdi
jmp    1283 <main+0x133>
lea    0xd99(%rip),%rdi
jmp    1283 <main+0x133>
lea    0xd96(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%eax
jmp    125f <main+0x10f>

LLVM IR (-O0):
%struct.iovec = type { i8*, i32 }
@.str = [14 x i8] c"buccaneer.txt\00"
@O_RDONLY = global i32 0
@.str.1 = [5 x i8] c"open\00"
@.str.2 = [6 x i8] c"readv\00"
@STDOUT_FILENO = global i32 0
@.str.3 = [6 x i8] c"close\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [48 x i8]
%3 = alloca [51 x i8]
%4 = alloca [49 x i8]
%5 = alloca [3 x %struct.iovec]
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32 0, i32* %1
%9 = bitcast [48 x i8]* %2 to i8*
%10 = bitcast [51 x i8]* %3 to i8*
%11 = bitcast [49 x i8]* %4 to i8*
%12 = load i32, i32* @O_RDONLY
%13 = call i32 @open(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %12)
store i32 %13, i32* %7
%14 = load i32, i32* %7
%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.1, i64 0, i64 0))
store i32 1, i32* %1
br label %68
18: ; preds = %0
%19 = getelementptr inbounds [48 x i8], [48 x i8]* %2, i64 0, i64 0
%20 = getelementptr inbounds [3 x %struct.iovec], [3 x %struct.iovec]* %5, i64 0, i64 0
%21 = getelementptr inbounds %struct.iovec, %struct.iovec* %20, i32 0, i32 0
store i8* %19, i8** %21
%22 = getelementptr inbounds [3 x %struct.iovec], [3 x %struct.iovec]* %5, i64 0, i64 0
%23 = getelementptr inbounds %struct.iovec, %struct.iovec* %22, i32 0, i32 1
store i32 48, i32* %23
%24 = getelementptr inbounds [51 x i8], [51 x i8]* %3, i64 0, i64 0
%25 = getelementptr inbounds [3 x %struct.iovec], [3 x %struct.iovec]* %5, i64 0, i64 1
%26 = getelementptr inbounds %struct.iovec, %struct.iovec* %25, i32 0, i32 0
store i8* %24, i8** %26
%27 = getelementptr inbounds [3 x %struct.iovec], [3 x %struct.iovec]* %5, i64 0, i64 1
%28 = getelementptr inbounds %struct.iovec, %struct.iovec* %27, i32 0, i32 1
store i32 51, i32* %28
%29 = getelementptr inbounds [49 x i8], [49 x i8]* %4, i64 0, i64 0
%30 = getelementptr inbounds [3 x %struct.iovec], [3 x %struct.iovec]* %5, i64 0, i64 2
%31 = getelementptr inbounds %struct.iovec, %struct.iovec* %30, i32 0, i32 0
store i8* %29, i8** %31
%32 = getelementptr inbounds [3 x %struct.iovec], [3 x %struct.iovec]* %5, i64 0, i64 2
%33 = getelementptr inbounds %struct.iovec, %struct.iovec* %32, i32 0, i32 1
store i32 49, i32* %33
%34 = load i32, i32* %7
%35 = getelementptr inbounds [3 x %struct.iovec], [3 x %struct.iovec]* %5, i64 0, i64 0
%36 = call i32 @readv(i32 %34, %struct.iovec* %35, i32 3)
store i32 %36, i32* %6
%37 = load i32, i32* %6
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %41
39: ; preds = %18
%40 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %1
br label %68
41: ; preds = %18
store i32 0, i32* %8
br label %42
42: ; preds = %58, %41
%43 = load i32, i32* %8
%44 = icmp slt i32 %43, 3
br i1 %44, label %45, label %61
45: ; preds = %42
%46 = load i32, i32* @STDOUT_FILENO
%47 = load i32, i32* %8
%48 = sext i32 %47 to i64
%49 = getelementptr inbounds [3 x %struct.iovec], [3 x %struct.iovec]* %5, i64 0, i64 %48
%50 = getelementptr inbounds %struct.iovec, %struct.iovec* %49, i32 0, i32 0
%51 = load i8*, i8** %50
%52 = load i32, i32* %8
%53 = sext i32 %52 to i64
%54 = getelementptr inbounds [3 x %struct.iovec], [3 x %struct.iovec]* %5, i64 0, i64 %53
%55 = getelementptr inbounds %struct.iovec, %struct.iovec* %54, i32 0, i32 1
%56 = load i32, i32* %55
%57 = call i32 @write(i32 %46, i8* %51, i32 %56)
br label %58
58: ; preds = %45
%59 = load i32, i32* %8
%60 = add nsw i32 %59, 1
store i32 %60, i32* %8
br label %42
61: ; preds = %42
%62 = load i32, i32* %7
%63 = call i64 @close(i32 %62)
%64 = icmp ne i64 %63, 0
br i1 %64, label %65, label %67
65: ; preds = %61
%66 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %1
br label %68
67: ; preds = %61
store i32 0, i32* %1
br label %68
68: ; preds = %67, %65, %39, %16
%69 = load i32, i32* %1
ret i32 %69
}

LLVM IR (-O2):
%struct.iovec = type { i8*, i32 }
@.str = [14 x i8] c"buccaneer.txt\00"
@O_RDONLY = global i32 0
@.str.1 = [5 x i8] c"open\00"
@.str.2 = [6 x i8] c"readv\00"
@STDOUT_FILENO = global i32 0
@.str.3 = [6 x i8] c"close\00"
define i32 @main() {
%1 = alloca [48 x i8]
%2 = alloca [51 x i8]
%3 = alloca [49 x i8]
%4 = alloca [3 x %struct.iovec]
%5 = getelementptr inbounds [48 x i8], [48 x i8]* %1, i64 0, i64 0
%6 = getelementptr inbounds [51 x i8], [51 x i8]* %2, i64 0, i64 0
%7 = getelementptr inbounds [49 x i8], [49 x i8]* %3, i64 0, i64 0
%8 = bitcast [3 x %struct.iovec]* %4 to i8*
%9 = load i32, i32* @O_RDONLY
%10 = tail call i32 @open(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %9)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %14
12: ; preds = %0
%13 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %43
14: ; preds = %0
%15 = getelementptr inbounds [3 x %struct.iovec], [3 x %struct.iovec]* %4, i64 0, i64 0
%16 = getelementptr inbounds [3 x %struct.iovec], [3 x %struct.iovec]* %4, i64 0, i64 0, i32 0
store i8* %5, i8** %16
%17 = getelementptr inbounds [3 x %struct.iovec], [3 x %struct.iovec]* %4, i64 0, i64 0, i32 1
store i32 48, i32* %17
%18 = getelementptr inbounds [3 x %struct.iovec], [3 x %struct.iovec]* %4, i64 0, i64 1, i32 0
store i8* %6, i8** %18
%19 = getelementptr inbounds [3 x %struct.iovec], [3 x %struct.iovec]* %4, i64 0, i64 1, i32 1
store i32 51, i32* %19
%20 = getelementptr inbounds [3 x %struct.iovec], [3 x %struct.iovec]* %4, i64 0, i64 2, i32 0
store i8* %7, i8** %20
%21 = getelementptr inbounds [3 x %struct.iovec], [3 x %struct.iovec]* %4, i64 0, i64 2, i32 1
store i32 49, i32* %21
%22 = call i32 @readv(i32 %10, %struct.iovec* %15, i32 3)
%23 = icmp eq i32 %22, -1
br i1 %23, label %39, label %24
24: ; preds = %14
%25 = load i32, i32* @STDOUT_FILENO
%26 = load i8*, i8** %16
%27 = load i32, i32* %17
%28 = call i32 @write(i32 %25, i8* %26, i32 %27)
%29 = load i32, i32* @STDOUT_FILENO
%30 = load i8*, i8** %18
%31 = load i32, i32* %19
%32 = call i32 @write(i32 %29, i8* %30, i32 %31)
%33 = load i32, i32* @STDOUT_FILENO
%34 = load i8*, i8** %20
%35 = load i32, i32* %21
%36 = call i32 @write(i32 %33, i8* %34, i32 %35)
%37 = call i64 @close(i32 %10)
%38 = icmp eq i64 %37, 0
br i1 %38, label %43, label %41
39: ; preds = %14
%40 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
br label %43
41: ; preds = %24
%42 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
br label %43
43: ; preds = %24, %41, %39, %12
%44 = phi i32 [ 1, %12 ], [ 1, %39 ], [ 1, %41 ], [ 0, %24 ]
ret i32 %44
}