#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 /*<<< orphan*/ shout(char *, char *);
int /*<<< orphan*/ socket_error(char *);
int read_res(int sock, char *buffer, int max_len) {
  int len = 0;
  int ret;
  char b;
  while (len < max_len) {
    ret = recv(sock, &b, 1, 0);
    if (ret > 0) {
      buffer[len++] = b;
      if (len >= 4 && buffer[len - 4] == '\r' && buffer[len - 3] == '\n' &&
          buffer[len - 2] == '\r' && buffer[len - 1] == '\n') {
        buffer[len] = 0;
        shout("[READ RES:] \n%s\n", buffer);
        return len;
      }
    } else if (ret == 0) {
      return 0;
    } else {
      socket_error("recv:");
      return -1;
    }
  }
  return -1;
}


x86 assembly (clang, -O0):
<read_res>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    125e <read_res+0x12e>
mov    -0x8(%rbp),%edi
lea    -0x1d(%rbp),%rsi
mov    $0x1,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jle    122b <read_res+0xfb>
mov    -0x1d(%rbp),%dl
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x18(%rbp)
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
cmpl   $0x4,-0x18(%rbp)
jl     1226 <read_res+0xf6>
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%ecx
sub    $0x4,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xd,%eax
jne    1226 <read_res+0xf6>
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%ecx
sub    $0x3,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    1226 <read_res+0xf6>
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%ecx
sub    $0x2,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xd,%eax
jne    1226 <read_res+0xf6>
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    1226 <read_res+0xf6>
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rsi
lea    0xdea(%rip),%rdi
call   1050 <shout@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1265 <read_res+0x135>
jmp    1259 <read_res+0x129>
cmpl   $0x0,-0x1c(%rbp)
jne    1241 <read_res+0x111>
movl   $0x0,-0x4(%rbp)
jmp    1265 <read_res+0x135>
lea    0xdc9(%rip),%rdi
call   1040 <socket_error@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1265 <read_res+0x135>
jmp    1149 <read_res+0x19>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_res>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    $0xffffffff,%r14d
test   %edx,%edx
jle    11f0 <read_res+0xc0>
mov    %rsi,%rbx
mov    %edi,%r12d
mov    %edx,%r13d
mov    $0xffffffff,%r14d
xor    %ebp,%ebp
lea    0x7(%rsp),%r15
jmp    117d <read_res+0x4d>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
add    $0x1,%rbp
add    $0x1,%r14
cmp    %rbp,%r13
je     11ea <read_res+0xba>
mov    %r12d,%edi
mov    %r15,%rsi
mov    $0x1,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %eax,%eax
jle    11dc <read_res+0xac>
movzbl 0x7(%rsp),%eax
mov    %al,(%rbx,%rbp,1)
cmp    $0x3,%rbp
jb     1170 <read_res+0x40>
lea    -0x2(%r14),%ecx
cmpb   $0xd,(%rbx,%rcx,1)
jne    1170 <read_res+0x40>
lea    -0x1(%r14),%ecx
cmpb   $0xa,(%rbx,%rcx,1)
jne    1170 <read_res+0x40>
mov    %r14d,%ecx
cmpb   $0xd,(%rbx,%rcx,1)
jne    1170 <read_res+0x40>
cmp    $0xa,%al
jne    1170 <read_res+0x40>
lea    0x1(%rbp),%r14d
movb   $0x0,0x1(%rbx,%rbp,1)
lea    0xe2e(%rip),%rdi
mov    %rbx,%rsi
call   1050 <shout@plt>
jmp    11f0 <read_res+0xc0>
je     1202 <read_res+0xd2>
lea    0xe2c(%rip),%rdi
call   1040 <socket_error@plt>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %r14d,%r14d
jmp    11f0 <read_res+0xc0>

