#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 ENOMEM;
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, long, int /*<<< orphan*/);
int ftell(int /*<<< orphan*/ *);
char *malloc(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ rewind(int /*<<< orphan*/ *);
int strlen(char *);
int /*<<< orphan*/ strstr(char *, char *);
int read_block_device(char *dev_name, char **file_content) {
  FILE *input = NULL;
  int len;
  input = fopen(dev_name, "rb");
  fseek(input, 0L, SEEK_END);
  len = ftell(input);
  rewind(input);
  *file_content = malloc(5);
  if (!*file_content) {
    perror("Insufficient memory\n");
    fclose(input);
    return -ENOMEM;
  }
  fread(*file_content, 5, 1, input);
  if (!strstr(*file_content, "<?xml"))
    return 1;
  free(*file_content);
  *file_content = malloc(len);
  if (!*file_content) {
    perror("Insufficient memory\n");
    fclose(input);
    return -ENOMEM;
  }
  rewind(input);
  fread(*file_content, len, 1, input);
  if (strlen(*file_content) >= len) {
    perror("Invalid data\n");
    fclose(input);
    return -1;
  }
  fclose(input);
  return 0;
}


x86 assembly (clang, -O0):
<read_block_device>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe2d(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    0x2e01(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   10a0 <fseek@plt>
mov    -0x20(%rbp),%rdi
call   1080 <ftell@plt>
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%rdi
call   1070 <rewind@plt>
mov    $0x5,%edi
call   1090 <malloc@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x18(%rbp),%rax
cmpq   $0x0,(%rax)
jne    1251 <read_block_device+0xa1>
lea    0xdd3(%rip),%rdi
call   10c0 <perror@plt>
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
xor    %eax,%eax
mov    0x2d91(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    134e <read_block_device+0x19e>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
mov    -0x20(%rbp),%rcx
mov    $0x5,%esi
mov    $0x1,%edx
call   1040 <fread@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
lea    0xd9f(%rip),%rsi
call   10d0 <strstr@plt>
cmp    $0x0,%eax
jne    1293 <read_block_device+0xe3>
movl   $0x1,-0x4(%rbp)
jmp    134e <read_block_device+0x19e>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
call   1030 <free@plt>
mov    -0x24(%rbp),%edi
call   1090 <malloc@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x18(%rbp),%rax
cmpq   $0x0,(%rax)
jne    12e7 <read_block_device+0x137>
lea    0xd3d(%rip),%rdi
call   10c0 <perror@plt>
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
xor    %eax,%eax
mov    0x2cfb(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    134e <read_block_device+0x19e>
mov    -0x20(%rbp),%rdi
call   1070 <rewind@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
mov    -0x24(%rbp),%esi
mov    -0x20(%rbp),%rcx
mov    $0x1,%edx
call   1040 <fread@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
call   1060 <strlen@plt>
cmp    -0x24(%rbp),%eax
jl     133e <read_block_device+0x18e>
lea    0xcfa(%rip),%rdi
call   10c0 <perror@plt>
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    134e <read_block_device+0x19e>
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_block_device>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
lea    0xe3f(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,%rbx
mov    0x2e18(%rip),%rax
mov    (%rax),%edx
xor    %ebp,%ebp
mov    %rbx,%rdi
xor    %esi,%esi
call   10a0 <fseek@plt>
mov    %rbx,%rdi
call   1080 <ftell@plt>
mov    %eax,%r15d
mov    %rbx,%rdi
call   1070 <rewind@plt>
mov    $0x5,%edi
call   1090 <malloc@plt>
mov    %rax,(%r14)
test   %rax,%rax
je     128b <read_block_device+0xdb>
mov    $0x1,%ebp
mov    %rax,%rdi
mov    $0x5,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1040 <fread@plt>
mov    (%r14),%rdi
lea    0xded(%rip),%rsi
call   10d0 <strstr@plt>
test   %eax,%eax
je     127e <read_block_device+0xce>
mov    (%r14),%rdi
call   1030 <free@plt>
mov    %r15d,%edi
call   1090 <malloc@plt>
mov    %rax,(%r14)
test   %rax,%rax
je     12aa <read_block_device+0xfa>
mov    %rbx,%rdi
call   1070 <rewind@plt>
mov    (%r14),%rdi
mov    %r15d,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1040 <fread@plt>
mov    (%r14),%rdi
call   1060 <strlen@plt>
cmp    %r15d,%eax
jge    12cb <read_block_device+0x11b>
mov    %rbx,%rdi
call   1050 <fclose@plt>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd71(%rip),%rdi
call   10c0 <perror@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
mov    0x2d32(%rip),%rax
sub    (%rax),%ebp
jmp    127e <read_block_device+0xce>
lea    0xd52(%rip),%rdi
call   10c0 <perror@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
mov    0x2d13(%rip),%rax
xor    %ebp,%ebp
sub    (%rax),%ebp
jmp    127e <read_block_device+0xce>
lea    0xd4c(%rip),%rdi
call   10c0 <perror@plt>
mov    %rbx,%rdi
call   1050 <fclose@plt>
mov    $0xffffffff,%ebp
jmp    127e <read_block_device+0xce>

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@SEEK_END = global i32 0
@.str.1 = [21 x i8] c"Insufficient memory\0A\00"
@ENOMEM = global i32 0
@.str.2 = [6 x i8] c"<?xml\00"
@.str.3 = [14 x i8] c"Invalid data\0A\00"
define i32 @read_block_device(i8* %0, i8** %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca i32
store i8* %0, i8** %4
store i8** %1, i8*** %5
store i32* null, i32** %6
%8 = load i8*, i8** %4
%9 = call i32* @fopen(i8* %8, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %9, i32** %6
%10 = load i32*, i32** %6
%11 = load i32, i32* @SEEK_END
%12 = call i32 @fseek(i32* %10, i64 0, i32 %11)
%13 = load i32*, i32** %6
%14 = call i32 @ftell(i32* %13)
store i32 %14, i32* %7
%15 = load i32*, i32** %6
%16 = call i32 @rewind(i32* %15)
%17 = call i8* @malloc(i32 5)
%18 = load i8**, i8*** %5
store i8* %17, i8** %18
%19 = load i8**, i8*** %5
%20 = load i8*, i8** %19
%21 = icmp ne i8* %20, null
br i1 %21, label %28, label %22
22: ; preds = %2
%23 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
%24 = load i32*, i32** %6
%25 = call i32 @fclose(i32* %24)
%26 = load i32, i32* @ENOMEM
%27 = sub nsw i32 0, %26
store i32 %27, i32* %3
br label %74
28: ; preds = %2
%29 = load i8**, i8*** %5
%30 = load i8*, i8** %29
%31 = load i32*, i32** %6
%32 = call i32 @fread(i8* %30, i32 5, i32 1, i32* %31)
%33 = load i8**, i8*** %5
%34 = load i8*, i8** %33
%35 = call i32 @strstr(i8* %34, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%36 = icmp ne i32 %35, 0
br i1 %36, label %38, label %37
37: ; preds = %28
store i32 1, i32* %3
br label %74
38: ; preds = %28
%39 = load i8**, i8*** %5
%40 = load i8*, i8** %39
%41 = call i32 @free(i8* %40)
%42 = load i32, i32* %7
%43 = call i8* @malloc(i32 %42)
%44 = load i8**, i8*** %5
store i8* %43, i8** %44
%45 = load i8**, i8*** %5
%46 = load i8*, i8** %45
%47 = icmp ne i8* %46, null
br i1 %47, label %54, label %48
48: ; preds = %38
%49 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
%50 = load i32*, i32** %6
%51 = call i32 @fclose(i32* %50)
%52 = load i32, i32* @ENOMEM
%53 = sub nsw i32 0, %52
store i32 %53, i32* %3
br label %74
54: ; preds = %38
%55 = load i32*, i32** %6
%56 = call i32 @rewind(i32* %55)
%57 = load i8**, i8*** %5
%58 = load i8*, i8** %57
%59 = load i32, i32* %7
%60 = load i32*, i32** %6
%61 = call i32 @fread(i8* %58, i32 %59, i32 1, i32* %60)
%62 = load i8**, i8*** %5
%63 = load i8*, i8** %62
%64 = call i32 @strlen(i8* %63)
%65 = load i32, i32* %7
%66 = icmp sge i32 %64, %65
br i1 %66, label %67, label %71
67: ; preds = %54
%68 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
%69 = load i32*, i32** %6
%70 = call i32 @fclose(i32* %69)
store i32 -1, i32* %3
br label %74
71: ; preds = %54
%72 = load i32*, i32** %6
%73 = call i32 @fclose(i32* %72)
store i32 0, i32* %3
br label %74
74: ; preds = %71, %67, %48, %37, %22
%75 = load i32, i32* %3
ret i32 %75
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@SEEK_END = global i32 0
@.str.1 = [21 x i8] c"Insufficient memory\0A\00"
@ENOMEM = global i32 0
@.str.2 = [6 x i8] c"<?xml\00"
@.str.3 = [14 x i8] c"Invalid data\0A\00"
define i32 @read_block_device(i8* nocapture %0, i8** nocapture %1) {
%3 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%4 = load i32, i32* @SEEK_END
%5 = tail call i32 @fseek(i32* %3, i64 0, i32 %4)
%6 = tail call i32 @ftell(i32* %3)
%7 = tail call i32 @rewind(i32* %3)
%8 = tail call dereferenceable_or_null(5) i8* @malloc(i32 5)
store i8* %8, i8** %1
%9 = icmp eq i8* %8, null
br i1 %9, label %10, label %15
10: ; preds = %2
%11 = tail call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
%12 = tail call i32 @fclose(i32* %3)
%13 = load i32, i32* @ENOMEM
%14 = sub nsw i32 0, %13
br label %42
15: ; preds = %2
%16 = tail call i32 @fread(i8* %8, i32 5, i32 1, i32* %3)
%17 = load i8*, i8** %1
%18 = tail call i32 @strstr(i8* %17, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%19 = icmp eq i32 %18, 0
br i1 %19, label %42, label %20
20: ; preds = %15
%21 = load i8*, i8** %1
%22 = tail call i32 @free(i8* %21)
%23 = tail call i8* @malloc(i32 %6)
store i8* %23, i8** %1
%24 = icmp eq i8* %23, null
br i1 %24, label %25, label %30
25: ; preds = %20
%26 = tail call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
%27 = tail call i32 @fclose(i32* %3)
%28 = load i32, i32* @ENOMEM
%29 = sub nsw i32 0, %28
br label %42
30: ; preds = %20
%31 = tail call i32 @rewind(i32* %3)
%32 = load i8*, i8** %1
%33 = tail call i32 @fread(i8* %32, i32 %6, i32 1, i32* %3)
%34 = load i8*, i8** %1
%35 = tail call i32 @strlen(i8* %34)
%36 = icmp slt i32 %35, %6
br i1 %36, label %40, label %37
37: ; preds = %30
%38 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
%39 = tail call i32 @fclose(i32* %3)
br label %42
40: ; preds = %30
%41 = tail call i32 @fclose(i32* %3)
br label %42
42: ; preds = %15, %40, %37, %25, %10
%43 = phi i32 [ -1, %37 ], [ 0, %40 ], [ %29, %25 ], [ %14, %10 ], [ 1, %15 ]
ret i32 %43
}