#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*/ printf(char *, ...);
int readhead13(int /*<<< orphan*/ *);
int readlength(int /*<<< orphan*/ *);
int readsecthead(unsigned char *, int /*<<< orphan*/ *);
int readstring(unsigned char *, int, int /*<<< orphan*/ *);
int readhead(FILE *f) {
  int t, len;
  unsigned char typ;
  unsigned char buffer[500];
  t = readhead13(f);
  if (t)
    return t;
  do {
    if (readsecthead(&typ, f) == -1)
      return -1;
    switch (typ) {
    case 0x61:
    case 0x62:
    case 0x63:
    case 0x64:
      t = readstring(buffer, 500, f);
      if (t)
        return -1;
      break;
    case 0x65:
      len = readlength(f);
      break;
    default:
      printf("Unknown header field '%c'.\n", typ);
      return -1;
    }
    switch (typ) {
    case 0x61:
      printf("Design name:    %s\n", buffer);
      break;
    case 0x62:
      printf("Target device:  xc%s\n", buffer);
      break;
    case 0x63:
      printf("Bitfile date:   %s\n", buffer);
      break;
    case 0x64:
      printf("Bitfile time:   %s\n", buffer);
      break;
    case 0x65:
      printf("Bitfile length: %08x\n", len);
      return len;
    }
  } while (1);
  return -1;
}


