#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;

/* Variables and functions */
int O_CREAT;
int O_TRUNC;
int O_WRONLY;
scalar_t__ close(int);
int open(char *, int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int, ...);
int strlen(char *);
int writev(int, struct iovec *, int);
int main() {
  struct iovec iov[3];
  ssize_t nr;
  int fd, i;
  char *buf[] = {".net", "groad", "www."};
  fd = open("groad.txt", O_WRONLY | O_CREAT | O_TRUNC, 0755);
  if (fd == -1) {
    perror("open");
    return 1;
  }
  for (i = 0; i < 3; i++) {
    iov[i].iov_base = buf[i];
    iov[i].iov_len = strlen(buf[i]);
    printf("%d : %d\n", i, iov[i].iov_len);
  }
  nr = writev(fd, iov, 3);
  if (nr == -1) {
    perror("writev");
    return 1;
  }
  printf("wrote %d bytes\n", nr);
  if (close(fd)) {
    perror("close");
    return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2c7a(%rip),%rax
mov    %rax,-0x70(%rbp)
mov    0x2c77(%rip),%rax
mov    %rax,-0x68(%rbp)
mov    0x2c74(%rip),%rax
mov    %rax,-0x60(%rbp)
mov    0x2e31(%rip),%rax
mov    (%rax),%esi
mov    0x2e58(%rip),%rax
or     (%rax),%esi
mov    0x2e47(%rip),%rax
or     (%rax),%esi
lea    0xe5e(%rip),%rdi
mov    $0x1ed,%edx
call   1070 <open@plt>
mov    %eax,-0x48(%rbp)
cmpl   $0xffffffff,-0x48(%rbp)
jne    11e1 <main+0x81>
lea    0xe4a(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12d8 <main+0x178>
movl   $0x0,-0x4c(%rbp)
cmpl   $0x3,-0x4c(%rbp)
jge    1260 <main+0x100>
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    %eax,%ecx
movslq -0x4c(%rbp),%rdx
lea    -0x40(%rbp),%rax
shl    $0x4,%rdx
add    %rdx,%rax
mov    %ecx,0x8(%rax)
mov    -0x4c(%rbp),%esi
movslq -0x4c(%rbp),%rcx
lea    -0x40(%rbp),%rax
shl    $0x4,%rcx
add    %rcx,%rax
mov    0x8(%rax),%edx
lea    0xdd4(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x4c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4c(%rbp)
jmp    11e8 <main+0x88>
mov    -0x48(%rbp),%edi
lea    -0x40(%rbp),%rsi
mov    $0x3,%edx
call   1030 <writev@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0xffffffff,-0x44(%rbp)
jne    1296 <main+0x136>
lea    0xda3(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12d8 <main+0x178>
mov    -0x44(%rbp),%esi
lea    0xd8f(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x48(%rbp),%edi
call   1060 <close@plt>
cmp    $0x0,%rax
je     12d1 <main+0x171>
lea    0xd7f(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12d8 <main+0x178>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x30,%rsp
mov    0x2e59(%rip),%rax
mov    0x2e82(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e6f(%rip),%rax
or     (%rax),%esi
lea    0xe86(%rip),%rdi
mov    $0x1ed,%edx
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     1253 <main+0xf3>
mov    %eax,%ebp
lea    0xe5a(%rip),%rdi
mov    %rdi,(%rsp)
call   1040 <strlen@plt>
mov    %eax,0x8(%rsp)
lea    0xe65(%rip),%rbx
xor    %r14d,%r14d
mov    %rbx,%rdi
xor    %esi,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe33(%rip),%rdi
mov    %rdi,0x10(%rsp)
call   1040 <strlen@plt>
mov    %eax,0x18(%rsp)
mov    %rbx,%rdi
mov    $0x1,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe13(%rip),%rdi
mov    %rdi,0x20(%rsp)
call   1040 <strlen@plt>
mov    %eax,0x28(%rsp)
mov    %rbx,%rdi
mov    $0x2,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rsp,%rsi
mov    %ebp,%edi
mov    $0x3,%edx
call   1030 <writev@plt>
cmp    $0xffffffff,%eax
je     125c <main+0xfc>
lea    0xdfd(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %ebp,%edi
call   1060 <close@plt>
test   %rax,%rax
jne    1265 <main+0x105>
mov    %r14d,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xdc0(%rip),%rdi
jmp    126c <main+0x10c>
lea    0xdc5(%rip),%rdi
jmp    126c <main+0x10c>
lea    0xdd3(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%r14d
jmp    1247 <main+0xe7>

LLVM IR (-O0):
%struct.iovec = type { i8*, i32 }
@.str = [5 x i8] c".net\00"
@.str.1 = [6 x i8] c"groad\00"
@.str.2 = [5 x i8] c"www.\00"
@__const.main.buf = [3 x i8*] [i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i32 0, i32 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i32 0, i32 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i32 0, i32 0)]
@.str.3 = [10 x i8] c"groad.txt\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@.str.4 = [5 x i8] c"open\00"
@.str.5 = [9 x i8] c"%d : %d\0A\00"
@.str.6 = [7 x i8] c"writev\00"
@.str.7 = [16 x i8] c"wrote %d bytes\0A\00"
@.str.8 = [6 x i8] c"close\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 ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i32 %12, i32 493)
store i32 %13, i32* %4
%14 = load i32, i32* %4
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %18
16: ; preds = %0
%17 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
store i32 1, i32* %1
br label %67
18: ; preds = %0
store i32 0, i32* %5
br label %19
19: ; preds = %47, %18
%20 = load i32, i32* %5
%21 = icmp slt i32 %20, 3
br i1 %21, label %22, label %50
22: ; preds = %19
%23 = load i32, i32* %5
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds [3 x i8*], [3 x i8*]* %6, i64 0, i64 %24
%26 = load i8*, i8** %25
%27 = load i32, i32* %5
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds [3 x %struct.iovec], [3 x %struct.iovec]* %2, i64 0, i64 %28
%30 = getelementptr inbounds %struct.iovec, %struct.iovec* %29, i32 0, i32 0
store i8* %26, i8** %30
%31 = load i32, i32* %5
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds [3 x i8*], [3 x i8*]* %6, i64 0, i64 %32
%34 = load i8*, i8** %33
%35 = call i32 @strlen(i8* %34)
%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 i32 %35, i32* %39
%40 = load i32, i32* %5
%41 = load i32, i32* %5
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds [3 x %struct.iovec], [3 x %struct.iovec]* %2, i64 0, i64 %42
%44 = getelementptr inbounds %struct.iovec, %struct.iovec* %43, i32 0, i32 1
%45 = load i32, i32* %44
%46 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0), i32 %40, i32 %45)
br label %47
47: ; preds = %22
%48 = load i32, i32* %5
%49 = add nsw i32 %48, 1
store i32 %49, i32* %5
br label %19
50: ; preds = %19
%51 = load i32, i32* %4
%52 = getelementptr inbounds [3 x %struct.iovec], [3 x %struct.iovec]* %2, i64 0, i64 0
%53 = call i32 @writev(i32 %51, %struct.iovec* %52, i32 3)
store i32 %53, i32* %3
%54 = load i32, i32* %3
%55 = icmp eq i32 %54, -1
br i1 %55, label %56, label %58
56: ; preds = %50
%57 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0))
store i32 1, i32* %1
br label %67
58: ; preds = %50
%59 = load i32, i32* %3
%60 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.7, i64 0, i64 0), i32 %59)
%61 = load i32, i32* %4
%62 = call i64 @close(i32 %61)
%63 = icmp ne i64 %62, 0
br i1 %63, label %64, label %66
64: ; preds = %58
%65 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.8, i64 0, i64 0))
store i32 1, i32* %1
br label %67
66: ; preds = %58
store i32 0, i32* %1
br label %67
67: ; preds = %66, %64, %56, %16
%68 = load i32, i32* %1
ret i32 %68
}

