#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 readv(int, struct iovec *, int);
int readv_test(int fd, struct iovec *iov, int iovcnt) {
  int ret, save_errno;
  int passed;
  printf("readv(): fd=%d, iov=0x%p, iovcnt=%d\n", fd, iov, iovcnt);
  fflush(NULL);
  passed = handle_gez(ret = readv(fd, iov, iovcnt));
  save_errno = errno;
  switch (passed) {
  case -1:
  case 0:
    if (ret != -1) {
      printf("\t\tRead %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):
<readv_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   1030 <printf@plt>
mov    -0x28(%rbp),%eax
mov    %eax,%edi
call   1040 <fflush@plt>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1050 <readv@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     120f <readv_test+0xcf>
jmp    11b3 <readv_test+0x73>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
sub    $0x1,%eax
ja     122e <readv_test+0xee>
jmp    11c7 <readv_test+0x87>
cmpl   $0xffffffff,-0x18(%rbp)
je     11e2 <readv_test+0xa2>
mov    -0x18(%rbp),%esi
lea    0xe4a(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x18(%rbp),%esi
lea    0xe4b(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xe49(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1040 <fflush@plt>
jmp    1248 <readv_test+0x108>
mov    -0x18(%rbp),%esi
lea    0xe3e(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1040 <fflush@plt>
jmp    1248 <readv_test+0x108>
mov    -0x20(%rbp),%esi
lea    0xe54(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1040 <fflush@plt>
mov    -0x1c(%rbp),%ecx
mov    0x2d8e(%rip),%rax
mov    %ecx,(%rax)
mov    -0x18(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readv_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   1040 <printf@plt>
xor    %edi,%edi
call   1050 <fflush@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r14d,%edx
call   1060 <readv@plt>
mov    %eax,%ebx
mov    %eax,%edi
call   1070 <handle_gez@plt>
mov    0x2e4a(%rip),%r14
mov    (%r14),%ebp
lea    0x1(%rax),%ecx
cmp    $0x2,%ecx
jae    11d4 <readv_test+0x84>
cmp    $0xffffffff,%ebx
je     11b6 <readv_test+0x66>
lea    0xe78(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xe7a(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xed6(%rip),%rdi
call   1030 <puts@plt>
jmp    11f4 <readv_test+0xa4>
cmp    $0xfffffffe,%eax
jne    11e4 <readv_test+0x94>
lea    0xe63(%rip),%rdi
mov    %ebx,%esi
jmp    11ed <readv_test+0x9d>
lea    0xe8d(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %edi,%edi
call   1050 <fflush@plt>
mov    %ebp,(%r14)
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.iovec = type { i32 }
@.str = [37 x i8] c"readv(): fd=%d, iov=0x%p, iovcnt=%d\0A\00"
@errno = global i32 0
@.str.1 = [18 x i8] c"\09\09Read %d blocks\0A\00"
@.str.2 = [12 x i8] c"\09\09ret = %d\0A\00"
@.str.3 = [20 x i8] c"\09Succeeded Phase 1\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 @readv_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 ([37 x i8], [37 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 @readv(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 %37 [
i32 -1, label %22
i32 0, label %22
i32 -2, label %33
]
22: ; preds = %3, %3
%23 = load i32, i32* %7
%24 = icmp ne i32 %23, -1
br i1 %24, label %25, label %28
25: ; preds = %22
%26 = load i32, i32* %7
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i32 %26)
br label %28
28: ; preds = %25, %22
%29 = load i32, i32* %7
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i32 %29)
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0))
%32 = call i32 @fflush(i32* null)
br label %41
33: ; preds = %3
%34 = load i32, i32* %7
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.4, i64 0, i64 0), i32 %34)
%36 = call i32 @fflush(i32* null)
br label %41
37: ; preds = %3
%38 = load i32, i32* %9
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.5, i64 0, i64 0), i32 %38)
%40 = call i32 @fflush(i32* null)
br label %41
41: ; preds = %37, %33, %28
%42 = load i32, i32* %8
store i32 %42, i32* @errno
%43 = load i32, i32* %7
ret i32 %43
}

LLVM IR (-O2):
%struct.iovec = type { i32 }
@.str = [37 x i8] c"readv(): fd=%d, iov=0x%p, iovcnt=%d\0A\00"
@errno = global i32 0
@.str.1 = [18 x i8] c"\09\09Read %d blocks\0A\00"
@.str.2 = [12 x i8] c"\09\09ret = %d\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 = [19 x i8] c"\09Succeeded Phase 1\00"
define i32 @readv_test(i32 %0, %struct.iovec* %1, i32 %2) {
%4 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 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 @readv(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 %9
i32 -2, label %16
]
9: ; preds = %3, %3
%10 = icmp eq i32 %6, -1
br i1 %10, label %13, label %11
11: ; preds = %9
%12 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i32 %6)
br label %13
13: ; preds = %11, %9
%14 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i32 %6)
%15 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str, 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, %13
%21 = tail call i32 @fflush(i32* null)
store i32 %8, i32* @errno
ret i32 %6
}