#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 */

/* Variables and functions */
int /*<<< orphan*/ LOCK_EX;
int /*<<< orphan*/ LOCK_UN;
int /*<<< orphan*/ SEEK_CUR;
int /*<<< orphan*/ close(int);
scalar_t__ flock(int, int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
scalar_t__ write32(int, int, int);
scalar_t__ writecons(int, int, int, int);
int db_init(int dbfd) {
  if (flock(dbfd, LOCK_EX)) {
    perror("persist: db_init: flock");
    close(dbfd);
    return (1);
  }
  if (write32(dbfd, 0, 8)) {
    close(dbfd);
    flock(dbfd, LOCK_UN);
    return (1);
  }
  if (write32(dbfd, 4, 0)) {
    close(dbfd);
    flock(dbfd, LOCK_UN);
    return (1);
  }
  if (writecons(dbfd, 8, 17, 0)) {
    close(dbfd);
    flock(dbfd, LOCK_UN);
    return (1);
  }
  if (writecons(dbfd, 17, 26, -26)) {
    close(dbfd);
    flock(dbfd, LOCK_UN);
    return (1);
  }
  if (lseek(dbfd, 0, SEEK_CUR) != 26) {
    fprintf(stderr, "persist: db_init: offset not 26 after writing, something "
                    "bad happened\n");
    flock(dbfd, LOCK_UN);
    return (1);
  }
  flock(dbfd, LOCK_UN);
  return (0);
}


x86 assembly (clang, -O0):
<db_init>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e63(%rip),%rax
mov    (%rax),%esi
call   1080 <flock@plt>
cmp    $0x0,%rax
je     11b6 <db_init+0x46>
lea    0xe63(%rip),%rdi
call   1070 <perror@plt>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
movl   $0x1,-0x4(%rbp)
jmp    1321 <db_init+0x1b1>
mov    -0x8(%rbp),%edi
xor    %esi,%esi
mov    $0x8,%edx
call   1030 <write32@plt>
cmp    $0x0,%rax
je     11f4 <db_init+0x84>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
mov    -0x8(%rbp),%edi
mov    0x2de7(%rip),%rax
mov    (%rax),%esi
call   1080 <flock@plt>
movl   $0x1,-0x4(%rbp)
jmp    1321 <db_init+0x1b1>
mov    -0x8(%rbp),%edi
mov    $0x4,%esi
xor    %edx,%edx
call   1030 <write32@plt>
cmp    $0x0,%rax
je     1232 <db_init+0xc2>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
mov    -0x8(%rbp),%edi
mov    0x2da9(%rip),%rax
mov    (%rax),%esi
call   1080 <flock@plt>
movl   $0x1,-0x4(%rbp)
jmp    1321 <db_init+0x1b1>
mov    -0x8(%rbp),%edi
mov    $0x8,%esi
mov    $0x11,%edx
xor    %ecx,%ecx
call   1090 <writecons@plt>
cmp    $0x0,%rax
je     1275 <db_init+0x105>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
mov    -0x8(%rbp),%edi
mov    0x2d66(%rip),%rax
mov    (%rax),%esi
call   1080 <flock@plt>
movl   $0x1,-0x4(%rbp)
jmp    1321 <db_init+0x1b1>
mov    -0x8(%rbp),%edi
mov    $0x11,%esi
mov    $0x1a,%edx
mov    $0xffffffe6,%ecx
call   1090 <writecons@plt>
cmp    $0x0,%rax
je     12bb <db_init+0x14b>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
mov    -0x8(%rbp),%edi
mov    0x2d20(%rip),%rax
mov    (%rax),%esi
call   1080 <flock@plt>
movl   $0x1,-0x4(%rbp)
jmp    1321 <db_init+0x1b1>
mov    -0x8(%rbp),%edi
mov    0x2d1b(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1040 <lseek@plt>
cmp    $0x1a,%eax
je     1309 <db_init+0x199>
mov    0x2cf2(%rip),%rax
mov    (%rax),%edi
lea    0xd31(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x8(%rbp),%edi
mov    0x2cd2(%rip),%rax
mov    (%rax),%esi
call   1080 <flock@plt>
movl   $0x1,-0x4(%rbp)
jmp    1321 <db_init+0x1b1>
mov    -0x8(%rbp),%edi
mov    0x2cb5(%rip),%rax
mov    (%rax),%esi
call   1080 <flock@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<db_init>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%ebx
mov    0x2e6c(%rip),%rax
mov    (%rax),%esi
call   1080 <flock@plt>
test   %rax,%rax
jne    1206 <db_init+0x96>
mov    %ebx,%edi
xor    %esi,%esi
mov    $0x8,%edx
call   1030 <write32@plt>
test   %rax,%rax
jne    11e1 <db_init+0x71>
mov    %ebx,%edi
mov    $0x4,%esi
xor    %edx,%edx
call   1030 <write32@plt>
test   %rax,%rax
jne    11e1 <db_init+0x71>
mov    %ebx,%edi
mov    $0x8,%esi
mov    $0x11,%edx
xor    %ecx,%ecx
call   1090 <writecons@plt>
test   %rax,%rax
jne    11e1 <db_init+0x71>
mov    %ebx,%edi
mov    $0x11,%esi
mov    $0x1a,%edx
mov    $0xffffffe6,%ecx
call   1090 <writecons@plt>
test   %rax,%rax
je     121b <db_init+0xab>
mov    %ebx,%edi
call   1050 <close@plt>
mov    0x2dd9(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1080 <flock@plt>
mov    $0x1,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xdf3(%rip),%rdi
call   1070 <perror@plt>
mov    %ebx,%edi
call   1050 <close@plt>
jmp    11f8 <db_init+0x88>
mov    0x2dbe(%rip),%rax
mov    (%rax),%edx
xor    %ebp,%ebp
mov    %ebx,%edi
xor    %esi,%esi
call   1040 <lseek@plt>
cmp    $0x1a,%eax
jne    1246 <db_init+0xd6>
mov    0x2d8d(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1080 <flock@plt>
jmp    11fd <db_init+0x8d>
mov    0x2d83(%rip),%rax
mov    (%rax),%edi
lea    0xdc2(%rip),%rsi
call   1060 <fprintf@plt>
jmp    11e8 <db_init+0x78>

LLVM IR (-O0):
@LOCK_EX = global i32 0
@.str = [24 x i8] c"persist: db_init: flock\00"
@LOCK_UN = global i32 0
@SEEK_CUR = global i32 0
@stderr = global i32 0
@.str.1 = [71 x i8] c"persist: db_init: offset not 26 after writing, something bad happened\0A\00"
define i32 @db_init(i32 %0) {
%2 = alloca i32
%3 = alloca i32
store i32 %0, i32* %3
%4 = load i32, i32* %3
%5 = load i32, i32* @LOCK_EX
%6 = call i64 @flock(i32 %4, i32 %5)
%7 = icmp ne i64 %6, 0
br i1 %7, label %8, label %12
8: ; preds = %1
%9 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
%10 = load i32, i32* %3
%11 = call i32 @close(i32 %10)
store i32 1, i32* %2
br label %67
12: ; preds = %1
%13 = load i32, i32* %3
%14 = call i64 @write32(i32 %13, i32 0, i32 8)
%15 = icmp ne i64 %14, 0
br i1 %15, label %16, label %22
16: ; preds = %12
%17 = load i32, i32* %3
%18 = call i32 @close(i32 %17)
%19 = load i32, i32* %3
%20 = load i32, i32* @LOCK_UN
%21 = call i64 @flock(i32 %19, i32 %20)
store i32 1, i32* %2
br label %67
22: ; preds = %12
%23 = load i32, i32* %3
%24 = call i64 @write32(i32 %23, i32 4, i32 0)
%25 = icmp ne i64 %24, 0
br i1 %25, label %26, label %32
26: ; preds = %22
%27 = load i32, i32* %3
%28 = call i32 @close(i32 %27)
%29 = load i32, i32* %3
%30 = load i32, i32* @LOCK_UN
%31 = call i64 @flock(i32 %29, i32 %30)
store i32 1, i32* %2
br label %67
32: ; preds = %22
%33 = load i32, i32* %3
%34 = call i64 @writecons(i32 %33, i32 8, i32 17, i32 0)
%35 = icmp ne i64 %34, 0
br i1 %35, label %36, label %42
36: ; preds = %32
%37 = load i32, i32* %3
%38 = call i32 @close(i32 %37)
%39 = load i32, i32* %3
%40 = load i32, i32* @LOCK_UN
%41 = call i64 @flock(i32 %39, i32 %40)
store i32 1, i32* %2
br label %67
42: ; preds = %32
%43 = load i32, i32* %3
%44 = call i64 @writecons(i32 %43, i32 17, i32 26, i32 -26)
%45 = icmp ne i64 %44, 0
br i1 %45, label %46, label %52
46: ; preds = %42
%47 = load i32, i32* %3
%48 = call i32 @close(i32 %47)
%49 = load i32, i32* %3
%50 = load i32, i32* @LOCK_UN
%51 = call i64 @flock(i32 %49, i32 %50)
store i32 1, i32* %2
br label %67
52: ; preds = %42
%53 = load i32, i32* %3
%54 = load i32, i32* @SEEK_CUR
%55 = call i32 @lseek(i32 %53, i32 0, i32 %54)
%56 = icmp ne i32 %55, 26
br i1 %56, label %57, label %63
57: ; preds = %52
%58 = load i32, i32* @stderr
%59 = call i32 @fprintf(i32 %58, i8* getelementptr inbounds ([71 x i8], [71 x i8]* @.str.1, i64 0, i64 0))
%60 = load i32, i32* %3
%61 = load i32, i32* @LOCK_UN
%62 = call i64 @flock(i32 %60, i32 %61)
store i32 1, i32* %2
br label %67
63: ; preds = %52
%64 = load i32, i32* %3
%65 = load i32, i32* @LOCK_UN
%66 = call i64 @flock(i32 %64, i32 %65)
store i32 0, i32* %2
br label %67
67: ; preds = %63, %57, %46, %36, %26, %16, %8
%68 = load i32, i32* %2
ret i32 %68
}

LLVM IR (-O2):
@LOCK_EX = global i32 0
@.str = [24 x i8] c"persist: db_init: flock\00"
@LOCK_UN = global i32 0
@SEEK_CUR = global i32 0
@stderr = global i32 0
@.str.1 = [71 x i8] c"persist: db_init: offset not 26 after writing, something bad happened\0A\00"
define i32 @db_init(i32 %0) {
%2 = load i32, i32* @LOCK_EX
%3 = tail call i64 @flock(i32 %0, i32 %2)
%4 = icmp eq i64 %3, 0
br i1 %4, label %8, label %5
5: ; preds = %1
%6 = tail call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
%7 = tail call i32 @close(i32 %0)
br label %48
8: ; preds = %1
%9 = tail call i64 @write32(i32 %0, i32 0, i32 8)
%10 = icmp eq i64 %9, 0
br i1 %10, label %15, label %11
11: ; preds = %8
%12 = tail call i32 @close(i32 %0)
%13 = load i32, i32* @LOCK_UN
%14 = tail call i64 @flock(i32 %0, i32 %13)
br label %48
15: ; preds = %8
%16 = tail call i64 @write32(i32 %0, i32 4, i32 0)
%17 = icmp eq i64 %16, 0
br i1 %17, label %22, label %18
18: ; preds = %15
%19 = tail call i32 @close(i32 %0)
%20 = load i32, i32* @LOCK_UN
%21 = tail call i64 @flock(i32 %0, i32 %20)
br label %48
22: ; preds = %15
%23 = tail call i64 @writecons(i32 %0, i32 8, i32 17, i32 0)
%24 = icmp eq i64 %23, 0
br i1 %24, label %29, label %25
25: ; preds = %22
%26 = tail call i32 @close(i32 %0)
%27 = load i32, i32* @LOCK_UN
%28 = tail call i64 @flock(i32 %0, i32 %27)
br label %48
29: ; preds = %22
%30 = tail call i64 @writecons(i32 %0, i32 17, i32 26, i32 -26)
%31 = icmp eq i64 %30, 0
br i1 %31, label %36, label %32
32: ; preds = %29
%33 = tail call i32 @close(i32 %0)
%34 = load i32, i32* @LOCK_UN
%35 = tail call i64 @flock(i32 %0, i32 %34)
br label %48
36: ; preds = %29
%37 = load i32, i32* @SEEK_CUR
%38 = tail call i32 @lseek(i32 %0, i32 0, i32 %37)
%39 = icmp eq i32 %38, 26
br i1 %39, label %45, label %40
40: ; preds = %36
%41 = load i32, i32* @stderr
%42 = tail call i32 @fprintf(i32 %41, i8* getelementptr inbounds ([71 x i8], [71 x i8]* @.str.1, i64 0, i64 0))
%43 = load i32, i32* @LOCK_UN
%44 = tail call i64 @flock(i32 %0, i32 %43)
br label %48
45: ; preds = %36
%46 = load i32, i32* @LOCK_UN
%47 = tail call i64 @flock(i32 %0, i32 %46)
br label %48
48: ; preds = %45, %40, %32, %25, %18, %11, %5
%49 = phi i32 [ 1, %5 ], [ 1, %11 ], [ 1, %18 ], [ 1, %25 ], [ 1, %32 ], [ 1, %40 ], [ 0, %45 ]
ret i32 %49
}