#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 */
typedef int /*<<< orphan*/ buf2;
typedef int /*<<< orphan*/ buf1;

/* Variables and functions */
int O_CREAT;
int O_TRUNC;
int O_WRONLY;
int /*<<< orphan*/ close(int);
int errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int open(char *, int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ test_writev(int, char *, int, char *, int);
int main(int argc, char *argv[]) {
  if (argc < 2) {
    printf("Usage: sola_writev file\n");
    exit(0);
  }
  int fd, n;
  fd = open(argv[1], O_WRONLY | O_TRUNC | O_CREAT);
  if (fd == -1) {
    printf("open %s failed, errno %d\n", argv[1], errno);
    exit(0);
  }
  char buf1[100] = {0};
  char buf2[200] = {0};
  for (n = 0; n < 1048576; ++n) {
    test_writev(fd, buf1, sizeof(buf1), buf2, sizeof(buf2));
  }
  close(fd);
  printf("test over\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x150,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    1198 <main+0x38>
lea    0xe76(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %edi,%edi
call   1080 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e19(%rip),%rax
mov    (%rax),%esi
mov    0x2e40(%rip),%rax
or     (%rax),%esi
mov    0x2e3f(%rip),%rax
or     (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11f3 <main+0x93>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
mov    0x2df4(%rip),%rax
mov    (%rax),%edx
lea    0xe34(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %edi,%edi
call   1080 <exit@plt>
lea    -0x80(%rbp),%rdi
xor    %esi,%esi
mov    $0x64,%edx
call   1050 <memset@plt>
lea    -0x150(%rbp),%rdi
xor    %esi,%esi
mov    $0xc8,%edx
call   1050 <memset@plt>
movl   $0x0,-0x18(%rbp)
cmpl   $0x100000,-0x18(%rbp)
jge    1256 <main+0xf6>
mov    -0x14(%rbp),%edi
lea    -0x80(%rbp),%rsi
lea    -0x150(%rbp),%rcx
mov    $0x64,%edx
mov    $0xc8,%r8d
call   1030 <test_writev@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    121d <main+0xbd>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
lea    0xdce(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x150,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x138,%rsp
cmp    $0x1,%edi
jle    1290 <main+0x130>
mov    %rsi,%r14
mov    0x2e40(%rip),%rax
mov    0x2e69(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x8(%r14),%rdi
mov    0x2e62(%rip),%rax
or     (%rax),%esi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     12a3 <main+0x143>
mov    %eax,%ebx
xorps  %xmm0,%xmm0
movaps %xmm0,0x50(%rsp)
movaps %xmm0,0x40(%rsp)
movaps %xmm0,0x30(%rsp)
movaps %xmm0,0x20(%rsp)
movaps %xmm0,0x10(%rsp)
movaps %xmm0,(%rsp)
movl   $0x0,0x60(%rsp)
movaps %xmm0,0x120(%rsp)
movaps %xmm0,0x110(%rsp)
movaps %xmm0,0x100(%rsp)
movaps %xmm0,0xf0(%rsp)
movaps %xmm0,0xe0(%rsp)
movaps %xmm0,0xd0(%rsp)
movaps %xmm0,0xc0(%rsp)
movaps %xmm0,0xb0(%rsp)
movaps %xmm0,0xa0(%rsp)
movaps %xmm0,0x90(%rsp)
movaps %xmm0,0x80(%rsp)
movaps %xmm0,0x70(%rsp)
movq   $0x0,0x130(%rsp)
mov    $0x100000,%ebp
mov    %rsp,%r14
lea    0x70(%rsp),%r15
cs nopw 0x0(%rax,%rax,1)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x64,%edx
mov    %r15,%rcx
mov    $0xc8,%r8d
call   1040 <test_writev@plt>
add    $0xffffffff,%ebp
jne    1250 <main+0xf0>
mov    %ebx,%edi
call   1060 <close@plt>
lea    0xd9f(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x138,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd8d(%rip),%rdi
call   1030 <puts@plt>
xor    %edi,%edi
call   1080 <exit@plt>
mov    0x8(%r14),%rsi
mov    0x2d22(%rip),%rax
mov    (%rax),%edx
lea    0xd49(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %edi,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [25 x i8] c"Usage: sola_writev file\0A\00"
@O_WRONLY = global i32 0
@O_TRUNC = global i32 0
@O_CREAT = global i32 0
@.str.1 = [26 x i8] c"open %s failed, errno %d\0A\00"
@errno = global i32 0
@.str.2 = [11 x i8] c"test over\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca [100 x i8]
%9 = alloca [200 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* %4
%11 = icmp slt i32 %10, 2
br i1 %11, label %12, label %15
12: ; preds = %2
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
%14 = call i32 @exit(i32 0)
unreachable
15: ; preds = %2
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 1
%18 = load i8*, i8** %17
%19 = load i32, i32* @O_WRONLY
%20 = load i32, i32* @O_TRUNC
%21 = or i32 %19, %20
%22 = load i32, i32* @O_CREAT
%23 = or i32 %21, %22
%24 = call i32 @open(i8* %18, i32 %23)
store i32 %24, i32* %6
%25 = load i32, i32* %6
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %34
27: ; preds = %15
%28 = load i8**, i8*** %5
%29 = getelementptr inbounds i8*, i8** %28, i64 1
%30 = load i8*, i8** %29
%31 = load i32, i32* @errno
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i8* %30, i32 %31)
%33 = call i32 @exit(i32 0)
unreachable
34: ; preds = %15
%35 = bitcast [100 x i8]* %8 to i8*
%36 = bitcast [200 x i8]* %9 to i8*
store i32 0, i32* %7
br label %37
37: ; preds = %45, %34
%38 = load i32, i32* %7
%39 = icmp slt i32 %38, 1048576
br i1 %39, label %40, label %48
40: ; preds = %37
%41 = load i32, i32* %6
%42 = getelementptr inbounds [100 x i8], [100 x i8]* %8, i64 0, i64 0
%43 = getelementptr inbounds [200 x i8], [200 x i8]* %9, i64 0, i64 0
%44 = call i32 @test_writev(i32 %41, i8* %42, i32 100, i8* %43, i32 200)
br label %45
45: ; preds = %40
%46 = load i32, i32* %7
%47 = add nsw i32 %46, 1
store i32 %47, i32* %7
br label %37
48: ; preds = %37
%49 = load i32, i32* %6
%50 = call i32 @close(i32 %49)
%51 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
ret i32 0
}

LLVM IR (-O2):
@O_WRONLY = global i32 0
@O_TRUNC = global i32 0
@O_CREAT = global i32 0
@.str.1 = [26 x i8] c"open %s failed, errno %d\0A\00"
@errno = global i32 0
@str = [10 x i8] c"test over\00"
@str.3 = [24 x i8] c"Usage: sola_writev file\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [100 x i8]
%4 = alloca [200 x i8]
%5 = icmp slt i32 %0, 2
br i1 %5, label %6, label %9
6: ; preds = %2
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str.3, i64 0, i64 0))
%8 = tail call i32 @exit(i32 0)
unreachable
9: ; preds = %2
%10 = getelementptr inbounds i8*, i8** %1, i64 1
%11 = load i8*, i8** %10
%12 = load i32, i32* @O_WRONLY
%13 = load i32, i32* @O_TRUNC
%14 = or i32 %13, %12
%15 = load i32, i32* @O_CREAT
%16 = or i32 %14, %15
%17 = tail call i32 @open(i8* %11, i32 %16)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %24
19: ; preds = %9
%20 = load i8*, i8** %10
%21 = load i32, i32* @errno
%22 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i8* %20, i32 %21)
%23 = tail call i32 @exit(i32 0)
unreachable
24: ; preds = %9
%25 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%26 = getelementptr inbounds [200 x i8], [200 x i8]* %4, i64 0, i64 0
br label %27
27: ; preds = %24, %27
%28 = phi i32 [ 0, %24 ], [ %30, %27 ]
%29 = call i32 @test_writev(i32 %17, i8* %25, i32 100, i8* %26, i32 200)
%30 = add nuw nsw i32 %28, 1
%31 = icmp eq i32 %30, 1048576
br i1 %31, label %32, label %27
32: ; preds = %27
%33 = call i32 @close(i32 %17)
%34 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @str, i64 0, i64 0))
ret i32 0
}