#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 /*<<< orphan*/ buf;

/* Variables and functions */
int O_CREAT;
int O_WRONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ memset(char *, int, int);
int open(char *, int, int);
int /*<<< orphan*/ writev(int, struct iovec *, int);
int main() {
  int fd;
  struct iovec wr_iovec[3];
  char buf[64];
  fd = open("foobar1", O_WRONLY | O_CREAT, 0666);
  memset(buf, (int)'B', sizeof(buf));
  wr_iovec[0].iov_base = buf;
  wr_iovec[0].iov_len = -1;
  wr_iovec[1].iov_base = NULL;
  wr_iovec[1].iov_len = 0;
  wr_iovec[2].iov_base = NULL;
  wr_iovec[2].iov_len = 0;
  writev(fd, wr_iovec, 1);
  writev(-1, wr_iovec, 1);
  writev(fd, wr_iovec, -1);
  writev(fd, wr_iovec, 0);
  wr_iovec[0].iov_base = buf;
  wr_iovec[0].iov_len = sizeof(buf);
  wr_iovec[1].iov_base = buf;
  wr_iovec[1].iov_len = 0;
  wr_iovec[2].iov_base = NULL;
  wr_iovec[2].iov_len = 0;
  writev(fd, wr_iovec, 3);
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e77(%rip),%rax
mov    (%rax),%esi
mov    0x2e96(%rip),%rax
or     (%rax),%esi
lea    0xe95(%rip),%rdi
mov    $0x1b6,%edx
call   1060 <open@plt>
mov    %eax,-0x8(%rbp)
lea    -0x80(%rbp),%rdi
mov    $0x42,%esi
mov    $0x40,%edx
call   1040 <memset@plt>
lea    -0x80(%rbp),%rax
mov    %rax,-0x40(%rbp)
movl   $0xffffffff,-0x38(%rbp)
movq   $0x0,-0x30(%rbp)
movl   $0x0,-0x28(%rbp)
movq   $0x0,-0x20(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x40(%rbp),%rsi
mov    $0x1,%edx
call   1030 <writev@plt>
lea    -0x40(%rbp),%rsi
mov    $0xffffffff,%edi
mov    $0x1,%edx
call   1030 <writev@plt>
mov    -0x8(%rbp),%edi
lea    -0x40(%rbp),%rsi
mov    $0xffffffff,%edx
call   1030 <writev@plt>
mov    -0x8(%rbp),%edi
lea    -0x40(%rbp),%rsi
xor    %edx,%edx
call   1030 <writev@plt>
lea    -0x80(%rbp),%rax
mov    %rax,-0x40(%rbp)
movl   $0x40,-0x38(%rbp)
lea    -0x80(%rbp),%rax
mov    %rax,-0x30(%rbp)
movl   $0x0,-0x28(%rbp)
movq   $0x0,-0x20(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x40(%rbp),%rsi
mov    $0x3,%edx
call   1030 <writev@plt>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x70,%rsp
mov    0x2e80(%rip),%rax
mov    0x2ea1(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe9e(%rip),%rdi
mov    $0x1b6,%edx
call   1060 <open@plt>
mov    %eax,%ebx
lea    0x30(%rsp),%r14
mov    %r14,%rdi
mov    $0x42,%esi
mov    $0x40,%edx
call   1040 <memset@plt>
mov    %r14,(%rsp)
movl   $0xffffffff,0x8(%rsp)
ff
movq   $0x0,0x10(%rsp)
movl   $0x0,0x18(%rsp)
movq   $0x0,0x20(%rsp)
movl   $0x0,0x28(%rsp)
mov    %rsp,%r15
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0x1,%edx
call   1030 <writev@plt>
mov    $0xffffffff,%edi
mov    %r15,%rsi
mov    $0x1,%edx
call   1030 <writev@plt>
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0xffffffff,%edx
call   1030 <writev@plt>
mov    %ebx,%edi
mov    %r15,%rsi
xor    %edx,%edx
call   1030 <writev@plt>
mov    %r14,(%rsp)
movl   $0x40,0x8(%rsp)
mov    %r14,0x10(%rsp)
movl   $0x0,0x18(%rsp)
movq   $0x0,0x20(%rsp)
movl   $0x0,0x28(%rsp)
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0x3,%edx
call   1030 <writev@plt>
mov    %ebx,%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x70,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.iovec = type { i8*, i32 }
@.str = [8 x i8] c"foobar1\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [3 x %struct.iovec]
%4 = alloca [64 x i8]
store i32 0, i32* %1
%5 = load i32, i32* @O_WRONLY
%6 = load i32, i32* @O_CREAT
%7 = or i32 %5, %6
%8 = call i32 @open(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 %7, i32 438)
store i32 %8, i32* %2
%9 = getelementptr inbounds [64 x i8], [64 x i8]* %4, i64 0, i64 0
%10 = call i32 @memset(i8* %9, i32 66, i32 64)
%11 = getelementptr inbounds [64 x i8], [64 x i8]* %4, i64 0, i64 0
%12 = getelementptr inbounds [3 x %struct.iovec], [3 x %struct.iovec]* %3, i64 0, i64 0
%13 = getelementptr inbounds %struct.iovec, %struct.iovec* %12, i32 0, i32 0
store i8* %11, i8** %13
%14 = getelementptr inbounds [3 x %struct.iovec], [3 x %struct.iovec]* %3, i64 0, i64 0
%15 = getelementptr inbounds %struct.iovec, %struct.iovec* %14, i32 0, i32 1
store i32 -1, i32* %15
%16 = getelementptr inbounds [3 x %struct.iovec], [3 x %struct.iovec]* %3, i64 0, i64 1
%17 = getelementptr inbounds %struct.iovec, %struct.iovec* %16, i32 0, i32 0
store i8* null, i8** %17
%18 = getelementptr inbounds [3 x %struct.iovec], [3 x %struct.iovec]* %3, i64 0, i64 1
%19 = getelementptr inbounds %struct.iovec, %struct.iovec* %18, i32 0, i32 1
store i32 0, i32* %19
%20 = getelementptr inbounds [3 x %struct.iovec], [3 x %struct.iovec]* %3, i64 0, i64 2
%21 = getelementptr inbounds %struct.iovec, %struct.iovec* %20, i32 0, i32 0
store i8* null, i8** %21
%22 = getelementptr inbounds [3 x %struct.iovec], [3 x %struct.iovec]* %3, i64 0, i64 2
%23 = getelementptr inbounds %struct.iovec, %struct.iovec* %22, i32 0, i32 1
store i32 0, i32* %23
%24 = load i32, i32* %2
%25 = getelementptr inbounds [3 x %struct.iovec], [3 x %struct.iovec]* %3, i64 0, i64 0
%26 = call i32 @writev(i32 %24, %struct.iovec* %25, i32 1)
%27 = getelementptr inbounds [3 x %struct.iovec], [3 x %struct.iovec]* %3, i64 0, i64 0
%28 = call i32 @writev(i32 -1, %struct.iovec* %27, i32 1)
%29 = load i32, i32* %2
%30 = getelementptr inbounds [3 x %struct.iovec], [3 x %struct.iovec]* %3, i64 0, i64 0
%31 = call i32 @writev(i32 %29, %struct.iovec* %30, i32 -1)
%32 = load i32, i32* %2
%33 = getelementptr inbounds [3 x %struct.iovec], [3 x %struct.iovec]* %3, i64 0, i64 0
%34 = call i32 @writev(i32 %32, %struct.iovec* %33, i32 0)
%35 = getelementptr inbounds [64 x i8], [64 x i8]* %4, i64 0, i64 0
%36 = getelementptr inbounds [3 x %struct.iovec], [3 x %struct.iovec]* %3, i64 0, i64 0
%37 = getelementptr inbounds %struct.iovec, %struct.iovec* %36, i32 0, i32 0
store i8* %35, i8** %37
%38 = getelementptr inbounds [3 x %struct.iovec], [3 x %struct.iovec]* %3, i64 0, i64 0
%39 = getelementptr inbounds %struct.iovec, %struct.iovec* %38, i32 0, i32 1
store i32 64, i32* %39
%40 = getelementptr inbounds [64 x i8], [64 x i8]* %4, i64 0, i64 0
%41 = getelementptr inbounds [3 x %struct.iovec], [3 x %struct.iovec]* %3, i64 0, i64 1
%42 = getelementptr inbounds %struct.iovec, %struct.iovec* %41, i32 0, i32 0
store i8* %40, i8** %42
%43 = getelementptr inbounds [3 x %struct.iovec], [3 x %struct.iovec]* %3, i64 0, i64 1
%44 = getelementptr inbounds %struct.iovec, %struct.iovec* %43, i32 0, i32 1
store i32 0, i32* %44
%45 = getelementptr inbounds [3 x %struct.iovec], [3 x %struct.iovec]* %3, i64 0, i64 2
%46 = getelementptr inbounds %struct.iovec, %struct.iovec* %45, i32 0, i32 0
store i8* null, i8** %46
%47 = getelementptr inbounds [3 x %struct.iovec], [3 x %struct.iovec]* %3, i64 0, i64 2
%48 = getelementptr inbounds %struct.iovec, %struct.iovec* %47, i32 0, i32 1
store i32 0, i32* %48
%49 = load i32, i32* %2
%50 = getelementptr inbounds [3 x %struct.iovec], [3 x %struct.iovec]* %3, i64 0, i64 0
%51 = call i32 @writev(i32 %49, %struct.iovec* %50, i32 3)
%52 = load i32, i32* %2
%53 = call i32 @close(i32 %52)
ret i32 0
}

LLVM IR (-O2):
%struct.iovec = type { i8*, i32 }
@.str = [8 x i8] c"foobar1\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
define i32 @main() {
%1 = alloca [3 x %struct.iovec]
%2 = alloca [64 x i8]
%3 = bitcast [3 x %struct.iovec]* %1 to i8*
%4 = getelementptr inbounds [64 x i8], [64 x i8]* %2, i64 0, i64 0
%5 = load i32, i32* @O_WRONLY
%6 = load i32, i32* @O_CREAT
%7 = or i32 %6, %5
%8 = tail call i32 @open(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 %7, i32 438)
%9 = call i32 @memset(i8* %4, i32 66, i32 64)
%10 = getelementptr inbounds [3 x %struct.iovec], [3 x %struct.iovec]* %1, i64 0, i64 0
%11 = getelementptr inbounds [3 x %struct.iovec], [3 x %struct.iovec]* %1, i64 0, i64 0, i32 0
store i8* %4, i8** %11
%12 = getelementptr inbounds [3 x %struct.iovec], [3 x %struct.iovec]* %1, i64 0, i64 0, i32 1
store i32 -1, i32* %12
%13 = getelementptr inbounds [3 x %struct.iovec], [3 x %struct.iovec]* %1, i64 0, i64 1, i32 0
store i8* null, i8** %13
%14 = getelementptr inbounds [3 x %struct.iovec], [3 x %struct.iovec]* %1, i64 0, i64 1, i32 1
store i32 0, i32* %14
%15 = getelementptr inbounds [3 x %struct.iovec], [3 x %struct.iovec]* %1, i64 0, i64 2, i32 0
store i8* null, i8** %15
%16 = getelementptr inbounds [3 x %struct.iovec], [3 x %struct.iovec]* %1, i64 0, i64 2, i32 1
store i32 0, i32* %16
%17 = call i32 @writev(i32 %8, %struct.iovec* %10, i32 1)
%18 = call i32 @writev(i32 -1, %struct.iovec* %10, i32 1)
%19 = call i32 @writev(i32 %8, %struct.iovec* %10, i32 -1)
%20 = call i32 @writev(i32 %8, %struct.iovec* %10, i32 0)
store i8* %4, i8** %11
store i32 64, i32* %12
store i8* %4, i8** %13
store i32 0, i32* %14
store i8* null, i8** %15
store i32 0, i32* %16
%21 = call i32 @writev(i32 %8, %struct.iovec* %10, i32 3)
%22 = call i32 @close(i32 %8)
ret i32 0
}