#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;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ flushconsole();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int open(char *, int, int);
int /*<<< orphan*/ printconsole(char *, char *);
int /*<<< orphan*/ putconsole(char);
int readbyte(int, char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ writebyte(int, char);
void receivefile(int fdtarg, char *filename) {
  char ch;
  int fdout;
  int nbytes;
  int ndots;
  int ret;
  fdout = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0644);
  if (fdout < 0) {
    fprintf(stderr, "ERROR: Failed to open '%s' for writing\n", filename);
    (void)writebyte(fdtarg, '>');
    return;
  }
  printconsole("Receiving file '%s':\n", filename);
  flushconsole();
  (void)writebyte(fdtarg, '+');
  do {
    ret = readbyte(fdtarg, &ch);
  } while (ret == 1 && ch != 'S' && ch != 'Q');
  nbytes = 0;
  ndots = 0;
  while (ret == 1) {
    if (ch == '>') {
      close(fdout);
      return;
    }
    writebyte(fdout, ch);
    if (++nbytes > 256) {
      nbytes = 0;
      putconsole('.');
      if (++ndots > 72) {
        putconsole('\n');
        ndots = 0;
      }
      flushconsole();
    }
    ret = readbyte(fdtarg, &ch);
    if (ch == '\r') {
      writebyte(fdtarg, '+');
    }
  }
  close(fdout);
}


