#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 dummy;
};
typedef int /*<<< orphan*/ iovec;

/* Variables and functions */
int O_CREAT;
int O_WRONLY;
int /*<<< orphan*/ close(int);
int open(char *, int, int);
int /*<<< orphan*/ populate_iovec(struct iovec *, char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ strlen(char *);
int writev(int, struct iovec *, int);
int main() {
  int fd, wbytes;
  char write_buf1[100] =
      " The writve() function shall be equivalent to write(). ";
  char write_buf2[100] =
      " The writev() function writes iovcnt buffers of data described ";
  char write_buf3[100] = " by iov to the file associated with the ";
  char write_buf4[100] = " file  descriptorfd.\n ";
  struct iovec iovec[4];
  fd = open("Write.txt", O_CREAT | O_WRONLY, 0644);
  populate_iovec(iovec, write_buf1, strlen(write_buf1));
  populate_iovec(iovec + 1, write_buf2, strlen(write_buf2));
  populate_iovec(iovec + 2, write_buf3, strlen(write_buf3));
  populate_iovec(iovec + 3, write_buf4, strlen(write_buf4));
  wbytes = writev(fd, iovec, sizeof(iovec) / sizeof(struct iovec));
  printf("\n Total number of bytes written inti file : %d \n", wbytes);
  close(fd);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x210,%rsp
lea    -0x70(%rbp),%rdi
lea    0xe7a(%rip),%rsi
mov    $0x64,%edx
call   1080 <memcpy@plt>
lea    -0xe0(%rbp),%rdi
lea    0xed2(%rip),%rsi
mov    $0x64,%edx
call   1080 <memcpy@plt>
lea    -0x150(%rbp),%rdi
lea    0xf2a(%rip),%rsi
mov    $0x64,%edx
call   1080 <memcpy@plt>
lea    -0x1c0(%rbp),%rdi
lea    0xf82(%rip),%rsi
mov    $0x64,%edx
call   1080 <memcpy@plt>
mov    0x2e19(%rip),%rax
mov    (%rax),%esi
mov    0x2de8(%rip),%rax
or     (%rax),%esi
lea    0xfc3(%rip),%rdi
mov    $0x1a4,%edx
call   1090 <open@plt>
mov    %eax,-0x4(%rbp)
lea    -0x1d0(%rbp),%rax
mov    %rax,-0x210(%rbp)
lea    -0x70(%rbp),%rax
mov    %rax,-0x208(%rbp)
lea    -0x70(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x210(%rbp),%rdi
mov    -0x208(%rbp),%rsi
mov    %eax,%edx
call   1030 <populate_iovec@plt>
lea    -0x1d0(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x200(%rbp)
lea    -0xe0(%rbp),%rax
mov    %rax,-0x1f8(%rbp)
lea    -0xe0(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x200(%rbp),%rdi
mov    -0x1f8(%rbp),%rsi
mov    %eax,%edx
call   1030 <populate_iovec@plt>
lea    -0x1d0(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x1f0(%rbp)
lea    -0x150(%rbp),%rax
mov    %rax,-0x1e8(%rbp)
lea    -0x150(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x1f0(%rbp),%rdi
mov    -0x1e8(%rbp),%rsi
mov    %eax,%edx
call   1030 <populate_iovec@plt>
lea    -0x1d0(%rbp),%rax
add    $0xc,%rax
mov    %rax,-0x1e0(%rbp)
lea    -0x1c0(%rbp),%rax
mov    %rax,-0x1d8(%rbp)
lea    -0x1c0(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x1e0(%rbp),%rdi
mov    -0x1d8(%rbp),%rsi
mov    %eax,%edx
call   1030 <populate_iovec@plt>
mov    -0x4(%rbp),%edi
lea    -0x1d0(%rbp),%rsi
mov    $0x4,%edx
call   1040 <writev@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%esi
lea    0xea5(%rip),%rdi
call   1060 <printf@plt>
mov    -0x4(%rbp),%edi
call   1070 <close@plt>
xor    %eax,%eax
add    $0x210,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x1c8,%rsp
movaps 0xedc(%rip),%xmm0
movaps %xmm0,0x1b0(%rsp)
movaps 0xebd(%rip),%xmm0
movaps %xmm0,0x1a0(%rsp)
movaps 0xe9e(%rip),%xmm0
movaps %xmm0,0x190(%rsp)
movaps 0xe7f(%rip),%xmm0
movaps %xmm0,0x180(%rsp)
movaps 0xe60(%rip),%xmm0
movaps %xmm0,0x170(%rsp)
movaps 0xe41(%rip),%xmm0
movaps %xmm0,0x160(%rsp)
movl   $0x0,0x1c0(%rsp)
movaps 0xee7(%rip),%xmm0
movaps %xmm0,0x140(%rsp)
movaps 0xec8(%rip),%xmm0
movaps %xmm0,0x130(%rsp)
movaps 0xea9(%rip),%xmm0
movaps %xmm0,0x120(%rsp)
movaps 0xe8a(%rip),%xmm0
movaps %xmm0,0x110(%rsp)
movaps 0xe6b(%rip),%xmm0
movaps %xmm0,0x100(%rsp)
movaps 0xe4c(%rip),%xmm0
movaps %xmm0,0xf0(%rsp)
movl   $0x0,0x150(%rsp)
movaps 0xef2(%rip),%xmm0
movaps %xmm0,0xd0(%rsp)
movaps 0xed3(%rip),%xmm0
movaps %xmm0,0xc0(%rsp)
movaps 0xeb4(%rip),%xmm0
movaps %xmm0,0xb0(%rsp)
movaps 0xe95(%rip),%xmm0
movaps %xmm0,0xa0(%rsp)
movaps 0xe76(%rip),%xmm0
movaps %xmm0,0x90(%rsp)
movaps 0xe57(%rip),%xmm0
movaps %xmm0,0x80(%rsp)
movl   $0x0,0xe0(%rsp)
movaps 0xefd(%rip),%xmm0
movaps %xmm0,0x60(%rsp)
movaps 0xee1(%rip),%xmm0
movaps %xmm0,0x50(%rsp)
movaps 0xec5(%rip),%xmm0
movaps %xmm0,0x40(%rsp)
movaps 0xea9(%rip),%xmm0
movaps %xmm0,0x30(%rsp)
movaps 0xe8d(%rip),%xmm0
movaps %xmm0,0x20(%rsp)
movaps 0xe71(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
movl   $0x0,0x70(%rsp)
mov    0x2d05(%rip),%rax
mov    0x2cd6(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xeaf(%rip),%rdi
mov    $0x1a4,%edx
call   1080 <open@plt>
mov    %eax,%ebp
lea    0x160(%rsp),%rbx
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %rsp,%r14
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <populate_iovec@plt>
lea    0x4(%rsp),%r15
lea    0xf0(%rsp),%rbx
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %r15,%rdi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <populate_iovec@plt>
lea    0x8(%rsp),%r15
lea    0x80(%rsp),%rbx
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %r15,%rdi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <populate_iovec@plt>
lea    0xc(%rsp),%r15
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %r15,%rdi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <populate_iovec@plt>
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0x4,%edx
call   1040 <writev@plt>
lea    0xe14(%rip),%rdi
mov    %eax,%esi
call   1060 <printf@plt>
mov    %ebp,%edi
call   1070 <close@plt>
xor    %eax,%eax
add    $0x1c8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.iovec = type { i32 }
@__const.main.write_buf1 = [100 x i8] c" The writve() function shall be equivalent to write(). \00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00"
@__const.main.write_buf2 = [100 x i8] c" The writev() function writes iovcnt buffers of data described \00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00"
@__const.main.write_buf3 = [100 x i8] c" by iov to the file associated with the \00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00"
@__const.main.write_buf4 = [100 x i8] c" file descriptorfd.\0A \00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00"
@.str = [10 x i8] c"Write.txt\00"
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@.str.1 = [49 x i8] c"\0A Total number of bytes written inti file : %d \0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [100 x i8]
%4 = alloca [100 x i8]
%5 = alloca [100 x i8]
%6 = alloca [100 x i8]
%7 = alloca [4 x %struct.iovec]
%8 = bitcast [100 x i8]* %3 to i8*
%9 = bitcast [100 x i8]* %4 to i8*
%10 = bitcast [100 x i8]* %5 to i8*
%11 = bitcast [100 x i8]* %6 to i8*
%12 = load i32, i32* @O_CREAT
%13 = load i32, i32* @O_WRONLY
%14 = or i32 %12, %13
%15 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %14, i32 420)
store i32 %15, i32* %1
%16 = getelementptr inbounds [4 x %struct.iovec], [4 x %struct.iovec]* %7, i64 0, i64 0
%17 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%18 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%19 = call i32 @strlen(i8* %18)
%20 = call i32 @populate_iovec(%struct.iovec* %16, i8* %17, i32 %19)
%21 = getelementptr inbounds [4 x %struct.iovec], [4 x %struct.iovec]* %7, i64 0, i64 0
%22 = getelementptr inbounds %struct.iovec, %struct.iovec* %21, i64 1
%23 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%24 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%25 = call i32 @strlen(i8* %24)
%26 = call i32 @populate_iovec(%struct.iovec* %22, i8* %23, i32 %25)
%27 = getelementptr inbounds [4 x %struct.iovec], [4 x %struct.iovec]* %7, i64 0, i64 0
%28 = getelementptr inbounds %struct.iovec, %struct.iovec* %27, i64 2
%29 = getelementptr inbounds [100 x i8], [100 x i8]* %5, i64 0, i64 0
%30 = getelementptr inbounds [100 x i8], [100 x i8]* %5, i64 0, i64 0
%31 = call i32 @strlen(i8* %30)
%32 = call i32 @populate_iovec(%struct.iovec* %28, i8* %29, i32 %31)
%33 = getelementptr inbounds [4 x %struct.iovec], [4 x %struct.iovec]* %7, i64 0, i64 0
%34 = getelementptr inbounds %struct.iovec, %struct.iovec* %33, i64 3
%35 = getelementptr inbounds [100 x i8], [100 x i8]* %6, i64 0, i64 0
%36 = getelementptr inbounds [100 x i8], [100 x i8]* %6, i64 0, i64 0
%37 = call i32 @strlen(i8* %36)
%38 = call i32 @populate_iovec(%struct.iovec* %34, i8* %35, i32 %37)
%39 = load i32, i32* %1
%40 = getelementptr inbounds [4 x %struct.iovec], [4 x %struct.iovec]* %7, i64 0, i64 0
%41 = call i32 @writev(i32 %39, %struct.iovec* %40, i32 4)
store i32 %41, i32* %2
%42 = load i32, i32* %2
%43 = call i32 @printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.1, i64 0, i64 0), i32 %42)
%44 = load i32, i32* %1
%45 = call i32 @close(i32 %44)
ret i32 0
}

LLVM IR (-O2):
%struct.iovec = type { i32 }
@__const.main.write_buf1 = [100 x i8] c" The writve() function shall be equivalent to write(). \00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00"
@__const.main.write_buf2 = [100 x i8] c" The writev() function writes iovcnt buffers of data described \00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00"
@__const.main.write_buf3 = [100 x i8] c" by iov to the file associated with the \00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00"
@__const.main.write_buf4 = [100 x i8] c" file descriptorfd.\0A \00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00"
@.str = [10 x i8] c"Write.txt\00"
@O_CREAT = global i32 0
@O_WRONLY = global i32 0
@.str.1 = [49 x i8] c"\0A Total number of bytes written inti file : %d \0A\00"
define i32 @main() {
%1 = alloca [100 x i8]
%2 = alloca [100 x i8]
%3 = alloca [100 x i8]
%4 = alloca [100 x i8]
%5 = alloca [4 x %struct.iovec]
%6 = getelementptr inbounds [100 x i8], [100 x i8]* %1, i64 0, i64 0
%7 = getelementptr inbounds [100 x i8], [100 x i8]* %2, i64 0, i64 0
%8 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%9 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%10 = bitcast [4 x %struct.iovec]* %5 to i8*
%11 = load i32, i32* @O_CREAT
%12 = load i32, i32* @O_WRONLY
%13 = or i32 %12, %11
%14 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %13, i32 420)
%15 = getelementptr inbounds [4 x %struct.iovec], [4 x %struct.iovec]* %5, i64 0, i64 0
%16 = call i32 @strlen(i8* %6)
%17 = call i32 @populate_iovec(%struct.iovec* %15, i8* %6, i32 %16)
%18 = getelementptr inbounds [4 x %struct.iovec], [4 x %struct.iovec]* %5, i64 0, i64 1
%19 = call i32 @strlen(i8* %7)
%20 = call i32 @populate_iovec(%struct.iovec* %18, i8* %7, i32 %19)
%21 = getelementptr inbounds [4 x %struct.iovec], [4 x %struct.iovec]* %5, i64 0, i64 2
%22 = call i32 @strlen(i8* %8)
%23 = call i32 @populate_iovec(%struct.iovec* %21, i8* %8, i32 %22)
%24 = getelementptr inbounds [4 x %struct.iovec], [4 x %struct.iovec]* %5, i64 0, i64 3
%25 = call i32 @strlen(i8* %9)
%26 = call i32 @populate_iovec(%struct.iovec* %24, i8* %9, i32 %25)
%27 = call i32 @writev(i32 %14, %struct.iovec* %15, i32 4)
%28 = call i32 @printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.1, i64 0, i64 0), i32 %27)
%29 = call i32 @close(i32 %14)
ret i32 0
}