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

/* Variables and functions */
int O_CREAT;
int O_WRONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ free(struct iovec *);
scalar_t__ malloc(int);
int open(char *, int);
int strlen(char *);
int /*<<< orphan*/ writev(int, struct iovec *, int);
int main(int argc, char *argv[]) {
  int fd;
  struct iovec *vec;
  struct iovec *vec_next;
  int i;
  char newline = '\n';
  char *filename = argv[1];
  argc -= 2;
  argv += 2;
  vec = (struct iovec *)malloc(2 * argc * sizeof(struct iovec));
  vec_next = vec;
  for (i = 0; i < argc; i++) {
    vec_next->iov_base = argv[i];
    vec_next->iov_len = strlen(argv[i]);
    ++vec_next;
    vec_next->iov_base = &newline;
    vec_next->iov_len = 1;
    ++vec_next;
  }
  fd = open(filename, O_WRONLY | O_CREAT);
  writev(fd, vec, 2 * argc);
  close(fd);
  free(vec);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movb   $0xa,-0x2d(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x38(%rbp)
mov    -0x8(%rbp),%eax
sub    $0x2,%eax
mov    %eax,-0x8(%rbp)
mov    -0x10(%rbp),%rax
add    $0x10,%rax
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%eax
shl    $0x1,%eax
cltq
shl    $0x4,%rax
mov    %eax,%edi
call   1070 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    1236 <main+0xd6>
mov    -0x10(%rbp),%rax
movslq -0x2c(%rbp),%rcx
mov    (%rax,%rcx,8),%rcx
mov    -0x28(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
movslq -0x2c(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
call   1050 <strlen@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,0x8(%rax)
mov    -0x28(%rbp),%rax
add    $0x10,%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
lea    -0x2d(%rbp),%rcx
mov    %rcx,(%rax)
mov    -0x28(%rbp),%rax
movl   $0x1,0x8(%rax)
mov    -0x28(%rbp),%rax
add    $0x10,%rax
mov    %rax,-0x28(%rbp)
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    11c1 <main+0x61>
mov    -0x38(%rbp),%rdi
mov    0x2d8f(%rip),%rax
mov    (%rax),%esi
mov    0x2dae(%rip),%rax
or     (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x8(%rbp),%edx
shl    $0x1,%edx
call   1040 <writev@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r13
mov    %edi,%r12d
movb   $0xa,0xf(%rsp)
mov    0x8(%rsi),%rax
mov    %rax,0x10(%rsp)
lea    -0x2(%r12),%r15d
mov    %r15d,%edi
shl    $0x5,%edi
call   1070 <malloc@plt>
mov    %rax,%rbx
cmp    $0x3,%r12d
jl     11dd <main+0x7d>
mov    %r15d,%r12d
shl    $0x3,%r12
xor    %ebp,%ebp
lea    0xf(%rsp),%r14
nopl   0x0(%rax)
mov    0x10(%r13,%rbp,1),%rax
mov    %rax,(%rbx,%rbp,4)
mov    0x10(%r13,%rbp,1),%rdi
call   1050 <strlen@plt>
mov    %eax,0x8(%rbx,%rbp,4)
mov    %r14,0x10(%rbx,%rbp,4)
movl   $0x1,0x18(%rbx,%rbp,4)
add    $0x8,%rbp
cmp    %rbp,%r12
jne    11b0 <main+0x50>
add    %r15d,%r15d
mov    0x2de9(%rip),%rax
mov    0x2e0a(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x10(%rsp),%rdi
call   1080 <open@plt>
mov    %eax,%ebp
mov    %eax,%edi
mov    %rbx,%rsi
mov    %r15d,%edx
call   1040 <writev@plt>
mov    %ebp,%edi
call   1060 <close@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.iovec = type { i8*, i32 }
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca %struct.iovec*
%8 = alloca %struct.iovec*
%9 = alloca i32
%10 = alloca i8
%11 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i8 10, i8* %10
%12 = load i8**, i8*** %5
%13 = getelementptr inbounds i8*, i8** %12, i64 1
%14 = load i8*, i8** %13
store i8* %14, i8** %11
%15 = load i32, i32* %4
%16 = sub nsw i32 %15, 2
store i32 %16, i32* %4
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 2
store i8** %18, i8*** %5
%19 = load i32, i32* %4
%20 = mul nsw i32 2, %19
%21 = sext i32 %20 to i64
%22 = mul i64 %21, 16
%23 = trunc i64 %22 to i32
%24 = call i64 @malloc(i32 %23)
%25 = inttoptr i64 %24 to %struct.iovec*
store %struct.iovec* %25, %struct.iovec** %7
%26 = load %struct.iovec*, %struct.iovec** %7
store %struct.iovec* %26, %struct.iovec** %8
store i32 0, i32* %9
br label %27
27: ; preds = %55, %2
%28 = load i32, i32* %9
%29 = load i32, i32* %4
%30 = icmp slt i32 %28, %29
br i1 %30, label %31, label %58
31: ; preds = %27
%32 = load i8**, i8*** %5
%33 = load i32, i32* %9
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds i8*, i8** %32, i64 %34
%36 = load i8*, i8** %35
%37 = load %struct.iovec*, %struct.iovec** %8
%38 = getelementptr inbounds %struct.iovec, %struct.iovec* %37, i32 0, i32 0
store i8* %36, i8** %38
%39 = load i8**, i8*** %5
%40 = load i32, i32* %9
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds i8*, i8** %39, i64 %41
%43 = load i8*, i8** %42
%44 = call i32 @strlen(i8* %43)
%45 = load %struct.iovec*, %struct.iovec** %8
%46 = getelementptr inbounds %struct.iovec, %struct.iovec* %45, i32 0, i32 1
store i32 %44, i32* %46
%47 = load %struct.iovec*, %struct.iovec** %8
%48 = getelementptr inbounds %struct.iovec, %struct.iovec* %47, i32 1
store %struct.iovec* %48, %struct.iovec** %8
%49 = load %struct.iovec*, %struct.iovec** %8
%50 = getelementptr inbounds %struct.iovec, %struct.iovec* %49, i32 0, i32 0
store i8* %10, i8** %50
%51 = load %struct.iovec*, %struct.iovec** %8
%52 = getelementptr inbounds %struct.iovec, %struct.iovec* %51, i32 0, i32 1
store i32 1, i32* %52
%53 = load %struct.iovec*, %struct.iovec** %8
%54 = getelementptr inbounds %struct.iovec, %struct.iovec* %53, i32 1
store %struct.iovec* %54, %struct.iovec** %8
br label %55
55: ; preds = %31
%56 = load i32, i32* %9
%57 = add nsw i32 %56, 1
store i32 %57, i32* %9
br label %27
58: ; preds = %27
%59 = load i8*, i8** %11
%60 = load i32, i32* @O_WRONLY
%61 = load i32, i32* @O_CREAT
%62 = or i32 %60, %61
%63 = call i32 @open(i8* %59, i32 %62)
store i32 %63, i32* %6
%64 = load i32, i32* %6
%65 = load %struct.iovec*, %struct.iovec** %7
%66 = load i32, i32* %4
%67 = mul nsw i32 2, %66
%68 = call i32 @writev(i32 %64, %struct.iovec* %65, i32 %67)
%69 = load i32, i32* %6
%70 = call i32 @close(i32 %69)
%71 = load %struct.iovec*, %struct.iovec** %7
%72 = call i32 @free(%struct.iovec* %71)
ret i32 0
}

LLVM IR (-O2):
%struct.iovec = type { i8*, i32 }
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i8
store i8 10, i8* %3
%4 = getelementptr inbounds i8*, i8** %1, i64 1
%5 = load i8*, i8** %4
%6 = add nsw i32 %0, -2
%7 = getelementptr inbounds i8*, i8** %1, i64 2
%8 = shl i32 %6, 5
%9 = tail call i64 @malloc(i32 %8)
%10 = inttoptr i64 %9 to %struct.iovec*
%11 = icmp sgt i32 %0, 2
br i1 %11, label %12, label %28
12: ; preds = %2
%13 = zext i32 %6 to i64
br label %14
14: ; preds = %12, %14
%15 = phi i64 [ 0, %12 ], [ %26, %14 ]
%16 = phi %struct.iovec* [ %10, %12 ], [ %25, %14 ]
%17 = getelementptr inbounds i8*, i8** %7, i64 %15
%18 = load i8*, i8** %17
%19 = getelementptr inbounds %struct.iovec, %struct.iovec* %16, i64 0, i32 0
store i8* %18, i8** %19
%20 = load i8*, i8** %17
%21 = call i32 @strlen(i8* %20)
%22 = getelementptr inbounds %struct.iovec, %struct.iovec* %16, i64 0, i32 1
store i32 %21, i32* %22
%23 = getelementptr inbounds %struct.iovec, %struct.iovec* %16, i64 1, i32 0
store i8* %3, i8** %23
%24 = getelementptr inbounds %struct.iovec, %struct.iovec* %16, i64 1, i32 1
store i32 1, i32* %24
%25 = getelementptr inbounds %struct.iovec, %struct.iovec* %16, i64 2
%26 = add nuw nsw i64 %15, 1
%27 = icmp eq i64 %26, %13
br i1 %27, label %28, label %14
28: ; preds = %14, %2
%29 = shl nsw i32 %6, 1
%30 = load i32, i32* @O_WRONLY
%31 = load i32, i32* @O_CREAT
%32 = or i32 %31, %30
%33 = call i32 @open(i8* %5, i32 %32)
%34 = call i32 @writev(i32 %33, %struct.iovec* %10, i32 %29)
%35 = call i32 @close(i32 %33)
%36 = call i32 @free(%struct.iovec* %10)
ret i32 0
}