#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 {
  int iov_len;
  scalar_t__ iov_base;
};
typedef int ssize_t;

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ SEEK_SET;
int errno;
int /*<<< orphan*/ lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ malloc(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int preadv(int, struct iovec *, int, int);
int /*<<< orphan*/ printf(char *, int, char *);
int main() {
  struct iovec buf[1];
  buf[0].iov_base = malloc(sizeof(char) * 10);
  buf[0].iov_len = 10;
  int fd = open("buccaneer.txt", O_RDONLY);
  if (fd == -1) {
    return errno;
  }
  ssize_t i = preadv(fd, buf, 1, 0);
  if (i == -1) {
    perror("preadv");
    return errno;
  }
  printf("%d %s\n", (int)buf[0].iov_len, (char *)buf[0].iov_base);
  lseek(fd, 0, SEEK_SET);
  i = preadv(fd, buf, 1, 10);
  if (i == -1) {
    perror("preadv");
    return errno;
  }
  printf("%d %s\n", (int)buf[0].iov_len, (char *)buf[0].iov_base);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0xa,%edi
call   1050 <malloc@plt>
mov    %rax,-0x18(%rbp)
movl   $0xa,-0x20(%rbp)
mov    0x2e5d(%rip),%rax
mov    (%rax),%esi
lea    0xe6c(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    11b7 <main+0x57>
mov    0x2e2b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    126d <main+0x10d>
mov    -0x24(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x1,%edx
xor    %ecx,%ecx
call   1060 <preadv@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0xffffffff,-0x28(%rbp)
jne    11f4 <main+0x94>
lea    0xe30(%rip),%rdi
call   1080 <perror@plt>
mov    0x2dee(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    126d <main+0x10d>
mov    -0x20(%rbp),%esi
mov    -0x18(%rbp),%rdx
lea    0xe13(%rip),%rdi
call   1030 <printf@plt>
mov    -0x24(%rbp),%edi
mov    0x2dbf(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1040 <lseek@plt>
mov    -0x24(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x1,%edx
mov    $0xa,%ecx
call   1060 <preadv@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0xffffffff,-0x28(%rbp)
jne    125a <main+0xfa>
lea    0xdca(%rip),%rdi
call   1080 <perror@plt>
mov    0x2d88(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    126d <main+0x10d>
mov    -0x20(%rbp),%esi
mov    -0x18(%rbp),%rdx
lea    0xdad(%rip),%rdi
call   1030 <printf@plt>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    $0xa,%edi
call   1050 <malloc@plt>
mov    %rax,0x8(%rsp)
movl   $0xa,(%rsp)
mov    0x2e65(%rip),%rax
mov    (%rax),%esi
lea    0xe74(%rip),%rdi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     1211 <main+0xb1>
mov    %eax,%ebx
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0x1,%edx
xor    %ecx,%ecx
call   1060 <preadv@plt>
cmp    $0xffffffff,%eax
je     1205 <main+0xa5>
mov    (%rsp),%esi
mov    0x8(%rsp),%rdx
lea    0xe58(%rip),%rdi
call   1030 <printf@plt>
mov    0x2e07(%rip),%rax
mov    (%rax),%edx
xor    %ebp,%ebp
mov    %ebx,%edi
xor    %esi,%esi
call   1040 <lseek@plt>
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0x1,%edx
mov    $0xa,%ecx
call   1060 <preadv@plt>
cmp    $0xffffffff,%eax
je     1205 <main+0xa5>
mov    (%rsp),%esi
mov    0x8(%rsp),%rdx
lea    0xe17(%rip),%rdi
call   1030 <printf@plt>
jmp    121a <main+0xba>
lea    0xe02(%rip),%rdi
call   1080 <perror@plt>
mov    0x2dc0(%rip),%rax
mov    (%rax),%ebp
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.iovec = type { i32, i64 }
@.str = [14 x i8] c"buccaneer.txt\00"
@O_RDONLY = global i32 0
@errno = global i32 0
@.str.1 = [7 x i8] c"preadv\00"
@.str.2 = [7 x i8] c"%d %s\0A\00"
@SEEK_SET = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca [1 x %struct.iovec]
%3 = alloca i32
%4 = alloca i32
store i32 0, i32* %1
%5 = call i64 @malloc(i32 10)
%6 = getelementptr inbounds [1 x %struct.iovec], [1 x %struct.iovec]* %2, i64 0, i64 0
%7 = getelementptr inbounds %struct.iovec, %struct.iovec* %6, i32 0, i32 1
store i64 %5, i64* %7
%8 = getelementptr inbounds [1 x %struct.iovec], [1 x %struct.iovec]* %2, i64 0, i64 0
%9 = getelementptr inbounds %struct.iovec, %struct.iovec* %8, i32 0, i32 0
store i32 10, i32* %9
%10 = load i32, i32* @O_RDONLY
%11 = call i32 @open(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %10)
store i32 %11, i32* %3
%12 = load i32, i32* %3
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %16
14: ; preds = %0
%15 = load i32, i32* @errno
store i32 %15, i32* %1
br label %54
16: ; preds = %0
%17 = load i32, i32* %3
%18 = getelementptr inbounds [1 x %struct.iovec], [1 x %struct.iovec]* %2, i64 0, i64 0
%19 = call i32 @preadv(i32 %17, %struct.iovec* %18, i32 1, i32 0)
store i32 %19, i32* %4
%20 = load i32, i32* %4
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %25
22: ; preds = %16
%23 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%24 = load i32, i32* @errno
store i32 %24, i32* %1
br label %54
25: ; preds = %16
%26 = getelementptr inbounds [1 x %struct.iovec], [1 x %struct.iovec]* %2, i64 0, i64 0
%27 = getelementptr inbounds %struct.iovec, %struct.iovec* %26, i32 0, i32 0
%28 = load i32, i32* %27
%29 = getelementptr inbounds [1 x %struct.iovec], [1 x %struct.iovec]* %2, i64 0, i64 0
%30 = getelementptr inbounds %struct.iovec, %struct.iovec* %29, i32 0, i32 1
%31 = load i64, i64* %30
%32 = inttoptr i64 %31 to i8*
%33 = call i32 @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32 %28, i8* %32)
%34 = load i32, i32* %3
%35 = load i32, i32* @SEEK_SET
%36 = call i32 @lseek(i32 %34, i32 0, i32 %35)
%37 = load i32, i32* %3
%38 = getelementptr inbounds [1 x %struct.iovec], [1 x %struct.iovec]* %2, i64 0, i64 0
%39 = call i32 @preadv(i32 %37, %struct.iovec* %38, i32 1, i32 10)
store i32 %39, i32* %4
%40 = load i32, i32* %4
%41 = icmp eq i32 %40, -1
br i1 %41, label %42, label %45
42: ; preds = %25
%43 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%44 = load i32, i32* @errno
store i32 %44, i32* %1
br label %54
45: ; preds = %25
%46 = getelementptr inbounds [1 x %struct.iovec], [1 x %struct.iovec]* %2, i64 0, i64 0
%47 = getelementptr inbounds %struct.iovec, %struct.iovec* %46, i32 0, i32 0
%48 = load i32, i32* %47
%49 = getelementptr inbounds [1 x %struct.iovec], [1 x %struct.iovec]* %2, i64 0, i64 0
%50 = getelementptr inbounds %struct.iovec, %struct.iovec* %49, i32 0, i32 1
%51 = load i64, i64* %50
%52 = inttoptr i64 %51 to i8*
%53 = call i32 @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32 %48, i8* %52)
br label %54
54: ; preds = %45, %42, %22, %14
%55 = load i32, i32* %1
ret i32 %55
}

LLVM IR (-O2):
%struct.iovec = type { i32, i64 }
@.str = [14 x i8] c"buccaneer.txt\00"
@O_RDONLY = global i32 0
@errno = global i32 0
@.str.1 = [7 x i8] c"preadv\00"
@.str.2 = [7 x i8] c"%d %s\0A\00"
@SEEK_SET = global i32 0
define i32 @main() {
%1 = alloca [1 x %struct.iovec]
%2 = bitcast [1 x %struct.iovec]* %1 to i8*
%3 = tail call i64 @malloc(i32 10)
%4 = getelementptr inbounds [1 x %struct.iovec], [1 x %struct.iovec]* %1, i64 0, i64 0
%5 = getelementptr inbounds [1 x %struct.iovec], [1 x %struct.iovec]* %1, i64 0, i64 0, i32 1
store i64 %3, i64* %5
%6 = getelementptr inbounds [1 x %struct.iovec], [1 x %struct.iovec]* %1, i64 0, i64 0, i32 0
store i32 10, i32* %6
%7 = load i32, i32* @O_RDONLY
%8 = tail call i32 @open(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %7)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %12
10: ; preds = %0
%11 = load i32, i32* @errno
br label %35
12: ; preds = %0
%13 = call i32 @preadv(i32 %8, %struct.iovec* %4, i32 1, i32 0)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %18
15: ; preds = %12
%16 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%17 = load i32, i32* @errno
br label %35
18: ; preds = %12
%19 = load i32, i32* %6
%20 = load i64, i64* %5
%21 = inttoptr i64 %20 to i8*
%22 = call i32 @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32 %19, i8* %21)
%23 = load i32, i32* @SEEK_SET
%24 = call i32 @lseek(i32 %8, i32 0, i32 %23)
%25 = call i32 @preadv(i32 %8, %struct.iovec* %4, i32 1, i32 10)
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %30
27: ; preds = %18
%28 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%29 = load i32, i32* @errno
br label %35
30: ; preds = %18
%31 = load i32, i32* %6
%32 = load i64, i64* %5
%33 = inttoptr i64 %32 to i8*
%34 = call i32 @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32 %31, i8* %33)
br label %35
35: ; preds = %15, %27, %30, %10
%36 = phi i32 [ %11, %10 ], [ %17, %15 ], [ %29, %27 ], [ 0, %30 ]
ret i32 %36
}