LLVM IR (-O2):
%struct.iovec = type { i8*, i32 }
@.str = [5 x i8] c".net\00"
@.str.1 = [6 x i8] c"groad\00"
@.str.2 = [5 x i8] c"www.\00"
@.str.3 = [10 x i8] c"groad.txt\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@.str.4 = [5 x i8] c"open\00"
@.str.5 = [9 x i8] c"%d : %d\0A\00"
@.str.6 = [7 x i8] c"writev\00"
@.str.7 = [16 x i8] c"wrote %d bytes\0A\00"
@.str.8 = [6 x i8] c"close\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 ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i32 %7, i32 493)
%9 = icmp eq i32 %8, -1
br i1 %9, label %26, 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 ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8** %11
%12 = tail call i32 @strlen(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%13 = getelementptr inbounds [3 x %struct.iovec], [3 x %struct.iovec]* %1, i64 0, i64 0, i32 1
store i32 %12, i32* %13
%14 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0), i32 0, i32 %12)
%15 = getelementptr inbounds [3 x %struct.iovec], [3 x %struct.iovec]* %1, i64 0, i64 1, i32 0
store i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8** %15
%16 = tail call i32 @strlen(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%17 = getelementptr inbounds [3 x %struct.iovec], [3 x %struct.iovec]* %1, i64 0, i64 1, i32 1
store i32 %16, i32* %17
%18 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0), i32 1, i32 %16)
%19 = getelementptr inbounds [3 x %struct.iovec], [3 x %struct.iovec]* %1, i64 0, i64 2, i32 0
store i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8** %19
%20 = tail call i32 @strlen(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%21 = getelementptr inbounds [3 x %struct.iovec], [3 x %struct.iovec]* %1, i64 0, i64 2, i32 1
store i32 %20, i32* %21
%22 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0), i32 2, i32 %20)
%23 = getelementptr inbounds [3 x %struct.iovec], [3 x %struct.iovec]* %1, i64 0, i64 0
%24 = call i32 @writev(i32 %8, %struct.iovec* %23, i32 3)
%25 = icmp eq i32 %24, -1
br i1 %25, label %28, label %30
26: ; preds = %0
%27 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
br label %36
28: ; preds = %10
%29 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.6, i64 0, i64 0))
br label %36
30: ; preds = %10
%31 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.7, i64 0, i64 0), i32 %24)
%32 = call i64 @close(i32 %8)
%33 = icmp eq i64 %32, 0
br i1 %33, label %36, label %34
34: ; preds = %30
%35 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.8, i64 0, i64 0))
br label %36
36: ; preds = %30, %34, %28, %26
%37 = phi i32 [ 1, %26 ], [ 1, %28 ], [ 1, %34 ], [ 0, %30 ]
ret i32 %37
}