#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*/ u_int;
typedef scalar_t__ ssize_t;

/* Variables and functions */
scalar_t__ EINTR;
int /*<<< orphan*/ err_msg(char *);
scalar_t__ errno;
scalar_t__ recv(int /*<<< orphan*/, char *, size_t, int /*<<< orphan*/);
scalar_t__ strstr(char *, char *);
ssize_t recv_msg(u_int fd, char *usr_buff, size_t nbytes) {
  size_t nleft = nbytes;
  ssize_t nrecv;
  char *recv_buff = usr_buff;
  while (nleft > 0) {
    if ((nrecv = recv(fd, recv_buff, nleft, 0)) < 0) {
      if (errno == EINTR)
        nrecv = 0;
      else
        err_msg("No more data in recv()");
      return 0;
    } else if ((nrecv == 0) || strstr(recv_buff, "\r\n"))
      break;
    nleft -= nrecv;
    recv_buff += nrecv;
  }
  return nrecv;
}


x86 assembly (clang, -O0):
<recv_msg>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x28(%rbp)
jbe    120d <recv_msg+0xdd>
mov    -0xc(%rbp),%edi
mov    -0x38(%rbp),%rsi
mov    -0x28(%rbp),%rdx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
jge    11be <recv_msg+0x8e>
mov    0x2e5b(%rip),%rax
mov    (%rax),%rax
mov    0x2e49(%rip),%rcx
cmp    (%rcx),%rax
jne    11a5 <recv_msg+0x75>
movq   $0x0,-0x30(%rbp)
jmp    11b1 <recv_msg+0x81>
lea    0xe54(%rip),%rdi
call   1040 <err_msg@plt>
movq   $0x0,-0x8(%rbp)
jmp    1215 <recv_msg+0xe5>
cmpq   $0x0,-0x30(%rbp)
je     11e3 <recv_msg+0xb3>
mov    -0x38(%rbp),%rdi
lea    0xe43(%rip),%rsi
call   1050 <strstr@plt>
cmp    $0x0,%rax
je     11e8 <recv_msg+0xb8>
jmp    120d <recv_msg+0xdd>
jmp    11ed <recv_msg+0xbd>
mov    -0x30(%rbp),%rcx
mov    -0x28(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x30(%rbp),%rax
add    -0x38(%rbp),%rax
mov    %rax,-0x38(%rbp)
jmp    1153 <recv_msg+0x23>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<recv_msg>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
test   %rdx,%rdx
je     1187 <recv_msg+0x57>
mov    %rdx,%rbx
mov    %rsi,%r14
mov    %edi,%r15d
lea    0xeca(%rip),%r12
nopl   (%rax)
mov    %r15d,%edi
mov    %r14,%rsi
mov    %rbx,%rdx
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %rax,%rax
js     1189 <recv_msg+0x59>
je     11ab <recv_msg+0x7b>
mov    %rax,%rbp
mov    %r14,%rdi
mov    %r12,%rsi
call   1050 <strstr@plt>
test   %rax,%rax
jne    11ad <recv_msg+0x7d>
sub    %rbp,%rbx
add    %rbp,%r14
test   %rbx,%rbx
jne    1150 <recv_msg+0x20>
jmp    11ad <recv_msg+0x7d>
jmp    11ad <recv_msg+0x7d>
mov    0x2e50(%rip),%rax
mov    (%rax),%rax
mov    0x2e3e(%rip),%rcx
cmp    (%rcx),%rax
je     11ab <recv_msg+0x7b>
lea    0xe5a(%rip),%rdi
call   1040 <err_msg@plt>
xor    %ebp,%ebp
mov    %rbp,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i64 0
@EINTR = global i64 0
@.str = [23 x i8] c"No more data in recv()\00"
@.str.1 = [3 x i8] c"\0D\0A\00"
define i64 @recv_msg(i32 %0, i8* %1, i64 %2) {
%4 = alloca i64
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
%9 = alloca i64
%10 = alloca i8*
store i32 %0, i32* %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%11 = load i64, i64* %7
store i64 %11, i64* %8
%12 = load i8*, i8** %6
store i8* %12, i8** %10
br label %13
13: ; preds = %39, %3
%14 = load i64, i64* %8
%15 = icmp ugt i64 %14, 0
br i1 %15, label %16, label %46
16: ; preds = %13
%17 = load i32, i32* %5
%18 = load i8*, i8** %10
%19 = load i64, i64* %8
%20 = call i64 @recv(i32 %17, i8* %18, i64 %19, i32 0)
store i64 %20, i64* %9
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %30
22: ; preds = %16
%23 = load i64, i64* @errno
%24 = load i64, i64* @EINTR
%25 = icmp eq i64 %23, %24
br i1 %25, label %26, label %27
26: ; preds = %22
store i64 0, i64* %9
br label %29
27: ; preds = %22
%28 = call i32 @err_msg(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
br label %29
29: ; preds = %27, %26
store i64 0, i64* %4
br label %48
30: ; preds = %16
%31 = load i64, i64* %9
%32 = icmp eq i64 %31, 0
br i1 %32, label %37, label %33
33: ; preds = %30
%34 = load i8*, i8** %10
%35 = call i64 @strstr(i8* %34, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%36 = icmp ne i64 %35, 0
br i1 %36, label %37, label %38
37: ; preds = %33, %30
br label %46
38: ; preds = %33
br label %39
39: ; preds = %38
%40 = load i64, i64* %9
%41 = load i64, i64* %8
%42 = sub i64 %41, %40
store i64 %42, i64* %8
%43 = load i64, i64* %9
%44 = load i8*, i8** %10
%45 = getelementptr inbounds i8, i8* %44, i64 %43
store i8* %45, i8** %10
br label %13
46: ; preds = %37, %13
%47 = load i64, i64* %9
store i64 %47, i64* %4
br label %48
48: ; preds = %46, %29
%49 = load i64, i64* %4
ret i64 %49
}

LLVM IR (-O2):
@errno = global i64 0
@EINTR = global i64 0
@.str = [23 x i8] c"No more data in recv()\00"
@.str.1 = [3 x i8] c"\0D\0A\00"
define i64 @recv_msg(i32 %0, i8* %1, i64 %2) {
%4 = icmp eq i64 %2, 0
br i1 %4, label %25, label %5
5: ; preds = %3, %18
%6 = phi i8* [ %22, %18 ], [ %1, %3 ]
%7 = phi i64 [ %21, %18 ], [ %2, %3 ]
%8 = tail call i64 @recv(i32 %0, i8* %6, i64 %7, i32 0)
%9 = icmp slt i64 %8, 0
br i1 %9, label %10, label %16
10: ; preds = %5
%11 = load i64, i64* @errno
%12 = load i64, i64* @EINTR
%13 = icmp eq i64 %11, %12
br i1 %13, label %25, label %14
14: ; preds = %10
%15 = tail call i32 @err_msg(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
br label %25
16: ; preds = %5
%17 = icmp eq i64 %8, 0
br i1 %17, label %25, label %18
18: ; preds = %16
%19 = tail call i64 @strstr(i8* %6, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%20 = icmp ne i64 %19, 0
%21 = sub i64 %7, %8
%22 = getelementptr inbounds i8, i8* %6, i64 %8
%23 = icmp eq i64 %21, 0
%24 = select i1 %20, i1 true, i1 %23
br i1 %24, label %25, label %5
25: ; preds = %16, %18, %3, %14, %10
%26 = phi i64 [ 0, %10 ], [ 0, %14 ], [ undef, %3 ], [ 0, %16 ], [ %8, %18 ]
ret i64 %26
}