#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 read(int, char *, int);
int recv_msg(int sockfd, char *msg, size_t max) {
  int n, rc;
  char c, *buf;
  for (n = 1; n <= max; n++) {
    rc = read(sockfd, &c, 1);
    if (rc == 1) {
      if (c == '\r')
        continue;
      else if (c == '\n') {
        *msg = 0;
        return n;
      } else
        *msg++ = c;
    }
  }
  *buf = 0;
  return rc;
}


x86 assembly (clang, -O0):
<recv_msg>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x1,-0x1c(%rbp)
movslq -0x1c(%rbp),%rax
cmp    -0x18(%rbp),%rax
ja     11b8 <recv_msg+0xa8>
mov    -0x8(%rbp),%edi
lea    -0x21(%rbp),%rsi
mov    $0x1,%edx
call   1030 <read@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x1,-0x20(%rbp)
jne    11a5 <recv_msg+0x95>
movsbl -0x21(%rbp),%eax
cmp    $0xd,%eax
jne    1168 <recv_msg+0x58>
jmp    11aa <recv_msg+0x9a>
movsbl -0x21(%rbp),%eax
cmp    $0xa,%eax
jne    1187 <recv_msg+0x77>
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11c5 <recv_msg+0xb5>
mov    -0x21(%rbp),%cl
mov    -0x10(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x10(%rbp)
mov    %cl,(%rax)
jmp    11a0 <recv_msg+0x90>
jmp    11a5 <recv_msg+0x95>
jmp    11aa <recv_msg+0x9a>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    112a <recv_msg+0x1a>
mov    -0x30(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<recv_msg>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %edi,%ebp
mov    $0x1,%ebx
lea    0x7(%rsp),%r15
jmp    1134 <recv_msg+0x24>
nopl   0x0(%rax,%rax,1)
add    $0x1,%rbx
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0x1,%edx
call   1030 <read@plt>
cmp    $0x1,%eax
jne    1130 <recv_msg+0x20>
movzbl 0x7(%rsp),%eax
cmp    $0xd,%al
je     1130 <recv_msg+0x20>
cmp    $0xa,%al
je     115e <recv_msg+0x4e>
mov    %al,(%r14)
add    $0x1,%r14
jmp    1130 <recv_msg+0x20>
movb   $0x0,(%r14)
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i32 @recv_msg(i32 %0, i8* nocapture writeonly %1, i64 %2) {
%4 = alloca i8
br label %5
5: ; preds = %20, %3
%6 = phi i64 [ %22, %20 ], [ 1, %3 ]
%7 = phi i8* [ %21, %20 ], [ %1, %3 ]
%8 = icmp ule i64 %6, %2
tail call void @llvm.assume(i1 %8)
%9 = call i32 @read(i32 %0, i8* %4, i32 1)
%10 = icmp ne i32 %9, 1
%11 = load i8, i8* %4
%12 = icmp eq i8 %11, 13
%13 = select i1 %10, i1 true, i1 %12
br i1 %13, label %20, label %14
14: ; preds = %5
%15 = icmp eq i8 %11, 10
br i1 %15, label %16, label %18
16: ; preds = %14
%17 = trunc i64 %6 to i32
store i8 0, i8* %7
ret i32 %17
18: ; preds = %14
%19 = getelementptr inbounds i8, i8* %7, i64 1
store i8 %11, i8* %7
br label %20
20: ; preds = %5, %18
%21 = phi i8* [ %7, %5 ], [ %19, %18 ]
%22 = add nuw i64 %6, 1
br label %5
}