#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*/ printf(char *, int, char *);
int readv(int, struct iovec *, int);
int main(void) {
  char foo[15], bar[16], baz[17];
  struct iovec iov[3];
  ssize_t nr;
  int fd, i;
  fd = open("animal.txt", O_RDONLY);
  if (fd == -1)
    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)
    return 1;
  for (i = 0; i < 3; i++) {
    printf("%d: %s", i, (char *)iov[i].iov_base);
  }
  if (close(fd))
    return 1;
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e8f(%rip),%rax
mov    (%rax),%esi
lea    0xe9e(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x88(%rbp)
cmpl   $0xffffffff,-0x88(%rbp)
jne    1186 <main+0x46>
movl   $0x1,-0x4(%rbp)
jmp    1260 <main+0x120>
lea    -0x13(%rbp),%rax
mov    %rax,-0x80(%rbp)
movl   $0xf,-0x78(%rbp)
lea    -0x30(%rbp),%rax
mov    %rax,-0x70(%rbp)
movl   $0x10,-0x68(%rbp)
lea    -0x50(%rbp),%rax
mov    %rax,-0x60(%rbp)
movl   $0x11,-0x58(%rbp)
mov    -0x88(%rbp),%edi
lea    -0x80(%rbp),%rsi
mov    $0x3,%edx
call   1050 <readv@plt>
mov    %eax,-0x84(%rbp)
cmpl   $0xffffffff,-0x84(%rbp)
jne    11e6 <main+0xa6>
movl   $0x1,-0x4(%rbp)
jmp    1260 <main+0x120>
movl   $0x0,-0x8c(%rbp)
cmpl   $0x3,-0x8c(%rbp)
jge    1238 <main+0xf8>
mov    -0x8c(%rbp),%esi
movslq -0x8c(%rbp),%rcx
lea    -0x80(%rbp),%rax
shl    $0x4,%rcx
add    %rcx,%rax
mov    (%rax),%rdx
lea    0xdec(%rip),%rdi
call   1030 <printf@plt>
mov    -0x8c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8c(%rbp)
jmp    11f0 <main+0xb0>
mov    -0x88(%rbp),%edi
call   1040 <close@plt>
cmp    $0x0,%rax
je     1259 <main+0x119>
movl   $0x1,-0x4(%rbp)
jmp    1260 <main+0x120>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
sub    $0x68,%rsp
mov    0x2e9b(%rip),%rax
mov    (%rax),%esi
lea    0xeaa(%rip),%rdi
call   1060 <open@plt>
mov    $0x1,%ebp
cmp    $0xffffffff,%eax
je     11fd <main+0xbd>
mov    %eax,%ebx
lea    0x31(%rsp),%rax
mov    %rax,(%rsp)
movl   $0xf,0x8(%rsp)
lea    0x40(%rsp),%rax
mov    %rax,0x10(%rsp)
movl   $0x10,0x18(%rsp)
lea    0x50(%rsp),%rax
mov    %rax,0x20(%rsp)
movl   $0x11,0x28(%rsp)
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0x3,%edx
call   1050 <readv@plt>
cmp    $0xffffffff,%eax
je     11fd <main+0xbd>
mov    (%rsp),%rdx
lea    0xe4c(%rip),%rbp
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>
xor    %ebp,%ebp
test   %rax,%rax
setne  %bpl
mov    %ebp,%eax
add    $0x68,%rsp
pop    %rbx
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.iovec = type { i8*, i32 }
@.str = [11 x i8] c"animal.txt\00"
@O_RDONLY = global i32 0
@.str.1 = [7 x i8] c"%d: %s\00"
define i32 @main() {
%1 = alloca [15 x i8]
%2 = alloca [16 x i8]
%3 = alloca [17 x i8]
%4 = alloca [3 x %struct.iovec]
%5 = getelementptr inbounds [15 x i8], [15 x i8]* %1, i64 0, i64 0
%6 = getelementptr inbounds [16 x i8], [16 x i8]* %2, i64 0, i64 0
%7 = getelementptr inbounds [17 x i8], [17 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 ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %9)
%11 = icmp eq i32 %10, -1
br i1 %11, label %32, label %12
12: ; preds = %0
%13 = getelementptr inbounds [3 x %struct.iovec], [3 x %struct.iovec]* %4, i64 0, i64 0
%14 = getelementptr inbounds [3 x %struct.iovec], [3 x %struct.iovec]* %4, i64 0, i64 0, i32 0
store i8* %5, i8** %14
%15 = getelementptr inbounds [3 x %struct.iovec], [3 x %struct.iovec]* %4, i64 0, i64 0, i32 1
store i32 15, i32* %15
%16 = getelementptr inbounds [3 x %struct.iovec], [3 x %struct.iovec]* %4, i64 0, i64 1, i32 0
store i8* %6, i8** %16
%17 = getelementptr inbounds [3 x %struct.iovec], [3 x %struct.iovec]* %4, i64 0, i64 1, i32 1
store i32 16, i32* %17
%18 = getelementptr inbounds [3 x %struct.iovec], [3 x %struct.iovec]* %4, i64 0, i64 2, i32 0
store i8* %7, i8** %18
%19 = getelementptr inbounds [3 x %struct.iovec], [3 x %struct.iovec]* %4, i64 0, i64 2, i32 1
store i32 17, i32* %19
%20 = call i32 @readv(i32 %10, %struct.iovec* %13, i32 3)
%21 = icmp eq i32 %20, -1
br i1 %21, label %32, label %22
22: ; preds = %12
%23 = load i8*, i8** %14
%24 = call i32 @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32 0, i8* %23)
%25 = load i8*, i8** %16
%26 = call i32 @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32 1, i8* %25)
%27 = load i8*, i8** %18
%28 = call i32 @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32 2, i8* %27)
%29 = call i64 @close(i32 %10)
%30 = icmp ne i64 %29, 0
%31 = zext i1 %30 to i32
br label %32
32: ; preds = %22, %12, %0
%33 = phi i32 [ 1, %0 ], [ 1, %12 ], [ %31, %22 ]
ret i32 %33
}