#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 /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, ...);
int magic_read(char *filename, char *magic, int magic_size) {
  FILE *file = NULL;
  file = fopen(filename, "r");
  if (!file) {
    printf("fopen %s failed\n", filename);
    return -1;
  }
  memset(magic, 0, magic_size);
  if (fread(magic, magic_size, 1, file) != 1) {
    printf("fread failed\n");
    return -1;
  }
  fclose(file);
  return 0;
}


x86 assembly (clang, -O0):
<magic_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe8a(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11a8 <magic_read+0x58>
mov    -0x10(%rbp),%rsi
lea    0xe6d(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11fe <magic_read+0xae>
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%edx
xor    %esi,%esi
call   1060 <memset@plt>
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%esi
mov    -0x28(%rbp),%rcx
mov    $0x1,%edx
call   1030 <fread@plt>
cmp    $0x1,%eax
je     11ee <magic_read+0x9e>
lea    0xe38(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11fe <magic_read+0xae>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<magic_read>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edx,%r15d
mov    %rsi,%r12
mov    %rdi,%rbp
lea    0xe88(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     11b7 <magic_read+0x57>
mov    %rax,%rbx
xor    %r14d,%r14d
mov    %r12,%rdi
xor    %esi,%esi
mov    %r15d,%edx
call   1070 <memset@plt>
mov    %r12,%rdi
mov    %r15d,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1040 <fread@plt>
cmp    $0x1,%eax
jne    11ca <magic_read+0x6a>
mov    %rbx,%rdi
call   1050 <fclose@plt>
jmp    11dc <magic_read+0x7c>
lea    0xe44(%rip),%rdi
mov    %rbp,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
jmp    11d6 <magic_read+0x76>
lea    0xe42(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [17 x i8] c"fopen %s failed\0A\00"
@.str.2 = [14 x i8] c"fread failed\0A\00"
define i32 @magic_read(i8* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32*
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i32* null, i32** %8
%9 = load i8*, i8** %5
%10 = call i32* @fopen(i8* %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %10, i32** %8
%11 = load i32*, i32** %8
%12 = icmp ne i32* %11, null
br i1 %12, label %16, label %13
13: ; preds = %3
%14 = load i8*, i8** %5
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* %14)
store i32 -1, i32* %4
br label %30
16: ; preds = %3
%17 = load i8*, i8** %6
%18 = load i32, i32* %7
%19 = call i32 @memset(i8* %17, i32 0, i32 %18)
%20 = load i8*, i8** %6
%21 = load i32, i32* %7
%22 = load i32*, i32** %8
%23 = call i32 @fread(i8* %20, i32 %21, i32 1, i32* %22)
%24 = icmp ne i32 %23, 1
br i1 %24, label %25, label %27
25: ; preds = %16
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %4
br label %30
27: ; preds = %16
%28 = load i32*, i32** %8
%29 = call i32 @fclose(i32* %28)
store i32 0, i32* %4
br label %30
30: ; preds = %27, %25, %13
%31 = load i32, i32* %4
ret i32 %31
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [17 x i8] c"fopen %s failed\0A\00"
@str = [13 x i8] c"fread failed\00"
define i32 @magic_read(i8* %0, i8* %1, i32 %2) {
%4 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %6, label %8
6: ; preds = %3
%7 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %16
8: ; preds = %3
%9 = tail call i32 @memset(i8* %1, i32 0, i32 %2)
%10 = tail call i32 @fread(i8* %1, i32 %2, i32 1, i32* %4)
%11 = icmp eq i32 %10, 1
br i1 %11, label %14, label %12
12: ; preds = %8
%13 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str, i64 0, i64 0))
br label %16
14: ; preds = %8
%15 = tail call i32 @fclose(i32* %4)
br label %16
16: ; preds = %14, %12, %6
%17 = phi i32 [ -1, %12 ], [ 0, %14 ], [ -1, %6 ]
ret i32 %17
}