#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 */
char *malloc(int);
int /*<<< orphan*/ perror(char *);
int read(int, char *, int);
char *read_header(int fd) {
  char *header = malloc(sizeof(char) * 2048);
  if (header == NULL) {
    perror("malloc() failed");
    return NULL;
  }
  ssize_t now_read = read(fd, header, 2048);
  if (now_read == -1) {
    header[0] = '\0';
    perror("read() failed");
  } else {
    header[now_read] = '\0';
  }
  return header;
}


x86 assembly (clang, -O0):
<read_header>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0xc(%rbp)
mov    $0x800,%edi
call   1040 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    116d <read_header+0x3d>
lea    0xea5(%rip),%rdi
call   1050 <perror@plt>
movq   $0x0,-0x8(%rbp)
jmp    11b7 <read_header+0x87>
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    $0x800,%edx
call   1030 <read@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11a3 <read_header+0x73>
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
lea    0xe77(%rip),%rdi
call   1050 <perror@plt>
jmp    11af <read_header+0x7f>
mov    -0x18(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_header>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%ebp
mov    $0x800,%edi
call   1040 <malloc@plt>
mov    %rax,%rbx
test   %rax,%rax
je     116b <read_header+0x3b>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x800,%edx
call   1030 <read@plt>
cmp    $0xffffffff,%eax
je     1179 <read_header+0x49>
cltq
movb   $0x0,(%rbx,%rax,1)
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe8e(%rip),%rdi
call   1050 <perror@plt>
jmp    1161 <read_header+0x31>
movb   $0x0,(%rbx)
lea    0xe8d(%rip),%rdi
call   1050 <perror@plt>
jmp    1161 <read_header+0x31>

LLVM IR (-O0):
@.str = [16 x i8] c"malloc() failed\00"
@.str.1 = [14 x i8] c"read() failed\00"
define i8* @read_header(i32 %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
store i32 %0, i32* %3
%6 = call i8* @malloc(i32 2048)
store i8* %6, i8** %4
%7 = load i8*, i8** %4
%8 = icmp eq i8* %7, null
br i1 %8, label %9, label %11
9: ; preds = %1
%10 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
store i8* null, i8** %2
br label %28
11: ; preds = %1
%12 = load i32, i32* %3
%13 = load i8*, i8** %4
%14 = call i32 @read(i32 %12, i8* %13, i32 2048)
store i32 %14, i32* %5
%15 = load i32, i32* %5
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %21
17: ; preds = %11
%18 = load i8*, i8** %4
%19 = getelementptr inbounds i8, i8* %18, i64 0
store i8 0, i8* %19
%20 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
br label %26
21: ; preds = %11
%22 = load i8*, i8** %4
%23 = load i32, i32* %5
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds i8, i8* %22, i64 %24
store i8 0, i8* %25
br label %26
26: ; preds = %21, %17
%27 = load i8*, i8** %4
store i8* %27, i8** %2
br label %28
28: ; preds = %26, %9
%29 = load i8*, i8** %2
ret i8* %29
}

LLVM IR (-O2):
@.str = [16 x i8] c"malloc() failed\00"
@.str.1 = [14 x i8] c"read() failed\00"
define noalias i8* @read_header(i32 %0) {
%2 = tail call dereferenceable_or_null(2048) i8* @malloc(i32 2048)
%3 = icmp eq i8* %2, null
br i1 %3, label %4, label %6
4: ; preds = %1
%5 = tail call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
br label %14
6: ; preds = %1
%7 = tail call i32 @read(i32 %0, i8* %2, i32 2048)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %11
9: ; preds = %6
store i8 0, i8* %2
%10 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
br label %14
11: ; preds = %6
%12 = sext i32 %7 to i64
%13 = getelementptr inbounds i8, i8* %2, i64 %12
store i8 0, i8* %13
br label %14
14: ; preds = %9, %11, %4
ret i8* %2
}