#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 portal_read_buf(int sock, char *buf, int size) {
  int i = 0;
  int n;
  char c;
  int quit = 0;
  while (i < size && quit == 0) {
    n = recv(sock, &c, 1, 0);
    if (n > 0) {
      buf[i] = c;
      i++;
    } else
      quit = 1;
  }
  return i;
}


x86 assembly (clang, -O0):
<portal_read_buf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x18(%rbp),%ecx
xor    %eax,%eax
cmp    -0x14(%rbp),%ecx
mov    %al,-0x25(%rbp)
jge    114b <portal_read_buf+0x3b>
cmpl   $0x0,-0x24(%rbp)
sete   %al
mov    %al,-0x25(%rbp)
mov    -0x25(%rbp),%al
test   $0x1,%al
jne    115b <portal_read_buf+0x4b>
jmp    11a3 <portal_read_buf+0x93>
mov    -0x4(%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    1197 <portal_read_buf+0x87>
mov    -0x1d(%rbp),%dl
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    119e <portal_read_buf+0x8e>
movl   $0x1,-0x24(%rbp)
jmp    1130 <portal_read_buf+0x20>
mov    -0x18(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<portal_read_buf>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
test   %edx,%edx
jle    115d <portal_read_buf+0x4d>
mov    %edx,%ebp
mov    %rsi,%r14
mov    %edi,%r15d
xor    %ebx,%ebx
lea    0xf(%rsp),%r12
nop
mov    %r15d,%edi
mov    %r12,%rsi
mov    $0x1,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %eax,%eax
jle    115f <portal_read_buf+0x4f>
movzbl 0xf(%rsp),%ecx
mov    %cl,(%r14,%rbx,1)
add    $0x1,%rbx
cmp    %ebp,%ebx
jge    115f <portal_read_buf+0x4f>
test   %eax,%eax
jg     1130 <portal_read_buf+0x20>
jmp    115f <portal_read_buf+0x4f>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @portal_read_buf(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8
%10 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
store i32 %2, i32* %6
store i32 0, i32* %7
store i32 0, i32* %10
br label %11
11: ; preds = %34, %3
%12 = load i32, i32* %7
%13 = load i32, i32* %6
%14 = icmp slt i32 %12, %13
br i1 %14, label %15, label %18
15: ; preds = %11
%16 = load i32, i32* %10
%17 = icmp eq i32 %16, 0
br label %18
18: ; preds = %15, %11
%19 = phi i1 [ false, %11 ], [ %17, %15 ]
br i1 %19, label %20, label %35
20: ; preds = %18
%21 = load i32, i32* %4
%22 = call i32 @recv(i32 %21, i8* %9, i32 1, i32 0)
store i32 %22, i32* %8
%23 = load i32, i32* %8
%24 = icmp sgt i32 %23, 0
br i1 %24, label %25, label %33
25: ; preds = %20
%26 = load i8, i8* %9
%27 = load i8*, i8** %5
%28 = load i32, i32* %7
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds i8, i8* %27, i64 %29
store i8 %26, i8* %30
%31 = load i32, i32* %7
%32 = add nsw i32 %31, 1
store i32 %32, i32* %7
br label %34
33: ; preds = %20
store i32 1, i32* %10
br label %34
34: ; preds = %33, %25
br label %11
35: ; preds = %18
%36 = load i32, i32* %7
ret i32 %36
}

LLVM IR (-O2):
define i32 @portal_read_buf(i32 %0, i8* nocapture writeonly %1, i32 %2) {
%4 = alloca i8
%5 = icmp sgt i32 %2, 0
br i1 %5, label %6, label %17
6: ; preds = %3, %10
%7 = phi i32 [ %14, %10 ], [ 0, %3 ]
%8 = call i32 @recv(i32 %0, i8* %4, i32 1, i32 0)
%9 = icmp sgt i32 %8, 0
br i1 %9, label %10, label %17
10: ; preds = %6
%11 = load i8, i8* %4
%12 = zext i32 %7 to i64
%13 = getelementptr inbounds i8, i8* %1, i64 %12
store i8 %11, i8* %13
%14 = add nuw nsw i32 %7, 1
%15 = icmp slt i32 %14, %2
%16 = and i1 %15, %9
br i1 %16, label %6, label %17
17: ; preds = %6, %10, %3
%18 = phi i32 [ 0, %3 ], [ %7, %6 ], [ %14, %10 ]
ret i32 %18
}