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

/* Variables and functions */
int O_CREAT;
int O_TRUNC;
int O_WRONLY;
scalar_t__ close(int);
int open(char *, int, int);
int /*<<< orphan*/ printf(char *, int);
scalar_t__ strlen(char *);
int writev(int, struct iovec *, int);
int main(void) {
  struct iovec iov[3];
  ssize_t nr;
  int fd, i;
  char *buf[] = {"I have a cat.\n", "She has a dog.\n", "He likes a dog.\n"};
  fd = open("animal.txt", O_WRONLY | O_CREAT | O_TRUNC, 0666);
  if (fd == -1)
    return 1;
  for (i = 0; i < 3; i++) {
    iov[i].iov_base = buf[i];
    iov[i].iov_len = strlen(buf[i]) + 1;
  }
  nr = writev(fd, iov, 3);
  if (nr == -1)
    return 1;
  printf("wrote %d bytes\n", nr);
  if (close(fd))
    return 1;
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2c8a(%rip),%rax
mov    %rax,-0x70(%rbp)
mov    0x2c87(%rip),%rax
mov    %rax,-0x68(%rbp)
mov    0x2c84(%rip),%rax
mov    %rax,-0x60(%rbp)
mov    0x2e41(%rip),%rax
mov    (%rax),%esi
mov    0x2e68(%rip),%rax
or     (%rax),%esi
mov    0x2e57(%rip),%rax
or     (%rax),%esi
lea    0xe8e(%rip),%rdi
mov    $0x1b6,%edx
call   1070 <open@plt>
mov    %eax,-0x48(%rbp)
cmpl   $0xffffffff,-0x48(%rbp)
jne    11c5 <main+0x75>
movl   $0x1,-0x4(%rbp)
jmp    1285 <main+0x135>
movl   $0x0,-0x4c(%rbp)
cmpl   $0x3,-0x4c(%rbp)
jge    1227 <main+0xd7>
movslq -0x4c(%rbp),%rax
mov    -0x70(%rbp,%rax,8),%rcx
movslq -0x4c(%rbp),%rdx
lea    -0x40(%rbp),%rax
shl    $0x4,%rdx
add    %rdx,%rax
mov    %rcx,(%rax)
movslq -0x4c(%rbp),%rax
mov    -0x70(%rbp,%rax,8),%rdi
call   1040 <strlen@plt>
mov    %rax,%rcx
add    $0x1,%rcx
movslq -0x4c(%rbp),%rdx
lea    -0x40(%rbp),%rax
shl    $0x4,%rdx
add    %rdx,%rax
mov    %rcx,0x8(%rax)
mov    -0x4c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4c(%rbp)
jmp    11cc <main+0x7c>
mov    -0x48(%rbp),%edi
lea    -0x40(%rbp),%rsi
mov    $0x3,%edx
call   1030 <writev@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0xffffffff,-0x44(%rbp)
jne    1251 <main+0x101>
movl   $0x1,-0x4(%rbp)
jmp    1285 <main+0x135>
mov    -0x44(%rbp),%esi
lea    0xde0(%rip),%rdi
call   1050 <printf@plt>
mov    -0x48(%rbp),%edi
call   1060 <close@plt>
cmp    $0x0,%rax
je     127e <main+0x12e>
movl   $0x1,-0x4(%rbp)
jmp    1285 <main+0x135>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
sub    $0x38,%rsp
mov    0x2e7b(%rip),%rax
mov    0x2ea4(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e91(%rip),%rax
or     (%rax),%esi
lea    0xec8(%rip),%rdi
mov    $0x1b6,%edx
call   1060 <open@plt>
mov    $0x1,%ebx
cmp    $0xffffffff,%eax
je     11ed <main+0xad>
mov    %eax,%ebp
lea    0xe7b(%rip),%rax
mov    %rax,(%rsp)
movq   $0xf,0x8(%rsp)
lea    0xe76(%rip),%rax
mov    %rax,0x10(%rsp)
movq   $0x10,0x18(%rsp)
lea    0xe71(%rip),%rax
mov    %rax,0x20(%rsp)
movq   $0x11,0x28(%rsp)
mov    %rsp,%rsi
mov    %ebp,%edi
mov    $0x3,%edx
call   1030 <writev@plt>
cmp    $0xffffffff,%eax
je     11ed <main+0xad>
lea    0xe64(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
mov    %ebp,%edi
call   1050 <close@plt>
xor    %ebx,%ebx
test   %rax,%rax
setne  %bl
mov    %ebx,%eax
add    $0x38,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.iovec = type { i8*, i64 }
@.str = [15 x i8] c"I have a cat.\0A\00"
@.str.1 = [16 x i8] c"She has a dog.\0A\00"
@.str.2 = [17 x i8] c"He likes a dog.\0A\00"
@__const.main.buf = [3 x i8*] [i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i32 0, i32 0), i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i32 0, i32 0), i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i32 0, i32 0)]
@.str.3 = [11 x i8] c"animal.txt\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@.str.4 = [16 x i8] c"wrote %d bytes\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [3 x %struct.iovec]
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca [3 x i8*]
store i32 0, i32* %1
%7 = bitcast [3 x i8*]* %6 to i8*
%8 = load i32, i32* @O_WRONLY
%9 = load i32, i32* @O_CREAT
%10 = or i32 %8, %9
%11 = load i32, i32* @O_TRUNC
%12 = or i32 %10, %11
%13 = call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i32 %12, i32 438)
store i32 %13, i32* %4
%14 = load i32, i32* %4
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %17
16: ; preds = %0
store i32 1, i32* %1
br label %58
17: ; preds = %0
store i32 0, i32* %5
br label %18
18: ; preds = %40, %17
%19 = load i32, i32* %5
%20 = icmp slt i32 %19, 3
br i1 %20, label %21, label %43
21: ; preds = %18
%22 = load i32, i32* %5
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds [3 x i8*], [3 x i8*]* %6, i64 0, i64 %23
%25 = load i8*, i8** %24
%26 = load i32, i32* %5
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds [3 x %struct.iovec], [3 x %struct.iovec]* %2, i64 0, i64 %27
%29 = getelementptr inbounds %struct.iovec, %struct.iovec* %28, i32 0, i32 0
store i8* %25, i8** %29
%30 = load i32, i32* %5
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds [3 x i8*], [3 x i8*]* %6, i64 0, i64 %31
%33 = load i8*, i8** %32
%34 = call i64 @strlen(i8* %33)
%35 = add nsw i64 %34, 1
%36 = load i32, i32* %5
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds [3 x %struct.iovec], [3 x %struct.iovec]* %2, i64 0, i64 %37
%39 = getelementptr inbounds %struct.iovec, %struct.iovec* %38, i32 0, i32 1
store i64 %35, i64* %39
br label %40
40: ; preds = %21
%41 = load i32, i32* %5
%42 = add nsw i32 %41, 1
store i32 %42, i32* %5
br label %18
43: ; preds = %18
%44 = load i32, i32* %4
%45 = getelementptr inbounds [3 x %struct.iovec], [3 x %struct.iovec]* %2, i64 0, i64 0
%46 = call i32 @writev(i32 %44, %struct.iovec* %45, i32 3)
store i32 %46, i32* %3
%47 = load i32, i32* %3
%48 = icmp eq i32 %47, -1
br i1 %48, label %49, label %50
49: ; preds = %43
store i32 1, i32* %1
br label %58
50: ; preds = %43
%51 = load i32, i32* %3
%52 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0), i32 %51)
%53 = load i32, i32* %4
%54 = call i64 @close(i32 %53)
%55 = icmp ne i64 %54, 0
br i1 %55, label %56, label %57
56: ; preds = %50
store i32 1, i32* %1
br label %58
57: ; preds = %50
store i32 0, i32* %1
br label %58
58: ; preds = %57, %56, %49, %16
%59 = load i32, i32* %1
ret i32 %59
}

