#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 readv(int, struct iovec *, int);
int main() {
  char foo[4];
  char bar[5];
  char baz[4];
  struct iovec iov[3];
  ssize_t nr;
  int fd, i;
  fd = open("groad.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("%s", (char *)iov[i].iov_base);
    printf("\n");
  }
  if (close(fd)) {
    perror("close");
    return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e82(%rip),%rax
mov    (%rax),%esi
lea    0xe91(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x58(%rbp)
cmpl   $0xffffffff,-0x58(%rbp)
jne    1199 <main+0x49>
lea    0xe82(%rip),%rdi
call   1070 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    127a <main+0x12a>
lea    -0x8(%rbp),%rax
mov    %rax,-0x50(%rbp)
movl   $0x4,-0x48(%rbp)
lea    -0xd(%rbp),%rax
mov    %rax,-0x40(%rbp)
movl   $0x5,-0x38(%rbp)
lea    -0x11(%rbp),%rax
mov    %rax,-0x30(%rbp)
movl   $0x4,-0x28(%rbp)
mov    -0x58(%rbp),%edi
lea    -0x50(%rbp),%rsi
mov    $0x3,%edx
call   1050 <readv@plt>
mov    %eax,-0x54(%rbp)
cmpl   $0xffffffff,-0x54(%rbp)
jne    11fc <main+0xac>
lea    0xe24(%rip),%rdi
call   1070 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    127a <main+0x12a>
movl   $0x0,-0x5c(%rbp)
cmpl   $0x3,-0x5c(%rbp)
jge    1249 <main+0xf9>
movslq -0x5c(%rbp),%rcx
lea    -0x50(%rbp),%rax
shl    $0x4,%rcx
add    %rcx,%rax
mov    (%rax),%rsi
lea    0xdef(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xde4(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x5c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x5c(%rbp)
jmp    1203 <main+0xb3>
mov    -0x58(%rbp),%edi
call   1040 <close@plt>
cmp    $0x0,%rax
je     1273 <main+0x123>
lea    0xdb8(%rip),%rdi
call   1070 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    127a <main+0x12a>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x50,%rsp
mov    0x2e79(%rip),%rax
mov    (%rax),%esi
lea    0xe88(%rip),%rdi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     1241 <main+0xe1>
mov    %eax,%ebx
lea    0xc(%rsp),%rax
mov    %rax,0x10(%rsp)
movl   $0x4,0x18(%rsp)
lea    0x4b(%rsp),%rax
mov    %rax,0x20(%rsp)
movl   $0x5,0x28(%rsp)
lea    0x8(%rsp),%rax
mov    %rax,0x30(%rsp)
movl   $0x4,0x38(%rsp)
lea    0x10(%rsp),%rsi
mov    %ebx,%edi
mov    $0x3,%edx
call   1060 <readv@plt>
cmp    $0xffffffff,%eax
je     124a <main+0xea>
mov    0x10(%rsp),%rsi
lea    0xe35(%rip),%rbp
xor    %r14d,%r14d
mov    %rbp,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    0x20(%rsp),%rsi
mov    %rbp,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    0x30(%rsp),%rsi
mov    %rbp,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    %ebx,%edi
call   1050 <close@plt>
test   %rax,%rax
jne    1253 <main+0xf3>
mov    %r14d,%eax
add    $0x50,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xdc2(%rip),%rdi
jmp    125a <main+0xfa>
lea    0xdbe(%rip),%rdi
jmp    125a <main+0xfa>
lea    0xdbe(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%r14d
jmp    1235 <main+0xd5>

LLVM IR (-O0):
%struct.iovec = type { i8*, i32 }
@.str = [10 x i8] c"groad.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 = [3 x i8] c"%s\00"
@.str.4 = [2 x i8] c"\0A\00"
@.str.5 = [6 x i8] c"close\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [4 x i8]
%3 = alloca [5 x i8]
%4 = alloca [4 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 ([10 x i8], [10 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 [4 x i8], [4 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 4, i32* %20
%21 = getelementptr inbounds [5 x i8], [5 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 5, i32* %25
%26 = getelementptr inbounds [4 x i8], [4 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 4, 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 = sext i32 %43 to i64
%45 = getelementptr inbounds [3 x %struct.iovec], [3 x %struct.iovec]* %5, i64 0, i64 %44
%46 = getelementptr inbounds %struct.iovec, %struct.iovec* %45, i32 0, i32 0
%47 = load i8*, i8** %46
%48 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %47)
%49 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
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.5, 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 = [10 x i8] c"groad.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 = [3 x i8] c"%s\00"
@.str.5 = [6 x i8] c"close\00"
define i32 @main() {
%1 = alloca [4 x i8]
%2 = alloca [5 x i8]
%3 = alloca [4 x i8]
%4 = alloca [3 x %struct.iovec]
%5 = getelementptr inbounds [4 x i8], [4 x i8]* %1, i64 0, i64 0
%6 = getelementptr inbounds [5 x i8], [5 x i8]* %2, i64 0, i64 0
%7 = getelementptr inbounds [4 x i8], [4 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 ([10 x i8], [10 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 %40
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 4, 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 5, 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 4, i32* %21
%22 = call i32 @readv(i32 %10, %struct.iovec* %15, i32 3)
%23 = icmp eq i32 %22, -1
br i1 %23, label %36, label %24
24: ; preds = %14
%25 = load i8*, i8** %16
%26 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %25)
%27 = call i32 @putchar(i32 10)
%28 = load i8*, i8** %18
%29 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %28)
%30 = call i32 @putchar(i32 10)
%31 = load i8*, i8** %20
%32 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %31)
%33 = call i32 @putchar(i32 10)
%34 = call i64 @close(i32 %10)
%35 = icmp eq i64 %34, 0
br i1 %35, label %40, label %38
36: ; preds = %14
%37 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
br label %40
38: ; preds = %24
%39 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
br label %40
40: ; preds = %24, %38, %36, %12
%41 = phi i32 [ 1, %12 ], [ 1, %36 ], [ 1, %38 ], [ 0, %24 ]
ret i32 %41
}