#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 /*<<< orphan*/ BIO;

/* Variables and functions */
int BIO_read(int /*<<< orphan*/ *, char *, int);
int /*<<< orphan*/ BIO_should_retry(int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
scalar_t__ malloc(int);
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ realloc(char *, int);
int /*<<< orphan*/ strcat(char *, char *);
char *C_bio_read(BIO *bio) {
  char *buf = (char *)malloc(sizeof(char));
  int len = 1;
  buf[0] = '\0';
  bool t = true;
  while (t) {
    char *buf_tmp = (char *)malloc(2 * sizeof(char));
    buf_tmp[1] = '\0';
    int x = BIO_read(bio, buf_tmp, 1);
    if (x == 0) {
      printf("Error while BIO_read - connection closed.\n");
    } else if (x < 0) {
      if (!BIO_should_retry(bio)) {
        printf("Error while BIO_read - failed read.\n");
      }
    }
    if (*buf_tmp == '\0') {
      t = false;
    } else {
      len++;
      buf = (char *)realloc(buf, len * (sizeof(char)));
      strcat(buf, buf_tmp);
    }
    free(buf_tmp);
  }
  printf("reading... %s ...OK\n", buf);
  return buf;
}


x86 assembly (clang, -O0):
<C_bio_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    $0x1,%edi
call   1060 <malloc@plt>
mov    %rax,-0x10(%rbp)
movl   $0x1,-0x14(%rbp)
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
movl   $0x1,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
je     127c <C_bio_read+0x10c>
mov    $0x2,%edi
call   1060 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
movb   $0x0,0x1(%rax)
mov    -0x8(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    $0x1,%edx
call   1040 <BIO_read@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jne    11f1 <C_bio_read+0x81>
lea    0xe1b(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1225 <C_bio_read+0xb5>
cmpl   $0x0,-0x24(%rbp)
jge    1220 <C_bio_read+0xb0>
mov    -0x8(%rbp),%rdi
call   1090 <BIO_should_retry@plt>
cmp    $0x0,%eax
jne    121b <C_bio_read+0xab>
lea    0xe17(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1220 <C_bio_read+0xb0>
jmp    1225 <C_bio_read+0xb5>
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    1241 <C_bio_read+0xd1>
movl   $0x0,-0x18(%rbp)
jmp    126e <C_bio_read+0xfe>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
movslq -0x14(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%esi
call   1070 <realloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1080 <strcat@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
jmp    119f <C_bio_read+0x2f>
mov    -0x10(%rbp),%rsi
lea    0xdc9(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x10(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<C_bio_read>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
mov    $0x1,%edi
call   1070 <malloc@plt>
mov    %rax,%r15
movb   $0x0,(%rax)
mov    $0x2,%ebp
lea    0xe8f(%rip),%r12
lea    0xe64(%rip),%r13
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    $0x2,%edi
call   1070 <malloc@plt>
mov    %rax,%rbx
movb   $0x0,0x1(%rax)
mov    %r14,%rdi
mov    %rax,%rsi
mov    $0x1,%edx
call   1050 <BIO_read@plt>
mov    %r12,%rdi
test   %eax,%eax
je     1200 <C_bio_read+0x80>
jns    1205 <C_bio_read+0x85>
mov    %r14,%rdi
call   10a0 <BIO_should_retry@plt>
mov    %r13,%rdi
test   %eax,%eax
jne    1205 <C_bio_read+0x85>
nopl   0x0(%rax)
call   1040 <puts@plt>
cmpb   $0x0,(%rbx)
je     122f <C_bio_read+0xaf>
mov    %r15,%rdi
mov    %ebp,%esi
call   1080 <realloc@plt>
mov    %rax,%r15
mov    %rax,%rdi
mov    %rbx,%rsi
call   1090 <strcat@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
add    $0x1,%ebp
jmp    11c0 <C_bio_read+0x40>
mov    %rbx,%rdi
call   1030 <free@plt>
lea    0xdc2(%rip),%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [43 x i8] c"Error while BIO_read - connection closed.\0A\00"
@.str.1 = [37 x i8] c"Error while BIO_read - failed read.\0A\00"
@.str.2 = [21 x i8] c"reading... %s ...OK\0A\00"
define i8* @C_bio_read(i32* %0) {
%2 = alloca i32*
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
store i32* %0, i32** %2
%8 = call i64 @malloc(i32 1)
%9 = inttoptr i64 %8 to i8*
store i8* %9, i8** %3
store i32 1, i32* %4
%10 = load i8*, i8** %3
%11 = getelementptr inbounds i8, i8* %10, i64 0
store i8 0, i8* %11
store i32 1, i32* %5
br label %12
12: ; preds = %57, %1
%13 = load i32, i32* %5
%14 = icmp ne i32 %13, 0
br i1 %14, label %15, label %60
15: ; preds = %12
%16 = call i64 @malloc(i32 2)
%17 = inttoptr i64 %16 to i8*
store i8* %17, i8** %6
%18 = load i8*, i8** %6
%19 = getelementptr inbounds i8, i8* %18, i64 1
store i8 0, i8* %19
%20 = load i32*, i32** %2
%21 = load i8*, i8** %6
%22 = call i32 @BIO_read(i32* %20, i8* %21, i32 1)
store i32 %22, i32* %7
%23 = load i32, i32* %7
%24 = icmp eq i32 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %15
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0))
br label %38
27: ; preds = %15
%28 = load i32, i32* %7
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %37
30: ; preds = %27
%31 = load i32*, i32** %2
%32 = call i32 @BIO_should_retry(i32* %31)
%33 = icmp ne i32 %32, 0
br i1 %33, label %36, label %34
34: ; preds = %30
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0))
br label %36
36: ; preds = %34, %30
br label %37
37: ; preds = %36, %27
br label %38
38: ; preds = %37, %25
%39 = load i8*, i8** %6
%40 = load i8, i8* %39
%41 = sext i8 %40 to i32
%42 = icmp eq i32 %41, 0
br i1 %42, label %43, label %44
43: ; preds = %38
store i32 0, i32* %5
br label %57
44: ; preds = %38
%45 = load i32, i32* %4
%46 = add nsw i32 %45, 1
store i32 %46, i32* %4
%47 = load i8*, i8** %3
%48 = load i32, i32* %4
%49 = sext i32 %48 to i64
%50 = mul i64 %49, 1
%51 = trunc i64 %50 to i32
%52 = call i64 @realloc(i8* %47, i32 %51)
%53 = inttoptr i64 %52 to i8*
store i8* %53, i8** %3
%54 = load i8*, i8** %3
%55 = load i8*, i8** %6
%56 = call i32 @strcat(i8* %54, i8* %55)
br label %57
57: ; preds = %44, %43
%58 = load i8*, i8** %6
%59 = call i32 @free(i8* %58)
br label %12
60: ; preds = %12
%61 = load i8*, i8** %3
%62 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i8* %61)
%63 = load i8*, i8** %3
ret i8* %63
}

LLVM IR (-O2):
@.str.2 = [21 x i8] c"reading... %s ...OK\0A\00"
@str = [36 x i8] c"Error while BIO_read - failed read.\00"
@str.3 = [42 x i8] c"Error while BIO_read - connection closed.\00"
define i8* @C_bio_read(i32* %0) {
%2 = tail call i64 @malloc(i32 1)
%3 = inttoptr i64 %2 to i8*
store i8 0, i8* %3
br label %4
4: ; preds = %23, %1
%5 = phi i8* [ %3, %1 ], [ %26, %23 ]
%6 = phi i32 [ 1, %1 ], [ %24, %23 ]
%7 = tail call i64 @malloc(i32 2)
%8 = inttoptr i64 %7 to i8*
%9 = getelementptr inbounds i8, i8* %8, i64 1
store i8 0, i8* %9
%10 = tail call i32 @BIO_read(i32* %0, i8* %8, i32 1)
%11 = icmp eq i32 %10, 0
br i1 %11, label %17, label %12
12: ; preds = %4
%13 = icmp slt i32 %10, 0
br i1 %13, label %14, label %20
14: ; preds = %12
%15 = tail call i32 @BIO_should_retry(i32* %0)
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %14, %4
%18 = phi i8* [ getelementptr inbounds ([42 x i8], [42 x i8]* @str.3, i64 0, i64 0), %4 ], [ getelementptr inbounds ([36 x i8], [36 x i8]* @str, i64 0, i64 0), %14 ]
%19 = tail call i32 @puts(i8* dereferenceable(1) %18)
br label %20
20: ; preds = %17, %12, %14
%21 = load i8, i8* %8
%22 = icmp eq i8 %21, 0
br i1 %22, label %29, label %23
23: ; preds = %20
%24 = add nuw nsw i32 %6, 1
%25 = tail call i64 @realloc(i8* %5, i32 %24)
%26 = inttoptr i64 %25 to i8*
%27 = tail call i32 @strcat(i8* %26, i8* %8)
%28 = tail call i32 @free(i8* %8)
br label %4
29: ; preds = %20
%30 = inttoptr i64 %7 to i8*
%31 = tail call i32 @free(i8* %30)
%32 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0), i8* %5)
ret i8* %5
}