#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*/ O_RDONLY;
int /*<<< orphan*/ S_IRUSR;
char *malloc(int);
int open(char const *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int func(const char *filename) {
  int fd = open(filename, O_RDONLY, S_IRUSR);
  if (-1 == fd) {
    return -1;
  }
  char *buf = malloc(10);
  int x = read(fd, buf, 10);
  if (x == -1) {
    printf("The read call was unsuccessful\n");
  } else {
    printf("Read %d bytes successfully\n", x);
  }
  return 0;
}


x86 assembly (clang, -O0):
<func>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e91(%rip),%rax
mov    (%rax),%esi
mov    0x2e78(%rip),%rax
mov    (%rax),%edx
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
mov    $0xffffffff,%eax
cmp    -0x14(%rbp),%eax
jne    1184 <func+0x44>
movl   $0xffffffff,-0x4(%rbp)
jmp    11db <func+0x9b>
mov    $0xa,%edi
call   1050 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x14(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    $0xa,%edx
call   1040 <read@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    11c3 <func+0x83>
lea    0xe49(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    11d4 <func+0x94>
mov    -0x24(%rbp),%esi
lea    0xe53(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<func>:
push   %rbx
mov    0x2e90(%rip),%rax
mov    (%rax),%esi
mov    0x2e77(%rip),%rax
mov    (%rax),%edx
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     11a1 <func+0x51>
mov    %eax,%ebx
mov    $0xa,%edi
call   1060 <malloc@plt>
mov    %ebx,%edi
mov    %rax,%rsi
mov    $0xa,%edx
call   1050 <read@plt>
cmp    $0xffffffff,%eax
je     11a8 <func+0x58>
lea    0xe6c(%rip),%rdi
xor    %ebx,%ebx
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    11b6 <func+0x66>
mov    $0xffffffff,%ebx
jmp    11b6 <func+0x66>
lea    0xe6d(%rip),%rdi
call   1030 <puts@plt>
xor    %ebx,%ebx
mov    %ebx,%eax
pop    %rbx
ret

LLVM IR (-O0):
@O_RDONLY = global i32 0
@S_IRUSR = global i32 0
@.str = [32 x i8] c"The read call was unsuccessful\0A\00"
@.str.1 = [28 x i8] c"Read %d bytes successfully\0A\00"
define i32 @func(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
store i8* %0, i8** %3
%7 = load i8*, i8** %3
%8 = load i32, i32* @O_RDONLY
%9 = load i32, i32* @S_IRUSR
%10 = call i32 @open(i8* %7, i32 %8, i32 %9)
store i32 %10, i32* %4
%11 = load i32, i32* %4
%12 = icmp eq i32 -1, %11
br i1 %12, label %13, label %14
13: ; preds = %1
store i32 -1, i32* %2
br label %27
14: ; preds = %1
%15 = call i8* @malloc(i32 10)
store i8* %15, i8** %5
%16 = load i32, i32* %4
%17 = load i8*, i8** %5
%18 = call i32 @read(i32 %16, i8* %17, i32 10)
store i32 %18, i32* %6
%19 = load i32, i32* %6
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %23
21: ; preds = %14
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
br label %26
23: ; preds = %14
%24 = load i32, i32* %6
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i32 %24)
br label %26
26: ; preds = %23, %21
store i32 0, i32* %2
br label %27
27: ; preds = %26, %13
%28 = load i32, i32* %2
ret i32 %28
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@S_IRUSR = global i32 0
@.str.1 = [28 x i8] c"Read %d bytes successfully\0A\00"
@str = [31 x i8] c"The read call was unsuccessful\00"
define i32 @func(i8* nocapture %0) {
%2 = load i32, i32* @O_RDONLY
%3 = load i32, i32* @S_IRUSR
%4 = tail call i32 @open(i8* %0, i32 %2, i32 %3)
%5 = icmp eq i32 %4, -1
br i1 %5, label %14, label %6
6: ; preds = %1
%7 = tail call dereferenceable_or_null(10) i8* @malloc(i32 10)
%8 = tail call i32 @read(i32 %4, i8* %7, i32 10)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %12
10: ; preds = %6
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @str, i64 0, i64 0))
br label %14
12: ; preds = %6
%13 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i32 %8)
br label %14
14: ; preds = %10, %12, %1
%15 = phi i32 [ -1, %1 ], [ 0, %12 ], [ 0, %10 ]
ret i32 %15
}