#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 atoi(char *);
int /*<<< orphan*/ close(int);
int errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int open(char *, int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ test_write(int, char *, int, char *, int);
int /*<<< orphan*/ test_writev(int, char *, int, char *, int);
int main(int argc, char *argv[]) {
  if (argc < 4) {
    printf("Usage: linu_writev file times mode\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);
  }
  int total = atoi(argv[2]);
  int mode = atoi(argv[3]);
  char buf1[100] = {0};
  char buf2[200] = {0};
  for (n = 0; n < total; ++n) {
    if (mode)
      test_writev(fd, buf1, sizeof(buf1), buf2, sizeof(buf2));
    else
      test_write(fd, buf1, sizeof(buf1), buf2, sizeof(buf2));
  }
  close(fd);
  printf("test over, %s %d K total\n", mode ? "writev" : "write",
         total * 300 / 1024);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x160,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x4,-0x8(%rbp)
jge    11b8 <main+0x38>
lea    0xe56(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
xor    %edi,%edi
call   10a0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2df9(%rip),%rax
mov    (%rax),%esi
mov    0x2e20(%rip),%rax
or     (%rax),%esi
mov    0x2e1f(%rip),%rax
or     (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1213 <main+0x93>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
mov    0x2dd4(%rip),%rax
mov    (%rax),%edx
lea    0xe1f(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
xor    %edi,%edi
call   10a0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1090 <atoi@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
call   1090 <atoi@plt>
mov    %eax,-0x20(%rbp)
lea    -0x90(%rbp),%rdi
xor    %esi,%esi
mov    $0x64,%edx
call   1060 <memset@plt>
lea    -0x160(%rbp),%rdi
xor    %esi,%esi
mov    $0xc8,%edx
call   1060 <memset@plt>
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    12d0 <main+0x150>
cmpl   $0x0,-0x20(%rbp)
je     129c <main+0x11c>
mov    -0x14(%rbp),%edi
lea    -0x90(%rbp),%rsi
lea    -0x160(%rbp),%rcx
mov    $0x64,%edx
mov    $0xc8,%r8d
call   1040 <test_writev@plt>
jmp    12bd <main+0x13d>
mov    -0x14(%rbp),%edi
lea    -0x90(%rbp),%rsi
lea    -0x160(%rbp),%rcx
mov    $0x64,%edx
mov    $0xc8,%r8d
call   1030 <test_write@plt>
jmp    12c2 <main+0x142>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1260 <main+0xe0>
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
mov    -0x20(%rbp),%ecx
lea    0xd7d(%rip),%rsi
lea    0xd6f(%rip),%rax
cmp    $0x0,%ecx
cmovne %rax,%rsi
imul   $0x12c,-0x1c(%rbp),%eax
mov    $0x400,%ecx
cltd
idiv   %ecx
mov    %eax,%edx
lea    0xd36(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x160,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x138,%rsp
cmp    $0x3,%edi
jle    130f <main+0x18f>
mov    %rsi,%rbp
mov    0x2e1c(%rip),%rax
mov    0x2e45(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x8(%rbp),%rdi
mov    0x2e3e(%rip),%rax
or     (%rax),%esi
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     1322 <main+0x1a2>
mov    %eax,%r13d
mov    0x10(%rbp),%rdi
call   1090 <atoi@plt>
mov    %eax,%r14d
mov    0x18(%rbp),%rdi
call   1090 <atoi@plt>
mov    %eax,%ebp
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)
test   %r14d,%r14d
jle    12bc <main+0x13c>
mov    %rsp,%r15
lea    0x70(%rsp),%r12
mov    %r14d,%ebx
jmp    129a <main+0x11a>
nopw   0x0(%rax,%rax,1)
call   1030 <test_write@plt>
add    $0xffffffff,%ebx
je     12bc <main+0x13c>
mov    %r13d,%edi
mov    %r15,%rsi
mov    $0x64,%edx
mov    %r12,%rcx
mov    $0xc8,%r8d
test   %ebp,%ebp
je     1290 <main+0x110>
call   1050 <test_writev@plt>
add    $0xffffffff,%ebx
jne    129a <main+0x11a>
mov    %r13d,%edi
call   1070 <close@plt>
test   %ebp,%ebp
lea    0xd6e(%rip),%rax
lea    0xd60(%rip),%rsi
cmove  %rax,%rsi
imul   $0x12c,%r14d,%eax
lea    0x3ff(%rax),%edx
test   %eax,%eax
cmovns %eax,%edx
sar    $0xa,%edx
lea    0xd26(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
xor    %eax,%eax
add    $0x138,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd2b(%rip),%rdi
call   1040 <puts@plt>
xor    %edi,%edi
call   10a0 <exit@plt>
mov    0x8(%rbp),%rsi
mov    0x2ca3(%rip),%rax
mov    (%rax),%edx
lea    0xcca(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
xor    %edi,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [36 x i8] c"Usage: linu_writev file times mode\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 = [26 x i8] c"test over, %s %d K total\0A\00"
@.str.3 = [7 x i8] c"writev\00"
@.str.4 = [6 x i8] c"write\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 i32
%9 = alloca i32
%10 = alloca [100 x i8]
%11 = alloca [200 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = load i32, i32* %4
%13 = icmp slt i32 %12, 4
br i1 %13, label %14, label %17
14: ; preds = %2
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0))
%16 = call i32 @exit(i32 0)
unreachable
17: ; preds = %2
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 1
%20 = load i8*, i8** %19
%21 = load i32, i32* @O_WRONLY
%22 = load i32, i32* @O_TRUNC
%23 = or i32 %21, %22
%24 = load i32, i32* @O_CREAT
%25 = or i32 %23, %24
%26 = call i32 @open(i8* %20, i32 %25)
store i32 %26, i32* %6
%27 = load i32, i32* %6
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %36
29: ; preds = %17
%30 = load i8**, i8*** %5
%31 = getelementptr inbounds i8*, i8** %30, i64 1
%32 = load i8*, i8** %31
%33 = load i32, i32* @errno
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i8* %32, i32 %33)
%35 = call i32 @exit(i32 0)
unreachable
36: ; preds = %17
%37 = load i8**, i8*** %5
%38 = getelementptr inbounds i8*, i8** %37, i64 2
%39 = load i8*, i8** %38
%40 = call i32 @atoi(i8* %39)
store i32 %40, i32* %8
%41 = load i8**, i8*** %5
%42 = getelementptr inbounds i8*, i8** %41, i64 3
%43 = load i8*, i8** %42
%44 = call i32 @atoi(i8* %43)
store i32 %44, i32* %9
%45 = bitcast [100 x i8]* %10 to i8*
%46 = bitcast [200 x i8]* %11 to i8*
store i32 0, i32* %7
br label %47
47: ; preds = %65, %36
%48 = load i32, i32* %7
%49 = load i32, i32* %8
%50 = icmp slt i32 %48, %49
br i1 %50, label %51, label %68
51: ; preds = %47
%52 = load i32, i32* %9
%53 = icmp ne i32 %52, 0
br i1 %53, label %54, label %59
54: ; preds = %51
%55 = load i32, i32* %6
%56 = getelementptr inbounds [100 x i8], [100 x i8]* %10, i64 0, i64 0
%57 = getelementptr inbounds [200 x i8], [200 x i8]* %11, i64 0, i64 0
%58 = call i32 @test_writev(i32 %55, i8* %56, i32 100, i8* %57, i32 200)
br label %64
59: ; preds = %51
%60 = load i32, i32* %6
%61 = getelementptr inbounds [100 x i8], [100 x i8]* %10, i64 0, i64 0
%62 = getelementptr inbounds [200 x i8], [200 x i8]* %11, i64 0, i64 0
%63 = call i32 @test_write(i32 %60, i8* %61, i32 100, i8* %62, i32 200)
br label %64
64: ; preds = %59, %54
br label %65
65: ; preds = %64
%66 = load i32, i32* %7
%67 = add nsw i32 %66, 1
store i32 %67, i32* %7
br label %47
68: ; preds = %47
%69 = load i32, i32* %6
%70 = call i32 @close(i32 %69)
%71 = load i32, i32* %9
%72 = icmp ne i32 %71, 0
%73 = zext i1 %72 to i64
%74 = select i1 %72, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0)
%75 = load i32, i32* %8
%76 = mul nsw i32 %75, 300
%77 = sdiv i32 %76, 1024
%78 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i8* %74, i32 %77)
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.2 = [26 x i8] c"test over, %s %d K total\0A\00"
@.str.3 = [7 x i8] c"writev\00"
@.str.4 = [6 x i8] c"write\00"
@str = [35 x i8] c"Usage: linu_writev file times mode\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [100 x i8]
%4 = alloca [200 x i8]
%5 = icmp slt i32 %0, 4
br i1 %5, label %6, label %9
6: ; preds = %2
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @str, 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 i8*, i8** %1, i64 2
%26 = load i8*, i8** %25
%27 = tail call i32 @atoi(i8* %26)
%28 = getelementptr inbounds i8*, i8** %1, i64 3
%29 = load i8*, i8** %28
%30 = tail call i32 @atoi(i8* %29)
%31 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%32 = getelementptr inbounds [200 x i8], [200 x i8]* %4, i64 0, i64 0
%33 = icmp sgt i32 %27, 0
br i1 %33, label %34, label %45
34: ; preds = %24
%35 = icmp eq i32 %30, 0
br label %36
36: ; preds = %34, %42
%37 = phi i32 [ 0, %34 ], [ %43, %42 ]
br i1 %35, label %40, label %38
38: ; preds = %36
%39 = call i32 @test_writev(i32 %17, i8* %31, i32 100, i8* %32, i32 200)
br label %42
40: ; preds = %36
%41 = call i32 @test_write(i32 %17, i8* %31, i32 100, i8* %32, i32 200)
br label %42
42: ; preds = %38, %40
%43 = add nuw nsw i32 %37, 1
%44 = icmp eq i32 %43, %27
br i1 %44, label %45, label %36
45: ; preds = %42, %24
%46 = call i32 @close(i32 %17)
%47 = icmp eq i32 %30, 0
%48 = select i1 %47, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0)
%49 = mul nsw i32 %27, 300
%50 = sdiv i32 %49, 1024
%51 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i8* %48, i32 %50)
ret i32 0
}