LLVM IR (-O2):
%struct.iovec = type { i8*, i64 }
@.str = [15 x i8] c"I have a cat.\0A\00"
@.str.1 = [16 x i8] c"She has a dog.\0A\00"
@.str.2 = [17 x i8] c"He likes a dog.\0A\00"
@.str.3 = [11 x i8] c"animal.txt\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@.str.4 = [16 x i8] c"wrote %d bytes\0A\00"
define i32 @main() {
%1 = alloca [3 x %struct.iovec]
%2 = bitcast [3 x %struct.iovec]* %1 to i8*
%3 = load i32, i32* @O_WRONLY
%4 = load i32, i32* @O_CREAT
%5 = or i32 %4, %3
%6 = load i32, i32* @O_TRUNC
%7 = or i32 %5, %6
%8 = tail call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i32 %7, i32 438)
%9 = icmp eq i32 %8, -1
br i1 %9, label %25, label %10
10: ; preds = %0
%11 = getelementptr inbounds [3 x %struct.iovec], [3 x %struct.iovec]* %1, i64 0, i64 0, i32 0
store i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8** %11
%12 = getelementptr inbounds [3 x %struct.iovec], [3 x %struct.iovec]* %1, i64 0, i64 0, i32 1
store i64 15, i64* %12
%13 = getelementptr inbounds [3 x %struct.iovec], [3 x %struct.iovec]* %1, i64 0, i64 1, i32 0
store i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8** %13
%14 = getelementptr inbounds [3 x %struct.iovec], [3 x %struct.iovec]* %1, i64 0, i64 1, i32 1
store i64 16, i64* %14
%15 = getelementptr inbounds [3 x %struct.iovec], [3 x %struct.iovec]* %1, i64 0, i64 2, i32 0
store i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i8** %15
%16 = getelementptr inbounds [3 x %struct.iovec], [3 x %struct.iovec]* %1, i64 0, i64 2, i32 1
store i64 17, i64* %16
%17 = getelementptr inbounds [3 x %struct.iovec], [3 x %struct.iovec]* %1, i64 0, i64 0
%18 = call i32 @writev(i32 %8, %struct.iovec* %17, i32 3)
%19 = icmp eq i32 %18, -1
br i1 %19, label %25, label %20
20: ; preds = %10
%21 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0), i32 %18)
%22 = call i64 @close(i32 %8)
%23 = icmp ne i64 %22, 0
%24 = zext i1 %23 to i32
br label %25
25: ; preds = %20, %10, %0
%26 = phi i32 [ 1, %0 ], [ 1, %10 ], [ %24, %20 ]
ret i32 %26
}