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

/* Variables and functions */
int errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ printf(char *, int);
int writev(int, struct iovec *, int);
void test_writev(int fd, char const *buf1, int len1, char const *buf2,
                 int len2) {
  struct iovec block[2];
  block[0].iov_base = buf1;
  block[0].iov_len = len1;
  block[1].iov_base = buf2;
  block[1].iov_len = len2;
  int ret = writev(fd, block, 2);
  if (ret == -1) {
    printf("fatal error while write, errno %d\n", errno);
    exit(-1);
  }
}


x86 assembly (clang, -O0):
<test_writev>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8d,-0x24(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x48(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0x38(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x50(%rbp),%rsi
mov    $0x2,%edx
call   1030 <writev@plt>
mov    %eax,-0x54(%rbp)
cmpl   $0xffffffff,-0x54(%rbp)
jne    11a3 <test_writev+0x73>
mov    0x2e55(%rip),%rax
mov    (%rax),%esi
lea    0xe6c(%rip),%rdi
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   1050 <exit@plt>
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_writev>:
sub    $0x28,%rsp
mov    %rsi,(%rsp)
mov    %edx,0x8(%rsp)
mov    %rcx,0x10(%rsp)
mov    %r8d,0x18(%rsp)
mov    %rsp,%rsi
mov    $0x2,%edx
call   1030 <writev@plt>
cmp    $0xffffffff,%eax
je     115d <test_writev+0x2d>
add    $0x28,%rsp
ret
mov    0x2e7c(%rip),%rax
mov    (%rax),%esi
lea    0xe93(%rip),%rdi
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   1050 <exit@plt>

LLVM IR (-O0):
%struct.iovec = type { i8*, i32 }
@.str = [35 x i8] c"fatal error while write, errno %d\0A\00"
@errno = global i32 0
define void @test_writev(i32 %0, i8* %1, i32 %2, i8* %3, i32 %4) {
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i32
%11 = alloca [2 x %struct.iovec]
%12 = alloca i32
store i32 %0, i32* %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store i8* %3, i8** %9
store i32 %4, i32* %10
%13 = load i8*, i8** %7
%14 = getelementptr inbounds [2 x %struct.iovec], [2 x %struct.iovec]* %11, i64 0, i64 0
%15 = getelementptr inbounds %struct.iovec, %struct.iovec* %14, i32 0, i32 0
store i8* %13, i8** %15
%16 = load i32, i32* %8
%17 = getelementptr inbounds [2 x %struct.iovec], [2 x %struct.iovec]* %11, i64 0, i64 0
%18 = getelementptr inbounds %struct.iovec, %struct.iovec* %17, i32 0, i32 1
store i32 %16, i32* %18
%19 = load i8*, i8** %9
%20 = getelementptr inbounds [2 x %struct.iovec], [2 x %struct.iovec]* %11, i64 0, i64 1
%21 = getelementptr inbounds %struct.iovec, %struct.iovec* %20, i32 0, i32 0
store i8* %19, i8** %21
%22 = load i32, i32* %10
%23 = getelementptr inbounds [2 x %struct.iovec], [2 x %struct.iovec]* %11, i64 0, i64 1
%24 = getelementptr inbounds %struct.iovec, %struct.iovec* %23, i32 0, i32 1
store i32 %22, i32* %24
%25 = load i32, i32* %6
%26 = getelementptr inbounds [2 x %struct.iovec], [2 x %struct.iovec]* %11, i64 0, i64 0
%27 = call i32 @writev(i32 %25, %struct.iovec* %26, i32 2)
store i32 %27, i32* %12
%28 = load i32, i32* %12
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %34
30: ; preds = %5
%31 = load i32, i32* @errno
%32 = call i32 @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i32 %31)
%33 = call i32 @exit(i32 -1)
unreachable
34: ; preds = %5
ret void
}

LLVM IR (-O2):
%struct.iovec = type { i8*, i32 }
@.str = [35 x i8] c"fatal error while write, errno %d\0A\00"
@errno = global i32 0
define void @test_writev(i32 %0, i8* %1, i32 %2, i8* %3, i32 %4) {
%6 = alloca [2 x %struct.iovec]
%7 = bitcast [2 x %struct.iovec]* %6 to i8*
%8 = getelementptr inbounds [2 x %struct.iovec], [2 x %struct.iovec]* %6, i64 0, i64 0
%9 = getelementptr inbounds [2 x %struct.iovec], [2 x %struct.iovec]* %6, i64 0, i64 0, i32 0
store i8* %1, i8** %9
%10 = getelementptr inbounds [2 x %struct.iovec], [2 x %struct.iovec]* %6, i64 0, i64 0, i32 1
store i32 %2, i32* %10
%11 = getelementptr inbounds [2 x %struct.iovec], [2 x %struct.iovec]* %6, i64 0, i64 1, i32 0
store i8* %3, i8** %11
%12 = getelementptr inbounds [2 x %struct.iovec], [2 x %struct.iovec]* %6, i64 0, i64 1, i32 1
store i32 %4, i32* %12
%13 = call i32 @writev(i32 %0, %struct.iovec* %8, i32 2)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %19
15: ; preds = %5
%16 = load i32, i32* @errno
%17 = call i32 @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i32 %16)
%18 = call i32 @exit(i32 -1)
unreachable
19: ; preds = %5
ret void
}