#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 unsigned int ssize_t;

/* Variables and functions */
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, unsigned int,
                           int /*<<< orphan*/);
int /*<<< orphan*/ free(unsigned char *);
unsigned char getCharToSend();
unsigned char *malloc(unsigned int);
int /*<<< orphan*/ memset(unsigned char *, int /*<<< orphan*/, unsigned int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ tcdrain(int);
unsigned int write(int, unsigned char *, unsigned int);
int testBlockW(int fd, unsigned int nbytes) {
  int status = 0;
  unsigned int i = 0U;
  unsigned char *bytesW = malloc(nbytes);
  (void)memset(bytesW, 0, nbytes);
  while (i < nbytes) {
    bytesW[i++] = getCharToSend();
  }
  ssize_t wlen = write(fd, bytesW, nbytes);
  if (wlen != nbytes) {
    fprintf(stderr, "Error from write: %ld, %d\n", wlen, errno);
    status = 1;
  }
  tcdrain(fd);
  free(bytesW);
  return status;
}


x86 assembly (clang, -O0):
<testBlockW>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
mov    -0x8(%rbp),%edi
call   1080 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%edx
xor    %esi,%esi
call   1060 <memset@plt>
mov    -0x10(%rbp),%eax
cmp    -0x8(%rbp),%eax
jae    11d4 <testBlockW+0x64>
mov    $0x0,%al
call   1090 <getCharToSend@plt>
mov    %al,%dl
mov    -0x18(%rbp),%rax
mov    -0x10(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x10(%rbp)
mov    %ecx,%ecx
mov    %dl,(%rax,%rcx,1)
jmp    11a6 <testBlockW+0x36>
mov    -0x4(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x8(%rbp),%edx
call   1050 <write@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x8(%rbp),%eax
je     121a <testBlockW+0xaa>
mov    0x2de7(%rip),%rax
mov    (%rax),%edi
mov    -0x1c(%rbp),%edx
mov    0x2dd3(%rip),%rax
mov    (%rax),%ecx
lea    0xdf2(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0x1,-0xc(%rbp)
mov    -0x4(%rbp),%edi
call   1040 <tcdrain@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0xc(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<testBlockW>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %esi,%r12d
mov    %edi,%r15d
mov    %esi,%edi
call   1080 <malloc@plt>
mov    %rax,%r13
xor    %r14d,%r14d
mov    %rax,%rdi
xor    %esi,%esi
mov    %r12d,%edx
call   1060 <memset@plt>
test   %r12d,%r12d
je     11c5 <testBlockW+0x55>
mov    %r12d,%ebp
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nop
xor    %eax,%eax
call   1090 <getCharToSend@plt>
mov    %al,0x0(%r13,%rbx,1)
add    $0x1,%rbx
cmp    %rbx,%rbp
jne    11b0 <testBlockW+0x40>
mov    %r15d,%edi
mov    %r13,%rsi
mov    %r12d,%edx
call   1050 <write@plt>
cmp    %r12d,%eax
je     11fe <testBlockW+0x8e>
mov    0x2e01(%rip),%rcx
mov    (%rcx),%edi
mov    0x2df0(%rip),%rcx
mov    (%rcx),%ecx
lea    0xe0f(%rip),%rsi
mov    %eax,%edx
call   1070 <fprintf@plt>
mov    $0x1,%r14d
mov    %r15d,%edi
call   1040 <tcdrain@plt>
mov    %r13,%rdi
call   1030 <free@plt>
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [27 x i8] c"Error from write: %ld, %d\0A\00"
@errno = global i32 0
define i32 @testBlockW(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
store i32 %0, i32* %3
store i32 %1, i32* %4
store i32 0, i32* %5
store i32 0, i32* %6
%9 = load i32, i32* %4
%10 = call i8* @malloc(i32 %9)
store i8* %10, i8** %7
%11 = load i8*, i8** %7
%12 = load i32, i32* %4
%13 = call i32 @memset(i8* %11, i32 0, i32 %12)
br label %14
14: ; preds = %18, %2
%15 = load i32, i32* %6
%16 = load i32, i32* %4
%17 = icmp ult i32 %15, %16
br i1 %17, label %18, label %25
18: ; preds = %14
%19 = call zeroext i8 (...) @getCharToSend()
%20 = load i8*, i8** %7
%21 = load i32, i32* %6
%22 = add i32 %21, 1
store i32 %22, i32* %6
%23 = zext i32 %21 to i64
%24 = getelementptr inbounds i8, i8* %20, i64 %23
store i8 %19, i8* %24
br label %14
25: ; preds = %14
%26 = load i32, i32* %3
%27 = load i8*, i8** %7
%28 = load i32, i32* %4
%29 = call i32 @write(i32 %26, i8* %27, i32 %28)
store i32 %29, i32* %8
%30 = load i32, i32* %8
%31 = load i32, i32* %4
%32 = icmp ne i32 %30, %31
br i1 %32, label %33, label %38
33: ; preds = %25
%34 = load i32, i32* @stderr
%35 = load i32, i32* %8
%36 = load i32, i32* @errno
%37 = call i32 @fprintf(i32 %34, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i32 %35, i32 %36)
store i32 1, i32* %5
br label %38
38: ; preds = %33, %25
%39 = load i32, i32* %3
%40 = call i32 @tcdrain(i32 %39)
%41 = load i8*, i8** %7
%42 = call i32 @free(i8* %41)
%43 = load i32, i32* %5
ret i32 %43
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [27 x i8] c"Error from write: %ld, %d\0A\00"
@errno = global i32 0
define i32 @testBlockW(i32 %0, i32 %1) {
%3 = tail call i8* @malloc(i32 %1)
%4 = tail call i32 @memset(i8* %3, i32 0, i32 %1)
%5 = icmp eq i32 %1, 0
br i1 %5, label %14, label %6
6: ; preds = %2
%7 = zext i32 %1 to i64
br label %8
8: ; preds = %6, %8
%9 = phi i64 [ 0, %6 ], [ %11, %8 ]
%10 = tail call zeroext i8 (...) @getCharToSend()
%11 = add nuw nsw i64 %9, 1
%12 = getelementptr inbounds i8, i8* %3, i64 %9
store i8 %10, i8* %12
%13 = icmp eq i64 %11, %7
br i1 %13, label %14, label %8
14: ; preds = %8, %2
%15 = tail call i32 @write(i32 %0, i8* %3, i32 %1)
%16 = icmp eq i32 %15, %1
br i1 %16, label %21, label %17
17: ; preds = %14
%18 = load i32, i32* @stderr
%19 = load i32, i32* @errno
%20 = tail call i32 @fprintf(i32 %18, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i32 %15, i32 %19)
br label %21
21: ; preds = %17, %14
%22 = phi i32 [ 1, %17 ], [ 0, %14 ]
%23 = tail call i32 @tcdrain(i32 %0)
%24 = tail call i32 @free(i8* %3)
ret i32 %22
}