x86 assembly (clang, -O0):
<readhead>:
push   %rbp
mov    %rsp,%rbp
sub    $0x220,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <readhead13@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     1180 <readhead+0x30>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1307 <readhead+0x1b7>
jmp    1185 <readhead+0x35>
mov    -0x10(%rbp),%rsi
lea    -0x19(%rbp),%rdi
call   1070 <readsecthead@plt>
cmp    $0xffffffff,%eax
jne    11a7 <readhead+0x57>
movl   $0xffffffff,-0x4(%rbp)
jmp    1307 <readhead+0x1b7>
movzbl -0x19(%rbp),%eax
mov    %eax,-0x214(%rbp)
add    $0xffffff9f,%eax
sub    $0x4,%eax
jb     11d6 <readhead+0x86>
jmp    11c2 <readhead+0x72>
mov    -0x214(%rbp),%eax
sub    $0x65,%eax
je     1209 <readhead+0xb9>
jmp    121a <readhead+0xca>
lea    -0x210(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    $0x1f4,%esi
call   1050 <readstring@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     1204 <readhead+0xb4>
movl   $0xffffffff,-0x4(%rbp)
jmp    1307 <readhead+0x1b7>
jmp    1238 <readhead+0xe8>
mov    -0x10(%rbp),%rdi
call   1040 <readlength@plt>
mov    %eax,-0x18(%rbp)
jmp    1238 <readhead+0xe8>
movzbl -0x19(%rbp),%esi
lea    0xdef(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1307 <readhead+0x1b7>
movzbl -0x19(%rbp),%eax
add    $0xffffff9f,%eax
mov    %eax,%ecx
mov    %rcx,-0x220(%rbp)
sub    $0x4,%eax
ja     12ec <readhead+0x19c>
mov    -0x220(%rbp),%rax
lea    0xda1(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
lea    -0x210(%rbp),%rsi
lea    0xdba(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    12ec <readhead+0x19c>
lea    -0x210(%rbp),%rsi
lea    0xdb4(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    12ec <readhead+0x19c>
lea    -0x210(%rbp),%rsi
lea    0xdb0(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    12ec <readhead+0x19c>
lea    -0x210(%rbp),%rsi
lea    0xdaa(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    12ec <readhead+0x19c>
mov    -0x18(%rbp),%esi
lea    0xda8(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1307 <readhead+0x1b7>
jmp    12f1 <readhead+0x1a1>
mov    $0x1,%al
test   $0x1,%al
jne    1185 <readhead+0x35>
jmp    1300 <readhead+0x1b0>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x220,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readhead>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x208,%rsp
mov    %rdi,%rbx
call   1060 <readhead13@plt>
mov    %eax,%r14d
test   %eax,%eax
je     1185 <readhead+0x35>
mov    %r14d,%eax
add    $0x208,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xb(%rsp),%rdi
mov    %rbx,%rsi
call   1070 <readsecthead@plt>
mov    $0xffffffff,%r14d
cmp    $0xffffffff,%eax
je     1170 <readhead+0x20>
lea    0x10(%rsp),%rbp
lea    0xe87(%rip),%r13
lea    0xb(%rsp),%r15
lea    0xe4b(%rip),%r12
jmp    11d8 <readhead+0x88>
lea    0xe86(%rip),%rdi
mov    %rbp,%rsi
xor    %eax,%eax
call   1030 <printf@plt>
mov    %r15,%rdi
mov    %rbx,%rsi
call   1070 <readsecthead@plt>
cmp    $0xffffffff,%eax
je     1170 <readhead+0x20>
movzbl 0xb(%rsp),%eax
lea    -0x61(%rax),%ecx
cmp    $0x4,%cl
jae    1200 <readhead+0xb0>
mov    %rbp,%rdi
mov    $0x1f4,%esi
mov    %rbx,%rdx
call   1050 <readstring@plt>
test   %eax,%eax
je     1210 <readhead+0xc0>
jmp    1170 <readhead+0x20>
xchg   %ax,%ax
cmp    $0x65,%al
jne    123c <readhead+0xec>
mov    %rbx,%rdi
call   1040 <readlength@plt>
mov    %eax,0xc(%rsp)
movzbl 0xb(%rsp),%eax
add    $0x9f,%al
cmp    $0x4,%al
ja     11c8 <readhead+0x78>
movzbl %al,%eax
movslq (%r12,%rax,4),%rax
add    %r12,%rax
mov    %r13,%rdi
jmp    *%rax
lea    0xe29(%rip),%rdi
jmp    11be <readhead+0x6e>
lea    0xe34(%rip),%rdi
jmp    11be <readhead+0x6e>
movzbl %al,%esi
lea    0xdce(%rip),%rdi
jmp    1257 <readhead+0x107>
lea    0xe33(%rip),%rdi
mov    0xc(%rsp),%r14d
mov    %r14d,%esi
xor    %eax,%eax
call   1030 <printf@plt>
jmp    1170 <readhead+0x20>

LLVM IR (-O0):
@.str = [28 x i8] c"Unknown header field '%c'.\0A\00"
@.str.1 = [20 x i8] c"Design name: %s\0A\00"
@.str.2 = [22 x i8] c"Target device: xc%s\0A\00"
@.str.3 = [20 x i8] c"Bitfile date: %s\0A\00"
@.str.4 = [20 x i8] c"Bitfile time: %s\0A\00"
@.str.5 = [22 x i8] c"Bitfile length: %08x\0A\00"
define i32 @readhead(i32* %0) {
%2 = alloca i32
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8
%7 = alloca [500 x i8]
store i32* %0, i32** %3
%8 = load i32*, i32** %3
%9 = call i32 @readhead13(i32* %8)
store i32 %9, i32* %4
%10 = load i32, i32* %4
%11 = icmp ne i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %1
%13 = load i32, i32* %4
store i32 %13, i32* %2
br label %60
14: ; preds = %1
br label %15
15: ; preds = %58, %14
%16 = load i32*, i32** %3
%17 = call i32 @readsecthead(i8* %6, i32* %16)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %20
19: ; preds = %15
store i32 -1, i32* %2
br label %60
20: ; preds = %15
%21 = load i8, i8* %6
%22 = zext i8 %21 to i32
switch i32 %22, label %34 [
i32 97, label %23
i32 98, label %23
i32 99, label %23
i32 100, label %23
i32 101, label %31
]
23: ; preds = %20, %20, %20, %20
%24 = getelementptr inbounds [500 x i8], [500 x i8]* %7, i64 0, i64 0
%25 = load i32*, i32** %3
%26 = call i32 @readstring(i8* %24, i32 500, i32* %25)
store i32 %26, i32* %4
%27 = load i32, i32* %4
%28 = icmp ne i32 %27, 0
br i1 %28, label %29, label %30
29: ; preds = %23
store i32 -1, i32* %2
br label %60
30: ; preds = %23
br label %38
31: ; preds = %20
%32 = load i32*, i32** %3
%33 = call i32 @readlength(i32* %32)
store i32 %33, i32* %5
br label %38
34: ; preds = %20
%35 = load i8, i8* %6
%36 = zext i8 %35 to i32
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i32 %36)
store i32 -1, i32* %2
br label %60
38: ; preds = %31, %30
%39 = load i8, i8* %6
%40 = zext i8 %39 to i32
switch i32 %40, label %57 [
i32 97, label %41
i32 98, label %44
i32 99, label %47
i32 100, label %50
i32 101, label %53
]
41: ; preds = %38
%42 = getelementptr inbounds [500 x i8], [500 x i8]* %7, i64 0, i64 0
%43 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8* %42)
br label %57
44: ; preds = %38
%45 = getelementptr inbounds [500 x i8], [500 x i8]* %7, i64 0, i64 0
%46 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), i8* %45)
br label %57
47: ; preds = %38
%48 = getelementptr inbounds [500 x i8], [500 x i8]* %7, i64 0, i64 0
%49 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), i8* %48)
br label %57
50: ; preds = %38
%51 = getelementptr inbounds [500 x i8], [500 x i8]* %7, i64 0, i64 0
%52 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0), i8* %51)
br label %57
53: ; preds = %38
%54 = load i32, i32* %5
%55 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0), i32 %54)
%56 = load i32, i32* %5
store i32 %56, i32* %2
br label %60
57: ; preds = %38, %50, %47, %44, %41
br label %58
58: ; preds = %57
br i1 true, label %15, label %59
59: ; preds = %58
store i32 -1, i32* %2
br label %60
60: ; preds = %59, %53, %34, %29, %19, %12
%61 = load i32, i32* %2
ret i32 %61
}

