#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;
scalar_t__ close(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int, char *);
int readv(int, struct iovec *, int);
int main() {
  char foo[48], bar[51], baz[49];
  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++)
    printf("%d: %s", i, (char *)iov[i].iov_base);
  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)
mov    0x2e7f(%rip),%rax
mov    (%rax),%esi
lea    0xe8e(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0xf8(%rbp)
cmpl   $0xffffffff,-0xf8(%rbp)
jne    11a2 <main+0x52>
lea    0xe7d(%rip),%rdi
call   1070 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12af <main+0x15f>
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   1050 <readv@plt>
mov    %eax,-0xf4(%rbp)
cmpl   $0xffffffff,-0xf4(%rbp)
jne    1226 <main+0xd6>
lea    0xdfe(%rip),%rdi
call   1070 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12af <main+0x15f>
movl   $0x0,-0xfc(%rbp)
cmpl   $0x3,-0xfc(%rbp)
jge    127b <main+0x12b>
mov    -0xfc(%rbp),%esi
movslq -0xfc(%rbp),%rcx
lea    -0xf0(%rbp),%rax
shl    $0x4,%rcx
add    %rcx,%rax
mov    (%rax),%rdx
lea    0xdb7(%rip),%rdi
call   1030 <printf@plt>
mov    -0xfc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xfc(%rbp)
jmp    1230 <main+0xe0>
mov    -0xf8(%rbp),%edi
call   1040 <close@plt>
cmp    $0x0,%rax
je     12a8 <main+0x158>
lea    0xd89(%rip),%rdi
call   1070 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12af <main+0x15f>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x100,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0xe0,%rsp
mov    0x2e86(%rip),%rax
mov    (%rax),%esi
lea    0xe95(%rip),%rdi
call   1060 <open@plt>
cmp    $0xffffffff,%eax
je     121e <main+0xce>
mov    %eax,%ebx
lea    0x30(%rsp),%rax
mov    %rax,(%rsp)
movl   $0x30,0x8(%rsp)
lea    0xa0(%rsp),%rax
mov    %rax,0x10(%rsp)
movl   $0x33,0x18(%rsp)
lea    0x60(%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     1227 <main+0xd7>
mov    (%rsp),%rdx
lea    0xe47(%rip),%rbp
xor    %r14d,%r14d
mov    %rbp,%rdi
xor    %esi,%esi
call   1030 <printf@plt>
mov    0x10(%rsp),%rdx
mov    %rbp,%rdi
mov    $0x1,%esi
call   1030 <printf@plt>
mov    0x20(%rsp),%rdx
mov    %rbp,%rdi
mov    $0x2,%esi
call   1030 <printf@plt>
mov    %ebx,%edi
call   1040 <close@plt>
test   %rax,%rax
jne    1230 <main+0xe0>
mov    %r14d,%eax
add    $0xe0,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xde9(%rip),%rdi
jmp    1237 <main+0xe7>
lea    0xde5(%rip),%rdi
jmp    1237 <main+0xe7>
lea    0xde9(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%r14d
jmp    120f <main+0xbf>

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

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"
@.str.3 = [7 x i8] c"%d: %s\00"
@.str.4 = [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 %37
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 %33, label %24
24: ; preds = %14
%25 = load i8*, i8** %16
%26 = call i32 @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i32 0, i8* %25)
%27 = load i8*, i8** %18
%28 = call i32 @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i32 1, i8* %27)
%29 = load i8*, i8** %20
%30 = call i32 @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i32 2, i8* %29)
%31 = call i64 @close(i32 %10)
%32 = icmp eq i64 %31, 0
br i1 %32, label %37, label %35
33: ; preds = %14
%34 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
br label %37
35: ; preds = %24
%36 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
br label %37
37: ; preds = %24, %35, %33, %12
%38 = phi i32 [ 1, %12 ], [ 1, %33 ], [ 1, %35 ], [ 0, %24 ]
ret i32 %38
}