#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 socklen_t;

/* Variables and functions */
int EPIPE;
int /*<<< orphan*/ MSG_NOSIGNAL;
int errno;
int /*<<< orphan*/ printf(char *);
int recv(int, void *, int, int /*<<< orphan*/);
int terima(int sock, void *buf, socklen_t len) {
  int pos = 0;
  int size = len;
  int res;
  int err;
  while (pos < len) {
    res = recv(sock, buf + pos, size, MSG_NOSIGNAL);
    err = errno;
    if (res == -1 || res == 0) {
      if (err == EPIPE)
        printf("broken pipe for recv\n");
      break;
    }
    pos += res;
    size -= res;
  }
  return err;
}


x86 assembly (clang, -O0):
<terima>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11c9 <terima+0xa9>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
movslq -0x18(%rbp),%rax
add    %rax,%rsi
mov    -0x1c(%rbp),%edx
mov    0x2e6d(%rip),%rax
mov    (%rax),%ecx
call   1030 <recv@plt>
mov    %eax,-0x20(%rbp)
mov    0x2e64(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
je     118d <terima+0x6d>
cmpl   $0x0,-0x20(%rbp)
jne    11b0 <terima+0x90>
mov    -0x24(%rbp),%eax
mov    0x2e49(%rip),%rcx
cmp    (%rcx),%eax
jne    11ab <terima+0x8b>
lea    0xe5a(%rip),%rdi
call   1040 <printf@plt>
jmp    11c9 <terima+0xa9>
mov    -0x20(%rbp),%eax
add    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x20(%rbp),%ecx
mov    -0x1c(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x1c(%rbp)
jmp    113f <terima+0x1f>
mov    -0x24(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<terima>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %edx,%edx
jle    1180 <terima+0x60>
mov    %edx,%r12d
mov    %rsi,%r14
mov    %edi,%r15d
xor    %ebp,%ebp
mov    0x2e8f(%rip),%r13
mov    %edx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
movslq %ebp,%rsi
add    %r14,%rsi
mov    0x0(%r13),%ecx
mov    %r15d,%edi
mov    %ebx,%edx
call   1030 <recv@plt>
lea    0x1(%rax),%ecx
cmp    $0x1,%ecx
jbe    1182 <terima+0x62>
add    %eax,%ebp
sub    %eax,%ebx
cmp    %r12d,%ebp
jl     1150 <terima+0x30>
mov    0x2e5c(%rip),%rax
mov    (%rax),%ebx
jmp    11a2 <terima+0x82>
jmp    11a2 <terima+0x82>
mov    0x2e4f(%rip),%rax
mov    (%rax),%ebx
mov    0x2e4e(%rip),%rax
cmp    (%rax),%ebx
jne    11a2 <terima+0x82>
lea    0xe63(%rip),%rdi
call   1040 <puts@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@MSG_NOSIGNAL = global i32 0
@errno = global i32 0
@EPIPE = global i32 0
@.str = [22 x i8] c"broken pipe for recv\0A\00"
define i32 @terima(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
store i32 %2, i32* %6
store i32 0, i32* %7
%11 = load i32, i32* %6
store i32 %11, i32* %8
br label %12
12: ; preds = %38, %3
%13 = load i32, i32* %7
%14 = load i32, i32* %6
%15 = icmp slt i32 %13, %14
br i1 %15, label %16, label %45
16: ; preds = %12
%17 = load i32, i32* %4
%18 = load i8*, i8** %5
%19 = load i32, i32* %7
%20 = sext i32 %19 to i64
%21 = getelementptr i8, i8* %18, i64 %20
%22 = load i32, i32* %8
%23 = load i32, i32* @MSG_NOSIGNAL
%24 = call i32 @recv(i32 %17, i8* %21, i32 %22, i32 %23)
store i32 %24, i32* %9
%25 = load i32, i32* @errno
store i32 %25, i32* %10
%26 = load i32, i32* %9
%27 = icmp eq i32 %26, -1
br i1 %27, label %31, label %28
28: ; preds = %16
%29 = load i32, i32* %9
%30 = icmp eq i32 %29, 0
br i1 %30, label %31, label %38
31: ; preds = %28, %16
%32 = load i32, i32* %10
%33 = load i32, i32* @EPIPE
%34 = icmp eq i32 %32, %33
br i1 %34, label %35, label %37
35: ; preds = %31
%36 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
br label %37
37: ; preds = %35, %31
br label %45
38: ; preds = %28
%39 = load i32, i32* %9
%40 = load i32, i32* %7
%41 = add nsw i32 %40, %39
store i32 %41, i32* %7
%42 = load i32, i32* %9
%43 = load i32, i32* %8
%44 = sub nsw i32 %43, %42
store i32 %44, i32* %8
br label %12
45: ; preds = %37, %12
%46 = load i32, i32* %10
ret i32 %46
}

LLVM IR (-O2):
@MSG_NOSIGNAL = global i32 0
@errno = global i32 0
@EPIPE = global i32 0
@str = [21 x i8] c"broken pipe for recv\00"
define i32 @terima(i32 %0, i8* %1, i32 %2) {
%4 = icmp sgt i32 %2, 0
br i1 %4, label %5, label %26
5: ; preds = %3, %20
%6 = phi i32 [ %21, %20 ], [ 0, %3 ]
%7 = phi i32 [ %22, %20 ], [ %2, %3 ]
%8 = sext i32 %6 to i64
%9 = getelementptr i8, i8* %1, i64 %8
%10 = load i32, i32* @MSG_NOSIGNAL
%11 = tail call i32 @recv(i32 %0, i8* %9, i32 %7, i32 %10)
%12 = add i32 %11, 1
%13 = icmp ult i32 %12, 2
br i1 %13, label %14, label %20
14: ; preds = %5
%15 = load i32, i32* @errno
%16 = load i32, i32* @EPIPE
%17 = icmp eq i32 %15, %16
br i1 %17, label %18, label %26
18: ; preds = %14
%19 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str, i64 0, i64 0))
br label %26
20: ; preds = %5
%21 = add nsw i32 %11, %6
%22 = sub nsw i32 %7, %11
%23 = icmp slt i32 %21, %2
br i1 %23, label %5, label %24
24: ; preds = %20
%25 = load i32, i32* @errno
br label %26
26: ; preds = %3, %24, %14, %18
%27 = phi i32 [ %15, %18 ], [ %15, %14 ], [ %25, %24 ], [ undef, %3 ]
ret i32 %27
}