#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 /*<<< orphan*/ buffer;

/* Variables and functions */
int /*<<< orphan*/ O_WRONLY;
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
int pwritev2(int, struct iovec *, int, int, int /*<<< orphan*/);
int main(void) {
  struct iovec iov;
  char buffer[] = "hello world\n";
  int fd, rc;
  fd = open("/dev/zero", O_WRONLY);
  if (fd < 0)
    return -1;
  iov.iov_base = buffer;
  iov.iov_len = sizeof(buffer);
  rc = pwritev2(fd, &iov, 1, -1, 0);
  (void)close(fd);
  return rc;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    0xeba(%rip),%rax
mov    %rax,-0x25(%rbp)
mov    0xeb8(%rip),%eax
mov    %eax,-0x1d(%rbp)
mov    0xeb3(%rip),%al
mov    %al,-0x19(%rbp)
mov    0x2e75(%rip),%rax
mov    (%rax),%esi
lea    0xea1(%rip),%rdi
call   1050 <open@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    118a <main+0x5a>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c3 <main+0x93>
lea    -0x25(%rbp),%rax
mov    %rax,-0x18(%rbp)
movl   $0xd,-0x10(%rbp)
mov    -0x2c(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x1,%edx
mov    $0xffffffff,%ecx
xor    %r8d,%r8d
call   1030 <pwritev2@plt>
mov    %eax,-0x30(%rbp)
mov    -0x2c(%rbp),%edi
call   1040 <close@plt>
mov    -0x30(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
sub    $0x28,%rsp
movabs $0xa646c726f7720,%rax
mov    %rax,0xd(%rsp)
movabs $0x6f77206f6c6c6568,%rax
20 77 6f
mov    %rax,0x8(%rsp)
mov    0x2e7d(%rip),%rax
mov    (%rax),%esi
lea    0xea9(%rip),%rdi
call   1050 <open@plt>
test   %eax,%eax
js     11a5 <main+0x75>
mov    %eax,%ebx
lea    0x8(%rsp),%rax
mov    %rax,0x18(%rsp)
movl   $0xd,0x20(%rsp)
lea    0x18(%rsp),%rsi
mov    %ebx,%edi
mov    $0x1,%edx
mov    $0xffffffff,%ecx
xor    %r8d,%r8d
call   1030 <pwritev2@plt>
mov    %eax,%ebp
mov    %ebx,%edi
call   1040 <close@plt>
jmp    11aa <main+0x7a>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x28,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.iovec = type { i8*, i32 }
@__const.main.buffer = [13 x i8] c"hello world\0A\00"
@.str = [10 x i8] c"/dev/zero\00"
@O_WRONLY = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca %struct.iovec
%3 = alloca [13 x i8]
%4 = alloca i32
%5 = alloca i32
store i32 0, i32* %1
%6 = bitcast [13 x i8]* %3 to i8*
%7 = load i32, i32* @O_WRONLY
%8 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %7)
store i32 %8, i32* %4
%9 = load i32, i32* %4
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %12
11: ; preds = %0
store i32 -1, i32* %1
br label %21
12: ; preds = %0
%13 = getelementptr inbounds [13 x i8], [13 x i8]* %3, i64 0, i64 0
%14 = getelementptr inbounds %struct.iovec, %struct.iovec* %2, i32 0, i32 0
store i8* %13, i8** %14
%15 = getelementptr inbounds %struct.iovec, %struct.iovec* %2, i32 0, i32 1
store i32 13, i32* %15
%16 = load i32, i32* %4
%17 = call i32 @pwritev2(i32 %16, %struct.iovec* %2, i32 1, i32 -1, i32 0)
store i32 %17, i32* %5
%18 = load i32, i32* %4
%19 = call i32 @close(i32 %18)
%20 = load i32, i32* %5
store i32 %20, i32* %1
br label %21
21: ; preds = %12, %11
%22 = load i32, i32* %1
ret i32 %22
}

LLVM IR (-O2):
%struct.iovec = type { i8*, i32 }
@__const.main.buffer = [13 x i8] c"hello world\0A\00"
@.str = [10 x i8] c"/dev/zero\00"
@O_WRONLY = global i32 0
define i32 @main() {
%1 = alloca %struct.iovec
%2 = alloca [13 x i8]
%3 = bitcast %struct.iovec* %1 to i8*
%4 = getelementptr inbounds [13 x i8], [13 x i8]* %2, i64 0, i64 0
%5 = load i32, i32* @O_WRONLY
%6 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = icmp slt i32 %6, 0
br i1 %7, label %13, label %8
8: ; preds = %0
%9 = getelementptr inbounds %struct.iovec, %struct.iovec* %1, i64 0, i32 0
store i8* %4, i8** %9
%10 = getelementptr inbounds %struct.iovec, %struct.iovec* %1, i64 0, i32 1
store i32 13, i32* %10
%11 = call i32 @pwritev2(i32 %6, %struct.iovec* %1, i32 1, i32 -1, i32 0)
%12 = call i32 @close(i32 %6)
br label %13
13: ; preds = %0, %8
%14 = phi i32 [ %11, %8 ], [ -1, %0 ]
ret i32 %14
}