#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 int8_t;
typedef char int64_t;
typedef int /*<<< orphan*/ dbv;
typedef int /*<<< orphan*/ dbid;
typedef int /*<<< orphan*/ cn;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int bgs_write(int /*<<< orphan*/ *, char *, int);
char htobe64(char);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ writedata(int /*<<< orphan*/ *);
void write_db(FILE *fp) {
  int r;
  r = bgs_write(fp, "kopou", 5);
  if (r == 0)
    printf("error writing kopou\n");
  int8_t dbid = 1;
  r = bgs_write(fp, &dbid, sizeof(dbid));
  if (r == 0)
    printf("error writing kopou dbid\n");
  int8_t dbv = 1;
  r = bgs_write(fp, &dbv, sizeof(dbv));
  if (r == 0)
    printf("error writing kopou db version\n");
  int64_t cn = 123456789;
  cn = htobe64(cn);
  r = bgs_write(fp, &cn, sizeof(cn));
  if (r == 0)
    printf("error writing kopou counts\n");
  writedata(fp);
}


x86 assembly (clang, -O0):
<write_db>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xea9(%rip),%rsi
mov    $0x5,%edx
call   1030 <bgs_write@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jne    117a <write_db+0x3a>
lea    0xe91(%rip),%rdi
call   1050 <printf@plt>
movb   $0x1,-0xd(%rbp)
mov    -0x8(%rbp),%rdi
lea    -0xd(%rbp),%rsi
mov    $0x1,%edx
call   1030 <bgs_write@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jne    11a9 <write_db+0x69>
lea    0xe77(%rip),%rdi
call   1050 <printf@plt>
movb   $0x1,-0xe(%rbp)
mov    -0x8(%rbp),%rdi
lea    -0xe(%rbp),%rsi
mov    $0x1,%edx
call   1030 <bgs_write@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jne    11d8 <write_db+0x98>
lea    0xe62(%rip),%rdi
call   1050 <printf@plt>
movb   $0x15,-0xf(%rbp)
movsbl -0xf(%rbp),%edi
call   1060 <htobe64@plt>
mov    %al,-0xf(%rbp)
mov    -0x8(%rbp),%rdi
lea    -0xf(%rbp),%rsi
mov    $0x1,%edx
call   1030 <bgs_write@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jne    1213 <write_db+0xd3>
lea    0xe47(%rip),%rdi
call   1050 <printf@plt>
mov    -0x8(%rbp),%rdi
call   1040 <writedata@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_db>:
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
lea    0xf12(%rip),%rsi
mov    $0x5,%edx
call   1040 <bgs_write@plt>
test   %eax,%eax
jne    1169 <write_db+0x29>
lea    0xeef(%rip),%rdi
call   1030 <puts@plt>
movb   $0x1,0xf(%rsp)
lea    0xf(%rsp),%rsi
mov    %rbx,%rdi
mov    $0x1,%edx
call   1040 <bgs_write@plt>
test   %eax,%eax
jne    1190 <write_db+0x50>
lea    0xeaf(%rip),%rdi
call   1030 <puts@plt>
movb   $0x1,0xe(%rsp)
lea    0xe(%rsp),%rsi
mov    %rbx,%rdi
mov    $0x1,%edx
call   1040 <bgs_write@plt>
test   %eax,%eax
jne    11b7 <write_db+0x77>
lea    0xe69(%rip),%rdi
call   1030 <puts@plt>
mov    $0x15,%edi
call   1060 <htobe64@plt>
mov    %al,0xd(%rsp)
lea    0xd(%rsp),%rsi
mov    %rbx,%rdi
mov    $0x1,%edx
call   1040 <bgs_write@plt>
test   %eax,%eax
jne    11e7 <write_db+0xa7>
lea    0xe1e(%rip),%rdi
call   1030 <puts@plt>
mov    %rbx,%rdi
call   1050 <writedata@plt>
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [6 x i8] c"kopou\00"
@.str.1 = [21 x i8] c"error writing kopou\0A\00"
@.str.2 = [26 x i8] c"error writing kopou dbid\0A\00"
@.str.3 = [32 x i8] c"error writing kopou db version\0A\00"
@.str.4 = [28 x i8] c"error writing kopou counts\0A\00"
define void @write_db(i32* %0) {
%2 = alloca i32*
%3 = alloca i32
%4 = alloca i8
%5 = alloca i8
%6 = alloca i8
store i32* %0, i32** %2
%7 = load i32*, i32** %2
%8 = call i32 @bgs_write(i32* %7, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 5)
store i32 %8, i32* %3
%9 = load i32, i32* %3
%10 = icmp eq i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %1
%12 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
br label %13
13: ; preds = %11, %1
store i8 1, i8* %4
%14 = load i32*, i32** %2
%15 = call i32 @bgs_write(i32* %14, i8* %4, i32 1)
store i32 %15, i32* %3
%16 = load i32, i32* %3
%17 = icmp eq i32 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %13
%19 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
br label %20
20: ; preds = %18, %13
store i8 1, i8* %5
%21 = load i32*, i32** %2
%22 = call i32 @bgs_write(i32* %21, i8* %5, i32 1)
store i32 %22, i32* %3
%23 = load i32, i32* %3
%24 = icmp eq i32 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %20
%26 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0))
br label %27
27: ; preds = %25, %20
store i8 21, i8* %6
%28 = load i8, i8* %6
%29 = call signext i8 @htobe64(i8 signext %28)
store i8 %29, i8* %6
%30 = load i32*, i32** %2
%31 = call i32 @bgs_write(i32* %30, i8* %6, i32 1)
store i32 %31, i32* %3
%32 = load i32, i32* %3
%33 = icmp eq i32 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %27
%35 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0))
br label %36
36: ; preds = %34, %27
%37 = load i32*, i32** %2
%38 = call i32 @writedata(i32* %37)
ret void
}

LLVM IR (-O2):
@.str = [6 x i8] c"kopou\00"
@str = [27 x i8] c"error writing kopou counts\00"
@str.5 = [31 x i8] c"error writing kopou db version\00"
@str.6 = [25 x i8] c"error writing kopou dbid\00"
@str.7 = [20 x i8] c"error writing kopou\00"
define void @write_db(i32* %0) {
%2 = alloca i8
%3 = alloca i8
%4 = alloca i8
%5 = tail call i32 @bgs_write(i32* %0, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 5)
%6 = icmp eq i32 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %1
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str.7, i64 0, i64 0))
br label %9
9: ; preds = %7, %1
store i8 1, i8* %2
%10 = call i32 @bgs_write(i32* %0, i8* %2, i32 1)
%11 = icmp eq i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %9
%13 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str.6, i64 0, i64 0))
br label %14
14: ; preds = %12, %9
store i8 1, i8* %3
%15 = call i32 @bgs_write(i32* %0, i8* %3, i32 1)
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %14
%18 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @str.5, i64 0, i64 0))
br label %19
19: ; preds = %17, %14
%20 = call signext i8 @htobe64(i8 signext 21)
store i8 %20, i8* %4
%21 = call i32 @bgs_write(i32* %0, i8* %4, i32 1)
%22 = icmp eq i32 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %19
%24 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str, i64 0, i64 0))
br label %25
25: ; preds = %23, %19
%26 = call i32 @writedata(i32* %0)
ret void
}