#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 */
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int errno;
int /*<<< orphan*/ ferror(int /*<<< orphan*/ *);
int /*<<< orphan*/ fflush(int /*<<< orphan*/ *);
int full_fwrite(char *, size_t, size_t, int /*<<< orphan*/ *);
int handle_gez(int);
int /*<<< orphan*/ printf(char *, ...);
int fwrite_test(char *ptr, size_t size, size_t nitems, FILE *stream) {
  int ret, save_errno;
  int passed;
  printf("fwrite(): ptr=[%.31s...], fp=0x%p\n", (char *)ptr, stream);
  fflush(NULL);
  passed = handle_gez(ret = full_fwrite(ptr, size, nitems, stream));
  save_errno = errno;
  switch (passed) {
  case -1:
    printf("\tFailed Phase 1: returned undefined value! "
           "ret = %d\n",
           ret);
    fflush(NULL);
    break;
  case 0:
    if ((ret == 0) && ferror(stream)) {
    }
    if (!ferror(stream)) {
      printf("\t\tWrote %d blocks\n", ret);
    } else {
      printf("\t\tHit end of file.\n");
    }
    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):
<fwrite_test>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x8(%rbp),%rsi
mov    -0x20(%rbp),%rdx
lea    0xe89(%rip),%rdi
xor    %eax,%eax
mov    %eax,-0x34(%rbp)
call   1040 <printf@plt>
mov    -0x34(%rbp),%eax
mov    %eax,%edi
call   1050 <fflush@plt>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rcx
call   1060 <full_fwrite@plt>
mov    %eax,%edi
mov    %edi,-0x24(%rbp)
call   1070 <handle_gez@plt>
mov    %eax,-0x2c(%rbp)
mov    0x2e2c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x2c(%rbp),%eax
mov    %eax,-0x30(%rbp)
sub    $0xfffffffe,%eax
je     1280 <fwrite_test+0x130>
jmp    11cd <fwrite_test+0x7d>
mov    -0x30(%rbp),%eax
sub    $0xffffffff,%eax
je     11ee <fwrite_test+0x9e>
jmp    11de <fwrite_test+0x8e>
mov    -0x30(%rbp),%eax
test   %eax,%eax
je     120d <fwrite_test+0xbd>
jmp    129f <fwrite_test+0x14f>
mov    -0x24(%rbp),%esi
lea    0xe2b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1050 <fflush@plt>
jmp    12b9 <fwrite_test+0x169>
cmpl   $0x0,-0x24(%rbp)
jne    122e <fwrite_test+0xde>
mov    -0x20(%rbp),%rdi
call   1030 <ferror@plt>
cmp    $0x0,%eax
je     122e <fwrite_test+0xde>
jmp    122e <fwrite_test+0xde>
mov    -0x20(%rbp),%rdi
call   1030 <ferror@plt>
cmp    $0x0,%eax
jne    1256 <fwrite_test+0x106>
mov    -0x24(%rbp),%esi
lea    0xe0e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1264 <fwrite_test+0x114>
lea    0xe0e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xe14(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1050 <fflush@plt>
jmp    12b9 <fwrite_test+0x169>
mov    -0x24(%rbp),%esi
lea    0xd99(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1050 <fflush@plt>
jmp    12b9 <fwrite_test+0x169>
mov    -0x2c(%rbp),%esi
lea    0xdea(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1050 <fflush@plt>
mov    -0x28(%rbp),%ecx
mov    0x2d1d(%rip),%rax
mov    %ecx,(%rax)
mov    -0x24(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fwrite_test>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rcx,%r14
mov    %rdx,%r15
mov    %rsi,%rbx
mov    %rdi,%rbp
lea    0xe86(%rip),%rdi
mov    %rbp,%rsi
mov    %rcx,%rdx
xor    %eax,%eax
call   1050 <printf@plt>
xor    %edi,%edi
call   1060 <fflush@plt>
mov    %rbp,%rdi
mov    %rbx,%rsi
mov    %r15,%rdx
mov    %r14,%rcx
call   1070 <full_fwrite@plt>
mov    %eax,%ebp
mov    %eax,%edi
call   1080 <handle_gez@plt>
mov    0x2e31(%rip),%r15
mov    (%r15),%ebx
cmp    $0xfffffffe,%eax
je     11c0 <fwrite_test+0x60>
test   %eax,%eax
je     11cb <fwrite_test+0x6b>
cmp    $0xffffffff,%eax
jne    11f1 <fwrite_test+0x91>
lea    0xe5c(%rip),%rdi
mov    %ebp,%esi
jmp    11fa <fwrite_test+0x9a>
test   %ebp,%ebp
jne    11d7 <fwrite_test+0x77>
mov    %r14,%rdi
call   1040 <ferror@plt>
mov    %r14,%rdi
call   1040 <ferror@plt>
test   %eax,%eax
je     1203 <fwrite_test+0xa3>
lea    0xebf(%rip),%rdi
call   1030 <puts@plt>
jmp    1213 <fwrite_test+0xb3>
lea    0xe73(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    121f <fwrite_test+0xbf>
lea    0xe4e(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe7c(%rip),%rdi
call   1030 <puts@plt>
xor    %edi,%edi
call   1060 <fflush@plt>
mov    %ebx,(%r15)
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [35 x i8] c"fwrite(): ptr=[%.31s...], fp=0x%p\0A\00"
@errno = global i32 0
@.str.1 = [53 x i8] c"\09Failed Phase 1: returned undefined value! ret = %d\0A\00"
@.str.2 = [19 x i8] c"\09\09Wrote %d blocks\0A\00"
@.str.3 = [20 x i8] c"\09\09Hit end of file.\0A\00"
@.str.4 = [20 x i8] c"\09Succeeded Phase 1\0A\00"
@.str.5 = [43 x i8] c"Internal syscalltester error: passed = %d\0A\00"
define i32 @fwrite_test(i8* %0, i64 %1, i64 %2, i32* %3) {
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64
%8 = alloca i32*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i8* %0, i8** %5
store i64 %1, i64* %6
store i64 %2, i64* %7
store i32* %3, i32** %8
%12 = load i8*, i8** %5
%13 = load i32*, i32** %8
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i8* %12, i32* %13)
%15 = call i32 @fflush(i32* null)
%16 = load i8*, i8** %5
%17 = load i64, i64* %6
%18 = load i64, i64* %7
%19 = load i32*, i32** %8
%20 = call i32 @full_fwrite(i8* %16, i64 %17, i64 %18, i32* %19)
store i32 %20, i32* %9
%21 = call i32 @handle_gez(i32 %20)
store i32 %21, i32* %11
%22 = load i32, i32* @errno
store i32 %22, i32* %10
%23 = load i32, i32* %11
switch i32 %23, label %52 [
i32 -1, label %24
i32 0, label %28
i32 -2, label %48
]
24: ; preds = %4
%25 = load i32, i32* %9
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.1, i64 0, i64 0), i32 %25)
%27 = call i32 @fflush(i32* null)
br label %56
28: ; preds = %4
%29 = load i32, i32* %9
%30 = icmp eq i32 %29, 0
br i1 %30, label %31, label %36
31: ; preds = %28
%32 = load i32*, i32** %8
%33 = call i32 @ferror(i32* %32)
%34 = icmp ne i32 %33, 0
br i1 %34, label %35, label %36
35: ; preds = %31
br label %36
36: ; preds = %35, %31, %28
%37 = load i32*, i32** %8
%38 = call i32 @ferror(i32* %37)
%39 = icmp ne i32 %38, 0
br i1 %39, label %43, label %40
40: ; preds = %36
%41 = load i32, i32* %9
%42 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i32 %41)
br label %45
43: ; preds = %36
%44 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0))
br label %45
45: ; preds = %43, %40
%46 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0))
%47 = call i32 @fflush(i32* null)
br label %56
48: ; preds = %4
%49 = load i32, i32* %9
%50 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str.1, i64 0, i64 0), i32 %49)
%51 = call i32 @fflush(i32* null)
br label %56
52: ; preds = %4
%53 = load i32, i32* %11
%54 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.5, i64 0, i64 0), i32 %53)
%55 = call i32 @fflush(i32* null)
br label %56
56: ; preds = %52, %48, %45, %24
%57 = load i32, i32* %10
store i32 %57, i32* @errno
%58 = load i32, i32* %9
ret i32 %58
}

