#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 O_CREAT;
int O_TRUNC;
int O_WRONLY;
scalar_t__ close(int);
int errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int fread(char *, size_t, int, int /*<<< orphan*/);
int /*<<< orphan*/ free(char *);
scalar_t__ full_write(int, char *, size_t);
int /*<<< orphan*/ getchar();
int open(char *, int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ puts(char *);
char *realloc(char *, size_t);
int scanf(char *, size_t *);
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ stdout;
int do_write_command(char *filename) {
  int fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0755);
  if (fd < 0)
    return -errno;
  puts("ready");
  fflush(stdout);
  int ret = 0;
  char *buf = NULL;
  while (1) {
    size_t bytes;
    if (scanf("%zu", &bytes) != 1) {
      perror("scanf");
      exit(2);
    }
    char *newbuf = realloc(buf, bytes);
    if (bytes && !newbuf) {
      perror("malloc");
      exit(2);
    }
    buf = newbuf;
    (void)getchar();
    if (bytes == 0)
      break;
    if (fread(buf, bytes, 1, stdin) != 1) {
      perror("fread");
      exit(2);
    }
    if (full_write(fd, buf, bytes) < 0) {
      ret = -errno;
      perror("write");
    }
  }
  free(buf);
  if (close(fd) < 0 && ret == 0)
    ret = -errno;
  if (ret == 0)
    puts("ok");
  return ret;
}


