#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*/ tmp_path;
typedef size_t ssize_t;
typedef int /*<<< orphan*/ path;

/* Variables and functions */
int /*<<< orphan*/ LOCK_EX;
int /*<<< orphan*/ close(int);
int flock(int, int /*<<< orphan*/);
int mkstemp(char *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ remove(char *);
int rename(char *, char *);
int snprintf(char *, int, char *, char const *, ...);
size_t write(int, char const *, size_t);
int write_db_value(const char *params_path, const char *key, const char *value,
                   size_t value_size) {
  int lock_fd = -1;
  int tmp_fd = -1;
  int result;
  char tmp_path[1024];
  char path[1024];
  result =
      snprintf(tmp_path, sizeof(tmp_path), "%s/.tmp_value_XXXXXX", params_path);
  if (result < 0) {
    goto cleanup;
  }
  tmp_fd = mkstemp(tmp_path);
  const ssize_t bytes_written = write(tmp_fd, value, value_size);
  if (bytes_written != value_size) {
    result = -20;
    goto cleanup;
  }
  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;
  }
  result = rename(tmp_path, path);
cleanup:
  if (lock_fd >= 0) {
    close(lock_fd);
  }
  if (tmp_fd >= 0) {
    if (result < 0) {
      remove(tmp_path);
    }
    close(tmp_fd);
  }
  return result;
}


