#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 recv(int, char *, int, int /*<<< orphan*/);
int recv_data(int fd, char *buf, int nReadBytes) {
  int cnt = 0;
  int rv = -1;
  while (cnt < nReadBytes && rv) {
    rv = recv(fd, (char *)buf + cnt, 1, 0);
    if (buf[cnt] == '\n') {
      return cnt;
    }
    if (rv < 0) {
      return -1;
    }
    ++cnt;
  }
  return cnt;
}


x86 assembly (clang, -O0):
<recv_data>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
movl   $0xffffffff,-0x1c(%rbp)
mov    -0x18(%rbp),%ecx
xor    %eax,%eax
cmp    -0x14(%rbp),%ecx
mov    %al,-0x1d(%rbp)
jge    114b <recv_data+0x3b>
cmpl   $0x0,-0x1c(%rbp)
setne  %al
mov    %al,-0x1d(%rbp)
mov    -0x1d(%rbp),%al
test   $0x1,%al
jne    115b <recv_data+0x4b>
jmp    11bc <recv_data+0xac>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
movslq -0x18(%rbp),%rax
add    %rax,%rsi
mov    $0x1,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    1198 <recv_data+0x88>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11c2 <recv_data+0xb2>
cmpl   $0x0,-0x1c(%rbp)
jge    11ae <recv_data+0x9e>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c2 <recv_data+0xb2>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1130 <recv_data+0x20>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<recv_data>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
test   %edx,%edx
jle    115c <recv_data+0x4c>
mov    %rsi,%r14
mov    %edi,%ebp
mov    %edx,%r15d
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nop
lea    (%r14,%rbx,1),%rsi
mov    %ebp,%edi
mov    $0x1,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
cmpb   $0xa,(%r14,%rbx,1)
je     1165 <recv_data+0x55>
test   %eax,%eax
js     1160 <recv_data+0x50>
add    $0x1,%rbx
cmp    %r15,%rbx
jae    1165 <recv_data+0x55>
test   %eax,%eax
jne    1130 <recv_data+0x20>
jmp    1165 <recv_data+0x55>
xor    %ebx,%ebx
jmp    1165 <recv_data+0x55>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i32 @recv_data(i32 %0, i8* %1, i32 %2) {
%4 = icmp sgt i32 %2, 0
br i1 %4, label %5, label %24
5: ; preds = %3
%6 = zext i32 %2 to i64
br label %7
7: ; preds = %5, %15
%8 = phi i64 [ 0, %5 ], [ %16, %15 ]
%9 = getelementptr inbounds i8, i8* %1, i64 %8
%10 = tail call i32 @recv(i32 %0, i8* %9, i32 1, i32 0)
%11 = load i8, i8* %9
%12 = icmp eq i8 %11, 10
br i1 %12, label %22, label %13
13: ; preds = %7
%14 = icmp slt i32 %10, 0
br i1 %14, label %24, label %15
15: ; preds = %13
%16 = add nuw nsw i64 %8, 1
%17 = icmp ult i64 %16, %6
%18 = icmp ne i32 %10, 0
%19 = select i1 %17, i1 %18, i1 false
br i1 %19, label %7, label %20
20: ; preds = %15
%21 = trunc i64 %16 to i32
br label %24
22: ; preds = %7
%23 = trunc i64 %8 to i32
br label %24
24: ; preds = %13, %20, %22, %3
%25 = phi i32 [ 0, %3 ], [ %21, %20 ], [ %23, %22 ], [ -1, %13 ]
ret i32 %25
}