#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 recvn(int s, char *buf, int len) {
  int received, left = len;
  char *ptr = buf;
  while (left > 0) {
    received = read(s, buf, left);
    if (received == -1)
      return -1;
    else if (received == 0)
      break;
    left -= received;
    ptr += received;
  }
  return (len - left);
}


x86 assembly (clang, -O0):
<recvn>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jle    1197 <recvn+0x87>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1030 <read@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    1162 <recvn+0x52>
movl   $0xffffffff,-0x4(%rbp)
jmp    11a0 <recvn+0x90>
cmpl   $0x0,-0x18(%rbp)
jne    1171 <recvn+0x61>
jmp    1197 <recvn+0x87>
jmp    1176 <recvn+0x66>
mov    -0x18(%rbp),%ecx
mov    -0x1c(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%ecx
mov    -0x28(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
jmp    1130 <recvn+0x20>
mov    -0x14(%rbp),%eax
sub    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<recvn>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%r14d
mov    %edx,%ebp
test   %edx,%edx
jle    1149 <recvn+0x39>
mov    %rsi,%r15
mov    %edi,%ebx
mov    %r14d,%ebp
nopl   0x0(%rax,%rax,1)
mov    %ebx,%edi
mov    %r15,%rsi
mov    %ebp,%edx
call   1030 <read@plt>
cmp    $0xffffffff,%eax
je     114e <recvn+0x3e>
test   %eax,%eax
je     1149 <recvn+0x39>
sub    %eax,%ebp
jg     1130 <recvn+0x20>
sub    %ebp,%r14d
jmp    1151 <recvn+0x41>
mov    %eax,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @recvn(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
%11 = load i32, i32* %7
store i32 %11, i32* %9
%12 = load i8*, i8** %6
store i8* %12, i8** %10
br label %13
13: ; preds = %29, %3
%14 = load i32, i32* %9
%15 = icmp sgt i32 %14, 0
br i1 %15, label %16, label %37
16: ; preds = %13
%17 = load i32, i32* %5
%18 = load i8*, i8** %6
%19 = load i32, i32* %9
%20 = call i32 @read(i32 %17, i8* %18, i32 %19)
store i32 %20, i32* %8
%21 = load i32, i32* %8
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %24
23: ; preds = %16
store i32 -1, i32* %4
br label %41
24: ; preds = %16
%25 = load i32, i32* %8
%26 = icmp eq i32 %25, 0
br i1 %26, label %27, label %28
27: ; preds = %24
br label %37
28: ; preds = %24
br label %29
29: ; preds = %28
%30 = load i32, i32* %8
%31 = load i32, i32* %9
%32 = sub nsw i32 %31, %30
store i32 %32, i32* %9
%33 = load i32, i32* %8
%34 = load i8*, i8** %10
%35 = sext i32 %33 to i64
%36 = getelementptr inbounds i8, i8* %34, i64 %35
store i8* %36, i8** %10
br label %13
37: ; preds = %27, %13
%38 = load i32, i32* %7
%39 = load i32, i32* %9
%40 = sub nsw i32 %38, %39
store i32 %40, i32* %4
br label %41
41: ; preds = %37, %23
%42 = load i32, i32* %4
ret i32 %42
}

LLVM IR (-O2):
define i32 @recvn(i32 %0, i8* nocapture %1, i32 %2) {
%4 = icmp sgt i32 %2, 0
br i1 %4, label %5, label %11
5: ; preds = %3, %8
%6 = phi i32 [ %9, %8 ], [ %2, %3 ]
%7 = tail call i32 @read(i32 %0, i8* %1, i32 %6)
switch i32 %7, label %8 [
i32 -1, label %14
i32 0, label %11
]
8: ; preds = %5
%9 = sub nsw i32 %6, %7
%10 = icmp sgt i32 %9, 0
br i1 %10, label %5, label %11
11: ; preds = %8, %5, %3
%12 = phi i32 [ %2, %3 ], [ %6, %5 ], [ %9, %8 ]
%13 = sub nsw i32 %2, %12
br label %14
14: ; preds = %5, %11
%15 = phi i32 [ %13, %11 ], [ %7, %5 ]
ret i32 %15
}