x86 assembly (clang, -O0):
<write_db_value>:
push   %rbp
mov    %rsp,%rbp
sub    $0x840,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
movl   $0xffffffff,-0x24(%rbp)
movl   $0xffffffff,-0x28(%rbp)
lea    -0x430(%rbp),%rdi
mov    -0x8(%rbp),%rcx
mov    $0x400,%esi
lea    0xe40(%rip),%rdx
mov    $0x0,%al
call   1050 <snprintf@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    11d9 <write_db_value+0x59>
jmp    12ca <write_db_value+0x14a>
lea    -0x430(%rbp),%rdi
call   1070 <mkstemp@plt>
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1040 <write@plt>
mov    %rax,-0x838(%rbp)
mov    -0x838(%rbp),%rax
cmp    -0x20(%rbp),%rax
je     121c <write_db_value+0x9c>
movl   $0xffffffec,-0x2c(%rbp)
jmp    12ca <write_db_value+0x14a>
lea    -0x830(%rbp),%rdi
mov    -0x8(%rbp),%rcx
mov    $0x400,%esi
lea    0xde2(%rip),%rdx
mov    $0x0,%al
call   1050 <snprintf@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    124c <write_db_value+0xcc>
jmp    12ca <write_db_value+0x14a>
lea    -0x830(%rbp),%rdi
xor    %esi,%esi
call   1080 <open@plt>
mov    %eax,-0x24(%rbp)
lea    -0x830(%rbp),%rdi
mov    -0x8(%rbp),%rcx
mov    -0x10(%rbp),%r8
mov    $0x400,%esi
lea    0xda6(%rip),%rdx
mov    $0x0,%al
call   1050 <snprintf@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    1291 <write_db_value+0x111>
jmp    12ca <write_db_value+0x14a>
mov    -0x24(%rbp),%edi
mov    0x2d4d(%rip),%rax
mov    (%rax),%esi
call   10a0 <flock@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    12b4 <write_db_value+0x134>
jmp    12ca <write_db_value+0x14a>
lea    -0x430(%rbp),%rdi
lea    -0x830(%rbp),%rsi
call   1090 <rename@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x24(%rbp)
jl     12dc <write_db_value+0x15c>
mov    -0x24(%rbp),%edi
call   1060 <close@plt>
cmpl   $0x0,-0x28(%rbp)
jl     1304 <write_db_value+0x184>
cmpl   $0x0,-0x2c(%rbp)
jge    12fc <write_db_value+0x17c>
lea    -0x430(%rbp),%rdi
call   1030 <remove@plt>
mov    -0x28(%rbp),%edi
call   1060 <close@plt>
mov    -0x2c(%rbp),%eax
add    $0x840,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_db_value>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x808,%rsp
mov    %rcx,%rbx
mov    %rdx,%r12
mov    %rsi,%r14
mov    %rdi,%r13
lea    0xe5c(%rip),%rdx
mov    %rsp,%rdi
mov    $0x400,%esi
mov    %r13,%rcx
xor    %eax,%eax
call   1050 <snprintf@plt>
test   %eax,%eax
js     1275 <write_db_value+0xf5>
mov    %rsp,%rdi
call   1070 <mkstemp@plt>
mov    %eax,%r15d
mov    %eax,%edi
mov    %r12,%rsi
mov    %rbx,%rdx
call   1040 <write@plt>
cmp    %rbx,%rax
jne    1279 <write_db_value+0xf9>
lea    0xe2f(%rip),%rdx
lea    0x400(%rsp),%rdi
mov    $0x400,%esi
mov    %r13,%rcx
xor    %eax,%eax
call   1050 <snprintf@plt>
test   %eax,%eax
js     1285 <write_db_value+0x105>
lea    0x400(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
call   1080 <open@plt>
mov    %eax,%r12d
lea    0xdfd(%rip),%rdx
mov    %rbx,%rdi
mov    $0x400,%esi
mov    %r13,%rcx
mov    %r14,%r8
xor    %eax,%eax
call   1050 <snprintf@plt>
test   %eax,%eax
js     125f <write_db_value+0xdf>
mov    0x2da7(%rip),%rax
mov    (%rax),%esi
mov    %r12d,%edi
call   10a0 <flock@plt>
test   %eax,%eax
js     125f <write_db_value+0xdf>
mov    %rsp,%rdi
lea    0x400(%rsp),%rsi
call   1090 <rename@plt>
mov    %eax,%ebp
test   %r12d,%r12d
js     126e <write_db_value+0xee>
mov    %r12d,%edi
call   1060 <close@plt>
test   %r15d,%r15d
jns    128c <write_db_value+0x10c>
jmp    12a0 <write_db_value+0x120>
mov    %eax,%ebp
jmp    12a0 <write_db_value+0x120>
mov    $0xffffffec,%ebp
test   %r15d,%r15d
jns    1290 <write_db_value+0x110>
jmp    12a0 <write_db_value+0x120>
mov    %eax,%ebp
test   %r15d,%r15d
js     12a0 <write_db_value+0x120>
test   %ebp,%ebp
jns    1298 <write_db_value+0x118>
mov    %rsp,%rdi
call   1030 <remove@plt>
mov    %r15d,%edi
call   1060 <close@plt>
mov    %ebp,%eax
add    $0x808,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [21 x i8] c"%s/.tmp_value_XXXXXX\00"
@.str.1 = [9 x i8] c"%s/.lock\00"
@.str.2 = [8 x i8] c"%s/d/%s\00"
@LOCK_EX = global i32 0
define i32 @write_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 i32
%12 = alloca [1024 x i8]
%13 = alloca [1024 x i8]
%14 = alloca i64
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
store i32 -1, i32* %10
%15 = getelementptr inbounds [1024 x i8], [1024 x i8]* %12, i64 0, i64 0
%16 = load i8*, i8** %5
%17 = call i32 (i8*, i32, i8*, i8*, ...) @snprintf(i8* %15, i32 1024, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %16)
store i32 %17, i32* %11
%18 = load i32, i32* %11
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %21
20: ; preds = %4
br label %60
21: ; preds = %4
%22 = getelementptr inbounds [1024 x i8], [1024 x i8]* %12, i64 0, i64 0
%23 = call i32 @mkstemp(i8* %22)
store i32 %23, i32* %10
%24 = load i32, i32* %10
%25 = load i8*, i8** %7
%26 = load i64, i64* %8
%27 = call i64 @write(i32 %24, i8* %25, i64 %26)
store i64 %27, i64* %14
%28 = load i64, i64* %14
%29 = load i64, i64* %8
%30 = icmp ne i64 %28, %29
br i1 %30, label %31, label %32
31: ; preds = %21
store i32 -20, i32* %11
br label %60
32: ; preds = %21
%33 = getelementptr inbounds [1024 x i8], [1024 x i8]* %13, i64 0, i64 0
%34 = load i8*, i8** %5
%35 = call i32 (i8*, i32, i8*, i8*, ...) @snprintf(i8* %33, i32 1024, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i8* %34)
store i32 %35, i32* %11
%36 = load i32, i32* %11
%37 = icmp slt i32 %36, 0
br i1 %37, label %38, label %39
38: ; preds = %32
br label %60
39: ; preds = %32
%40 = getelementptr inbounds [1024 x i8], [1024 x i8]* %13, i64 0, i64 0
%41 = call i32 @open(i8* %40, i32 0)
store i32 %41, i32* %9
%42 = getelementptr inbounds [1024 x i8], [1024 x i8]* %13, i64 0, i64 0
%43 = load i8*, i8** %5
%44 = load i8*, i8** %6
%45 = call i32 (i8*, i32, i8*, i8*, ...) @snprintf(i8* %42, i32 1024, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8* %43, i8* %44)
store i32 %45, i32* %11
%46 = load i32, i32* %11
%47 = icmp slt i32 %46, 0
br i1 %47, label %48, label %49
48: ; preds = %39
br label %60
49: ; preds = %39
%50 = load i32, i32* %9
%51 = load i32, i32* @LOCK_EX
%52 = call i32 @flock(i32 %50, i32 %51)
store i32 %52, i32* %11
%53 = load i32, i32* %11
%54 = icmp slt i32 %53, 0
br i1 %54, label %55, label %56
55: ; preds = %49
br label %60
56: ; preds = %49
%57 = getelementptr inbounds [1024 x i8], [1024 x i8]* %12, i64 0, i64 0
%58 = getelementptr inbounds [1024 x i8], [1024 x i8]* %13, i64 0, i64 0
%59 = call i32 @rename(i8* %57, i8* %58)
store i32 %59, i32* %11
br label %60
60: ; preds = %56, %55, %48, %38, %31, %20
%61 = load i32, i32* %9
%62 = icmp sge i32 %61, 0
br i1 %62, label %63, label %66
63: ; preds = %60
%64 = load i32, i32* %9
%65 = call i32 @close(i32 %64)
br label %66
66: ; preds = %63, %60
%67 = load i32, i32* %10
%68 = icmp sge i32 %67, 0
br i1 %68, label %69, label %78
69: ; preds = %66
%70 = load i32, i32* %11
%71 = icmp slt i32 %70, 0
br i1 %71, label %72, label %75
72: ; preds = %69
%73 = getelementptr inbounds [1024 x i8], [1024 x i8]* %12, i64 0, i64 0
%74 = call i32 @remove(i8* %73)
br label %75
75: ; preds = %72, %69
%76 = load i32, i32* %10
%77 = call i32 @close(i32 %76)
br label %78
78: ; preds = %75, %66
%79 = load i32, i32* %11
ret i32 %79
}

