#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*/ exit(int);
char *malloc(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int main(int argc, char **argv) {
  char *p;
  int fd, readsz;
  if (!(p = malloc(1024 * 16))) {
    printf("failed to malloc memory\n");
    exit(1);
  }
  fd = open("amacc.c", 0);
  readsz = read(fd, p, 1024 * 16);
  printf("read %d bytes\nContents:\n______________________________\n%s", readsz,
         p);
  printf("\n_______________________________\n");
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x4000,%edi
call   1050 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    1196 <main+0x46>
lea    0xe7b(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
lea    0xe7c(%rip),%rdi
xor    %esi,%esi
call   1060 <open@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    $0x4000,%edx
call   1040 <read@plt>
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%esi
mov    -0x18(%rbp),%rdx
lea    0xe58(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xe84(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
xor    %edi,%edi
call   1070 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbx
mov    $0x4000,%edi
call   1060 <malloc@plt>
test   %rax,%rax
jne    1186 <main+0x26>
lea    0xecb(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    %rax,%rbx
lea    0xe70(%rip),%rdi
xor    %esi,%esi
call   1070 <open@plt>
mov    %eax,%edi
mov    %rbx,%rsi
mov    $0x4000,%edx
call   1050 <read@plt>
lea    0xe5b(%rip),%rdi
mov    %eax,%esi
mov    %rbx,%rdx
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xe9a(%rip),%rdi
call   1030 <puts@plt>
xor    %edi,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [25 x i8] c"failed to malloc memory\0A\00"
@.str.1 = [8 x i8] c"amacc.c\00"
@.str.2 = [58 x i8] c"read %d bytes\0AContents:\0A______________________________\0A%s\00"
@.str.3 = [34 x i8] c"\0A_______________________________\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = call i8* @malloc(i32 16384)
store i8* %9, i8** %6
%10 = icmp ne i8* %9, null
br i1 %10, label %14, label %11
11: ; preds = %2
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0))
%13 = call i32 @exit(i32 1)
unreachable
14: ; preds = %2
%15 = call i32 @open(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i32 0)
store i32 %15, i32* %7
%16 = load i32, i32* %7
%17 = load i8*, i8** %6
%18 = call i32 @read(i32 %16, i8* %17, i32 16384)
store i32 %18, i32* %8
%19 = load i32, i32* %8
%20 = load i8*, i8** %6
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str.2, i64 0, i64 0), i32 %19, i8* %20)
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.3, i64 0, i64 0))
%23 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@.str.1 = [8 x i8] c"amacc.c\00"
@.str.2 = [58 x i8] c"read %d bytes\0AContents:\0A______________________________\0A%s\00"
@str = [24 x i8] c"failed to malloc memory\00"
@str.4 = [33 x i8] c"\0A_______________________________\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call dereferenceable_or_null(16384) i8* @malloc(i32 16384)
%4 = icmp eq i8* %3, null
br i1 %4, label %5, label %8
5: ; preds = %2
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str, i64 0, i64 0))
%7 = tail call i32 @exit(i32 1)
unreachable
8: ; preds = %2
%9 = tail call i32 @open(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i32 0)
%10 = tail call i32 @read(i32 %9, i8* %3, i32 16384)
%11 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([58 x i8], [58 x i8]* @.str.2, i64 0, i64 0), i32 %10, i8* %3)
%12 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @str.4, i64 0, i64 0))
%13 = tail call i32 @exit(i32 0)
unreachable
}