#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*/ buf;

/* Variables and functions */
int atoi(char *);
int /*<<< orphan*/ memcpy(char *, char *, int);
int read(int, char *, int);
scalar_t__ strcmp(char *, char *);
int /*<<< orphan*/ write(int, char *, int);
void cmd_write(int argc, char *argv[]) {
  int fd, size = -1;
  int read_in, rsz;
  char buf[512];
  fd = atoi(argv[1]);
  if (argc > 2)
    size = atoi(argv[2]);
  for (rsz = 0; rsz < size || size == -1;) {
    if (size != -1)
      read_in =
          read(1, buf,
               ((sizeof(buf)) < (size - rsz) ? (sizeof(buf)) : (size - rsz)));
    else
      read_in = read(1, buf, sizeof(buf));
    if (read_in == -1)
      return;
    if (size == -1 && read_in >= 3) {
      char local[sizeof(buf)];
      memcpy(local, buf, sizeof(buf));
      if (local[read_in - 1] == '\n')
        local[read_in - 1] = '\0';
      if (local[read_in - 1] == '\r')
        local[read_in - 1] = '\0';
      if (strcmp(local, "EOF") == 0)
        return;
    }
    write(fd, buf, read_in);
    rsz += read_in;
  }
}


x86 assembly (clang, -O0):
<cmd_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0xffffffff,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1070 <atoi@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x2,-0x4(%rbp)
jle    1193 <cmd_write+0x43>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1070 <atoi@plt>
mov    %eax,-0x18(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%ecx
mov    $0x1,%al
cmp    -0x18(%rbp),%ecx
mov    %al,-0x421(%rbp)
jl     11bb <cmd_write+0x6b>
cmpl   $0xffffffff,-0x18(%rbp)
sete   %al
mov    %al,-0x421(%rbp)
mov    -0x421(%rbp),%al
test   $0x1,%al
jne    11ce <cmd_write+0x7e>
jmp    132c <cmd_write+0x1dc>
cmpl   $0xffffffff,-0x18(%rbp)
je     123f <cmd_write+0xef>
lea    -0x220(%rbp),%rax
mov    %rax,-0x430(%rbp)
mov    -0x18(%rbp),%eax
sub    -0x20(%rbp),%eax
movslq %eax,%rcx
mov    $0x200,%eax
cmp    %rcx,%rax
jae    120e <cmd_write+0xbe>
mov    $0x200,%eax
mov    %rax,-0x438(%rbp)
jmp    121d <cmd_write+0xcd>
mov    -0x18(%rbp),%eax
sub    -0x20(%rbp),%eax
cltq
mov    %rax,-0x438(%rbp)
mov    -0x430(%rbp),%rsi
mov    -0x438(%rbp),%rax
mov    %eax,%edx
mov    $0x1,%edi
call   1040 <read@plt>
mov    %eax,-0x1c(%rbp)
jmp    1258 <cmd_write+0x108>
lea    -0x220(%rbp),%rsi
mov    $0x1,%edi
mov    $0x200,%edx
call   1040 <read@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    1267 <cmd_write+0x117>
jmp    132c <cmd_write+0x1dc>
cmpl   $0xffffffff,-0x18(%rbp)
jne    130c <cmd_write+0x1bc>
cmpl   $0x3,-0x1c(%rbp)
jl     130c <cmd_write+0x1bc>
lea    -0x420(%rbp),%rdi
lea    -0x220(%rbp),%rsi
mov    $0x200,%edx
call   1060 <memcpy@plt>
mov    -0x1c(%rbp),%eax
sub    $0x1,%eax
cltq
movsbl -0x420(%rbp,%rax,1),%eax
ff
cmp    $0xa,%eax
jne    12bc <cmd_write+0x16c>
mov    -0x1c(%rbp),%eax
sub    $0x1,%eax
cltq
movb   $0x0,-0x420(%rbp,%rax,1)
mov    -0x1c(%rbp),%eax
sub    $0x1,%eax
cltq
movsbl -0x420(%rbp,%rax,1),%eax
ff
cmp    $0xd,%eax
jne    12e5 <cmd_write+0x195>
mov    -0x1c(%rbp),%eax
sub    $0x1,%eax
cltq
movb   $0x0,-0x420(%rbp,%rax,1)
lea    -0x420(%rbp),%rdi
lea    0xd0d(%rip),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%rax
jne    1307 <cmd_write+0x1b7>
jmp    132c <cmd_write+0x1dc>
jmp    130c <cmd_write+0x1bc>
mov    -0x14(%rbp),%edi
lea    -0x220(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1030 <write@plt>
mov    -0x1c(%rbp),%eax
add    -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
jmp    119a <cmd_write+0x4a>
add    $0x440,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cmd_write>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x418,%rsp
mov    %rsi,%rbx
mov    %edi,%ebp
mov    0x8(%rsi),%rdi
call   1070 <atoi@plt>
mov    %eax,0xc(%rsp)
cmp    $0x3,%ebp
jl     11a2 <cmd_write+0x52>
mov    0x10(%rbx),%rdi
call   1070 <atoi@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
sete   %r12b
je     11aa <cmd_write+0x5a>
test   %ebp,%ebp
jg     11aa <cmd_write+0x5a>
add    $0x418,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0xffffffff,%ebp
mov    $0x1,%r12b
xor    %r15d,%r15d
lea    0x210(%rsp),%r13
mov    %r12d,%r14d
xor    $0x1,%r14b
jmp    11f7 <cmd_write+0xa7>
xchg   %ax,%ax
movb   $0x0,0x10(%rsp,%rax,1)
lea    0x10(%rsp),%rdi
lea    0xe2f(%rip),%rsi
call   1050 <strcmp@plt>
test   %rax,%rax
je     1190 <cmd_write+0x40>
mov    0xc(%rsp),%edi
mov    %r13,%rsi
mov    %ebx,%edx
call   1030 <write@plt>
add    %ebx,%r15d
cmp    %r15d,%ebp
setg   %al
or     %r12b,%al
je     1190 <cmd_write+0x40>
mov    %ebp,%edx
sub    %r15d,%edx
cmp    $0x200,%edx
mov    $0x200,%eax
cmovae %eax,%edx
test   %r12b,%r12b
cmovne %eax,%edx
mov    $0x1,%edi
mov    %r13,%rsi
call   1040 <read@plt>
cmp    $0xffffffff,%eax
je     1190 <cmd_write+0x40>
mov    %eax,%ebx
cmp    $0x3,%eax
setl   %al
or     %r14b,%al
jne    11db <cmd_write+0x8b>
lea    0x10(%rsp),%rdi
mov    %r13,%rsi
mov    $0x200,%edx
call   1060 <memcpy@plt>
lea    -0x1(%rbx),%eax
movzbl 0x10(%rsp,%rax,1),%ecx
cmp    $0xd,%cl
je     11c0 <cmd_write+0x70>
cmp    $0xa,%cl
je     11c0 <cmd_write+0x70>
jmp    11c5 <cmd_write+0x75>

LLVM IR (-O0):
@.str = [4 x i8] c"EOF\00"
define void @cmd_write(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i8**
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca [512 x i8]
%10 = alloca [512 x i8]
store i32 %0, i32* %3
store i8** %1, i8*** %4
store i32 -1, i32* %6
%11 = load i8**, i8*** %4
%12 = getelementptr inbounds i8*, i8** %11, i64 1
%13 = load i8*, i8** %12
%14 = call i32 @atoi(i8* %13)
store i32 %14, i32* %5
%15 = load i32, i32* %3
%16 = icmp sgt i32 %15, 2
br i1 %16, label %17, label %22
17: ; preds = %2
%18 = load i8**, i8*** %4
%19 = getelementptr inbounds i8*, i8** %18, i64 2
%20 = load i8*, i8** %19
%21 = call i32 @atoi(i8* %20)
store i32 %21, i32* %6
br label %22
22: ; preds = %17, %2
store i32 0, i32* %8
br label %23
23: ; preds = %100, %22
%24 = load i32, i32* %8
%25 = load i32, i32* %6
%26 = icmp slt i32 %24, %25
br i1 %26, label %30, label %27
27: ; preds = %23
%28 = load i32, i32* %6
%29 = icmp eq i32 %28, -1
br label %30
30: ; preds = %27, %23
%31 = phi i1 [ true, %23 ], [ %29, %27 ]
br i1 %31, label %32, label %108
32: ; preds = %30
%33 = load i32, i32* %6
%34 = icmp ne i32 %33, -1
br i1 %34, label %35, label %52
35: ; preds = %32
%36 = getelementptr inbounds [512 x i8], [512 x i8]* %9, i64 0, i64 0
%37 = load i32, i32* %6
%38 = load i32, i32* %8
%39 = sub nsw i32 %37, %38
%40 = sext i32 %39 to i64
%41 = icmp ult i64 512, %40
br i1 %41, label %42, label %43
42: ; preds = %35
br label %48
43: ; preds = %35
%44 = load i32, i32* %6
%45 = load i32, i32* %8
%46 = sub nsw i32 %44, %45
%47 = sext i32 %46 to i64
br label %48
48: ; preds = %43, %42
%49 = phi i64 [ 512, %42 ], [ %47, %43 ]
%50 = trunc i64 %49 to i32
%51 = call i32 @read(i32 1, i8* %36, i32 %50)
store i32 %51, i32* %7
br label %55
52: ; preds = %32
%53 = getelementptr inbounds [512 x i8], [512 x i8]* %9, i64 0, i64 0
%54 = call i32 @read(i32 1, i8* %53, i32 512)
store i32 %54, i32* %7
br label %55
55: ; preds = %52, %48
%56 = load i32, i32* %7
%57 = icmp eq i32 %56, -1
br i1 %57, label %58, label %59
58: ; preds = %55
br label %108
59: ; preds = %55
%60 = load i32, i32* %6
%61 = icmp eq i32 %60, -1
br i1 %61, label %62, label %100
62: ; preds = %59
%63 = load i32, i32* %7
%64 = icmp sge i32 %63, 3
br i1 %64, label %65, label %100
65: ; preds = %62
%66 = getelementptr inbounds [512 x i8], [512 x i8]* %10, i64 0, i64 0
%67 = getelementptr inbounds [512 x i8], [512 x i8]* %9, i64 0, i64 0
%68 = call i32 @memcpy(i8* %66, i8* %67, i32 512)
%69 = load i32, i32* %7
%70 = sub nsw i32 %69, 1
%71 = sext i32 %70 to i64
%72 = getelementptr inbounds [512 x i8], [512 x i8]* %10, i64 0, i64 %71
%73 = load i8, i8* %72
%74 = sext i8 %73 to i32
%75 = icmp eq i32 %74, 10
br i1 %75, label %76, label %81
76: ; preds = %65
%77 = load i32, i32* %7
%78 = sub nsw i32 %77, 1
%79 = sext i32 %78 to i64
%80 = getelementptr inbounds [512 x i8], [512 x i8]* %10, i64 0, i64 %79
store i8 0, i8* %80
br label %81
81: ; preds = %76, %65
%82 = load i32, i32* %7
%83 = sub nsw i32 %82, 1
%84 = sext i32 %83 to i64
%85 = getelementptr inbounds [512 x i8], [512 x i8]* %10, i64 0, i64 %84
%86 = load i8, i8* %85
%87 = sext i8 %86 to i32
%88 = icmp eq i32 %87, 13
br i1 %88, label %89, label %94
89: ; preds = %81
%90 = load i32, i32* %7
%91 = sub nsw i32 %90, 1
%92 = sext i32 %91 to i64
%93 = getelementptr inbounds [512 x i8], [512 x i8]* %10, i64 0, i64 %92
store i8 0, i8* %93
br label %94
94: ; preds = %89, %81
%95 = getelementptr inbounds [512 x i8], [512 x i8]* %10, i64 0, i64 0
%96 = call i64 @strcmp(i8* %95, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%97 = icmp eq i64 %96, 0
br i1 %97, label %98, label %99
98: ; preds = %94
br label %108
99: ; preds = %94
br label %100
100: ; preds = %99, %62, %59
%101 = load i32, i32* %5
%102 = getelementptr inbounds [512 x i8], [512 x i8]* %9, i64 0, i64 0
%103 = load i32, i32* %7
%104 = call i32 @write(i32 %101, i8* %102, i32 %103)
%105 = load i32, i32* %7
%106 = load i32, i32* %8
%107 = add nsw i32 %106, %105
store i32 %107, i32* %8
br label %23
108: ; preds = %58, %98, %30
ret void
}

LLVM IR (-O2):
@.str = [4 x i8] c"EOF\00"
define void @cmd_write(i32 %0, i8** nocapture %1) {
%3 = alloca [512 x i8]
%4 = alloca [512 x i8]
%5 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 0
%6 = getelementptr inbounds i8*, i8** %1, i64 1
%7 = load i8*, i8** %6
%8 = tail call i32 @atoi(i8* %7)
%9 = icmp sgt i32 %0, 2
br i1 %9, label %10, label %17
10: ; preds = %2
%11 = getelementptr inbounds i8*, i8** %1, i64 2
%12 = load i8*, i8** %11
%13 = tail call i32 @atoi(i8* %12)
%14 = icmp eq i32 %13, -1
%15 = icmp sgt i32 %13, 0
%16 = or i1 %14, %15
br i1 %16, label %17, label %47
17: ; preds = %2, %10
%18 = phi i1 [ %14, %10 ], [ true, %2 ]
%19 = phi i32 [ %13, %10 ], [ -1, %2 ]
%20 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
br label %21
21: ; preds = %17, %42
%22 = phi i32 [ 0, %17 ], [ %44, %42 ]
%23 = sub nsw i32 %19, %22
%24 = icmp ult i32 %23, 512
%25 = select i1 %24, i32 %23, i32 512
%26 = select i1 %18, i32 512, i32 %25
%27 = call i32 @read(i32 1, i8* %5, i32 %26)
%28 = icmp eq i32 %27, -1
br i1 %28, label %47, label %29
29: ; preds = %21
%30 = icmp sgt i32 %27, 2
%31 = and i1 %18, %30
br i1 %31, label %32, label %42
32: ; preds = %29
%33 = call i32 @memcpy(i8* %20, i8* %5, i32 512)
%34 = add nsw i32 %27, -1
%35 = zext i32 %34 to i64
%36 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 %35
%37 = load i8, i8* %36
switch i8 %37, label %39 [
i8 10, label %38
i8 13, label %38
]
38: ; preds = %32, %32
store i8 0, i8* %36
br label %39
39: ; preds = %38, %32
%40 = call i64 @strcmp(i8* %20, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%41 = icmp eq i64 %40, 0
br i1 %41, label %47, label %42
42: ; preds = %39, %29
%43 = call i32 @write(i32 %8, i8* %5, i32 %27)
%44 = add nsw i32 %27, %22
%45 = icmp sgt i32 %19, %44
%46 = or i1 %18, %45
br i1 %46, label %21, label %47
47: ; preds = %39, %21, %42, %10
ret void
}