#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 */
scalar_t__ EINTR;
int /*<<< orphan*/ assert(int);
scalar_t__ errno;
char *malloc(int);
int /*<<< orphan*/ perror(char *);
int read(int, char *, int);
int read_at_least(int fd, char **data, int size) {
  int received_len = 0, current_size = size + 128;
  char *buf = malloc(current_size);
  while (1) {
    int max = current_size - received_len;
    assert(max > 0);
    int ret = read(fd, buf + received_len, max);
    if (ret == -1) {
      if (errno == EINTR)
        continue;
      perror("read_safe");
      return -1;
    }
    received_len += ret;
    if (received_len >= size)
      break;
  }
  *data = buf;
  return received_len;
}


x86 assembly (clang, -O0):
<read_at_least>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x14(%rbp),%eax
add    $0x80,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%edi
call   1050 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x1c(%rbp),%eax
sub    -0x18(%rbp),%eax
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
setg   %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
mov    -0x8(%rbp),%edi
mov    -0x28(%rbp),%rsi
movslq -0x18(%rbp),%rax
add    %rax,%rsi
mov    -0x2c(%rbp),%edx
call   1030 <read@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0xffffffff,-0x30(%rbp)
jne    11e4 <read_at_least+0xa4>
mov    0x2e2c(%rip),%rax
mov    (%rax),%rax
mov    0x2e1a(%rip),%rcx
cmp    (%rcx),%rax
jne    11cc <read_at_least+0x8c>
jmp    1170 <read_at_least+0x30>
lea    0xe2d(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1214 <read_at_least+0xd4>
mov    -0x30(%rbp),%eax
add    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jl     11fe <read_at_least+0xbe>
jmp    1203 <read_at_least+0xc3>
jmp    1170 <read_at_least+0x30>
mov    -0x28(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_at_least>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rsi,0x18(%rsp)
mov    %edi,%ebp
mov    %rdx,0x20(%rsp)
lea    0x80(%rdx),%edi
mov    %edi,0x8(%rsp)
call   1050 <malloc@plt>
mov    %rax,0x10(%rsp)
xor    %ecx,%ecx
mov    0x2e69(%rip),%r13
mov    0x2e5a(%rip),%rbx
jmp    118c <read_at_least+0x4c>
mov    0xc(%rsp),%ecx
add    %eax,%ecx
cmp    0x20(%rsp),%ecx
jge    11e6 <read_at_least+0xa6>
xor    %r14d,%r14d
mov    0x8(%rsp),%r12d
sub    %ecx,%r12d
setg   %r14b
mov    %ecx,0xc(%rsp)
movslq %ecx,%r15
add    0x10(%rsp),%r15
nopw   0x0(%rax,%rax,1)
mov    %r14d,%edi
call   1040 <assert@plt>
mov    %ebp,%edi
mov    %r15,%rsi
mov    %r12d,%edx
call   1030 <read@plt>
cmp    $0xffffffff,%eax
jne    1180 <read_at_least+0x40>
mov    0x0(%r13),%rax
cmp    (%rbx),%rax
je     11b0 <read_at_least+0x70>
lea    0xe26(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%ecx
jmp    11f3 <read_at_least+0xb3>
mov    0x18(%rsp),%rax
mov    0x10(%rsp),%rdx
mov    %rdx,(%rax)
mov    %ecx,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i64 0
@EINTR = global i64 0
@.str = [10 x i8] c"read_safe\00"
define i32 @read_at_least(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*
%11 = alloca i32
%12 = alloca i32
store i32 %0, i32* %5
store i8** %1, i8*** %6
store i32 %2, i32* %7
store i32 0, i32* %8
%13 = load i32, i32* %7
%14 = add nsw i32 %13, 128
store i32 %14, i32* %9
%15 = load i32, i32* %9
%16 = call i8* @malloc(i32 %15)
store i8* %16, i8** %10
br label %17
17: ; preds = %3, %38, %49
%18 = load i32, i32* %9
%19 = load i32, i32* %8
%20 = sub nsw i32 %18, %19
store i32 %20, i32* %11
%21 = load i32, i32* %11
%22 = icmp sgt i32 %21, 0
%23 = zext i1 %22 to i32
%24 = call i32 @assert(i32 %23)
%25 = load i32, i32* %5
%26 = load i8*, i8** %10
%27 = load i32, i32* %8
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds i8, i8* %26, i64 %28
%30 = load i32, i32* %11
%31 = call i32 @read(i32 %25, i8* %29, i32 %30)
store i32 %31, i32* %12
%32 = load i32, i32* %12
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %41
34: ; preds = %17
%35 = load i64, i64* @errno
%36 = load i64, i64* @EINTR
%37 = icmp eq i64 %35, %36
br i1 %37, label %38, label %39
38: ; preds = %34
br label %17
39: ; preds = %34
%40 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %4
br label %54
41: ; preds = %17
%42 = load i32, i32* %12
%43 = load i32, i32* %8
%44 = add nsw i32 %43, %42
store i32 %44, i32* %8
%45 = load i32, i32* %8
%46 = load i32, i32* %7
%47 = icmp sge i32 %45, %46
br i1 %47, label %48, label %49
48: ; preds = %41
br label %50
49: ; preds = %41
br label %17
50: ; preds = %48
%51 = load i8*, i8** %10
%52 = load i8**, i8*** %6
store i8* %51, i8** %52
%53 = load i32, i32* %8
store i32 %53, i32* %4
br label %54
54: ; preds = %50, %39
%55 = load i32, i32* %4
ret i32 %55
}

LLVM IR (-O2):
@errno = global i64 0
@EINTR = global i64 0
@.str = [10 x i8] c"read_safe\00"
define i32 @read_at_least(i32 %0, i8** nocapture writeonly %1, i32 %2) {
%4 = add nsw i32 %2, 128
%5 = tail call i8* @malloc(i32 %4)
br label %6
6: ; preds = %23, %3
%7 = phi i32 [ %24, %23 ], [ 0, %3 ]
%8 = sub nsw i32 %4, %7
%9 = icmp sgt i32 %8, 0
%10 = zext i1 %9 to i32
%11 = sext i32 %7 to i64
%12 = getelementptr inbounds i8, i8* %5, i64 %11
br label %13
13: ; preds = %6, %17
%14 = tail call i32 @assert(i32 %10)
%15 = tail call i32 @read(i32 %0, i8* %12, i32 %8)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %23
17: ; preds = %13
%18 = load i64, i64* @errno
%19 = load i64, i64* @EINTR
%20 = icmp eq i64 %18, %19
br i1 %20, label %13, label %21
21: ; preds = %17
%22 = tail call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
br label %27
23: ; preds = %13
%24 = add nsw i32 %15, %7
%25 = icmp slt i32 %24, %2
br i1 %25, label %6, label %26
26: ; preds = %23
store i8* %5, i8** %1
br label %27
27: ; preds = %21, %26
%28 = phi i32 [ %24, %26 ], [ -1, %21 ]
ret i32 %28
}