#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 */

/* Variables and functions */
int errno;
int /*<<< orphan*/ fflush(int /*<<< orphan*/ *);
int full_write(int, char *, int /*<<< orphan*/);
int handle_gez(int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ strlen(char *);
int write_test(int fd, char *buf, size_t count) {
  int ret, save_errno;
  int passed;
  printf("write(): fd=%d, buf=[%.31s...], count=%d\n", fd, buf, count);
  fflush(NULL);
  passed = handle_gez(ret = full_write(fd, buf, strlen(buf)));
  save_errno = errno;
  switch (passed) {
  case -1:
  case 0:
    if (ret != -1) {
      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):
<write_test>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x4(%rbp),%esi
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rcx
lea    0xe8b(%rip),%rdi
xor    %eax,%eax
mov    %eax,-0x38(%rbp)
call   1040 <printf@plt>
mov    -0x38(%rbp),%eax
mov    %eax,%edi
call   1050 <fflush@plt>
mov    -0x4(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x10(%rbp),%rdi
mov    %rdi,-0x30(%rbp)
call   1030 <strlen@plt>
mov    -0x34(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    %eax,%edx
call   1060 <full_write@plt>
mov    %eax,%edi
mov    %edi,-0x1c(%rbp)
call   1070 <handle_gez@plt>
mov    %eax,-0x24(%rbp)
mov    0x2e22(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0x28(%rbp)
sub    $0xfffffffe,%eax
je     1233 <write_test+0xe3>
jmp    11d7 <write_test+0x87>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
sub    $0x1,%eax
ja     1252 <write_test+0x102>
jmp    11eb <write_test+0x9b>
cmpl   $0xffffffff,-0x1c(%rbp)
je     1217 <write_test+0xc7>
mov    -0x1c(%rbp),%esi
lea    0xe2b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x1c(%rbp),%esi
lea    0xe2d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe2b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1050 <fflush@plt>
jmp    126c <write_test+0x11c>
mov    -0x1c(%rbp),%esi
lea    0xe20(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1050 <fflush@plt>
jmp    126c <write_test+0x11c>
mov    -0x24(%rbp),%esi
lea    0xe36(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1050 <fflush@plt>
mov    -0x20(%rbp),%ecx
mov    0x2d6a(%rip),%rax
mov    %ecx,(%rax)
mov    -0x1c(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_test>:
push   %rbp
push   %r14
push   %rbx
mov    %rdx,%rcx
mov    %rsi,%rbx
mov    %edi,%ebp
lea    0xe8d(%rip),%rdi
mov    %ebp,%esi
mov    %rbx,%rdx
xor    %eax,%eax
call   1050 <printf@plt>
xor    %edi,%edi
call   1060 <fflush@plt>
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1070 <full_write@plt>
mov    %eax,%ebx
mov    %eax,%edi
call   1080 <handle_gez@plt>
mov    0x2e36(%rip),%r14
mov    (%r14),%ebp
lea    0x1(%rax),%ecx
cmp    $0x2,%ecx
jae    11e8 <write_test+0x88>
cmp    $0xffffffff,%ebx
je     11da <write_test+0x7a>
lea    0xe69(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe6c(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xec8(%rip),%rdi
call   1030 <puts@plt>
jmp    1208 <write_test+0xa8>
cmp    $0xfffffffe,%eax
jne    11f8 <write_test+0x98>
lea    0xe55(%rip),%rdi
mov    %ebx,%esi
jmp    1201 <write_test+0xa1>
lea    0xe7f(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %edi,%edi
call   1060 <fflush@plt>
mov    %ebp,(%r14)
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [42 x i8] c"write(): fd=%d, buf=[%.31s...], count=%d\0A\00"
@errno = global i32 0
@.str.1 = [19 x i8] c"\09\09Wrote %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 @write_test(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
store i64 %2, i64* %6
%10 = load i32, i32* %4
%11 = load i8*, i8** %5
%12 = load i64, i64* %6
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0), i32 %10, i8* %11, i64 %12)
%14 = call i32 @fflush(i32* null)
%15 = load i32, i32* %4
%16 = load i8*, i8** %5
%17 = load i8*, i8** %5
%18 = call i32 @strlen(i8* %17)
%19 = call i32 @full_write(i32 %15, i8* %16, i32 %18)
store i32 %19, i32* %7
%20 = call i32 @handle_gez(i32 %19)
store i32 %20, i32* %9
%21 = load i32, i32* @errno
store i32 %21, i32* %8
%22 = load i32, i32* %9
switch i32 %22, label %38 [
i32 -1, label %23
i32 0, label %23
i32 -2, label %34
]
23: ; preds = %3, %3
%24 = load i32, i32* %7
%25 = icmp ne i32 %24, -1
br i1 %25, label %26, label %31
26: ; preds = %23
%27 = load i32, i32* %7
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i32 %27)
%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)
br label %31
31: ; preds = %26, %23
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, 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, %31
%43 = load i32, i32* %8
store i32 %43, i32* @errno
%44 = load i32, i32* %7
ret i32 %44
}

LLVM IR (-O2):
@.str = [42 x i8] c"write(): fd=%d, buf=[%.31s...], count=%d\0A\00"
@errno = global i32 0
@.str.1 = [19 x i8] c"\09\09Wrote %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 @write_test(i32 %0, i8* %1, i64 %2) {
%4 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0), i32 %0, i8* %1, i64 %2)
%5 = tail call i32 @fflush(i32* null)
%6 = tail call i32 @strlen(i8* %1)
%7 = tail call i32 @full_write(i32 %0, i8* %1, i32 %6)
%8 = tail call i32 @handle_gez(i32 %7)
%9 = load i32, i32* @errno
switch i32 %8, label %19 [
i32 -1, label %10
i32 0, label %10
i32 -2, label %17
]
10: ; preds = %3, %3
%11 = icmp eq i32 %7, -1
br i1 %11, label %15, label %12
12: ; preds = %10
%13 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i32 %7)
%14 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i32 %7)
br label %15
15: ; preds = %12, %10
%16 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str, i64 0, i64 0))
br label %21
17: ; preds = %3
%18 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([53 x i8], [53 x i8]* @.str.4, i64 0, i64 0), i32 %7)
br label %21
19: ; preds = %3
%20 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([43 x i8], [43 x i8]* @.str.5, i64 0, i64 0), i32 %8)
br label %21
21: ; preds = %19, %17, %15
%22 = tail call i32 @fflush(i32* null)
store i32 %9, i32* @errno
ret i32 %7
}