#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 /*<<< orphan*/ F_GETFL;
int /*<<< orphan*/ F_SETFL;
int O_NONBLOCK;
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int);
int fcntl(int /*<<< orphan*/, int /*<<< orphan*/, int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ memset(char *, char, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int write(int /*<<< orphan*/, char *, int);
int main(void) {
  int flags, count, resid, loops;
  char buf[100000], *ptr;
  memset(buf, 'a', 100000);
  if ((flags = fcntl(STDOUT_FILENO, F_GETFL, 0)) < 0) {
    perror("getting file flags");
    exit(1);
  }
  if (fcntl(STDOUT_FILENO, F_SETFL, flags | O_NONBLOCK) < 0) {
    perror("setting file flags");
    exit(2);
  }
  for (loops = 0; loops < 50; loops++) {
    ptr = buf;
    resid = 100000;
    while (resid > 0) {
      count = write(STDOUT_FILENO, ptr, resid);
      if (count >= 0) {
        ptr += count;
        resid -= count;
        fprintf(stderr, "wrote %d bytes\n", count);
      } else {
        fprintf(stderr, "write error: %s\n", strerror(errno));
      }
    }
  }
  (void)fcntl(STDOUT_FILENO, F_SETFL, flags);
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x186d0,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x186c0(%rbp),%rdi
mov    $0x61,%esi
mov    $0x186a0,%edx
call   1050 <memset@plt>
mov    0x2e51(%rip),%rax
mov    (%rax),%edi
mov    0x2e10(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1030 <fcntl@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jge    11d3 <main+0x63>
lea    0xe3c(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    0x2e16(%rip),%rax
mov    (%rax),%edi
mov    0x2de5(%rip),%rax
mov    (%rax),%esi
mov    -0x8(%rbp),%edx
mov    0x2df1(%rip),%rax
or     (%rax),%edx
call   1030 <fcntl@plt>
cmp    $0x0,%eax
jge    1215 <main+0xa5>
lea    0xe0d(%rip),%rdi
call   1070 <perror@plt>
mov    $0x2,%edi
call   1080 <exit@plt>
movl   $0x0,-0x14(%rbp)
cmpl   $0x32,-0x14(%rbp)
jge    12f7 <main+0x187>
lea    -0x186c0(%rbp),%rax
mov    %rax,-0x186c8(%rbp)
movl   $0x186a0,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
jle    12e4 <main+0x174>
mov    0x2da4(%rip),%rax
mov    (%rax),%edi
mov    -0x186c8(%rbp),%rsi
mov    -0x10(%rbp),%edx
call   1040 <write@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jl     12ab <main+0x13b>
mov    -0xc(%rbp),%ecx
mov    -0x186c8(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x186c8(%rbp)
mov    -0xc(%rbp),%ecx
mov    -0x10(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x10(%rbp)
mov    0x2d3d(%rip),%rax
mov    (%rax),%edi
mov    -0xc(%rbp),%edx
lea    0xd87(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
jmp    12df <main+0x16f>
mov    0x2d1e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x186cc(%rbp)
mov    0x2cff(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x186cc(%rbp),%edi
mov    %rax,%rdx
lea    0xd5e(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
jmp    123b <main+0xcb>
jmp    12e9 <main+0x179>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    121c <main+0xac>
mov    0x2cf2(%rip),%rax
mov    (%rax),%edi
mov    0x2cc1(%rip),%rax
mov    (%rax),%esi
mov    -0x8(%rbp),%edx
call   1030 <fcntl@plt>
xor    %edi,%edi
call   1080 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x186b8,%rsp
lea    0x10(%rsp),%rdi
mov    $0x61,%esi
mov    $0x186a0,%edx
call   1050 <memset@plt>
mov    0x2e54(%rip),%r15
mov    (%r15),%edi
mov    0x2e12(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1030 <fcntl@plt>
test   %eax,%eax
jns    11c9 <main+0x59>
lea    0xe46(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    %eax,%ebx
mov    (%r15),%edi
mov    0x2df3(%rip),%rax
mov    (%rax),%esi
mov    0x2e02(%rip),%rax
mov    (%rax),%edx
or     %ebx,%edx
call   1030 <fcntl@plt>
test   %eax,%eax
jns    1201 <main+0x91>
lea    0xe21(%rip),%rdi
call   1070 <perror@plt>
mov    $0x2,%edi
call   1080 <exit@plt>
mov    %ebx,0x8(%rsp)
mov    0x2dc4(%rip),%r12
lea    0xe13(%rip),%r13
xor    %eax,%eax
jmp    122c <main+0xbc>
nopw   0x0(%rax,%rax,1)
mov    0xc(%rsp),%eax
add    $0x1,%eax
cmp    $0x32,%eax
je     1298 <main+0x128>
mov    %eax,0xc(%rsp)
mov    $0x186a0,%ebx
lea    0x10(%rsp),%rbp
jmp    126a <main+0xfa>
nopl   0x0(%rax)
mov    (%r12),%r14d
mov    0x2d75(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    %r14d,%edi
lea    0xdda(%rip),%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
test   %ebx,%ebx
jle    1220 <main+0xb0>
mov    (%r15),%edi
mov    %rbp,%rsi
mov    %ebx,%edx
call   1040 <write@plt>
test   %eax,%eax
js     1240 <main+0xd0>
mov    %eax,%ecx
add    %rcx,%rbp
sub    %eax,%ebx
mov    (%r12),%edi
mov    %r13,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1060 <fprintf@plt>
test   %ebx,%ebx
jg     126a <main+0xfa>
jmp    1220 <main+0xb0>
mov    (%r15),%edi
mov    0x2d26(%rip),%rax
mov    (%rax),%esi
mov    0x8(%rsp),%edx
call   1030 <fcntl@plt>
xor    %edi,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@STDOUT_FILENO = global i32 0
@F_GETFL = global i32 0
@.str = [19 x i8] c"getting file flags\00"
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
@.str.1 = [19 x i8] c"setting file flags\00"
@stderr = global i32 0
@.str.2 = [16 x i8] c"wrote %d bytes\0A\00"
@.str.3 = [17 x i8] c"write error: %s\0A\00"
@errno = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca [100000 x i8]
%7 = alloca i8*
store i32 0, i32* %1
%8 = getelementptr inbounds [100000 x i8], [100000 x i8]* %6, i64 0, i64 0
%9 = call i32 @memset(i8* %8, i8 signext 97, i32 100000)
%10 = load i32, i32* @STDOUT_FILENO
%11 = load i32, i32* @F_GETFL
%12 = call i32 @fcntl(i32 %10, i32 %11, i32 0)
store i32 %12, i32* %2
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %0
%15 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
%16 = call i32 @exit(i32 1)
unreachable
17: ; preds = %0
%18 = load i32, i32* @STDOUT_FILENO
%19 = load i32, i32* @F_SETFL
%20 = load i32, i32* %2
%21 = load i32, i32* @O_NONBLOCK
%22 = or i32 %20, %21
%23 = call i32 @fcntl(i32 %18, i32 %19, i32 %22)
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %28
25: ; preds = %17
%26 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
%27 = call i32 @exit(i32 2)
unreachable
28: ; preds = %17
store i32 0, i32* %5
br label %29
29: ; preds = %62, %28
%30 = load i32, i32* %5
%31 = icmp slt i32 %30, 50
br i1 %31, label %32, label %65
32: ; preds = %29
%33 = getelementptr inbounds [100000 x i8], [100000 x i8]* %6, i64 0, i64 0
store i8* %33, i8** %7
store i32 100000, i32* %4
br label %34
34: ; preds = %60, %32
%35 = load i32, i32* %4
%36 = icmp sgt i32 %35, 0
br i1 %36, label %37, label %61
37: ; preds = %34
%38 = load i32, i32* @STDOUT_FILENO
%39 = load i8*, i8** %7
%40 = load i32, i32* %4
%41 = call i32 @write(i32 %38, i8* %39, i32 %40)
store i32 %41, i32* %3
%42 = load i32, i32* %3
%43 = icmp sge i32 %42, 0
br i1 %43, label %44, label %55
44: ; preds = %37
%45 = load i32, i32* %3
%46 = load i8*, i8** %7
%47 = sext i32 %45 to i64
%48 = getelementptr inbounds i8, i8* %46, i64 %47
store i8* %48, i8** %7
%49 = load i32, i32* %3
%50 = load i32, i32* %4
%51 = sub nsw i32 %50, %49
store i32 %51, i32* %4
%52 = load i32, i32* @stderr
%53 = load i32, i32* %3
%54 = call i32 (i32, i8*, ...) @fprintf(i32 %52, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i32 %53)
br label %60
55: ; preds = %37
%56 = load i32, i32* @stderr
%57 = load i32, i32* @errno
%58 = call i8* @strerror(i32 %57)
%59 = call i32 (i32, i8*, ...) @fprintf(i32 %56, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i8* %58)
br label %60
60: ; preds = %55, %44
br label %34
61: ; preds = %34
br label %62
62: ; preds = %61
%63 = load i32, i32* %5
%64 = add nsw i32 %63, 1
store i32 %64, i32* %5
br label %29
65: ; preds = %29
%66 = load i32, i32* @STDOUT_FILENO
%67 = load i32, i32* @F_SETFL
%68 = load i32, i32* %2
%69 = call i32 @fcntl(i32 %66, i32 %67, i32 %68)
%70 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@STDOUT_FILENO = global i32 0
@F_GETFL = global i32 0
@.str = [19 x i8] c"getting file flags\00"
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
@.str.1 = [19 x i8] c"setting file flags\00"
@stderr = global i32 0
@.str.2 = [16 x i8] c"wrote %d bytes\0A\00"
@.str.3 = [17 x i8] c"write error: %s\0A\00"
@errno = global i32 0
define i32 @main() {
%1 = alloca [100000 x i8]
%2 = getelementptr inbounds [100000 x i8], [100000 x i8]* %1, i64 0, i64 0
%3 = call i32 @memset(i8* %2, i8 signext 97, i32 100000)
%4 = load i32, i32* @STDOUT_FILENO
%5 = load i32, i32* @F_GETFL
%6 = call i32 @fcntl(i32 %4, i32 %5, i32 0)
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %11
8: ; preds = %0
%9 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
%10 = call i32 @exit(i32 1)
unreachable
11: ; preds = %0
%12 = load i32, i32* @STDOUT_FILENO
%13 = load i32, i32* @F_SETFL
%14 = load i32, i32* @O_NONBLOCK
%15 = or i32 %14, %6
%16 = call i32 @fcntl(i32 %12, i32 %13, i32 %15)
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %11
%19 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0))
%20 = call i32 @exit(i32 2)
unreachable
21: ; preds = %11, %44
%22 = phi i32 [ %45, %44 ], [ 0, %11 ]
br label %23
23: ; preds = %21, %40
%24 = phi i8* [ %2, %21 ], [ %42, %40 ]
%25 = phi i32 [ 100000, %21 ], [ %41, %40 ]
%26 = load i32, i32* @STDOUT_FILENO
%27 = call i32 @write(i32 %26, i8* %24, i32 %25)
%28 = icmp sgt i32 %27, -1
br i1 %28, label %29, label %35
29: ; preds = %23
%30 = zext i32 %27 to i64
%31 = getelementptr inbounds i8, i8* %24, i64 %30
%32 = sub nsw i32 %25, %27
%33 = load i32, i32* @stderr
%34 = call i32 (i32, i8*, ...) @fprintf(i32 %33, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i32 %27)
br label %40
35: ; preds = %23
%36 = load i32, i32* @stderr
%37 = load i32, i32* @errno
%38 = call i8* @strerror(i32 %37)
%39 = call i32 (i32, i8*, ...) @fprintf(i32 %36, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i8* %38)
br label %40
40: ; preds = %35, %29
%41 = phi i32 [ %32, %29 ], [ %25, %35 ]
%42 = phi i8* [ %31, %29 ], [ %24, %35 ]
%43 = icmp sgt i32 %41, 0
br i1 %43, label %23, label %44
44: ; preds = %40
%45 = add nuw nsw i32 %22, 1
%46 = icmp eq i32 %45, 50
br i1 %46, label %47, label %21
47: ; preds = %44
%48 = load i32, i32* @STDOUT_FILENO
%49 = load i32, i32* @F_SETFL
%50 = call i32 @fcntl(i32 %48, i32 %49, i32 %6)
%51 = call i32 @exit(i32 0)
unreachable
}