#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 ssize_t;

/* Variables and functions */
int /*<<< orphan*/ ENOMEM;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ free(char *);
int read(int, char *, int);
char *realloc(char *, int);
ssize_t readnsa(int fd, char **ptr) {
  ssize_t nread, n;
  char *buffer;
  buffer = 0;
  nread = 0;
  n = 0;
  while (1) {
    buffer = realloc(buffer, nread + 1024);
    if (!buffer) {
      errno = ENOMEM;
      return -1;
    }
    if ((n = read(fd, buffer + nread, 1024)) < 0) {
      free(buffer);
      return -1;
    } else if (n == 0) {
      break;
    }
    nread += n;
    *ptr = buffer;
  }
  return nread;
}


x86 assembly (clang, -O0):
<readnsa>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x20(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x14(%rbp),%esi
add    $0x400,%esi
call   1050 <realloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1194 <readnsa+0x64>
mov    0x2e63(%rip),%rax
mov    (%rax),%ecx
mov    0x2e52(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1200 <readnsa+0xd0>
mov    -0x8(%rbp),%edi
mov    -0x20(%rbp),%rsi
movslq -0x14(%rbp),%rax
add    %rax,%rsi
mov    $0x400,%edx
call   1040 <read@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jge    11cd <readnsa+0x9d>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1200 <readnsa+0xd0>
cmpl   $0x0,-0x18(%rbp)
jne    11dc <readnsa+0xac>
jmp    11fa <readnsa+0xca>
jmp    11e1 <readnsa+0xb1>
mov    -0x18(%rbp),%eax
add    -0x14(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x20(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
jmp    1155 <readnsa+0x25>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readnsa>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%r14
mov    %edi,%r15d
xor    %ebx,%ebx
xor    %edi,%edi
mov    $0x400,%esi
call   1050 <realloc@plt>
test   %rax,%rax
je     119c <readnsa+0x6c>
mov    %rax,%rbp
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
movslq %ebx,%rsi
add    %rbp,%rsi
mov    %r15d,%edi
mov    $0x400,%edx
call   1040 <read@plt>
test   %eax,%eax
js     11b0 <readnsa+0x80>
je     11bd <readnsa+0x8d>
lea    (%rax,%rbx,1),%r12d
mov    %rbp,(%r14)
lea    (%rax,%rbx,1),%esi
add    $0x400,%esi
mov    %rbp,%rdi
call   1050 <realloc@plt>
mov    %rax,%rbp
mov    %r12d,%ebx
test   %rax,%rax
jne    1160 <readnsa+0x30>
mov    0x2e3d(%rip),%rax
mov    (%rax),%eax
mov    0x2e2c(%rip),%rcx
mov    %eax,(%rcx)
jmp    11b8 <readnsa+0x88>
mov    %rbp,%rdi
call   1030 <free@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@ENOMEM = global i32 0
@errno = global i32 0
define i32 @readnsa(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i8* null, i8** %8
store i32 0, i32* %6
store i32 0, i32* %7
br label %9
9: ; preds = %2, %34
%10 = load i8*, i8** %8
%11 = load i32, i32* %6
%12 = add nsw i32 %11, 1024
%13 = call i8* @realloc(i8* %10, i32 %12)
store i8* %13, i8** %8
%14 = load i8*, i8** %8
%15 = icmp ne i8* %14, null
br i1 %15, label %18, label %16
16: ; preds = %9
%17 = load i32, i32* @ENOMEM
store i32 %17, i32* @errno
store i32 -1, i32* %3
br label %42
18: ; preds = %9
%19 = load i32, i32* %4
%20 = load i8*, i8** %8
%21 = load i32, i32* %6
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds i8, i8* %20, i64 %22
%24 = call i32 @read(i32 %19, i8* %23, i32 1024)
store i32 %24, i32* %7
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %29
26: ; preds = %18
%27 = load i8*, i8** %8
%28 = call i32 @free(i8* %27)
store i32 -1, i32* %3
br label %42
29: ; preds = %18
%30 = load i32, i32* %7
%31 = icmp eq i32 %30, 0
br i1 %31, label %32, label %33
32: ; preds = %29
br label %40
33: ; preds = %29
br label %34
34: ; preds = %33
%35 = load i32, i32* %7
%36 = load i32, i32* %6
%37 = add nsw i32 %36, %35
store i32 %37, i32* %6
%38 = load i8*, i8** %8
%39 = load i8**, i8*** %5
store i8* %38, i8** %39
br label %9
40: ; preds = %32
%41 = load i32, i32* %6
store i32 %41, i32* %3
br label %42
42: ; preds = %40, %26, %16
%43 = load i32, i32* %3
ret i32 %43
}

LLVM IR (-O2):
@ENOMEM = global i32 0
@errno = global i32 0
define i32 @readnsa(i32 %0, i8** nocapture writeonly %1) {
%3 = tail call i8* @realloc(i8* null, i32 1024)
%4 = icmp eq i8* %3, null
br i1 %4, label %5, label %7
5: ; preds = %18, %2
%6 = load i32, i32* @ENOMEM
store i32 %6, i32* @errno
br label %23
7: ; preds = %2, %18
%8 = phi i8* [ %21, %18 ], [ %3, %2 ]
%9 = phi i32 [ %19, %18 ], [ 0, %2 ]
%10 = sext i32 %9 to i64
%11 = getelementptr inbounds i8, i8* %8, i64 %10
%12 = tail call i32 @read(i32 %0, i8* %11, i32 1024)
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %7
%15 = tail call i32 @free(i8* %8)
br label %23
16: ; preds = %7
%17 = icmp eq i32 %12, 0
br i1 %17, label %23, label %18
18: ; preds = %16
%19 = add nsw i32 %12, %9
store i8* %8, i8** %1
%20 = add nsw i32 %19, 1024
%21 = tail call i8* @realloc(i8* %8, i32 %20)
%22 = icmp eq i8* %21, null
br i1 %22, label %5, label %7
23: ; preds = %16, %14, %5
%24 = phi i32 [ -1, %14 ], [ -1, %5 ], [ %9, %16 ]
ret i32 %24
}