x86 assembly (clang, -O0):
<receivefile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e26(%rip),%rax
mov    (%rax),%esi
mov    0x2e55(%rip),%rax
or     (%rax),%esi
mov    0x2e44(%rip),%rax
or     (%rax),%esi
mov    $0x1a4,%edx
call   1080 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    11f0 <receivefile+0x70>
mov    0x2e04(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe27(%rip),%rsi
call   1050 <fprintf@plt>
mov    -0x4(%rbp),%edi
mov    $0x3e,%esi
call   10a0 <writebyte@plt>
jmp    131f <receivefile+0x19f>
mov    -0x10(%rbp),%rsi
lea    0xe2d(%rip),%rdi
call   1040 <printconsole@plt>
mov    $0x0,%al
call   1090 <flushconsole@plt>
mov    -0x4(%rbp),%edi
mov    $0x2b,%esi
call   10a0 <writebyte@plt>
mov    -0x4(%rbp),%edi
lea    -0x11(%rbp),%rsi
call   1070 <readbyte@plt>
mov    %eax,-0x24(%rbp)
xor    %eax,%eax
cmpl   $0x1,-0x24(%rbp)
mov    %al,-0x25(%rbp)
jne    1251 <receivefile+0xd1>
movsbl -0x11(%rbp),%ecx
xor    %eax,%eax
cmp    $0x53,%ecx
mov    %al,-0x25(%rbp)
je     1251 <receivefile+0xd1>
movsbl -0x11(%rbp),%eax
cmp    $0x51,%eax
setne  %al
mov    %al,-0x25(%rbp)
mov    -0x25(%rbp),%al
test   $0x1,%al
jne    1214 <receivefile+0x94>
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
cmpl   $0x1,-0x24(%rbp)
jne    1317 <receivefile+0x197>
movsbl -0x11(%rbp),%eax
cmp    $0x3e,%eax
jne    128e <receivefile+0x10e>
mov    -0x18(%rbp),%edi
call   1030 <close@plt>
jmp    131f <receivefile+0x19f>
mov    -0x18(%rbp),%edi
movsbl -0x11(%rbp),%esi
call   10a0 <writebyte@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
cmp    $0x100,%eax
jle    12e9 <receivefile+0x169>
movl   $0x0,-0x1c(%rbp)
mov    $0x2e,%edi
call   1060 <putconsole@plt>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
cmp    $0x48,%eax
jle    12e2 <receivefile+0x162>
mov    $0xa,%edi
call   1060 <putconsole@plt>
movl   $0x0,-0x20(%rbp)
mov    $0x0,%al
call   1090 <flushconsole@plt>
mov    -0x4(%rbp),%edi
lea    -0x11(%rbp),%rsi
call   1070 <readbyte@plt>
mov    %eax,-0x24(%rbp)
movsbl -0x11(%rbp),%eax
cmp    $0xd,%eax
jne    1312 <receivefile+0x192>
mov    -0x4(%rbp),%edi
mov    $0x2b,%esi
call   10a0 <writebyte@plt>
jmp    126a <receivefile+0xea>
mov    -0x18(%rbp),%edi
call   1030 <close@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<receivefile>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    0x2e2e(%rip),%rcx
mov    0x2e5f(%rip),%rax
mov    (%rax),%eax
or     (%rcx),%eax
mov    %rsi,%r14
mov    %edi,%r13d
mov    0x2e46(%rip),%rcx
or     (%rcx),%eax
mov    %rsi,%rdi
mov    %eax,%esi
mov    $0x1a4,%edx
call   1080 <open@plt>
test   %eax,%eax
js     12b5 <receivefile+0x135>
mov    %eax,%r15d
lea    0xe5b(%rip),%rdi
mov    %r14,%rsi
call   1040 <printconsole@plt>
xor    %eax,%eax
call   1090 <flushconsole@plt>
mov    %r13d,%edi
mov    $0x2b,%esi
call   10a0 <writebyte@plt>
lea    0x7(%rsp),%rbp
xchg   %ax,%ax
mov    %r13d,%edi
mov    %rbp,%rsi
call   1070 <readbyte@plt>
cmp    $0x1,%eax
jne    129e <receivefile+0x11e>
movzbl 0x7(%rsp),%eax
or     $0x2,%al
cmp    $0x53,%al
jne    11f0 <receivefile+0x70>
xor    %r12d,%r12d
lea    0x7(%rsp),%r14
xor    %ebx,%ebx
jmp    1225 <receivefile+0xa5>
nopl   0x0(%rax,%rax,1)
cmp    $0x1,%ebp
jne    129e <receivefile+0x11e>
movzbl 0x7(%rsp),%eax
cmp    $0x3e,%al
je     129e <receivefile+0x11e>
movsbl %al,%esi
mov    %r15d,%edi
call   10a0 <writebyte@plt>
cmp    $0x100,%ebx
jl     1260 <receivefile+0xe0>
mov    $0x2e,%edi
call   1060 <putconsole@plt>
cmp    $0x48,%r12d
jge    1265 <receivefile+0xe5>
add    $0x1,%r12d
jmp    1272 <receivefile+0xf2>
nopw   0x0(%rax,%rax,1)
add    $0x1,%ebx
jmp    127b <receivefile+0xfb>
mov    $0xa,%edi
call   1060 <putconsole@plt>
xor    %r12d,%r12d
xor    %ebx,%ebx
xor    %eax,%eax
call   1090 <flushconsole@plt>
mov    %r13d,%edi
mov    %r14,%rsi
call   1070 <readbyte@plt>
mov    %eax,%ebp
cmpb   $0xd,0x7(%rsp)
jne    1220 <receivefile+0xa0>
mov    %r13d,%edi
mov    $0x2b,%esi
call   10a0 <writebyte@plt>
jmp    1220 <receivefile+0xa0>
mov    %r15d,%edi
call   1030 <close@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d14(%rip),%rax
mov    (%rax),%edi
lea    0xd3b(%rip),%rsi
mov    %r14,%rdx
call   1050 <fprintf@plt>
mov    %r13d,%edi
mov    $0x3e,%esi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    10a0 <writebyte@plt>

LLVM IR (-O0):
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@stderr = global i32 0
@.str = [40 x i8] c"ERROR: Failed to open '%s' for writing\0A\00"
@.str.1 = [22 x i8] c"Receiving file '%s':\0A\00"
define void @receivefile(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %3
store i8* %1, i8** %4
%10 = load i8*, i8** %4
%11 = load i32, i32* @O_WRONLY
%12 = load i32, i32* @O_CREAT
%13 = or i32 %11, %12
%14 = load i32, i32* @O_TRUNC
%15 = or i32 %13, %14
%16 = call i32 @open(i8* %10, i32 %15, i32 420)
store i32 %16, i32* %6
%17 = load i32, i32* %6
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %25
19: ; preds = %2
%20 = load i32, i32* @stderr
%21 = load i8*, i8** %4
%22 = call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0), i8* %21)
%23 = load i32, i32* %3
%24 = call i32 @writebyte(i32 %23, i8 signext 62)
br label %87
25: ; preds = %2
%26 = load i8*, i8** %4
%27 = call i32 @printconsole(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i8* %26)
%28 = call i32 (...) @flushconsole()
%29 = load i32, i32* %3
%30 = call i32 @writebyte(i32 %29, i8 signext 43)
br label %31
31: ; preds = %45, %25
%32 = load i32, i32* %3
%33 = call i32 @readbyte(i32 %32, i8* %5)
store i32 %33, i32* %9
br label %34
34: ; preds = %31
%35 = load i32, i32* %9
%36 = icmp eq i32 %35, 1
br i1 %36, label %37, label %45
37: ; preds = %34
%38 = load i8, i8* %5
%39 = sext i8 %38 to i32
%40 = icmp ne i32 %39, 83
br i1 %40, label %41, label %45
41: ; preds = %37
%42 = load i8, i8* %5
%43 = sext i8 %42 to i32
%44 = icmp ne i32 %43, 81
br label %45
45: ; preds = %41, %37, %34
%46 = phi i1 [ false, %37 ], [ false, %34 ], [ %44, %41 ]
br i1 %46, label %31, label %47
47: ; preds = %45
store i32 0, i32* %7
store i32 0, i32* %8
br label %48
48: ; preds = %83, %47
%49 = load i32, i32* %9
%50 = icmp eq i32 %49, 1
br i1 %50, label %51, label %84
51: ; preds = %48
%52 = load i8, i8* %5
%53 = sext i8 %52 to i32
%54 = icmp eq i32 %53, 62
br i1 %54, label %55, label %58
55: ; preds = %51
%56 = load i32, i32* %6
%57 = call i32 @close(i32 %56)
br label %87
58: ; preds = %51
%59 = load i32, i32* %6
%60 = load i8, i8* %5
%61 = call i32 @writebyte(i32 %59, i8 signext %60)
%62 = load i32, i32* %7
%63 = add nsw i32 %62, 1
store i32 %63, i32* %7
%64 = icmp sgt i32 %63, 256
br i1 %64, label %65, label %74
65: ; preds = %58
store i32 0, i32* %7
%66 = call i32 @putconsole(i8 signext 46)
%67 = load i32, i32* %8
%68 = add nsw i32 %67, 1
store i32 %68, i32* %8
%69 = icmp sgt i32 %68, 72
br i1 %69, label %70, label %72
70: ; preds = %65
%71 = call i32 @putconsole(i8 signext 10)
store i32 0, i32* %8
br label %72
72: ; preds = %70, %65
%73 = call i32 (...) @flushconsole()
br label %74
74: ; preds = %72, %58
%75 = load i32, i32* %3
%76 = call i32 @readbyte(i32 %75, i8* %5)
store i32 %76, i32* %9
%77 = load i8, i8* %5
%78 = sext i8 %77 to i32
%79 = icmp eq i32 %78, 13
br i1 %79, label %80, label %83
80: ; preds = %74
%81 = load i32, i32* %3
%82 = call i32 @writebyte(i32 %81, i8 signext 43)
br label %83
83: ; preds = %80, %74
br label %48
84: ; preds = %48
%85 = load i32, i32* %6
%86 = call i32 @close(i32 %85)
br label %87
87: ; preds = %84, %55, %19
ret void
}

