#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 fread(unsigned char *, unsigned long, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(unsigned char *);
unsigned char *malloc(int);
scalar_t__ strncmp(char *, char *, int);
unsigned char *read_hdr(FILE *f, size_t *hdr_len) {
  unsigned char *hdr;
  unsigned long format_data_length;
  hdr = malloc(256);
  if (hdr == NULL)
    return NULL;
  if (fread(hdr, 44, 1, f) != 1)
    goto fail;
  if (strncmp((char *)hdr + 12, "fmt ", 4))
    goto fail;
  format_data_length = ((((unsigned long)((hdr + 16)[3])) << 24) |
                        (((unsigned long)((hdr + 16)[2])) << 16) |
                        (((unsigned long)((hdr + 16)[1])) << 8) |
                        (((unsigned long)((hdr + 16)[0]))));
  if ((format_data_length < 16) || (format_data_length > 100))
    goto fail;
  *hdr_len = 44;
  if (format_data_length > 16) {
    if (fread(hdr + 44, format_data_length - 16, 1, f) != 1)
      goto fail;
    *hdr_len += format_data_length - 16;
  }
  return hdr;
fail:
  if (hdr != NULL)
    free(hdr);
  return NULL;
}


x86 assembly (clang, -O0):
<read_hdr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    $0x100,%edi
call   1060 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1176 <read_hdr+0x36>
movq   $0x0,-0x8(%rbp)
jmp    1294 <read_hdr+0x154>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x2c,%esi
mov    $0x1,%edx
call   1050 <fread@plt>
cmp    $0x1,%eax
je     119b <read_hdr+0x5b>
jmp    1278 <read_hdr+0x138>
mov    -0x20(%rbp),%rdi
add    $0xc,%rdi
lea    0xe56(%rip),%rsi
mov    $0x4,%edx
call   1040 <strncmp@plt>
cmp    $0x0,%rax
je     11c3 <read_hdr+0x83>
jmp    1278 <read_hdr+0x138>
mov    -0x20(%rbp),%rax
movzbl 0x13(%rax),%eax
shl    $0x18,%rax
mov    -0x20(%rbp),%rcx
movzbl 0x12(%rcx),%ecx
shl    $0x10,%rcx
or     %rcx,%rax
mov    -0x20(%rbp),%rcx
movzbl 0x11(%rcx),%ecx
shl    $0x8,%rcx
or     %rcx,%rax
mov    -0x20(%rbp),%rcx
movzbl 0x10(%rcx),%ecx
or     %rcx,%rax
mov    %rax,-0x28(%rbp)
cmpq   $0x10,-0x28(%rbp)
jb     1212 <read_hdr+0xd2>
cmpq   $0x64,-0x28(%rbp)
jbe    1217 <read_hdr+0xd7>
jmp    1278 <read_hdr+0x138>
mov    -0x18(%rbp),%rax
movq   $0x2c,(%rax)
cmpq   $0x10,-0x28(%rbp)
jbe    126b <read_hdr+0x12b>
mov    -0x20(%rbp),%rdi
add    $0x2c,%rdi
mov    -0x28(%rbp),%rsi
sub    $0x10,%rsi
mov    -0x10(%rbp),%rcx
mov    $0x1,%edx
call   1050 <fread@plt>
cmp    $0x1,%eax
je     1259 <read_hdr+0x119>
jmp    1278 <read_hdr+0x138>
mov    -0x28(%rbp),%rcx
sub    $0x10,%rcx
mov    -0x18(%rbp),%rax
add    (%rax),%rcx
mov    %rcx,(%rax)
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1294 <read_hdr+0x154>
cmpq   $0x0,-0x20(%rbp)
je     128c <read_hdr+0x14c>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_hdr>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r15
mov    $0x100,%edi
call   1060 <malloc@plt>
test   %rax,%rax
je     11e1 <read_hdr+0xa1>
mov    %rax,%rbx
mov    $0x2c,%esi
mov    %rax,%rdi
mov    $0x1,%edx
mov    %r15,%rcx
call   1050 <fread@plt>
cmp    $0x1,%eax
jne    11d9 <read_hdr+0x99>
lea    0xc(%rbx),%rdi
lea    0xe77(%rip),%rsi
mov    $0x4,%edx
call   1040 <strncmp@plt>
test   %rax,%rax
jne    11d9 <read_hdr+0x99>
mov    0x10(%rbx),%r12d
lea    -0x65(%r12),%rax
cmp    $0xffffffffffffffab,%rax
jb     11d9 <read_hdr+0x99>
movq   $0x2c,(%r14)
cmp    $0x11,%r12d
jb     11e3 <read_hdr+0xa3>
mov    %rbx,%rdi
add    $0x2c,%rdi
add    $0xfffffffffffffff0,%r12
mov    %r12,%rsi
mov    $0x1,%edx
mov    %r15,%rcx
call   1050 <fread@plt>
cmp    $0x1,%eax
jne    11d9 <read_hdr+0x99>
add    %r12,(%r14)
jmp    11e3 <read_hdr+0xa3>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [5 x i8] c"fmt \00"
define i8* @read_hdr(i32* %0, i64* %1) {
%3 = alloca i8*
%4 = alloca i32*
%5 = alloca i64*
%6 = alloca i8*
%7 = alloca i64
store i32* %0, i32** %4
store i64* %1, i64** %5
%8 = call i8* @malloc(i32 256)
store i8* %8, i8** %6
%9 = load i8*, i8** %6
%10 = icmp eq i8* %9, null
br i1 %10, label %11, label %12
11: ; preds = %2
store i8* null, i8** %3
br label %85
12: ; preds = %2
%13 = load i8*, i8** %6
%14 = load i32*, i32** %4
%15 = call i32 @fread(i8* %13, i64 44, i32 1, i32* %14)
%16 = icmp ne i32 %15, 1
br i1 %16, label %17, label %18
17: ; preds = %12
br label %78
18: ; preds = %12
%19 = load i8*, i8** %6
%20 = getelementptr inbounds i8, i8* %19, i64 12
%21 = call i64 @strncmp(i8* %20, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 4)
%22 = icmp ne i64 %21, 0
br i1 %22, label %23, label %24
23: ; preds = %18
br label %78
24: ; preds = %18
%25 = load i8*, i8** %6
%26 = getelementptr inbounds i8, i8* %25, i64 16
%27 = getelementptr inbounds i8, i8* %26, i64 3
%28 = load i8, i8* %27
%29 = zext i8 %28 to i64
%30 = shl i64 %29, 24
%31 = load i8*, i8** %6
%32 = getelementptr inbounds i8, i8* %31, i64 16
%33 = getelementptr inbounds i8, i8* %32, i64 2
%34 = load i8, i8* %33
%35 = zext i8 %34 to i64
%36 = shl i64 %35, 16
%37 = or i64 %30, %36
%38 = load i8*, i8** %6
%39 = getelementptr inbounds i8, i8* %38, i64 16
%40 = getelementptr inbounds i8, i8* %39, i64 1
%41 = load i8, i8* %40
%42 = zext i8 %41 to i64
%43 = shl i64 %42, 8
%44 = or i64 %37, %43
%45 = load i8*, i8** %6
%46 = getelementptr inbounds i8, i8* %45, i64 16
%47 = getelementptr inbounds i8, i8* %46, i64 0
%48 = load i8, i8* %47
%49 = zext i8 %48 to i64
%50 = or i64 %44, %49
store i64 %50, i64* %7
%51 = load i64, i64* %7
%52 = icmp ult i64 %51, 16
br i1 %52, label %56, label %53
53: ; preds = %24
%54 = load i64, i64* %7
%55 = icmp ugt i64 %54, 100
br i1 %55, label %56, label %57
56: ; preds = %53, %24
br label %78
57: ; preds = %53
%58 = load i64*, i64** %5
store i64 44, i64* %58
%59 = load i64, i64* %7
%60 = icmp ugt i64 %59, 16
br i1 %60, label %61, label %76
61: ; preds = %57
%62 = load i8*, i8** %6
%63 = getelementptr inbounds i8, i8* %62, i64 44
%64 = load i64, i64* %7
%65 = sub i64 %64, 16
%66 = load i32*, i32** %4
%67 = call i32 @fread(i8* %63, i64 %65, i32 1, i32* %66)
%68 = icmp ne i32 %67, 1
br i1 %68, label %69, label %70
69: ; preds = %61
br label %78
70: ; preds = %61
%71 = load i64, i64* %7
%72 = sub i64 %71, 16
%73 = load i64*, i64** %5
%74 = load i64, i64* %73
%75 = add i64 %74, %72
store i64 %75, i64* %73
br label %76
76: ; preds = %70, %57
%77 = load i8*, i8** %6
store i8* %77, i8** %3
br label %85
78: ; preds = %69, %56, %23, %17
%79 = load i8*, i8** %6
%80 = icmp ne i8* %79, null
br i1 %80, label %81, label %84
81: ; preds = %78
%82 = load i8*, i8** %6
%83 = call i32 @free(i8* %82)
br label %84
84: ; preds = %81, %78
store i8* null, i8** %3
br label %85
85: ; preds = %84, %76, %11
%86 = load i8*, i8** %3
ret i8* %86
}

