#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 iov_len;
  char *iov_base;
};

/* Variables and functions */
int O_CREAT;
int O_TRUNC;
int O_WRONLY;
int errno;
int open(char *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
int pwritev(int, struct iovec *, int, int);
int main() {
  int fd = open("test.txt", O_WRONLY | O_CREAT | O_TRUNC);
  if (fd == -1) {
    perror("open");
    return errno;
  }
  struct iovec iov[2];
  for (int i = 0; i < 2; i++) {
    iov[i].iov_len = 10;
    iov[i].iov_base = "=1234567890";
  }
  int ret = pwritev(fd, iov, 2, 5);
  if (ret == -1) {
    perror("pwritev");
    return errno;
  }
  printf("%d\n", ret);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e6a(%rip),%rax
mov    (%rax),%esi
mov    0x2e99(%rip),%rax
or     (%rax),%esi
mov    0x2e88(%rip),%rax
or     (%rax),%esi
lea    0xe8f(%rip),%rdi
call   1050 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    11a0 <main+0x60>
lea    0xe7f(%rip),%rdi
call   1060 <perror@plt>
mov    0x2e3a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1244 <main+0x104>
movl   $0x0,-0x34(%rbp)
cmpl   $0x2,-0x34(%rbp)
jge    11ee <main+0xae>
movslq -0x34(%rbp),%rcx
lea    -0x30(%rbp),%rax
shl    $0x4,%rcx
add    %rcx,%rax
movl   $0xa,(%rax)
movslq -0x34(%rbp),%rcx
lea    -0x30(%rbp),%rax
shl    $0x4,%rcx
add    %rcx,%rax
lea    0xe32(%rip),%rcx
mov    %rcx,0x8(%rax)
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    11a7 <main+0x67>
mov    -0x8(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x2,%edx
mov    $0x5,%ecx
call   1040 <pwritev@plt>
mov    %eax,-0x38(%rbp)
cmpl   $0xffffffff,-0x38(%rbp)
jne    122e <main+0xee>
lea    0xe02(%rip),%rdi
call   1060 <perror@plt>
mov    0x2dac(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1244 <main+0x104>
mov    -0x38(%rbp),%esi
lea    0xdea(%rip),%rdi
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
sub    $0x28,%rsp
mov    0x2e75(%rip),%rax
mov    0x2ea6(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e93(%rip),%rax
or     (%rax),%esi
lea    0xe9a(%rip),%rdi
call   1050 <open@plt>
cmp    $0xffffffff,%eax
je     11be <main+0x7e>
movl   $0xa,(%rsp)
lea    0xe90(%rip),%rcx
mov    %rcx,0x8(%rsp)
movl   $0xa,0x10(%rsp)
mov    %rcx,0x18(%rsp)
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0x2,%edx
mov    $0x5,%ecx
call   1040 <pwritev@plt>
cmp    $0xffffffff,%eax
je     11c7 <main+0x87>
lea    0xe72(%rip),%rdi
mov    %eax,%esi
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x28,%rsp
ret
lea    0xe44(%rip),%rdi
jmp    11ce <main+0x8e>
lea    0xe4c(%rip),%rdi
call   1060 <perror@plt>
mov    0x2df6(%rip),%rax
mov    (%rax),%eax
add    $0x28,%rsp
ret

LLVM IR (-O0):
%struct.iovec = type { i32, i8* }
@.str = [9 x i8] c"test.txt\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@.str.1 = [5 x i8] c"open\00"
@errno = global i32 0
@.str.2 = [12 x i8] c"=1234567890\00"
@.str.3 = [8 x i8] c"pwritev\00"
@.str.4 = [4 x i8] c"%d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [2 x %struct.iovec]
%4 = alloca i32
%5 = alloca i32
store i32 0, i32* %1
%6 = load i32, i32* @O_WRONLY
%7 = load i32, i32* @O_CREAT
%8 = or i32 %6, %7
%9 = load i32, i32* @O_TRUNC
%10 = or i32 %8, %9
%11 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %10)
store i32 %11, i32* %2
%12 = load i32, i32* %2
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %17
14: ; preds = %0
%15 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%16 = load i32, i32* @errno
store i32 %16, i32* %1
br label %45
17: ; preds = %0
store i32 0, i32* %4
br label %18
18: ; preds = %30, %17
%19 = load i32, i32* %4
%20 = icmp slt i32 %19, 2
br i1 %20, label %21, label %33
21: ; preds = %18
%22 = load i32, i32* %4
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds [2 x %struct.iovec], [2 x %struct.iovec]* %3, i64 0, i64 %23
%25 = getelementptr inbounds %struct.iovec, %struct.iovec* %24, i32 0, i32 0
store i32 10, i32* %25
%26 = load i32, i32* %4
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds [2 x %struct.iovec], [2 x %struct.iovec]* %3, i64 0, i64 %27
%29 = getelementptr inbounds %struct.iovec, %struct.iovec* %28, i32 0, i32 1
store i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i8** %29
br label %30
30: ; preds = %21
%31 = load i32, i32* %4
%32 = add nsw i32 %31, 1
store i32 %32, i32* %4
br label %18
33: ; preds = %18
%34 = load i32, i32* %2
%35 = getelementptr inbounds [2 x %struct.iovec], [2 x %struct.iovec]* %3, i64 0, i64 0
%36 = call i32 @pwritev(i32 %34, %struct.iovec* %35, i32 2, i32 5)
store i32 %36, i32* %5
%37 = load i32, i32* %5
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %42
39: ; preds = %33
%40 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
%41 = load i32, i32* @errno
store i32 %41, i32* %1
br label %45
42: ; preds = %33
%43 = load i32, i32* %5
%44 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i32 %43)
store i32 0, i32* %1
br label %45
45: ; preds = %42, %39, %14
%46 = load i32, i32* %1
ret i32 %46
}

LLVM IR (-O2):
%struct.iovec = type { i32, i8* }
@.str = [9 x i8] c"test.txt\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@.str.1 = [5 x i8] c"open\00"
@errno = global i32 0
@.str.2 = [12 x i8] c"=1234567890\00"
@.str.3 = [8 x i8] c"pwritev\00"
@.str.4 = [4 x i8] c"%d\0A\00"
define i32 @main() {
%1 = alloca [2 x %struct.iovec]
%2 = load i32, i32* @O_WRONLY
%3 = load i32, i32* @O_CREAT
%4 = or i32 %3, %2
%5 = load i32, i32* @O_TRUNC
%6 = or i32 %4, %5
%7 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %6)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %12
9: ; preds = %0
%10 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%11 = load i32, i32* @errno
br label %28
12: ; preds = %0
%13 = bitcast [2 x %struct.iovec]* %1 to i8*
%14 = getelementptr inbounds [2 x %struct.iovec], [2 x %struct.iovec]* %1, i64 0, i64 0, i32 0
store i32 10, i32* %14
%15 = getelementptr inbounds [2 x %struct.iovec], [2 x %struct.iovec]* %1, i64 0, i64 0, i32 1
store i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i8** %15
%16 = getelementptr inbounds [2 x %struct.iovec], [2 x %struct.iovec]* %1, i64 0, i64 1, i32 0
store i32 10, i32* %16
%17 = getelementptr inbounds [2 x %struct.iovec], [2 x %struct.iovec]* %1, i64 0, i64 1, i32 1
store i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i8** %17
%18 = getelementptr inbounds [2 x %struct.iovec], [2 x %struct.iovec]* %1, i64 0, i64 0
%19 = call i32 @pwritev(i32 %7, %struct.iovec* %18, i32 2, i32 5)
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %24
21: ; preds = %12
%22 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
%23 = load i32, i32* @errno
br label %26
24: ; preds = %12
%25 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i32 %19)
br label %26
26: ; preds = %24, %21
%27 = phi i32 [ %23, %21 ], [ 0, %24 ]
br label %28
28: ; preds = %26, %9
%29 = phi i32 [ %11, %9 ], [ %27, %26 ]
ret i32 %29
}