#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 /*<<< orphan*/ close(int);
scalar_t__ malloc(int);
size_t read(int, char *, size_t);
char *read_from_file(int fd, size_t num_bytes) {
  size_t num_bytes_read;
  char *read_buf;
  if (num_bytes == 0) {
    return NULL;
  } else {
    num_bytes++;
    read_buf = (char *)malloc(sizeof(char) * num_bytes);
    if (read_buf != NULL) {
      if ((num_bytes_read = read(fd, read_buf, num_bytes)) != 0) {
        if (num_bytes != -1) {
          close(fd);
          read_buf[num_bytes_read] = '\0';
          return read_buf;
        } else {
          return NULL;
        }
      } else {
        return NULL;
      }
    } else {
      return NULL;
    }
  }
}


x86 assembly (clang, -O0):
<read_from_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1157 <read_from_file+0x27>
movq   $0x0,-0x8(%rbp)
jmp    11ed <read_from_file+0xbd>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     11e5 <read_from_file+0xb5>
mov    -0xc(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1040 <read@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     11d8 <read_from_file+0xa8>
cmpq   $0xffffffffffffffff,-0x18(%rbp)
je     11cb <read_from_file+0x9b>
mov    -0xc(%rbp),%edi
call   1030 <close@plt>
mov    -0x28(%rbp),%rax
mov    -0x20(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11ed <read_from_file+0xbd>
movq   $0x0,-0x8(%rbp)
jmp    11ed <read_from_file+0xbd>
movq   $0x0,-0x8(%rbp)
jmp    11ed <read_from_file+0xbd>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_from_file>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
test   %rsi,%rsi
je     1185 <read_from_file+0x55>
mov    %rsi,%rbp
mov    %edi,%r14d
add    $0x1,%rbp
mov    %ebp,%edi
call   1050 <malloc@plt>
test   %rax,%rax
je     1185 <read_from_file+0x55>
mov    %rax,%rbx
mov    %r14d,%edi
mov    %rax,%rsi
mov    %rbp,%rdx
call   1040 <read@plt>
mov    %rax,%r15
xor    %eax,%eax
cmp    $0xffffffffffffffff,%rbp
je     1187 <read_from_file+0x57>
test   %r15,%r15
je     1187 <read_from_file+0x57>
mov    %r14d,%edi
call   1030 <close@plt>
movb   $0x0,(%rbx,%r15,1)
mov    %rbx,%rax
jmp    1187 <read_from_file+0x57>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i8* @read_from_file(i32 %0, i64 %1) {
%3 = icmp eq i64 %1, 0
br i1 %3, label %18, label %4
4: ; preds = %2
%5 = add i64 %1, 1
%6 = trunc i64 %5 to i32
%7 = tail call i64 @malloc(i32 %6)
%8 = inttoptr i64 %7 to i8*
%9 = icmp eq i64 %7, 0
br i1 %9, label %18, label %10
10: ; preds = %4
%11 = tail call i64 @read(i32 %0, i8* %8, i64 %5)
%12 = icmp eq i64 %11, 0
%13 = icmp eq i64 %5, -1
%14 = or i1 %13, %12
br i1 %14, label %18, label %15
15: ; preds = %10
%16 = tail call i32 @close(i32 %0)
%17 = getelementptr inbounds i8, i8* %8, i64 %11
store i8 0, i8* %17
br label %18
18: ; preds = %4, %10, %2, %15
%19 = phi i8* [ %8, %15 ], [ null, %2 ], [ null, %10 ], [ null, %4 ]
ret i8* %19
}