LLVM IR (-O2):
@.str = [21 x i8] c"%s/.tmp_value_XXXXXX\00"
@.str.1 = [9 x i8] c"%s/.lock\00"
@.str.2 = [8 x i8] c"%s/d/%s\00"
@LOCK_EX = global i32 0
define i32 @write_db_value(i8* %0, i8* %1, i8* nocapture %2, i64 %3) {
%5 = alloca [1024 x i8]
%6 = alloca [1024 x i8]
%7 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%8 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%9 = call i32 (i8*, i32, i8*, i8*, ...) @snprintf(i8* %7, i32 1024, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %0)
%10 = icmp slt i32 %9, 0
br i1 %10, label %46, label %11
11: ; preds = %4
%12 = call i32 @mkstemp(i8* %7)
%13 = call i64 @write(i32 %12, i8* %2, i64 %3)
%14 = icmp eq i64 %13, %3
br i1 %14, label %15, label %36
15: ; preds = %11
%16 = call i32 (i8*, i32, i8*, i8*, ...) @snprintf(i8* %8, i32 1024, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%17 = icmp slt i32 %16, 0
br i1 %17, label %33, label %18
18: ; preds = %15
%19 = call i32 @open(i8* %8, i32 0)
%20 = call i32 (i8*, i32, i8*, i8*, ...) @snprintf(i8* %8, i32 1024, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8* %0, i8* %1)
%21 = icmp slt i32 %20, 0
br i1 %21, label %28, label %22
22: ; preds = %18
%23 = load i32, i32* @LOCK_EX
%24 = call i32 @flock(i32 %19, i32 %23)
%25 = icmp slt i32 %24, 0
br i1 %25, label %28, label %26
26: ; preds = %22
%27 = call i32 @rename(i8* %7, i8* %8)
br label %28
28: ; preds = %22, %18, %26
%29 = phi i32 [ %20, %18 ], [ %24, %22 ], [ %27, %26 ]
%30 = icmp sgt i32 %19, -1
br i1 %30, label %31, label %33
31: ; preds = %28
%32 = call i32 @close(i32 %19)
br label %33
33: ; preds = %15, %31, %28
%34 = phi i32 [ %29, %31 ], [ %29, %28 ], [ %16, %15 ]
%35 = icmp sgt i32 %12, -1
br i1 %35, label %38, label %46
36: ; preds = %11
%37 = icmp sgt i32 %12, -1
br i1 %37, label %40, label %46
38: ; preds = %33
%39 = icmp slt i32 %34, 0
br i1 %39, label %40, label %43
40: ; preds = %36, %38
%41 = phi i32 [ %34, %38 ], [ -20, %36 ]
%42 = call i32 @remove(i8* %7)
br label %43
43: ; preds = %40, %38
%44 = phi i32 [ %41, %40 ], [ %34, %38 ]
%45 = call i32 @close(i32 %12)
br label %46
46: ; preds = %4, %36, %43, %33
%47 = phi i32 [ %44, %43 ], [ %34, %33 ], [ -20, %36 ], [ %9, %4 ]
ret i32 %47
}