#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 char *time_t;
struct tm {
  int dummy;
};
typedef int int8_t;
typedef char *int64_t;
typedef int /*<<< orphan*/ dbf;
typedef int /*<<< orphan*/ cn;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
char *be64toh(char *);
int bgs_read(int /*<<< orphan*/ *, ...);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int);
scalar_t__ feof(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
struct tm *gmtime(char **);
int memcmp(char *, char *, int);
int /*<<< orphan*/ printf(char *, ...);
char *strerror(int /*<<< orphan*/);
int /*<<< orphan*/ strftime(char *, size_t, char *, struct tm *);
void read_db(void) {
  FILE *fp;
  int r;
  fp = fopen("dbbucket.tmp", "r");
  if (!fp) {
    printf("fp fail\n");
    exit(1);
  }
  char signature[5];
  r = bgs_read(fp, signature, 5);
  if (r = 0) {
    if (feof(fp))
      printf("eof reading\n");
    else
      printf("error reading %s\n", strerror(errno));
  }
  r = memcmp("kopou", signature, 5);
  if (r == 0)
    printf("it is kopou db file\n");
  int8_t dbid;
  r = bgs_read(fp, &dbid, sizeof(int8_t));
  if (dbid == 1) {
    printf("dbid is good %d\n", dbid);
  }
  int8_t dbv;
  r = bgs_read(fp, &dbv, sizeof(int8_t));
  if (dbv == 1) {
    printf("dbv is good %d\n", dbv);
  }
  time_t dbf;
  r = bgs_read(fp, &dbf, sizeof(dbf));
  dbf = be64toh(dbf);
  char buf[128];
  size_t len = 128;
  struct tm *tm = gmtime(&dbf);
  strftime(buf, len, "Date: %a, %d %b %Y %H:%M:%S %Z\r\n", tm);
  printf("%s\n", buf);
  int64_t cn;
  r = bgs_read(fp, &cn, sizeof(cn));
  cn = be64toh(cn);
  printf("count %zu\n", cn);
}


x86 assembly (clang, -O0):
<read_db>:
push   %rbp
mov    %rsp,%rbp
sub    $0xd0,%rsp
lea    0xe4e(%rip),%rdi
lea    0xe54(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    11e5 <read_db+0x45>
lea    0xe3b(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x8(%rbp),%rdi
lea    -0x11(%rbp),%rsi
mov    $0x5,%edx
mov    $0x0,%al
call   1080 <bgs_read@plt>
mov    %eax,-0xc(%rbp)
movl   $0x0,-0xc(%rbp)
xor    %eax,%eax
test   $0x1,%al
jne    1212 <read_db+0x72>
jmp    125c <read_db+0xbc>
mov    -0x8(%rbp),%rdi
call   1060 <feof@plt>
cmp    $0x0,%rax
je     1238 <read_db+0x98>
lea    0xdec(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1257 <read_db+0xb7>
mov    0x2da1(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    %rax,%rsi
lea    0xdd5(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    125c <read_db+0xbc>
lea    -0x11(%rbp),%rsi
lea    0xdd0(%rip),%rdi
mov    $0x5,%edx
call   1040 <memcmp@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jne    128c <read_db+0xec>
lea    0xdb8(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x8(%rbp),%rdi
lea    -0x18(%rbp),%rsi
mov    $0x4,%edx
mov    $0x0,%al
call   1080 <bgs_read@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x1,-0x18(%rbp)
jne    12be <read_db+0x11e>
mov    -0x18(%rbp),%esi
lea    0xd9b(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x8(%rbp),%rdi
lea    -0x1c(%rbp),%rsi
mov    $0x4,%edx
mov    $0x0,%al
call   1080 <bgs_read@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x1,-0x1c(%rbp)
jne    12f0 <read_db+0x150>
mov    -0x1c(%rbp),%esi
lea    0xd7a(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x8(%rbp),%rdi
lea    -0x28(%rbp),%rsi
mov    $0x8,%edx
mov    $0x0,%al
call   1080 <bgs_read@plt>
mov    %eax,-0xc(%rbp)
mov    -0x28(%rbp),%rdi
call   1050 <be64toh@plt>
mov    %rax,-0x28(%rbp)
movq   $0x80,-0xb8(%rbp)
lea    -0x28(%rbp),%rdi
call   10a0 <gmtime@plt>
mov    %rax,-0xc0(%rbp)
lea    -0xb0(%rbp),%rdi
mov    -0xb8(%rbp),%rsi
mov    -0xc0(%rbp),%rcx
lea    0xd28(%rip),%rdx
call   1070 <strftime@plt>
lea    -0xb0(%rbp),%rsi
lea    0xcd5(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x8(%rbp),%rdi
lea    -0xc8(%rbp),%rsi
mov    $0x8,%edx
mov    $0x0,%al
call   1080 <bgs_read@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc8(%rbp),%rdi
call   1050 <be64toh@plt>
mov    %rax,-0xc8(%rbp)
mov    -0xc8(%rbp),%rsi
lea    0xcf4(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
add    $0xd0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_db>:
push   %r14
push   %rbx
sub    $0xa8,%rsp
lea    0xe5f(%rip),%rdi
lea    0xe65(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     12de <read_db+0x14e>
mov    %rax,%rbx
lea    0x1b(%rsp),%r14
mov    %rax,%rdi
mov    %r14,%rsi
mov    $0x5,%edx
xor    %eax,%eax
call   1080 <bgs_read@plt>
lea    0xe38(%rip),%rdi
mov    %r14,%rsi
mov    $0x5,%edx
call   1050 <memcmp@plt>
test   %eax,%eax
jne    11f4 <read_db+0x64>
lea    0xe7b(%rip),%rdi
call   1030 <puts@plt>
lea    0x4(%rsp),%rsi
mov    $0x4,%edx
mov    %rbx,%rdi
xor    %eax,%eax
call   1080 <bgs_read@plt>
cmpl   $0x1,0x4(%rsp)
jne    1222 <read_db+0x92>
lea    0xdff(%rip),%rdi
mov    $0x1,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %rsp,%rsi
mov    $0x4,%edx
mov    %rbx,%rdi
xor    %eax,%eax
call   1080 <bgs_read@plt>
cmpl   $0x1,(%rsp)
jne    124d <read_db+0xbd>
lea    0xde5(%rip),%rdi
mov    $0x1,%esi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0x10(%rsp),%r14
mov    $0x8,%edx
mov    %rbx,%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1080 <bgs_read@plt>
mov    0x10(%rsp),%rdi
call   1060 <be64toh@plt>
mov    %rax,0x10(%rsp)
mov    %r14,%rdi
call   10a0 <gmtime@plt>
lea    0xdb4(%rip),%rdx
lea    0x20(%rsp),%r14
mov    $0x80,%esi
mov    %r14,%rdi
mov    %rax,%rcx
call   1070 <strftime@plt>
mov    %r14,%rdi
call   1030 <puts@plt>
lea    0x8(%rsp),%rsi
mov    $0x8,%edx
mov    %rbx,%rdi
xor    %eax,%eax
call   1080 <bgs_read@plt>
mov    0x8(%rsp),%rdi
call   1060 <be64toh@plt>
mov    %rax,0x8(%rsp)
lea    0xd8e(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
add    $0xa8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xd7d(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
%struct.tm = type { i32 }
@.str = [13 x i8] c"dbbucket.tmp\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [9 x i8] c"fp fail\0A\00"
@.str.3 = [13 x i8] c"eof reading\0A\00"
@.str.4 = [18 x i8] c"error reading %s\0A\00"
@errno = global i32 0
@.str.5 = [6 x i8] c"kopou\00"
@.str.6 = [21 x i8] c"it is kopou db file\0A\00"
@.str.7 = [17 x i8] c"dbid is good %d\0A\00"
@.str.8 = [16 x i8] c"dbv is good %d\0A\00"
@.str.9 = [33 x i8] c"Date: %a, %d %b %Y %H:%M:%S %Z\0D\0A\00"
@.str.10 = [4 x i8] c"%s\0A\00"
@.str.11 = [11 x i8] c"count %zu\0A\00"
define void @read_db() {
%1 = alloca i32*
%2 = alloca i32
%3 = alloca [5 x i8]
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca [128 x i8]
%8 = alloca i64
%9 = alloca %struct.tm*
%10 = alloca i8*
%11 = call i32* @fopen(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %11, i32** %1
%12 = load i32*, i32** %1
%13 = icmp ne i32* %12, null
br i1 %13, label %17, label %14
14: ; preds = %0
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
%16 = call i32 @exit(i32 1)
unreachable
17: ; preds = %0
%18 = load i32*, i32** %1
%19 = getelementptr inbounds [5 x i8], [5 x i8]* %3, i64 0, i64 0
%20 = call i32 (i32*, ...) @bgs_read(i32* %18, i8* %19, i32 5)
store i32 %20, i32* %2
store i32 0, i32* %2
br i1 false, label %21, label %32
21: ; preds = %17
%22 = load i32*, i32** %1
%23 = call i64 @feof(i32* %22)
%24 = icmp ne i64 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %21
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
br label %31
27: ; preds = %21
%28 = load i32, i32* @errno
%29 = call i8* @strerror(i32 %28)
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0), i8* %29)
br label %31
31: ; preds = %27, %25
br label %32
32: ; preds = %31, %17
%33 = getelementptr inbounds [5 x i8], [5 x i8]* %3, i64 0, i64 0
%34 = call i32 @memcmp(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0), i8* %33, i32 5)
store i32 %34, i32* %2
%35 = load i32, i32* %2
%36 = icmp eq i32 %35, 0
br i1 %36, label %37, label %39
37: ; preds = %32
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.6, i64 0, i64 0))
br label %39
39: ; preds = %37, %32
%40 = load i32*, i32** %1
%41 = call i32 (i32*, ...) @bgs_read(i32* %40, i32* %4, i64 4)
store i32 %41, i32* %2
%42 = load i32, i32* %4
%43 = icmp eq i32 %42, 1
br i1 %43, label %44, label %47
44: ; preds = %39
%45 = load i32, i32* %4
%46 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.7, i64 0, i64 0), i32 %45)
br label %47
47: ; preds = %44, %39
%48 = load i32*, i32** %1
%49 = call i32 (i32*, ...) @bgs_read(i32* %48, i32* %5, i64 4)
store i32 %49, i32* %2
%50 = load i32, i32* %5
%51 = icmp eq i32 %50, 1
br i1 %51, label %52, label %55
52: ; preds = %47
%53 = load i32, i32* %5
%54 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.8, i64 0, i64 0), i32 %53)
br label %55
55: ; preds = %52, %47
%56 = load i32*, i32** %1
%57 = call i32 (i32*, ...) @bgs_read(i32* %56, i8** %6, i64 8)
store i32 %57, i32* %2
%58 = load i8*, i8** %6
%59 = call i8* @be64toh(i8* %58)
store i8* %59, i8** %6
store i64 128, i64* %8
%60 = call %struct.tm* @gmtime(i8** %6)
store %struct.tm* %60, %struct.tm** %9
%61 = getelementptr inbounds [128 x i8], [128 x i8]* %7, i64 0, i64 0
%62 = load i64, i64* %8
%63 = load %struct.tm*, %struct.tm** %9
%64 = call i32 @strftime(i8* %61, i64 %62, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.9, i64 0, i64 0), %struct.tm* %63)
%65 = getelementptr inbounds [128 x i8], [128 x i8]* %7, i64 0, i64 0
%66 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.10, i64 0, i64 0), i8* %65)
%67 = load i32*, i32** %1
%68 = call i32 (i32*, ...) @bgs_read(i32* %67, i8** %10, i64 8)
store i32 %68, i32* %2
%69 = load i8*, i8** %10
%70 = call i8* @be64toh(i8* %69)
store i8* %70, i8** %10
%71 = load i8*, i8** %10
%72 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.11, i64 0, i64 0), i8* %71)
ret void
}

LLVM IR (-O2):
%struct.tm = type { i32 }
@.str = [13 x i8] c"dbbucket.tmp\00"
@.str.1 = [2 x i8] c"r\00"
@errno = global i32 0
@.str.5 = [6 x i8] c"kopou\00"
@.str.7 = [17 x i8] c"dbid is good %d\0A\00"
@.str.8 = [16 x i8] c"dbv is good %d\0A\00"
@.str.9 = [33 x i8] c"Date: %a, %d %b %Y %H:%M:%S %Z\0D\0A\00"
@.str.11 = [11 x i8] c"count %zu\0A\00"
@str = [8 x i8] c"fp fail\00"
@str.12 = [20 x i8] c"it is kopou db file\00"
define void @read_db() {
%1 = alloca [5 x i8]
%2 = alloca i32
%3 = alloca i32
%4 = alloca i8*
%5 = alloca [128 x i8]
%6 = alloca i8*
%7 = tail call i32* @fopen(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%8 = icmp eq i32* %7, null
br i1 %8, label %9, label %12
9: ; preds = %0
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @str, i64 0, i64 0))
%11 = tail call i32 @exit(i32 1)
unreachable
12: ; preds = %0
%13 = getelementptr inbounds [5 x i8], [5 x i8]* %1, i64 0, i64 0
%14 = call i32 (i32*, ...) @bgs_read(i32* %7, i8* %13, i32 5)
%15 = call i32 @memcmp(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0), i8* %13, i32 5)
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %12
%18 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str.12, i64 0, i64 0))
br label %19
19: ; preds = %17, %12
%20 = bitcast i32* %2 to i8*
%21 = call i32 (i32*, ...) @bgs_read(i32* %7, i32* %2, i64 4)
%22 = load i32, i32* %2
%23 = icmp eq i32 %22, 1
br i1 %23, label %24, label %26
24: ; preds = %19
%25 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.7, i64 0, i64 0), i32 1)
br label %26
26: ; preds = %24, %19
%27 = bitcast i32* %3 to i8*
%28 = call i32 (i32*, ...) @bgs_read(i32* %7, i32* %3, i64 4)
%29 = load i32, i32* %3
%30 = icmp eq i32 %29, 1
br i1 %30, label %31, label %33
31: ; preds = %26
%32 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.8, i64 0, i64 0), i32 1)
br label %33
33: ; preds = %31, %26
%34 = bitcast i8** %4 to i8*
%35 = call i32 (i32*, ...) @bgs_read(i32* %7, i8** %4, i64 8)
%36 = load i8*, i8** %4
%37 = call i8* @be64toh(i8* %36)
store i8* %37, i8** %4
%38 = getelementptr inbounds [128 x i8], [128 x i8]* %5, i64 0, i64 0
%39 = call %struct.tm* @gmtime(i8** %4)
%40 = call i32 @strftime(i8* %38, i64 128, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.9, i64 0, i64 0), %struct.tm* %39)
%41 = call i32 @puts(i8* %38)
%42 = bitcast i8** %6 to i8*
%43 = call i32 (i32*, ...) @bgs_read(i32* %7, i8** %6, i64 8)
%44 = load i8*, i8** %6
%45 = call i8* @be64toh(i8* %44)
store i8* %45, i8** %6
%46 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.11, i64 0, i64 0), i8* %45)
ret void
}