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

/* Variables and functions */
char do_peek(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
int getref(int /*<<< orphan*/ *);
char *getstring(int /*<<< orphan*/ *);
int /*<<< orphan*/ strcmp(char *, char *);
int db_read_header(FILE *f, int *grow) {
  int load_format = 0;
  char *special;
  *grow = 0;
  if (do_peek(f) != '*') {
    return 0;
  }
  special = getstring(f);
  if (!strcmp(special, "***Foxen8 TinyMUCK DUMP Format***")) {
    load_format = 10;
  } else if (!strcmp(special, "***Foxen9 TinyMUCK DUMP Format***")) {
    load_format = 11;
  }
  free(special);
  *grow = getref(f);
  getref(f);
  char buf[((2048) * 4)];
  for (int i = 0, n = getref(f); i < n; i++) {
    if (fgets(buf, sizeof(buf), f) == NULL) {
      return 0;
    }
  }
  return load_format;
}


x86 assembly (clang, -O0):
<db_read_header>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2040,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x18(%rbp),%rax
movl   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
call   1070 <do_peek@plt>
movsbl %al,%eax
cmp    $0x2a,%eax
je     11a5 <db_read_header+0x45>
movl   $0x0,-0x4(%rbp)
jmp    1294 <db_read_header+0x134>
mov    -0x10(%rbp),%rdi
call   1040 <getstring@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
lea    0xe43(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%eax
jne    11d7 <db_read_header+0x77>
movl   $0xa,-0x1c(%rbp)
jmp    11fc <db_read_header+0x9c>
mov    -0x28(%rbp),%rdi
lea    0xe40(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%eax
jne    11f7 <db_read_header+0x97>
movl   $0xb,-0x1c(%rbp)
jmp    11fc <db_read_header+0x9c>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x10(%rbp),%rdi
call   1080 <getref@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x10(%rbp),%rdi
call   1080 <getref@plt>
movl   $0x0,-0x2034(%rbp)
mov    -0x10(%rbp),%rdi
call   1080 <getref@plt>
mov    %eax,-0x2038(%rbp)
mov    -0x2034(%rbp),%eax
cmp    -0x2038(%rbp),%eax
jge    128e <db_read_header+0x12e>
lea    -0x2030(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    $0x2000,%esi
call   1050 <fgets@plt>
cmp    $0x0,%rax
jne    1275 <db_read_header+0x115>
movl   $0x0,-0x4(%rbp)
jmp    1294 <db_read_header+0x134>
jmp    127a <db_read_header+0x11a>
mov    -0x2034(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2034(%rbp)
jmp    1238 <db_read_header+0xd8>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x2040,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<db_read_header>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x2000,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
movl   $0x0,(%rsi)
call   1070 <do_peek@plt>
xor    %r12d,%r12d
cmp    $0x2a,%al
jne    121f <db_read_header+0xbf>
mov    %rbx,%rdi
call   1040 <getstring@plt>
mov    %rax,%r15
lea    0xe63(%rip),%rsi
mov    %rax,%rdi
call   1060 <strcmp@plt>
test   %eax,%eax
je     11c8 <db_read_header+0x68>
lea    0xe72(%rip),%rsi
mov    %r15,%rdi
call   1060 <strcmp@plt>
xor    %ecx,%ecx
test   %eax,%eax
mov    $0xb,%r12d
cmovne %ecx,%r12d
jmp    11ce <db_read_header+0x6e>
mov    $0xa,%r12d
mov    %r15,%rdi
call   1030 <free@plt>
mov    %rbx,%rdi
call   1080 <getref@plt>
mov    %eax,(%r14)
mov    %rbx,%rdi
call   1080 <getref@plt>
mov    %rbx,%rdi
call   1080 <getref@plt>
test   %eax,%eax
jle    121f <db_read_header+0xbf>
mov    %eax,%ebp
mov    %rsp,%r14
nopw   0x0(%rax,%rax,1)
mov    %r14,%rdi
mov    $0x2000,%esi
mov    %rbx,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
je     121c <db_read_header+0xbc>
add    $0xffffffff,%ebp
jne    1200 <db_read_header+0xa0>
jmp    121f <db_read_header+0xbf>
xor    %r12d,%r12d
mov    %r12d,%eax
add    $0x2000,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [34 x i8] c"***Foxen8 TinyMUCK DUMP Format***\00"
@.str.1 = [34 x i8] c"***Foxen9 TinyMUCK DUMP Format***\00"
define i32 @db_read_header(i32* %0, i32* %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca [8192 x i8]
%9 = alloca i32
%10 = alloca i32
store i32* %0, i32** %4
store i32* %1, i32** %5
store i32 0, i32* %6
%11 = load i32*, i32** %5
store i32 0, i32* %11
%12 = load i32*, i32** %4
%13 = call signext i8 @do_peek(i32* %12)
%14 = sext i8 %13 to i32
%15 = icmp ne i32 %14, 42
br i1 %15, label %16, label %17
16: ; preds = %2
store i32 0, i32* %3
br label %56
17: ; preds = %2
%18 = load i32*, i32** %4
%19 = call i8* @getstring(i32* %18)
store i8* %19, i8** %7
%20 = load i8*, i8** %7
%21 = call i32 @strcmp(i8* %20, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0))
%22 = icmp ne i32 %21, 0
br i1 %22, label %24, label %23
23: ; preds = %17
store i32 10, i32* %6
br label %30
24: ; preds = %17
%25 = load i8*, i8** %7
%26 = call i32 @strcmp(i8* %25, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0))
%27 = icmp ne i32 %26, 0
br i1 %27, label %29, label %28
28: ; preds = %24
store i32 11, i32* %6
br label %29
29: ; preds = %28, %24
br label %30
30: ; preds = %29, %23
%31 = load i8*, i8** %7
%32 = call i32 @free(i8* %31)
%33 = load i32*, i32** %4
%34 = call i32 @getref(i32* %33)
%35 = load i32*, i32** %5
store i32 %34, i32* %35
%36 = load i32*, i32** %4
%37 = call i32 @getref(i32* %36)
store i32 0, i32* %9
%38 = load i32*, i32** %4
%39 = call i32 @getref(i32* %38)
store i32 %39, i32* %10
br label %40
40: ; preds = %51, %30
%41 = load i32, i32* %9
%42 = load i32, i32* %10
%43 = icmp slt i32 %41, %42
br i1 %43, label %44, label %54
44: ; preds = %40
%45 = getelementptr inbounds [8192 x i8], [8192 x i8]* %8, i64 0, i64 0
%46 = load i32*, i32** %4
%47 = call i32* @fgets(i8* %45, i32 8192, i32* %46)
%48 = icmp eq i32* %47, null
br i1 %48, label %49, label %50
49: ; preds = %44
store i32 0, i32* %3
br label %56
50: ; preds = %44
br label %51
51: ; preds = %50
%52 = load i32, i32* %9
%53 = add nsw i32 %52, 1
store i32 %53, i32* %9
br label %40
54: ; preds = %40
%55 = load i32, i32* %6
store i32 %55, i32* %3
br label %56
56: ; preds = %54, %49, %16
%57 = load i32, i32* %3
ret i32 %57
}

LLVM IR (-O2):
@.str = [34 x i8] c"***Foxen8 TinyMUCK DUMP Format***\00"
@.str.1 = [34 x i8] c"***Foxen9 TinyMUCK DUMP Format***\00"
define i32 @db_read_header(i32* %0, i32* nocapture writeonly %1) {
%3 = alloca [8192 x i8]
store i32 0, i32* %1
%4 = tail call signext i8 @do_peek(i32* %0)
%5 = icmp eq i8 %4, 42
br i1 %5, label %6, label %31
6: ; preds = %2
%7 = tail call i8* @getstring(i32* %0)
%8 = tail call i32 @strcmp(i8* dereferenceable(1) %7, i8* dereferenceable(34) getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0))
%9 = icmp eq i32 %8, 0
br i1 %9, label %14, label %10
10: ; preds = %6
%11 = tail call i32 @strcmp(i8* dereferenceable(1) %7, i8* dereferenceable(34) getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0))
%12 = icmp eq i32 %11, 0
%13 = select i1 %12, i32 11, i32 0
br label %14
14: ; preds = %10, %6
%15 = phi i32 [ 10, %6 ], [ %13, %10 ]
%16 = tail call i32 @free(i8* %7)
%17 = tail call i32 @getref(i32* %0)
store i32 %17, i32* %1
%18 = tail call i32 @getref(i32* %0)
%19 = getelementptr inbounds [8192 x i8], [8192 x i8]* %3, i64 0, i64 0
%20 = tail call i32 @getref(i32* %0)
%21 = icmp sgt i32 %20, 0
br i1 %21, label %24, label %29
22: ; preds = %24
%23 = icmp eq i32 %28, %20
br i1 %23, label %29, label %24
24: ; preds = %14, %22
%25 = phi i32 [ %28, %22 ], [ 0, %14 ]
%26 = call i32* @fgets(i8* %19, i32 8192, i32* %0)
%27 = icmp eq i32* %26, null
%28 = add nuw nsw i32 %25, 1
br i1 %27, label %29, label %22
29: ; preds = %22, %24, %14
%30 = phi i32 [ %15, %14 ], [ 0, %24 ], [ %15, %22 ]
br label %31
31: ; preds = %2, %29
%32 = phi i32 [ %30, %29 ], [ 0, %2 ]
ret i32 %32
}