#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);
char *realloc(char *, int);
int read_safe(int fd, char **data) {
  int received_len = 0, current_size = 4096;
  char *buf = malloc(4096);
  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 (ret == max) {
      if (current_size <= received_len) {
        buf = realloc(buf, current_size * 2);
        current_size *= 2;
      }
      continue;
    }
    break;
  }
  *data = buf;
  return received_len;
}


x86 assembly (clang, -O0):
<read_safe>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x1000,-0x18(%rbp)
mov    $0x1000,%edi
call   1050 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%eax
sub    -0x14(%rbp),%eax
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
setg   %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
mov    -0x8(%rbp),%edi
mov    -0x20(%rbp),%rsi
movslq -0x14(%rbp),%rax
add    %rax,%rsi
mov    -0x24(%rbp),%edx
call   1030 <read@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0xffffffff,-0x28(%rbp)
jne    11ef <read_safe+0x9f>
mov    0x2e21(%rip),%rax
mov    (%rax),%rax
mov    0x2e0f(%rip),%rcx
cmp    (%rcx),%rax
jne    11d7 <read_safe+0x87>
jmp    117b <read_safe+0x2b>
lea    0xe22(%rip),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1247 <read_safe+0xf7>
mov    -0x28(%rbp),%eax
add    -0x14(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x28(%rbp),%eax
cmp    -0x24(%rbp),%eax
jne    1231 <read_safe+0xe1>
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jg     122c <read_safe+0xdc>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%esi
shl    $0x1,%esi
call   1060 <realloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    117b <read_safe+0x2b>
jmp    1236 <read_safe+0xe6>
mov    -0x20(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_safe>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,0x10(%rsp)
mov    %edi,%ebx
mov    $0x1000,%ebp
mov    $0x1000,%edi
call   1050 <malloc@plt>
mov    %rax,(%rsp)
xor    %r12d,%r12d
mov    0x2e5e(%rip),%r15
mov    0x2e4f(%rip),%r13
mov    %ebp,0x8(%rsp)
nopl   (%rax)
xor    %r14d,%r14d
sub    %r12d,%ebp
setg   %r14b
mov    %r12d,0xc(%rsp)
movslq %r12d,%r12
add    (%rsp),%r12
cs nopw 0x0(%rax,%rax,1)
mov    %r14d,%edi
call   1040 <assert@plt>
mov    %ebx,%edi
mov    %r12,%rsi
mov    %ebp,%edx
call   1030 <read@plt>
cmp    $0xffffffff,%eax
jne    11e0 <read_safe+0x90>
mov    (%r15),%rax
cmp    0x0(%r13),%rax
je     11b0 <read_safe+0x60>
jmp    1208 <read_safe+0xb8>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    0xc(%rsp),%r12d
add    %eax,%r12d
cmp    %ebp,%eax
jne    121c <read_safe+0xcc>
mov    0x8(%rsp),%ebp
cmp    %r12d,%ebp
jg     1190 <read_safe+0x40>
add    %ebp,%ebp
mov    (%rsp),%rdi
mov    %ebp,%esi
call   1060 <realloc@plt>
mov    %rax,(%rsp)
jmp    1189 <read_safe+0x39>
lea    0xdf1(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%r12d
jmp    1228 <read_safe+0xd8>
mov    0x10(%rsp),%rax
mov    (%rsp),%rcx
mov    %rcx,(%rax)
mov    %r12d,%eax
add    $0x18,%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_safe(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %6
store i32 4096, i32* %7
%11 = call i8* @malloc(i32 4096)
store i8* %11, i8** %8
br label %12
12: ; preds = %2, %33, %54
%13 = load i32, i32* %7
%14 = load i32, i32* %6
%15 = sub nsw i32 %13, %14
store i32 %15, i32* %9
%16 = load i32, i32* %9
%17 = icmp sgt i32 %16, 0
%18 = zext i1 %17 to i32
%19 = call i32 @assert(i32 %18)
%20 = load i32, i32* %4
%21 = load i8*, i8** %8
%22 = load i32, i32* %6
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds i8, i8* %21, i64 %23
%25 = load i32, i32* %9
%26 = call i32 @read(i32 %20, i8* %24, i32 %25)
store i32 %26, i32* %10
%27 = load i32, i32* %10
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %36
29: ; preds = %12
%30 = load i64, i64* @errno
%31 = load i64, i64* @EINTR
%32 = icmp eq i64 %30, %31
br i1 %32, label %33, label %34
33: ; preds = %29
br label %12
34: ; preds = %29
%35 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %60
36: ; preds = %12
%37 = load i32, i32* %10
%38 = load i32, i32* %6
%39 = add nsw i32 %38, %37
store i32 %39, i32* %6
%40 = load i32, i32* %10
%41 = load i32, i32* %9
%42 = icmp eq i32 %40, %41
br i1 %42, label %43, label %55
43: ; preds = %36
%44 = load i32, i32* %7
%45 = load i32, i32* %6
%46 = icmp sle i32 %44, %45
br i1 %46, label %47, label %54
47: ; preds = %43
%48 = load i8*, i8** %8
%49 = load i32, i32* %7
%50 = mul nsw i32 %49, 2
%51 = call i8* @realloc(i8* %48, i32 %50)
store i8* %51, i8** %8
%52 = load i32, i32* %7
%53 = mul nsw i32 %52, 2
store i32 %53, i32* %7
br label %54
54: ; preds = %47, %43
br label %12
55: ; preds = %36
br label %56
56: ; preds = %55
%57 = load i8*, i8** %8
%58 = load i8**, i8*** %5
store i8* %57, i8** %58
%59 = load i32, i32* %6
store i32 %59, i32* %3
br label %60
60: ; preds = %56, %34
%61 = load i32, i32* %3
ret i32 %61
}

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