LLVM IR (-O2):
@.str = [28 x i8] c"Unknown header field '%c'.\0A\00"
@.str.1 = [20 x i8] c"Design name: %s\0A\00"
@.str.2 = [22 x i8] c"Target device: xc%s\0A\00"
@.str.3 = [20 x i8] c"Bitfile date: %s\0A\00"
@.str.4 = [20 x i8] c"Bitfile time: %s\0A\00"
@.str.5 = [22 x i8] c"Bitfile length: %08x\0A\00"
define i32 @readhead(i32* %0) {
%2 = alloca i8
%3 = alloca [500 x i8]
%4 = getelementptr inbounds [500 x i8], [500 x i8]* %3, i64 0, i64 0
%5 = tail call i32 @readhead13(i32* %0)
%6 = icmp eq i32 %5, 0
br i1 %6, label %7, label %35
7: ; preds = %1
%8 = call i32 @readsecthead(i8* %2, i32* %0)
%9 = icmp eq i32 %8, -1
br i1 %9, label %35, label %10
10: ; preds = %7, %32
%11 = phi i32 [ %22, %32 ], [ undef, %7 ]
%12 = load i8, i8* %2
switch i8 %12, label %18 [
i8 97, label %13
i8 98, label %13
i8 99, label %13
i8 100, label %13
i8 101, label %16
]
13: ; preds = %10, %10, %10, %10
%14 = call i32 @readstring(i8* %4, i32 500, i32* %0)
%15 = icmp eq i32 %14, 0
br i1 %15, label %21, label %35
16: ; preds = %10
%17 = call i32 @readlength(i32* %0)
br label %21
18: ; preds = %10
%19 = zext i8 %12 to i32
%20 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i32 %19)
br label %35
21: ; preds = %13, %16
%22 = phi i32 [ %17, %16 ], [ %11, %13 ]
%23 = load i8, i8* %2
switch i8 %23, label %32 [
i8 97, label %29
i8 98, label %24
i8 99, label %25
i8 100, label %26
i8 101, label %27
]
24: ; preds = %21
br label %29
25: ; preds = %21
br label %29
26: ; preds = %21
br label %29
27: ; preds = %21
%28 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0), i32 %22)
br label %35
29: ; preds = %21, %26, %25, %24
%30 = phi i8* [ getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0), %24 ], [ getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0), %25 ], [ getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0), %26 ], [ getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), %21 ]
%31 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) %30, i8* %4)
br label %32
32: ; preds = %29, %21
%33 = call i32 @readsecthead(i8* %2, i32* %0)
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %10
35: ; preds = %32, %13, %7, %1, %27, %18
%36 = phi i32 [ -1, %18 ], [ %22, %27 ], [ %5, %1 ], [ -1, %7 ], [ -1, %13 ], [ -1, %32 ]
ret i32 %36
}