LLVM IR (-O2):
@.str = [5 x i8] c"fmt \00"
define i8* @read_hdr(i32* nocapture %0, i64* nocapture %1) {
%3 = tail call dereferenceable_or_null(256) i8* @malloc(i32 256)
%4 = icmp eq i8* %3, null
br i1 %4, label %45, label %5
5: ; preds = %2
%6 = tail call i32 @fread(i8* %3, i64 44, i32 1, i32* %0)
%7 = icmp eq i32 %6, 1
br i1 %7, label %8, label %43
8: ; preds = %5
%9 = getelementptr inbounds i8, i8* %3, i64 12
%10 = tail call i64 @strncmp(i8* %9, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 4)
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %43
12: ; preds = %8
%13 = getelementptr inbounds i8, i8* %3, i64 16
%14 = getelementptr inbounds i8, i8* %3, i64 19
%15 = load i8, i8* %14
%16 = zext i8 %15 to i64
%17 = shl nuw nsw i64 %16, 24
%18 = getelementptr inbounds i8, i8* %3, i64 18
%19 = load i8, i8* %18
%20 = zext i8 %19 to i64
%21 = shl nuw nsw i64 %20, 16
%22 = or i64 %21, %17
%23 = getelementptr inbounds i8, i8* %3, i64 17
%24 = load i8, i8* %23
%25 = zext i8 %24 to i64
%26 = shl nuw nsw i64 %25, 8
%27 = or i64 %22, %26
%28 = load i8, i8* %13
%29 = zext i8 %28 to i64
%30 = or i64 %27, %29
%31 = add nsw i64 %30, -101
%32 = icmp ult i64 %31, -85
br i1 %32, label %43, label %33
33: ; preds = %12
store i64 44, i64* %1
%34 = icmp ugt i64 %30, 16
br i1 %34, label %35, label %45
35: ; preds = %33
%36 = getelementptr inbounds i8, i8* %3, i64 44
%37 = add nsw i64 %30, -16
%38 = tail call i32 @fread(i8* %36, i64 %37, i32 1, i32* %0)
%39 = icmp eq i32 %38, 1
br i1 %39, label %40, label %43
40: ; preds = %35
%41 = load i64, i64* %1
%42 = add i64 %41, %37
store i64 %42, i64* %1
br label %45
43: ; preds = %5, %8, %12, %35
%44 = tail call i32 @free(i8* %3)
br label %45
45: ; preds = %33, %40, %2, %43
%46 = phi i8* [ null, %43 ], [ null, %2 ], [ %3, %40 ], [ %3, %33 ]
ret i8* %46
}