x86 assembly (clang, -O0):
<do_write_command>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2dd9(%rip),%rax
mov    (%rax),%esi
mov    0x2e18(%rip),%rax
or     (%rax),%esi
mov    0x2e07(%rip),%rax
or     (%rax),%esi
mov    $0x1ed,%edx
call   10c0 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1215 <do_write_command+0x55>
xor    %eax,%eax
mov    0x2dbd(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    138b <do_write_command+0x1cb>
lea    0xde4(%rip),%rdi
call   1040 <puts@plt>
mov    0x2d98(%rip),%rax
mov    (%rax),%edi
call   1080 <fflush@plt>
movl   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
lea    0xdc1(%rip),%rdi
lea    -0x28(%rbp),%rsi
call   10b0 <scanf@plt>
cmp    $0x1,%eax
je     126d <do_write_command+0xad>
lea    0xdac(%rip),%rdi
call   10d0 <perror@plt>
mov    $0x2,%edi
call   10e0 <exit@plt>
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   10a0 <realloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     12aa <do_write_command+0xea>
cmpq   $0x0,-0x30(%rbp)
jne    12aa <do_write_command+0xea>
lea    0xd75(%rip),%rdi
call   10d0 <perror@plt>
mov    $0x2,%edi
call   10e0 <exit@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    $0x0,%al
call   1070 <getchar@plt>
cmpq   $0x0,-0x28(%rbp)
jne    12c9 <do_write_command+0x109>
jmp    133c <do_write_command+0x17c>
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    0x2d00(%rip),%rax
mov    (%rax),%ecx
mov    $0x1,%edx
call   1050 <fread@plt>
cmp    $0x1,%eax
je     1303 <do_write_command+0x143>
lea    0xd23(%rip),%rdi
call   10d0 <perror@plt>
mov    $0x2,%edi
call   10e0 <exit@plt>
mov    -0x14(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1090 <full_write@plt>
cmp    $0x0,%rax
jge    1337 <do_write_command+0x177>
xor    %eax,%eax
mov    0x2ca2(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x18(%rbp)
lea    0xceb(%rip),%rdi
call   10d0 <perror@plt>
jmp    123e <do_write_command+0x7e>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
cmp    $0x0,%rax
jge    136f <do_write_command+0x1af>
cmpl   $0x0,-0x18(%rbp)
jne    136f <do_write_command+0x1af>
xor    %eax,%eax
mov    0x2c5e(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jne    1385 <do_write_command+0x1c5>
lea    0xca3(%rip),%rdi
call   1040 <puts@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_write_command>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    0x2ddb(%rip),%rax
mov    0x2e1c(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e09(%rip),%rax
or     (%rax),%esi
mov    $0x1ed,%edx
call   10c0 <open@plt>
test   %eax,%eax
js     1332 <do_write_command+0x172>
mov    %eax,%r13d
lea    0xdfb(%rip),%rdi
call   1040 <puts@plt>
mov    0x2daf(%rip),%rax
mov    (%rax),%edi
call   1080 <fflush@plt>
lea    0xde7(%rip),%rdi
lea    0x10(%rsp),%rsi
call   10b0 <scanf@plt>
cmp    $0x1,%eax
jne    134e <do_write_command+0x18e>
mov    0x2d9f(%rip),%r14
lea    0xdc6(%rip),%r15
lea    0x10(%rsp),%r12
xor    %ebx,%ebx
movl   $0x0,0xc(%rsp)
jmp    1274 <do_write_command+0xb4>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r15,%rdi
mov    %r12,%rsi
call   10b0 <scanf@plt>
cmp    $0x1,%eax
jne    134e <do_write_command+0x18e>
mov    0x10(%rsp),%rbp
mov    %rbx,%rdi
mov    %rbp,%rsi
call   10a0 <realloc@plt>
mov    %rax,%rbx
test   %rbp,%rbp
je     1295 <do_write_command+0xd5>
test   %rbx,%rbx
je     1357 <do_write_command+0x197>
xor    %eax,%eax
call   1070 <getchar@plt>
mov    0x10(%rsp),%rsi
test   %rsi,%rsi
je     12f4 <do_write_command+0x134>
mov    (%r14),%ecx
mov    %rbx,%rdi
mov    $0x1,%edx
call   1050 <fread@plt>
cmp    $0x1,%eax
jne    1360 <do_write_command+0x1a0>
mov    0x10(%rsp),%rdx
mov    %r13d,%edi
mov    %rbx,%rsi
call   1090 <full_write@plt>
test   %rax,%rax
jns    1260 <do_write_command+0xa0>
xor    %ecx,%ecx
mov    0x2ceb(%rip),%rax
sub    (%rax),%ecx
mov    %ecx,0xc(%rsp)
lea    0xd33(%rip),%rdi
call   10d0 <perror@plt>
jmp    1260 <do_write_command+0xa0>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r13d,%edi
call   1060 <close@plt>
xor    %ecx,%ecx
mov    0x2cbb(%rip),%rdx
sub    (%rdx),%ecx
mov    0xc(%rsp),%edx
test   %edx,%edx
cmovne %edx,%ecx
test   %rax,%rax
cmovns %edx,%ecx
test   %ecx,%ecx
jne    133d <do_write_command+0x17d>
lea    0xcfa(%rip),%rdi
call   1040 <puts@plt>
xor    %ecx,%ecx
jmp    133d <do_write_command+0x17d>
mov    0x2c8f(%rip),%rax
xor    %ecx,%ecx
sub    (%rax),%ecx
mov    %ecx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xcb5(%rip),%rdi
jmp    1367 <do_write_command+0x1a7>
lea    0xcb2(%rip),%rdi
jmp    1367 <do_write_command+0x1a7>
lea    0xcb0(%rip),%rdi
call   10d0 <perror@plt>
mov    $0x2,%edi
call   10e0 <exit@plt>

LLVM IR (-O0):
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@errno = global i32 0
@.str = [6 x i8] c"ready\00"
@stdout = global i32 0
@.str.1 = [4 x i8] c"%zu\00"
@.str.2 = [6 x i8] c"scanf\00"
@.str.3 = [7 x i8] c"malloc\00"
@stdin = global i32 0
@.str.4 = [6 x i8] c"fread\00"
@.str.5 = [6 x i8] c"write\00"
@.str.6 = [3 x i8] c"ok\00"
define i32 @do_write_command(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i8*
store i8* %0, i8** %3
%9 = load i8*, i8** %3
%10 = load i32, i32* @O_WRONLY
%11 = load i32, i32* @O_CREAT
%12 = or i32 %10, %11
%13 = load i32, i32* @O_TRUNC
%14 = or i32 %12, %13
%15 = call i32 @open(i8* %9, i32 %14, i32 493)
store i32 %15, i32* %4
%16 = load i32, i32* %4
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %1
%19 = load i32, i32* @errno
%20 = sub nsw i32 0, %19
store i32 %20, i32* %2
br label %88
21: ; preds = %1
%22 = call i32 @puts(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%23 = load i32, i32* @stdout
%24 = call i32 @fflush(i32 %23)
store i32 0, i32* %5
store i8* null, i8** %6
br label %25
25: ; preds = %21, %68
%26 = call i32 @scanf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i64* %7)
%27 = icmp ne i32 %26, 1
br i1 %27, label %28, label %31
28: ; preds = %25
%29 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%30 = call i32 @exit(i32 2)
unreachable
31: ; preds = %25
%32 = load i8*, i8** %6
%33 = load i64, i64* %7
%34 = call i8* @realloc(i8* %32, i64 %33)
store i8* %34, i8** %8
%35 = load i64, i64* %7
%36 = icmp ne i64 %35, 0
br i1 %36, label %37, label %43
37: ; preds = %31
%38 = load i8*, i8** %8
%39 = icmp ne i8* %38, null
br i1 %39, label %43, label %40
40: ; preds = %37
%41 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%42 = call i32 @exit(i32 2)
unreachable
43: ; preds = %37, %31
%44 = load i8*, i8** %8
store i8* %44, i8** %6
%45 = call i32 (...) @getchar()
%46 = load i64, i64* %7
%47 = icmp eq i64 %46, 0
br i1 %47, label %48, label %49
48: ; preds = %43
br label %69
49: ; preds = %43
%50 = load i8*, i8** %6
%51 = load i64, i64* %7
%52 = load i32, i32* @stdin
%53 = call i32 @fread(i8* %50, i64 %51, i32 1, i32 %52)
%54 = icmp ne i32 %53, 1
br i1 %54, label %55, label %58
55: ; preds = %49
%56 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
%57 = call i32 @exit(i32 2)
unreachable
58: ; preds = %49
%59 = load i32, i32* %4
%60 = load i8*, i8** %6
%61 = load i64, i64* %7
%62 = call i64 @full_write(i32 %59, i8* %60, i64 %61)
%63 = icmp slt i64 %62, 0
br i1 %63, label %64, label %68
64: ; preds = %58
%65 = load i32, i32* @errno
%66 = sub nsw i32 0, %65
store i32 %66, i32* %5
%67 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
br label %68
68: ; preds = %64, %58
br label %25
69: ; preds = %48
%70 = load i8*, i8** %6
%71 = call i32 @free(i8* %70)
%72 = load i32, i32* %4
%73 = call i64 @close(i32 %72)
%74 = icmp slt i64 %73, 0
br i1 %74, label %75, label %81
75: ; preds = %69
%76 = load i32, i32* %5
%77 = icmp eq i32 %76, 0
br i1 %77, label %78, label %81
78: ; preds = %75
%79 = load i32, i32* @errno
%80 = sub nsw i32 0, %79
store i32 %80, i32* %5
br label %81
81: ; preds = %78, %75, %69
%82 = load i32, i32* %5
%83 = icmp eq i32 %82, 0
br i1 %83, label %84, label %86
84: ; preds = %81
%85 = call i32 @puts(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0))
br label %86
86: ; preds = %84, %81
%87 = load i32, i32* %5
store i32 %87, i32* %2
br label %88
88: ; preds = %86, %18
%89 = load i32, i32* %2
ret i32 %89
}

LLVM IR (-O2):
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@errno = global i32 0
@.str = [6 x i8] c"ready\00"
@stdout = global i32 0
@.str.1 = [4 x i8] c"%zu\00"
@.str.2 = [6 x i8] c"scanf\00"
@.str.3 = [7 x i8] c"malloc\00"
@stdin = global i32 0
@.str.4 = [6 x i8] c"fread\00"
@.str.5 = [6 x i8] c"write\00"
@.str.6 = [3 x i8] c"ok\00"
define i32 @do_write_command(i8* nocapture %0) {
%2 = alloca i64
%3 = load i32, i32* @O_WRONLY
%4 = load i32, i32* @O_CREAT
%5 = or i32 %4, %3
%6 = load i32, i32* @O_TRUNC
%7 = or i32 %5, %6
%8 = tail call i32 @open(i8* %0, i32 %7, i32 493)
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %13
10: ; preds = %1
%11 = load i32, i32* @errno
%12 = sub nsw i32 0, %11
br label %69
13: ; preds = %1
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%15 = load i32, i32* @stdout
%16 = tail call i32 @fflush(i32 %15)
%17 = bitcast i64* %2 to i8*
%18 = call i32 @scanf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i64* %2)
%19 = icmp eq i32 %18, 1
br i1 %19, label %23, label %20
20: ; preds = %53, %13
%21 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%22 = call i32 @exit(i32 2)
unreachable
23: ; preds = %13, %53
%24 = phi i8* [ %27, %53 ], [ null, %13 ]
%25 = phi i32 [ %54, %53 ], [ 0, %13 ]
%26 = load i64, i64* %2
%27 = call i8* @realloc(i8* %24, i64 %26)
%28 = icmp eq i64 %26, 0
%29 = icmp ne i8* %27, null
%30 = or i1 %28, %29
br i1 %30, label %34, label %31
31: ; preds = %23
%32 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%33 = call i32 @exit(i32 2)
unreachable
34: ; preds = %23
%35 = call i32 (...) @getchar()
%36 = load i64, i64* %2
%37 = icmp eq i64 %36, 0
br i1 %37, label %57, label %38
38: ; preds = %34
%39 = load i32, i32* @stdin
%40 = call i32 @fread(i8* %27, i64 %36, i32 1, i32 %39)
%41 = icmp eq i32 %40, 1
br i1 %41, label %45, label %42
42: ; preds = %38
%43 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
%44 = call i32 @exit(i32 2)
unreachable
45: ; preds = %38
%46 = load i64, i64* %2
%47 = call i64 @full_write(i32 %8, i8* %27, i64 %46)
%48 = icmp slt i64 %47, 0
br i1 %48, label %49, label %53
49: ; preds = %45
%50 = load i32, i32* @errno
%51 = sub nsw i32 0, %50
%52 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
br label %53
53: ; preds = %45, %49
%54 = phi i32 [ %51, %49 ], [ %25, %45 ]
%55 = call i32 @scanf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i64* %2)
%56 = icmp eq i32 %55, 1
br i1 %56, label %23, label %20
57: ; preds = %34
%58 = call i32 @free(i8* %27)
%59 = call i64 @close(i32 %8)
%60 = icmp slt i64 %59, 0
%61 = icmp eq i32 %25, 0
%62 = select i1 %60, i1 %61, i1 false
%63 = load i32, i32* @errno
%64 = sub nsw i32 0, %63
%65 = select i1 %62, i32 %64, i32 %25
%66 = icmp eq i32 %65, 0
br i1 %66, label %67, label %69
67: ; preds = %57
%68 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0))
br label %69
69: ; preds = %57, %67, %10
%70 = phi i32 [ %12, %10 ], [ 0, %67 ], [ %65, %57 ]
ret i32 %70
}