LLVM IR (-O2):
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@stderr = global i32 0
@.str = [40 x i8] c"ERROR: Failed to open '%s' for writing\0A\00"
@.str.1 = [22 x i8] c"Receiving file '%s':\0A\00"
define void @receivefile(i32 %0, i8* %1) {
%3 = alloca i8
%4 = load i32, i32* @O_WRONLY
%5 = load i32, i32* @O_CREAT
%6 = or i32 %5, %4
%7 = load i32, i32* @O_TRUNC
%8 = or i32 %6, %7
%9 = tail call i32 @open(i8* %1, i32 %8, i32 420)
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %15
11: ; preds = %2
%12 = load i32, i32* @stderr
%13 = tail call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0), i8* %1)
%14 = tail call i32 @writebyte(i32 %0, i8 signext 62)
br label %58
15: ; preds = %2
%16 = tail call i32 @printconsole(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i8* %1)
%17 = tail call i32 (...) @flushconsole()
%18 = tail call i32 @writebyte(i32 %0, i8 signext 43)
br label %19
19: ; preds = %23, %15
%20 = call i32 @readbyte(i32 %0, i8* %3)
%21 = freeze i32 %20
%22 = icmp eq i32 %21, 1
br i1 %22, label %23, label %56
23: ; preds = %19
%24 = load i8, i8* %3
switch i8 %24, label %19 [
i8 83, label %25
i8 81, label %25
]
25: ; preds = %23, %23
br label %26
26: ; preds = %25, %54
%27 = phi i32 [ %48, %54 ], [ 0, %25 ]
%28 = phi i32 [ %47, %54 ], [ 0, %25 ]
%29 = load i8, i8* %3
%30 = icmp eq i8 %29, 62
br i1 %30, label %31, label %33
31: ; preds = %26
%32 = call i32 @close(i32 %9)
br label %58
33: ; preds = %26
%34 = call i32 @writebyte(i32 %9, i8 signext %29)
%35 = add nsw i32 %28, 1
%36 = icmp sgt i32 %28, 255
br i1 %36, label %37, label %46
37: ; preds = %33
%38 = call i32 @putconsole(i8 signext 46)
%39 = add nsw i32 %27, 1
%40 = icmp sgt i32 %27, 71
br i1 %40, label %41, label %43
41: ; preds = %37
%42 = call i32 @putconsole(i8 signext 10)
br label %43
43: ; preds = %41, %37
%44 = phi i32 [ 0, %41 ], [ %39, %37 ]
%45 = call i32 (...) @flushconsole()
br label %46
46: ; preds = %43, %33
%47 = phi i32 [ 0, %43 ], [ %35, %33 ]
%48 = phi i32 [ %44, %43 ], [ %27, %33 ]
%49 = call i32 @readbyte(i32 %0, i8* %3)
%50 = load i8, i8* %3
%51 = icmp eq i8 %50, 13
br i1 %51, label %52, label %54
52: ; preds = %46
%53 = call i32 @writebyte(i32 %0, i8 signext 43)
br label %54
54: ; preds = %52, %46
%55 = icmp eq i32 %49, 1
br i1 %55, label %26, label %56
56: ; preds = %19, %54
%57 = call i32 @close(i32 %9)
br label %58
58: ; preds = %56, %31, %11
ret void
}