#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*/ memset(char *, char, int);
int /*<<< orphan*/ printf(char *, int, int);
int write(int, char *, int);
int main(void) {
  char buf[204800];
  char *ptr;
  int nwrite, ntowrite = 204800;
  memset(buf, 'c', 204800);
  ptr = buf;
  while (ntowrite > 0) {
    errno = 0;
    nwrite = write(1, ptr, ntowrite);
    printf("Wrote = %d, errno = %d\n", nwrite, errno);
    if (nwrite > 0) {
      ptr += nwrite;
      ntowrite -= nwrite;
    }
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x32020,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x32000,-0x32020(%rbp)
lea    -0x32010(%rbp),%rdi
mov    $0x63,%esi
mov    $0x32000,%edx
call   1050 <memset@plt>
lea    -0x32010(%rbp),%rax
mov    %rax,-0x32018(%rbp)
cmpl   $0x0,-0x32020(%rbp)
jle    1202 <main+0xd2>
mov    0x2e5c(%rip),%rax
movl   $0x0,(%rax)
mov    -0x32018(%rbp),%rsi
mov    -0x32020(%rbp),%edx
mov    $0x1,%edi
call   1030 <write@plt>
mov    %eax,-0x3201c(%rbp)
mov    -0x3201c(%rbp),%esi
mov    0x2e2c(%rip),%rax
mov    (%rax),%edx
lea    0xe43(%rip),%rdi
call   1040 <printf@plt>
cmpl   $0x0,-0x3201c(%rbp)
jle    11fd <main+0xcd>
mov    -0x3201c(%rbp),%ecx
mov    -0x32018(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x32018(%rbp)
mov    -0x3201c(%rbp),%ecx
mov    -0x32020(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x32020(%rbp)
jmp    1170 <main+0x40>
mov    -0x4(%rbp),%eax
add    $0x32020,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x32008,%rsp
mov    %rsp,%r15
mov    $0x32000,%ebp
mov    %r15,%rdi
mov    $0x63,%esi
mov    $0x32000,%edx
call   1050 <memset@plt>
mov    0x2e7e(%rip),%r12
lea    0xe97(%rip),%r14
xor    %r13d,%r13d
nopl   0x0(%rax)
movl   $0x0,(%r12)
mov    $0x1,%edi
mov    %r15,%rsi
mov    %ebp,%edx
call   1030 <write@plt>
mov    %eax,%ebx
mov    (%r12),%edx
mov    %r14,%rdi
mov    %eax,%esi
call   1040 <printf@plt>
test   %ebx,%ebx
cmovs  %r13d,%ebx
add    %rbx,%r15
sub    %ebx,%ebp
jg     1170 <main+0x40>
xor    %eax,%eax
add    $0x32008,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i32 0
@.str = [24 x i8] c"Wrote = %d, errno = %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [204800 x i8]
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
store i32 0, i32* %1
store i32 204800, i32* %5
%6 = getelementptr inbounds [204800 x i8], [204800 x i8]* %2, i64 0, i64 0
%7 = call i32 @memset(i8* %6, i8 signext 99, i32 204800)
%8 = getelementptr inbounds [204800 x i8], [204800 x i8]* %2, i64 0, i64 0
store i8* %8, i8** %3
br label %9
9: ; preds = %29, %0
%10 = load i32, i32* %5
%11 = icmp sgt i32 %10, 0
br i1 %11, label %12, label %30
12: ; preds = %9
store i32 0, i32* @errno
%13 = load i8*, i8** %3
%14 = load i32, i32* %5
%15 = call i32 @write(i32 1, i8* %13, i32 %14)
store i32 %15, i32* %4
%16 = load i32, i32* %4
%17 = load i32, i32* @errno
%18 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i32 %16, i32 %17)
%19 = load i32, i32* %4
%20 = icmp sgt i32 %19, 0
br i1 %20, label %21, label %29
21: ; preds = %12
%22 = load i32, i32* %4
%23 = load i8*, i8** %3
%24 = sext i32 %22 to i64
%25 = getelementptr inbounds i8, i8* %23, i64 %24
store i8* %25, i8** %3
%26 = load i32, i32* %4
%27 = load i32, i32* %5
%28 = sub nsw i32 %27, %26
store i32 %28, i32* %5
br label %29
29: ; preds = %21, %12
br label %9
30: ; preds = %9
%31 = load i32, i32* %1
ret i32 %31
}

LLVM IR (-O2):
@errno = global i32 0
@.str = [24 x i8] c"Wrote = %d, errno = %d\0A\00"
define i32 @main() {
%1 = alloca [204800 x i8]
%2 = getelementptr inbounds [204800 x i8], [204800 x i8]* %1, i64 0, i64 0
%3 = call i32 @memset(i8* %2, i8 signext 99, i32 204800)
br label %4
4: ; preds = %0, %4
%5 = phi i32 [ 204800, %0 ], [ %14, %4 ]
%6 = phi i8* [ %2, %0 ], [ %13, %4 ]
store i32 0, i32* @errno
%7 = call i32 @write(i32 1, i8* %6, i32 %5)
%8 = load i32, i32* @errno
%9 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i32 %7, i32 %8)
%10 = icmp sgt i32 %7, 0
%11 = select i1 %10, i32 %7, i32 0
%12 = zext i32 %11 to i64
%13 = getelementptr i8, i8* %6, i64 %12
%14 = sub nsw i32 %5, %11
%15 = icmp sgt i32 %14, 0
br i1 %15, label %4, label %16
16: ; preds = %4
ret i32 0
}