#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 dummy;
};

/* Variables and functions */
int errno;
int /*<<< orphan*/ fflush(int /*<<< orphan*/ *);
int handle_gez(int);
int /*<<< orphan*/ printf(char *, ...);
int writev(int, struct iovec *, int);
int writev_test(int fd, struct iovec *iov, int iovcnt) {
  int ret, save_errno;
  int passed;
  printf("writev(): fd=%d, iov=0x%p, iovcnt=%d\n", fd, iov, iovcnt);
  fflush(NULL);
  passed = handle_gez(ret = writev(fd, iov, iovcnt));
  save_errno = errno;
  switch (passed) {
  case -1:
    printf("\t\tret = %d\n", ret);
    printf("\tSucceeded Phase 1\n");
    fflush(NULL);
    break;
  case 0:
    printf("\t\tWrote %d blocks\n", ret);
    printf("\t\tret = %d\n", ret);
    printf("\tSucceeded Phase 1\n");
    fflush(NULL);
    break;
  case -2:
    printf("\tFailed Phase 1: returned undefined value! "
           "ret = %d\n",
           ret);
    fflush(NULL);
    break;
  default:
    printf("Internal syscalltester error: passed = %d\n", passed);
    fflush(NULL);
    break;
  }
  errno = save_errno;
  return ret;
}