LLVM IR (-O2):
@.str = [35 x i8] c"fwrite(): ptr=[%.31s...], fp=0x%p\0A\00"
@errno = global i32 0
@.str.1 = [53 x i8] c"\09Failed Phase 1: returned undefined value! ret = %d\0A\00"
@.str.2 = [19 x i8] c"\09\09Wrote %d blocks\0A\00"
@.str.5 = [43 x i8] c"Internal syscalltester error: passed = %d\0A\00"
@str = [19 x i8] c"\09Succeeded Phase 1\00"
@str.6 = [19 x i8] c"\09\09Hit end of file.\00"
define i32 @fwrite_test(i8* %0, i64 %1, i64 %2, i32* %3) {
%5 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0), i8* %0, i32* %3)
%6 = tail call i32 @fflush(i32* null)
%7 = tail call i32 @full_fwrite(i8* %0, i64 %1, i64 %2, i32* %3)
%8 = tail call i32 @handle_gez(i32 %7)
%9 = load i32, i32* @errno
switch i32 %8, label %27 [
i32 -1, label %10
i32 0, label %12
i32 -2, label %25
]
10: ; preds = %4
%11 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([53 x i8], [53 x i8]* @.str.1, i64 0, i64 0), i32 %7)
br label %29
12: ; preds = %4
%13 = icmp eq i32 %7, 0
br i1 %13, label %14, label %16
14: ; preds = %12
%15 = tail call i32 @ferror(i32* %3)
br label %16
16: ; preds = %14, %12
%17 = tail call i32 @ferror(i32* %3)
%18 = icmp eq i32 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %16
%20 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i32 %7)
br label %23
21: ; preds = %16
%22 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str.6, i64 0, i64 0))
br label %23
23: ; preds = %21, %19
%24 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str, i64 0, i64 0))
br label %29
25: ; preds = %4
%26 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([53 x i8], [53 x i8]* @.str.1, i64 0, i64 0), i32 %7)
br label %29
27: ; preds = %4
%28 = 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 %29
29: ; preds = %27, %25, %23, %10
%30 = tail call i32 @fflush(i32* null)
store i32 %9, i32* @errno
ret i32 %7
}