LLVM IR (-O0):
@.str = [17 x i8] c"[READ RES:] \0A%s\0A\00"
@.str.1 = [6 x i8] c"recv:\00"
define i32 @read_res(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i32 0, i32* %8
br label %11
11: ; preds = %80, %3
%12 = load i32, i32* %8
%13 = load i32, i32* %7
%14 = icmp slt i32 %12, %13
br i1 %14, label %15, label %81
15: ; preds = %11
%16 = load i32, i32* %5
%17 = call i32 @recv(i32 %16, i8* %10, i32 1, i32 0)
store i32 %17, i32* %9
%18 = load i32, i32* %9
%19 = icmp sgt i32 %18, 0
br i1 %19, label %20, label %74
20: ; preds = %15
%21 = load i8, i8* %10
%22 = load i8*, i8** %6
%23 = load i32, i32* %8
%24 = add nsw i32 %23, 1
store i32 %24, i32* %8
%25 = sext i32 %23 to i64
%26 = getelementptr inbounds i8, i8* %22, i64 %25
store i8 %21, i8* %26
%27 = load i32, i32* %8
%28 = icmp sge i32 %27, 4
br i1 %28, label %29, label %73
29: ; preds = %20
%30 = load i8*, i8** %6
%31 = load i32, i32* %8
%32 = sub nsw i32 %31, 4
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds i8, i8* %30, i64 %33
%35 = load i8, i8* %34
%36 = sext i8 %35 to i32
%37 = icmp eq i32 %36, 13
br i1 %37, label %38, label %73
38: ; preds = %29
%39 = load i8*, i8** %6
%40 = load i32, i32* %8
%41 = sub nsw i32 %40, 3
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds i8, i8* %39, i64 %42
%44 = load i8, i8* %43
%45 = sext i8 %44 to i32
%46 = icmp eq i32 %45, 10
br i1 %46, label %47, label %73
47: ; preds = %38
%48 = load i8*, i8** %6
%49 = load i32, i32* %8
%50 = sub nsw i32 %49, 2
%51 = sext i32 %50 to i64
%52 = getelementptr inbounds i8, i8* %48, i64 %51
%53 = load i8, i8* %52
%54 = sext i8 %53 to i32
%55 = icmp eq i32 %54, 13
br i1 %55, label %56, label %73
56: ; preds = %47
%57 = load i8*, i8** %6
%58 = load i32, i32* %8
%59 = sub nsw i32 %58, 1
%60 = sext i32 %59 to i64
%61 = getelementptr inbounds i8, i8* %57, i64 %60
%62 = load i8, i8* %61
%63 = sext i8 %62 to i32
%64 = icmp eq i32 %63, 10
br i1 %64, label %65, label %73
65: ; preds = %56
%66 = load i8*, i8** %6
%67 = load i32, i32* %8
%68 = sext i32 %67 to i64
%69 = getelementptr inbounds i8, i8* %66, i64 %68
store i8 0, i8* %69
%70 = load i8*, i8** %6
%71 = call i32 @shout(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* %70)
%72 = load i32, i32* %8
store i32 %72, i32* %4
br label %82
73: ; preds = %56, %47, %38, %29, %20
br label %80
74: ; preds = %15
%75 = load i32, i32* %9
%76 = icmp eq i32 %75, 0
br i1 %76, label %77, label %78
77: ; preds = %74
store i32 0, i32* %4
br label %82
78: ; preds = %74
%79 = call i32 @socket_error(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %4
br label %82
80: ; preds = %73
br label %11
81: ; preds = %11
store i32 -1, i32* %4
br label %82
82: ; preds = %81, %78, %77, %65
%83 = load i32, i32* %4
ret i32 %83
}

LLVM IR (-O2):
@.str = [17 x i8] c"[READ RES:] \0A%s\0A\00"
@.str.1 = [6 x i8] c"recv:\00"
define i32 @read_res(i32 %0, i8* %1, i32 %2) {
%4 = alloca i8
%5 = icmp sgt i32 %2, 0
br i1 %5, label %6, label %48
6: ; preds = %3
%7 = zext i32 %2 to i64
br label %8
8: ; preds = %6, %46
%9 = phi i64 [ 0, %6 ], [ %14, %46 ]
%10 = call i32 @recv(i32 %0, i8* %4, i32 1, i32 0)
%11 = icmp sgt i32 %10, 0
br i1 %11, label %12, label %42
12: ; preds = %8
%13 = load i8, i8* %4
%14 = add nuw nsw i64 %9, 1
%15 = getelementptr inbounds i8, i8* %1, i64 %9
store i8 %13, i8* %15
%16 = icmp ugt i64 %9, 2
br i1 %16, label %17, label %46
17: ; preds = %12
%18 = add nuw i64 %9, 4294967293
%19 = and i64 %18, 4294967295
%20 = getelementptr inbounds i8, i8* %1, i64 %19
%21 = load i8, i8* %20
%22 = icmp eq i8 %21, 13
br i1 %22, label %23, label %46
23: ; preds = %17
%24 = add nuw i64 %9, 4294967294
%25 = and i64 %24, 4294967295
%26 = getelementptr inbounds i8, i8* %1, i64 %25
%27 = load i8, i8* %26
%28 = icmp eq i8 %27, 10
br i1 %28, label %29, label %46
29: ; preds = %23
%30 = add nuw i64 %9, 4294967295
%31 = and i64 %30, 4294967295
%32 = getelementptr inbounds i8, i8* %1, i64 %31
%33 = load i8, i8* %32
%34 = icmp eq i8 %33, 13
%35 = icmp eq i8 %13, 10
%36 = select i1 %34, i1 %35, i1 false
br i1 %36, label %37, label %46
37: ; preds = %29
%38 = trunc i64 %14 to i32
%39 = and i64 %14, 4294967295
%40 = getelementptr inbounds i8, i8* %1, i64 %39
store i8 0, i8* %40
%41 = call i32 @shout(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* %1)
br label %48
42: ; preds = %8
%43 = icmp eq i32 %10, 0
br i1 %43, label %48, label %44
44: ; preds = %42
%45 = call i32 @socket_error(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
br label %48
46: ; preds = %12, %17, %23, %29
%47 = icmp eq i64 %14, %7
br i1 %47, label %48, label %8
48: ; preds = %46, %3, %42, %44, %37
%49 = phi i32 [ %38, %37 ], [ -1, %44 ], [ 0, %42 ], [ -1, %3 ], [ -1, %46 ]
ret i32 %49
}