#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 read(int, char *, int);
int /*<<< orphan*/ write(int, char *, int);
int recvreply(int fd) {
  char buf[100];
  int rr;
  int out = 0;
  int ret = 0;
  int off = 0;
  while ((rr = read(fd, buf, sizeof(buf))) > 0) {
    if (!out) {
      if (buf[0] == '#') {
        off = 1;
        out = 1;
      } else {
        ret = -1;
        out = 2;
      }
    } else if (off)
      off = 0;
    write(out, buf + off, rr - off);
  }
  return ret;
}


x86 assembly (clang, -O0):
<recvreply>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x0,-0x78(%rbp)
movl   $0x0,-0x7c(%rbp)
movl   $0x0,-0x80(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x70(%rbp),%rsi
mov    $0x64,%edx
call   1040 <read@plt>
mov    %eax,-0x74(%rbp)
cmp    $0x0,%eax
jle    11d1 <recvreply+0xb1>
cmpl   $0x0,-0x78(%rbp)
jne    119d <recvreply+0x7d>
movsbl -0x70(%rbp),%eax
cmp    $0x23,%eax
jne    118a <recvreply+0x6a>
movl   $0x1,-0x80(%rbp)
movl   $0x1,-0x78(%rbp)
jmp    1198 <recvreply+0x78>
movl   $0xffffffff,-0x7c(%rbp)
movl   $0x2,-0x78(%rbp)
jmp    11b3 <recvreply+0x93>
cmpl   $0x0,-0x80(%rbp)
je     11ae <recvreply+0x8e>
movl   $0x0,-0x80(%rbp)
jmp    11b3 <recvreply+0x93>
mov    -0x78(%rbp),%edi
lea    -0x70(%rbp),%rsi
movslq -0x80(%rbp),%rax
add    %rax,%rsi
mov    -0x74(%rbp),%edx
sub    -0x80(%rbp),%edx
call   1030 <write@plt>
jmp    1143 <recvreply+0x23>
mov    -0x7c(%rbp),%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<recvreply>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x68,%rsp
mov    %edi,%ebx
mov    %rsp,%rsi
mov    $0x64,%edx
call   1040 <read@plt>
test   %eax,%eax
jle    11ac <recvreply+0x8c>
xor    %r12d,%r12d
mov    $0x1,%r13d
mov    %rsp,%r14
xor    %r15d,%r15d
xor    %ebp,%ebp
jmp    11a3 <recvreply+0x83>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
xor    %ecx,%ecx
cmpb   $0x23,(%rsp)
sete   %cl
mov    $0xffffffff,%edx
cmovne %edx,%r15d
cmove  %r13d,%r12d
mov    $0x2,%ebp
sub    %ecx,%ebp
mov    %r12d,%ecx
lea    (%rsp,%rcx,1),%rsi
sub    %r12d,%eax
mov    %ebp,%edi
mov    %eax,%edx
call   1030 <write@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x64,%edx
call   1040 <read@plt>
test   %eax,%eax
jle    11af <recvreply+0x8f>
test   %ebp,%ebp
je     1160 <recvreply+0x40>
xor    %r12d,%r12d
jmp    117d <recvreply+0x5d>
xor    %r15d,%r15d
mov    %r15d,%eax
add    $0x68,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @recvreply(i32 %0) {
%2 = alloca i32
%3 = alloca [100 x i8]
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %2
store i32 0, i32* %5
store i32 0, i32* %6
store i32 0, i32* %7
br label %8
8: ; preds = %29, %1
%9 = load i32, i32* %2
%10 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%11 = call i32 @read(i32 %9, i8* %10, i32 100)
store i32 %11, i32* %4
%12 = icmp sgt i32 %11, 0
br i1 %12, label %13, label %39
13: ; preds = %8
%14 = load i32, i32* %5
%15 = icmp ne i32 %14, 0
br i1 %15, label %24, label %16
16: ; preds = %13
%17 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%18 = load i8, i8* %17
%19 = sext i8 %18 to i32
%20 = icmp eq i32 %19, 35
br i1 %20, label %21, label %22
21: ; preds = %16
store i32 1, i32* %7
store i32 1, i32* %5
br label %23
22: ; preds = %16
store i32 -1, i32* %6
store i32 2, i32* %5
br label %23
23: ; preds = %22, %21
br label %29
24: ; preds = %13
%25 = load i32, i32* %7
%26 = icmp ne i32 %25, 0
br i1 %26, label %27, label %28
27: ; preds = %24
store i32 0, i32* %7
br label %28
28: ; preds = %27, %24
br label %29
29: ; preds = %28, %23
%30 = load i32, i32* %5
%31 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%32 = load i32, i32* %7
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds i8, i8* %31, i64 %33
%35 = load i32, i32* %4
%36 = load i32, i32* %7
%37 = sub nsw i32 %35, %36
%38 = call i32 @write(i32 %30, i8* %34, i32 %37)
br label %8
39: ; preds = %8
%40 = load i32, i32* %6
ret i32 %40
}

LLVM IR (-O2):
define i32 @recvreply(i32 %0) {
%2 = alloca [100 x i8]
%3 = getelementptr inbounds [100 x i8], [100 x i8]* %2, i64 0, i64 0
%4 = call i32 @read(i32 %0, i8* %3, i32 100)
%5 = icmp sgt i32 %4, 0
br i1 %5, label %6, label %28
6: ; preds = %1, %18
%7 = phi i32 [ %26, %18 ], [ %4, %1 ]
%8 = phi i32 [ %21, %18 ], [ 0, %1 ]
%9 = phi i32 [ %20, %18 ], [ 0, %1 ]
%10 = phi i32 [ %19, %18 ], [ 0, %1 ]
%11 = icmp eq i32 %10, 0
br i1 %11, label %12, label %18
12: ; preds = %6
%13 = load i8, i8* %3
%14 = icmp eq i8 %13, 35
%15 = select i1 %14, i32 1, i32 2
%16 = select i1 %14, i32 %9, i32 -1
%17 = select i1 %14, i32 1, i32 %8
br label %18
18: ; preds = %6, %12
%19 = phi i32 [ %15, %12 ], [ %10, %6 ]
%20 = phi i32 [ %16, %12 ], [ %9, %6 ]
%21 = phi i32 [ %17, %12 ], [ 0, %6 ]
%22 = zext i32 %21 to i64
%23 = getelementptr inbounds [100 x i8], [100 x i8]* %2, i64 0, i64 %22
%24 = sub nsw i32 %7, %21
%25 = call i32 @write(i32 %19, i8* %23, i32 %24)
%26 = call i32 @read(i32 %0, i8* %3, i32 100)
%27 = icmp sgt i32 %26, 0
br i1 %27, label %6, label %28
28: ; preds = %18, %1
%29 = phi i32 [ 0, %1 ], [ %20, %18 ]
ret i32 %29
}