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

/* Variables and functions */
int /*<<< orphan*/ LOCK_EX;
int /*<<< orphan*/ close(int);
int flock(int, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
char *read_file(char *, size_t *);
int snprintf(char *, int, char *, char const *, ...);
int read_db_value(const char *params_path, const char *key, char **value,
                  size_t *value_sz) {
  int lock_fd = -1;
  int result;
  char path[1024];
  result = snprintf(path, sizeof(path), "%s/.lock", params_path);
  if (result < 0) {
    goto cleanup;
  }
  lock_fd = open(path, 0);
  result = snprintf(path, sizeof(path), "%s/d/%s", params_path, key);
  if (result < 0) {
    goto cleanup;
  }
  result = flock(lock_fd, LOCK_EX);
  if (result < 0) {
    goto cleanup;
  }
  *value = read_file(path, value_sz);
  if (*value == NULL) {
    result = -22;
    goto cleanup;
  }
  if (value_sz != NULL) {
    *value_sz -= 1;
  }
  result = 0;
cleanup:
  if (lock_fd >= 0) {
    close(lock_fd);
  }
  return result;
}


x86 assembly (clang, -O0):
<read_db_value>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
movl   $0xffffffff,-0x24(%rbp)
lea    -0x430(%rbp),%rdi
mov    -0x8(%rbp),%rcx
mov    $0x400,%esi
lea    0xe77(%rip),%rdx
mov    $0x0,%al
call   1030 <snprintf@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jge    11a2 <read_db_value+0x52>
jmp    125e <read_db_value+0x10e>
lea    -0x430(%rbp),%rdi
xor    %esi,%esi
call   1060 <open@plt>
mov    %eax,-0x24(%rbp)
lea    -0x430(%rbp),%rdi
mov    -0x8(%rbp),%rcx
mov    -0x10(%rbp),%r8
mov    $0x400,%esi
lea    0xe3b(%rip),%rdx
mov    $0x0,%al
call   1030 <snprintf@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jge    11e7 <read_db_value+0x97>
jmp    125e <read_db_value+0x10e>
mov    -0x24(%rbp),%edi
mov    0x2df7(%rip),%rax
mov    (%rax),%esi
call   1070 <flock@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jge    120a <read_db_value+0xba>
jmp    125e <read_db_value+0x10e>
lea    -0x430(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1050 <read_file@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x18(%rbp),%rax
cmpq   $0x0,(%rax)
jne    123e <read_db_value+0xee>
movl   $0xffffffea,-0x28(%rbp)
jmp    125e <read_db_value+0x10e>
cmpq   $0x0,-0x20(%rbp)
je     1257 <read_db_value+0x107>
mov    -0x20(%rbp),%rax
mov    (%rax),%rcx
sub    $0x1,%rcx
mov    %rcx,(%rax)
movl   $0x0,-0x28(%rbp)
cmpl   $0x0,-0x24(%rbp)
jl     1270 <read_db_value+0x120>
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
mov    -0x28(%rbp),%eax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_db_value>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x408,%rsp
mov    %rcx,%r15
mov    %rdx,%r14
mov    %rsi,%r13
mov    %rdi,%rbp
lea    0xe8c(%rip),%rdx
mov    %rsp,%rdi
mov    $0x400,%esi
mov    %rbp,%rcx
xor    %eax,%eax
call   1030 <snprintf@plt>
test   %eax,%eax
js     11f4 <read_db_value+0xa4>
mov    %rsp,%rbx
mov    %rbx,%rdi
xor    %esi,%esi
call   1060 <open@plt>
mov    %eax,%r12d
lea    0xe68(%rip),%rdx
mov    %rbx,%rdi
mov    $0x400,%esi
mov    %rbp,%rcx
mov    %r13,%r8
xor    %eax,%eax
call   1030 <snprintf@plt>
test   %eax,%eax
js     11f8 <read_db_value+0xa8>
mov    0x2e27(%rip),%rax
mov    (%rax),%esi
mov    %r12d,%edi
call   1070 <flock@plt>
test   %eax,%eax
js     1201 <read_db_value+0xb1>
mov    %rsp,%rdi
mov    %r15,%rsi
call   1050 <read_file@plt>
mov    %rax,(%r14)
test   %rax,%rax
je     1224 <read_db_value+0xd4>
xor    %ebx,%ebx
test   %r15,%r15
je     1203 <read_db_value+0xb3>
addq   $0xffffffffffffffff,(%r15)
test   %r12d,%r12d
jns    1208 <read_db_value+0xb8>
jmp    1210 <read_db_value+0xc0>
mov    %eax,%ebx
jmp    1210 <read_db_value+0xc0>
mov    %eax,%ebx
test   %r12d,%r12d
jns    1208 <read_db_value+0xb8>
jmp    1210 <read_db_value+0xc0>
mov    %eax,%ebx
test   %r12d,%r12d
js     1210 <read_db_value+0xc0>
mov    %r12d,%edi
call   1040 <close@plt>
mov    %ebx,%eax
add    $0x408,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0xffffffea,%ebx
test   %r12d,%r12d
jns    1208 <read_db_value+0xb8>
jmp    1210 <read_db_value+0xc0>

LLVM IR (-O0):
@.str = [9 x i8] c"%s/.lock\00"
@.str.1 = [8 x i8] c"%s/d/%s\00"
@LOCK_EX = global i32 0
define i32 @read_db_value(i8* %0, i8* %1, i8** %2, i64* %3) {
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8**
%8 = alloca i64*
%9 = alloca i32
%10 = alloca i32
%11 = alloca [1024 x i8]
store i8* %0, i8** %5
store i8* %1, i8** %6
store i8** %2, i8*** %7
store i64* %3, i64** %8
store i32 -1, i32* %9
%12 = getelementptr inbounds [1024 x i8], [1024 x i8]* %11, i64 0, i64 0
%13 = load i8*, i8** %5
%14 = call i32 (i8*, i32, i8*, i8*, ...) @snprintf(i8* %12, i32 1024, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* %13)
store i32 %14, i32* %10
%15 = load i32, i32* %10
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %4
br label %52
18: ; preds = %4
%19 = getelementptr inbounds [1024 x i8], [1024 x i8]* %11, i64 0, i64 0
%20 = call i32 @open(i8* %19, i32 0)
store i32 %20, i32* %9
%21 = getelementptr inbounds [1024 x i8], [1024 x i8]* %11, i64 0, i64 0
%22 = load i8*, i8** %5
%23 = load i8*, i8** %6
%24 = call i32 (i8*, i32, i8*, i8*, ...) @snprintf(i8* %21, i32 1024, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8* %22, i8* %23)
store i32 %24, i32* %10
%25 = load i32, i32* %10
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %28
27: ; preds = %18
br label %52
28: ; preds = %18
%29 = load i32, i32* %9
%30 = load i32, i32* @LOCK_EX
%31 = call i32 @flock(i32 %29, i32 %30)
store i32 %31, i32* %10
%32 = load i32, i32* %10
%33 = icmp slt i32 %32, 0
br i1 %33, label %34, label %35
34: ; preds = %28
br label %52
35: ; preds = %28
%36 = getelementptr inbounds [1024 x i8], [1024 x i8]* %11, i64 0, i64 0
%37 = load i64*, i64** %8
%38 = call i8* @read_file(i8* %36, i64* %37)
%39 = load i8**, i8*** %7
store i8* %38, i8** %39
%40 = load i8**, i8*** %7
%41 = load i8*, i8** %40
%42 = icmp eq i8* %41, null
br i1 %42, label %43, label %44
43: ; preds = %35
store i32 -22, i32* %10
br label %52
44: ; preds = %35
%45 = load i64*, i64** %8
%46 = icmp ne i64* %45, null
br i1 %46, label %47, label %51
47: ; preds = %44
%48 = load i64*, i64** %8
%49 = load i64, i64* %48
%50 = sub i64 %49, 1
store i64 %50, i64* %48
br label %51
51: ; preds = %47, %44
store i32 0, i32* %10
br label %52
52: ; preds = %51, %43, %34, %27, %17
%53 = load i32, i32* %9
%54 = icmp sge i32 %53, 0
br i1 %54, label %55, label %58
55: ; preds = %52
%56 = load i32, i32* %9
%57 = call i32 @close(i32 %56)
br label %58
58: ; preds = %55, %52
%59 = load i32, i32* %10
ret i32 %59
}

LLVM IR (-O2):
@.str = [9 x i8] c"%s/.lock\00"
@.str.1 = [8 x i8] c"%s/d/%s\00"
@LOCK_EX = global i32 0
define i32 @read_db_value(i8* %0, i8* %1, i8** nocapture writeonly %2, i64* %3) {
%5 = alloca [1024 x i8]
%6 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%7 = call i32 (i8*, i32, i8*, i8*, ...) @snprintf(i8* %6, i32 1024, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* %0)
%8 = icmp slt i32 %7, 0
br i1 %8, label %30, label %9
9: ; preds = %4
%10 = call i32 @open(i8* %6, i32 0)
%11 = call i32 (i8*, i32, i8*, i8*, ...) @snprintf(i8* %6, i32 1024, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i8* %0, i8* %1)
%12 = icmp slt i32 %11, 0
br i1 %12, label %25, label %13
13: ; preds = %9
%14 = load i32, i32* @LOCK_EX
%15 = call i32 @flock(i32 %10, i32 %14)
%16 = icmp slt i32 %15, 0
br i1 %16, label %25, label %17
17: ; preds = %13
%18 = call i8* @read_file(i8* %6, i64* %3)
store i8* %18, i8** %2
%19 = icmp eq i8* %18, null
br i1 %19, label %25, label %20
20: ; preds = %17
%21 = icmp eq i64* %3, null
br i1 %21, label %25, label %22
22: ; preds = %20
%23 = load i64, i64* %3
%24 = add i64 %23, -1
store i64 %24, i64* %3
br label %25
25: ; preds = %20, %22, %17, %13, %9
%26 = phi i32 [ %11, %9 ], [ %15, %13 ], [ -22, %17 ], [ 0, %22 ], [ 0, %20 ]
%27 = icmp sgt i32 %10, -1
br i1 %27, label %28, label %30
28: ; preds = %25
%29 = call i32 @close(i32 %10)
br label %30
30: ; preds = %4, %28, %25
%31 = phi i32 [ %26, %28 ], [ %26, %25 ], [ %7, %4 ]
ret i32 %31
}