#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 uint8_t;

/* Variables and functions */
scalar_t__ ENOENT;
int /*<<< orphan*/ LOCK_EX;
int /*<<< orphan*/ LOCK_UN;
int O_CREAT;
int O_RDWR;
int O_SYNC;
int S_IRWXG;
int S_IRWXU;
int /*<<< orphan*/ close(int);
scalar_t__ db_init(int);
int /*<<< orphan*/ db_store(int, char *, int, int *);
scalar_t__ errno;
scalar_t__ flock(int, int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int open(char *, int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
int main(void) {
  int dbfd = open("test.per", O_RDWR | O_SYNC, S_IRWXU | S_IRWXG);
  if (dbfd < 0) {
    if (errno == ENOENT) {
      dbfd = open("test.per", O_RDWR | O_SYNC | O_CREAT, S_IRWXU | S_IRWXG);
      if (dbfd < 0) {
        perror("persist: open");
        return (1);
      } else {
        if (db_init(dbfd)) {
          fprintf(stderr, "persist: failed to initialise db\n");
          return (1);
        }
      }
    } else {
      perror("persist: open");
      return (1);
    }
  }
  if (flock(dbfd, LOCK_EX)) {
    perror("persist: flock");
    close(dbfd);
    return (1);
  }
  uint8_t ans = 42;
  if (!db_store(dbfd, "Answer", 0x80, &ans)) {
    fprintf(stderr, "persist: db_store failed\n");
    return (1);
  }
  flock(dbfd, LOCK_UN);
  close(dbfd);
  return (0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e52(%rip),%rax
mov    (%rax),%esi
mov    0x2e41(%rip),%rax
or     (%rax),%esi
mov    0x2e48(%rip),%rax
mov    (%rax),%edx
mov    0x2e1f(%rip),%rax
or     (%rax),%edx
lea    0xe56(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    128e <main+0x11e>
mov    0x2de5(%rip),%rax
mov    (%rax),%rax
mov    0x2dc3(%rip),%rcx
cmp    (%rcx),%rax
jne    1271 <main+0x101>
mov    0x2dfb(%rip),%rax
mov    (%rax),%esi
mov    0x2dea(%rip),%rax
or     (%rax),%esi
mov    0x2e09(%rip),%rax
or     (%rax),%esi
mov    0x2de8(%rip),%rax
mov    (%rax),%edx
mov    0x2dbf(%rip),%rax
or     (%rax),%edx
lea    0xdf6(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    1234 <main+0xc4>
lea    0xde6(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1332 <main+0x1c2>
mov    -0x8(%rbp),%edi
call   1040 <db_init@plt>
cmp    $0x0,%rax
je     1267 <main+0xf7>
mov    0x2d63(%rip),%rax
mov    (%rax),%edi
lea    0xdc1(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1332 <main+0x1c2>
jmp    126c <main+0xfc>
jmp    1289 <main+0x119>
lea    0xd91(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1332 <main+0x1c2>
jmp    128e <main+0x11e>
mov    -0x8(%rbp),%edi
mov    0x2d30(%rip),%rax
mov    (%rax),%esi
call   1090 <flock@plt>
cmp    $0x0,%rax
je     12c9 <main+0x159>
lea    0xd89(%rip),%rdi
call   1080 <perror@plt>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
movl   $0x1,-0x4(%rbp)
jmp    1332 <main+0x1c2>
movl   $0x2a,-0xc(%rbp)
mov    -0x8(%rbp),%edi
lea    0xd6e(%rip),%rsi
mov    $0x80,%edx
lea    -0xc(%rbp),%rcx
call   1030 <db_store@plt>
cmp    $0x0,%eax
jne    1312 <main+0x1a2>
mov    0x2cb8(%rip),%rax
mov    (%rax),%edi
lea    0xd4e(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1332 <main+0x1c2>
mov    -0x8(%rbp),%edi
mov    0x2c84(%rip),%rax
mov    (%rax),%esi
call   1090 <flock@plt>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    0x2e54(%rip),%r15
mov    0x2e45(%rip),%r13
mov    0x0(%r13),%esi
or     (%r15),%esi
mov    0x2e47(%rip),%r14
mov    0x2e20(%rip),%r12
mov    (%r12),%edx
or     (%r14),%edx
lea    0xe52(%rip),%rdi
call   1070 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
js     1215 <main+0xa5>
mov    0x2e08(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1090 <flock@plt>
test   %rax,%rax
jne    12a2 <main+0x132>
movl   $0x2a,0xc(%rsp)
lea    0xe67(%rip),%rsi
lea    0xc(%rsp),%rcx
mov    %ebx,%edi
mov    $0x80,%edx
call   1030 <db_store@plt>
test   %eax,%eax
je     127a <main+0x10a>
mov    0x2d9f(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1090 <flock@plt>
mov    %ebx,%edi
call   1050 <close@plt>
xor    %eax,%eax
jmp    1294 <main+0x124>
mov    0x2d8c(%rip),%rax
mov    (%rax),%rax
mov    0x2d6a(%rip),%rcx
cmp    (%rcx),%rax
jne    12b7 <main+0x147>
mov    0x0(%r13),%esi
or     (%r15),%esi
mov    0x2dbb(%rip),%rax
or     (%rax),%esi
mov    (%r12),%edx
or     (%r14),%edx
lea    0xdb3(%rip),%rdi
call   1070 <open@plt>
test   %eax,%eax
js     12b7 <main+0x147>
mov    %eax,%ebx
mov    %eax,%edi
call   1040 <db_init@plt>
test   %rax,%rax
je     11b9 <main+0x49>
mov    0x2d41(%rip),%rax
mov    (%rax),%edi
lea    0xd9f(%rip),%rsi
jmp    128a <main+0x11a>
mov    0x2d2f(%rip),%rax
mov    (%rax),%edi
lea    0xdc5(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret
lea    0xd90(%rip),%rdi
call   1080 <perror@plt>
mov    %ebx,%edi
call   1050 <close@plt>
jmp    128f <main+0x11f>
lea    0xd4b(%rip),%rdi
call   1080 <perror@plt>
jmp    128f <main+0x11f>

LLVM IR (-O0):
@.str = [9 x i8] c"test.per\00"
@O_RDWR = global i32 0
@O_SYNC = global i32 0
@S_IRWXU = global i32 0
@S_IRWXG = global i32 0
@errno = global i64 0
@ENOENT = global i64 0
@O_CREAT = global i32 0
@.str.1 = [14 x i8] c"persist: open\00"
@stderr = global i32 0
@.str.2 = [34 x i8] c"persist: failed to initialise db\0A\00"
@LOCK_EX = global i32 0
@.str.3 = [15 x i8] c"persist: flock\00"
@.str.4 = [7 x i8] c"Answer\00"
@.str.5 = [26 x i8] c"persist: db_store failed\0A\00"
@LOCK_UN = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
store i32 0, i32* %1
%4 = load i32, i32* @O_RDWR
%5 = load i32, i32* @O_SYNC
%6 = or i32 %4, %5
%7 = load i32, i32* @S_IRWXU
%8 = load i32, i32* @S_IRWXG
%9 = or i32 %7, %8
%10 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %6, i32 %9)
store i32 %10, i32* %2
%11 = load i32, i32* %2
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %43
13: ; preds = %0
%14 = load i64, i64* @errno
%15 = load i64, i64* @ENOENT
%16 = icmp eq i64 %14, %15
br i1 %16, label %17, label %40
17: ; preds = %13
%18 = load i32, i32* @O_RDWR
%19 = load i32, i32* @O_SYNC
%20 = or i32 %18, %19
%21 = load i32, i32* @O_CREAT
%22 = or i32 %20, %21
%23 = load i32, i32* @S_IRWXU
%24 = load i32, i32* @S_IRWXG
%25 = or i32 %23, %24
%26 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %22, i32 %25)
store i32 %26, i32* %2
%27 = load i32, i32* %2
%28 = icmp slt i32 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %17
%30 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %1
br label %65
31: ; preds = %17
%32 = load i32, i32* %2
%33 = call i64 @db_init(i32 %32)
%34 = icmp ne i64 %33, 0
br i1 %34, label %35, label %38
35: ; preds = %31
%36 = load i32, i32* @stderr
%37 = call i32 @fprintf(i32 %36, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %1
br label %65
38: ; preds = %31
br label %39
39: ; preds = %38
br label %42
40: ; preds = %13
%41 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %1
br label %65
42: ; preds = %39
br label %43
43: ; preds = %42, %0
%44 = load i32, i32* %2
%45 = load i32, i32* @LOCK_EX
%46 = call i64 @flock(i32 %44, i32 %45)
%47 = icmp ne i64 %46, 0
br i1 %47, label %48, label %52
48: ; preds = %43
%49 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
%50 = load i32, i32* %2
%51 = call i32 @close(i32 %50)
store i32 1, i32* %1
br label %65
52: ; preds = %43
store i32 42, i32* %3
%53 = load i32, i32* %2
%54 = call i32 @db_store(i32 %53, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0), i32 128, i32* %3)
%55 = icmp ne i32 %54, 0
br i1 %55, label %59, label %56
56: ; preds = %52
%57 = load i32, i32* @stderr
%58 = call i32 @fprintf(i32 %57, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.5, i64 0, i64 0))
store i32 1, i32* %1
br label %65
59: ; preds = %52
%60 = load i32, i32* %2
%61 = load i32, i32* @LOCK_UN
%62 = call i64 @flock(i32 %60, i32 %61)
%63 = load i32, i32* %2
%64 = call i32 @close(i32 %63)
store i32 0, i32* %1
br label %65
65: ; preds = %59, %56, %48, %40, %35, %29
%66 = load i32, i32* %1
ret i32 %66
}

LLVM IR (-O2):
@.str = [9 x i8] c"test.per\00"
@O_RDWR = global i32 0
@O_SYNC = global i32 0
@S_IRWXU = global i32 0
@S_IRWXG = global i32 0
@errno = global i64 0
@ENOENT = global i64 0
@O_CREAT = global i32 0
@.str.1 = [14 x i8] c"persist: open\00"
@stderr = global i32 0
@.str.2 = [34 x i8] c"persist: failed to initialise db\0A\00"
@LOCK_EX = global i32 0
@.str.3 = [15 x i8] c"persist: flock\00"
@.str.4 = [7 x i8] c"Answer\00"
@.str.5 = [26 x i8] c"persist: db_store failed\0A\00"
@LOCK_UN = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = load i32, i32* @O_RDWR
%3 = load i32, i32* @O_SYNC
%4 = or i32 %3, %2
%5 = load i32, i32* @S_IRWXU
%6 = load i32, i32* @S_IRWXG
%7 = or i32 %6, %5
%8 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %4, i32 %7)
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %35
10: ; preds = %0
%11 = load i64, i64* @errno
%12 = load i64, i64* @ENOENT
%13 = icmp eq i64 %11, %12
br i1 %13, label %14, label %33
14: ; preds = %10
%15 = load i32, i32* @O_RDWR
%16 = load i32, i32* @O_SYNC
%17 = or i32 %16, %15
%18 = load i32, i32* @O_CREAT
%19 = or i32 %17, %18
%20 = load i32, i32* @S_IRWXU
%21 = load i32, i32* @S_IRWXG
%22 = or i32 %21, %20
%23 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %19, i32 %22)
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %14
%26 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
br label %56
27: ; preds = %14
%28 = tail call i64 @db_init(i32 %23)
%29 = icmp eq i64 %28, 0
br i1 %29, label %35, label %30
30: ; preds = %27
%31 = load i32, i32* @stderr
%32 = tail call i32 @fprintf(i32 %31, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0))
br label %56
33: ; preds = %10
%34 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
br label %56
35: ; preds = %27, %0
%36 = phi i32 [ %23, %27 ], [ %8, %0 ]
%37 = load i32, i32* @LOCK_EX
%38 = tail call i64 @flock(i32 %36, i32 %37)
%39 = icmp eq i64 %38, 0
br i1 %39, label %43, label %40
40: ; preds = %35
%41 = tail call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
%42 = tail call i32 @close(i32 %36)
br label %56
43: ; preds = %35
%44 = bitcast i32* %1 to i8*
store i32 42, i32* %1
%45 = call i32 @db_store(i32 %36, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0), i32 128, i32* %1)
%46 = icmp eq i32 %45, 0
br i1 %46, label %47, label %50
47: ; preds = %43
%48 = load i32, i32* @stderr
%49 = call i32 @fprintf(i32 %48, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.5, i64 0, i64 0))
br label %54
50: ; preds = %43
%51 = load i32, i32* @LOCK_UN
%52 = call i64 @flock(i32 %36, i32 %51)
%53 = call i32 @close(i32 %36)
br label %54
54: ; preds = %50, %47
%55 = phi i32 [ 0, %50 ], [ 1, %47 ]
br label %56
56: ; preds = %54, %40, %33, %30, %25
%57 = phi i32 [ 1, %25 ], [ 1, %30 ], [ 1, %40 ], [ %55, %54 ], [ 1, %33 ]
ret i32 %57
}