x86 assembly (clang, -O0):
<writev_test>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x4(%rbp),%esi
mov    -0x10(%rbp),%rdx
mov    -0x14(%rbp),%ecx
lea    0xe9d(%rip),%rdi
xor    %eax,%eax
mov    %eax,-0x28(%rbp)
call   1040 <printf@plt>
mov    -0x28(%rbp),%eax
mov    %eax,%edi
call   1050 <fflush@plt>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <writev@plt>
mov    %eax,%edi
mov    %edi,-0x18(%rbp)
call   1060 <handle_gez@plt>
mov    %eax,-0x20(%rbp)
mov    0x2e46(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x20(%rbp),%eax
mov    %eax,-0x24(%rbp)
sub    $0xfffffffe,%eax
je     123f <writev_test+0xff>
jmp    11b3 <writev_test+0x73>
mov    -0x24(%rbp),%eax
sub    $0xffffffff,%eax
je     11d4 <writev_test+0x94>
jmp    11c4 <writev_test+0x84>
mov    -0x24(%rbp),%eax
test   %eax,%eax
je     1201 <writev_test+0xc1>
jmp    125e <writev_test+0x11e>
mov    -0x18(%rbp),%esi
lea    0xe48(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe46(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1050 <fflush@plt>
jmp    1278 <writev_test+0x138>
mov    -0x18(%rbp),%esi
lea    0xe3b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%esi
lea    0xe0a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe08(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1050 <fflush@plt>
jmp    1278 <writev_test+0x138>
mov    -0x18(%rbp),%esi
lea    0xe10(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1050 <fflush@plt>
jmp    1278 <writev_test+0x138>
mov    -0x20(%rbp),%esi
lea    0xe26(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1050 <fflush@plt>
mov    -0x1c(%rbp),%ecx
mov    0x2d5e(%rip),%rax
mov    %ecx,(%rax)
mov    -0x18(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<writev_test>:
push   %rbp
push   %r14
push   %rbx
mov    %edx,%r14d
mov    %rsi,%rbx
mov    %edi,%ebp
lea    0xe9d(%rip),%rdi
mov    %ebp,%esi
mov    %rbx,%rdx
mov    %r14d,%ecx
xor    %eax,%eax
call   1050 <printf@plt>
xor    %edi,%edi
call   1060 <fflush@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r14d,%edx
call   1030 <writev@plt>
mov    %eax,%ebx
mov    %eax,%edi
call   1070 <handle_gez@plt>
mov    0x2e4a(%rip),%r14
mov    (%r14),%ebp
cmp    $0xfffffffe,%eax
je     11b2 <writev_test+0x62>
test   %eax,%eax
je     11c4 <writev_test+0x74>
cmp    $0xffffffff,%eax
je     11d4 <writev_test+0x84>
lea    0xecc(%rip),%rdi
mov    %eax,%esi
jmp    11bb <writev_test+0x6b>
lea    0xe8c(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    11f0 <writev_test+0xa0>
lea    0xe67(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe4b(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xeba(%rip),%rdi
call   1040 <puts@plt>
xor    %edi,%edi
call   1060 <fflush@plt>
mov    %ebp,(%r14)
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.iovec = type { i32 }
@.str = [38 x i8] c"writev(): fd=%d, iov=0x%p, iovcnt=%d\0A\00"
@errno = global i32 0
@.str.1 = [12 x i8] c"\09\09ret = %d\0A\00"
@.str.2 = [20 x i8] c"\09Succeeded Phase 1\0A\00"
@.str.3 = [19 x i8] c"\09\09Wrote %d blocks\0A\00"
@.str.4 = [53 x i8] c"\09Failed Phase 1: returned undefined value! ret = %d\0A\00"
@.str.5 = [43 x i8] c"Internal syscalltester error: passed = %d\0A\00"
define i32 @writev_test(i32 %0, %struct.iovec* %1, i32 %2) {
%4 = alloca i32
%5 = alloca %struct.iovec*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %4
store %struct.iovec* %1, %struct.iovec** %5
store i32 %2, i32* %6
%10 = load i32, i32* %4
%11 = load %struct.iovec*, %struct.iovec** %5
%12 = load i32, i32* %6
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0), i32 %10, %struct.iovec* %11, i32 %12)
%14 = call i32 @fflush(i32* null)
%15 = load i32, i32* %4
%16 = load %struct.iovec*, %struct.iovec** %5
%17 = load i32, i32* %6
%18 = call i32 @writev(i32 %15, %struct.iovec* %16, i32 %17)
store i32 %18, i32* %7
%19 = call i32 @handle_gez(i32 %18)
store i32 %19, i32* %9
%20 = load i32, i32* @errno
store i32 %20, i32* %8
%21 = load i32, i32* %9
switch i32 %21, label %38 [
i32 -1, label %22
i32 0, label %27
i32 -2, label %34
]
22: ; preds = %3
%23 = load i32, i32* %7
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %23)
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
%26 = call i32 @fflush(i32* null)
br label %42
27: ; preds = %3
%28 = load i32, i32* %7
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i32 %28)
%30 = load i32, i32* %7
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %30)
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
%33 = call i32 @fflush(i32* null)
br label %42
34: ; preds = %3
%35 = load i32, i32* %7
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.4, i64 0, i64 0), i32 %35)
%37 = call i32 @fflush(i32* null)
br label %42
38: ; preds = %3
%39 = load i32, i32* %9
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.5, i64 0, i64 0), i32 %39)
%41 = call i32 @fflush(i32* null)
br label %42
42: ; preds = %38, %34, %27, %22
%43 = load i32, i32* %8
store i32 %43, i32* @errno
%44 = load i32, i32* %7
ret i32 %44
}

LLVM IR (-O2):
%struct.iovec = type { i32 }
@.str = [38 x i8] c"writev(): fd=%d, iov=0x%p, iovcnt=%d\0A\00"
@errno = global i32 0
@.str.1 = [12 x i8] c"\09\09ret = %d\0A\00"
@.str.3 = [19 x i8] c"\09\09Wrote %d blocks\0A\00"
@.str.4 = [53 x i8] c"\09Failed Phase 1: returned undefined value! ret = %d\0A\00"
@.str.5 = [43 x i8] c"Internal syscalltester error: passed = %d\0A\00"
@str.6 = [19 x i8] c"\09Succeeded Phase 1\00"
define i32 @writev_test(i32 %0, %struct.iovec* %1, i32 %2) {
%4 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @.str, i64 0, i64 0), i32 %0, %struct.iovec* %1, i32 %2)
%5 = tail call i32 @fflush(i32* null)
%6 = tail call i32 @writev(i32 %0, %struct.iovec* %1, i32 %2)
%7 = tail call i32 @handle_gez(i32 %6)
%8 = load i32, i32* @errno
switch i32 %7, label %18 [
i32 -1, label %9
i32 0, label %12
i32 -2, label %16
]
9: ; preds = %3
%10 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %6)
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str.6, i64 0, i64 0))
br label %20
12: ; preds = %3
%13 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0), i32 %6)
%14 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %6)
%15 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str.6, i64 0, i64 0))
br label %20
16: ; preds = %3
%17 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([53 x i8], [53 x i8]* @.str.4, i64 0, i64 0), i32 %6)
br label %20
18: ; preds = %3
%19 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([43 x i8], [43 x i8]* @.str.5, i64 0, i64 0), i32 %7)
br label %20
20: ; preds = %18, %16, %12, %9
%21 = tail call i32 @fflush(i32* null)
store i32 %8, i32* @errno
ret i32 %6
}