#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 */
struct accel_state {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ LOG_INFO;
long accel_add_entry(struct accel_state *, unsigned long, unsigned long);
int /*<<< orphan*/ assoc_addr(unsigned long, long);
int /*<<< orphan*/ assoc_len(unsigned long, unsigned long);
int /*<<< orphan*/ fence();
unsigned long reserve_key(void *, unsigned int, unsigned int);
int /*<<< orphan*/ syslog(int /*<<< orphan*/, char *, unsigned long, long,
                          unsigned long);
int /*<<< orphan*/ write_val(unsigned long, void *);
int accel_set(struct accel_state *accel, const char *key, unsigned int keylen,
              void *value, unsigned long vallen, unsigned int weight) {
  unsigned long hash;
  long accel_addr;
  fence();
  hash = reserve_key((void *)key, weight, keylen);
  if (hash == 0xffffff)
    return -1;
  accel_addr = accel_add_entry(accel, hash, vallen);
  if (accel_addr < 0)
    return -1;
  fence();
  assoc_addr(hash, accel_addr);
  fence();
  assoc_len(hash, vallen);
  fence();
  write_val(hash, value);
  syslog(LOG_INFO, "Hash %lu, Addr %ld, Len: %lu\n", hash, accel_addr, vallen);
  return 0;
}


x86 assembly (clang, -O0):
<accel_set>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8,-0x30(%rbp)
mov    %r9d,-0x34(%rbp)
mov    $0x0,%al
call   1060 <fence@plt>
mov    -0x18(%rbp),%rdi
mov    -0x34(%rbp),%esi
mov    -0x1c(%rbp),%edx
call   1050 <reserve_key@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0xffffff,-0x40(%rbp)
jne    11c3 <accel_set+0x53>
movl   $0xffffffff,-0x4(%rbp)
jmp    1253 <accel_set+0xe3>
mov    -0x10(%rbp),%rdi
mov    -0x40(%rbp),%rsi
mov    -0x30(%rbp),%rdx
call   1080 <accel_add_entry@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
jge    11ef <accel_set+0x7f>
movl   $0xffffffff,-0x4(%rbp)
jmp    1253 <accel_set+0xe3>
mov    $0x0,%al
call   1060 <fence@plt>
mov    -0x40(%rbp),%rdi
mov    -0x48(%rbp),%rsi
call   1030 <assoc_addr@plt>
mov    $0x0,%al
call   1060 <fence@plt>
mov    -0x40(%rbp),%rdi
mov    -0x30(%rbp),%rsi
call   1040 <assoc_len@plt>
mov    $0x0,%al
call   1060 <fence@plt>
mov    -0x40(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1090 <write_val@plt>
mov    0x2dae(%rip),%rax
mov    (%rax),%edi
mov    -0x40(%rbp),%rdx
mov    -0x48(%rbp),%rcx
mov    -0x30(%rbp),%r8
lea    0xdb9(%rip),%rsi
call   1070 <syslog@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<accel_set>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r9d,%r13d
mov    %r8,%r15
mov    %rcx,%r14
mov    %edx,%ebp
mov    %rsi,%rbx
mov    %rdi,%r12
xor    %eax,%eax
call   1060 <fence@plt>
mov    %rbx,%rdi
mov    %r13d,%esi
mov    %ebp,%edx
call   1050 <reserve_key@plt>
mov    $0xffffffff,%r13d
cmp    $0xffffff,%rax
je     121e <accel_set+0xae>
mov    %rax,%rbx
mov    %r12,%rdi
mov    %rax,%rsi
mov    %r15,%rdx
call   1080 <accel_add_entry@plt>
test   %rax,%rax
js     121e <accel_set+0xae>
mov    %rax,%r12
xor    %r13d,%r13d
xor    %eax,%eax
call   1060 <fence@plt>
mov    %rbx,%rdi
mov    %r12,%rsi
call   1030 <assoc_addr@plt>
xor    %eax,%eax
call   1060 <fence@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
call   1040 <assoc_len@plt>
xor    %eax,%eax
call   1060 <fence@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1090 <write_val@plt>
mov    0x2dd9(%rip),%rax
mov    (%rax),%edi
lea    0xdf0(%rip),%rsi
mov    %rbx,%rdx
mov    %r12,%rcx
mov    %r15,%r8
call   1070 <syslog@plt>
mov    %r13d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.accel_state = type { i32 }
@LOG_INFO = global i32 0
@.str = [30 x i8] c"Hash %lu, Addr %ld, Len: %lu\0A\00"
define i32 @accel_set(%struct.accel_state* %0, i8* %1, i32 %2, i8* %3, i64 %4, i32 %5) {
%7 = alloca i32
%8 = alloca %struct.accel_state*
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i8*
%12 = alloca i64
%13 = alloca i32
%14 = alloca i64
%15 = alloca i64
store %struct.accel_state* %0, %struct.accel_state** %8
store i8* %1, i8** %9
store i32 %2, i32* %10
store i8* %3, i8** %11
store i64 %4, i64* %12
store i32 %5, i32* %13
%16 = call i32 (...) @fence()
%17 = load i8*, i8** %9
%18 = load i32, i32* %13
%19 = load i32, i32* %10
%20 = call i64 @reserve_key(i8* %17, i32 %18, i32 %19)
store i64 %20, i64* %14
%21 = load i64, i64* %14
%22 = icmp eq i64 %21, 16777215
br i1 %22, label %23, label %24
23: ; preds = %6
store i32 -1, i32* %7
br label %50
24: ; preds = %6
%25 = load %struct.accel_state*, %struct.accel_state** %8
%26 = load i64, i64* %14
%27 = load i64, i64* %12
%28 = call i64 @accel_add_entry(%struct.accel_state* %25, i64 %26, i64 %27)
store i64 %28, i64* %15
%29 = load i64, i64* %15
%30 = icmp slt i64 %29, 0
br i1 %30, label %31, label %32
31: ; preds = %24
store i32 -1, i32* %7
br label %50
32: ; preds = %24
%33 = call i32 (...) @fence()
%34 = load i64, i64* %14
%35 = load i64, i64* %15
%36 = call i32 @assoc_addr(i64 %34, i64 %35)
%37 = call i32 (...) @fence()
%38 = load i64, i64* %14
%39 = load i64, i64* %12
%40 = call i32 @assoc_len(i64 %38, i64 %39)
%41 = call i32 (...) @fence()
%42 = load i64, i64* %14
%43 = load i8*, i8** %11
%44 = call i32 @write_val(i64 %42, i8* %43)
%45 = load i32, i32* @LOG_INFO
%46 = load i64, i64* %14
%47 = load i64, i64* %15
%48 = load i64, i64* %12
%49 = call i32 @syslog(i32 %45, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i64 %46, i64 %47, i64 %48)
store i32 0, i32* %7
br label %50
50: ; preds = %32, %31, %23
%51 = load i32, i32* %7
ret i32 %51
}

LLVM IR (-O2):
%struct.accel_state = type { i32 }
@LOG_INFO = global i32 0
@.str = [30 x i8] c"Hash %lu, Addr %ld, Len: %lu\0A\00"
define i32 @accel_set(%struct.accel_state* %0, i8* %1, i32 %2, i8* %3, i64 %4, i32 %5) {
%7 = tail call i32 (...) @fence()
%8 = tail call i64 @reserve_key(i8* %1, i32 %5, i32 %2)
%9 = icmp eq i64 %8, 16777215
br i1 %9, label %22, label %10
10: ; preds = %6
%11 = tail call i64 @accel_add_entry(%struct.accel_state* %0, i64 %8, i64 %4)
%12 = icmp slt i64 %11, 0
br i1 %12, label %22, label %13
13: ; preds = %10
%14 = tail call i32 (...) @fence()
%15 = tail call i32 @assoc_addr(i64 %8, i64 %11)
%16 = tail call i32 (...) @fence()
%17 = tail call i32 @assoc_len(i64 %8, i64 %4)
%18 = tail call i32 (...) @fence()
%19 = tail call i32 @write_val(i64 %8, i8* %3)
%20 = load i32, i32* @LOG_INFO
%21 = tail call i32 @syslog(i32 %20, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i64 %8, i64 %11, i64 %4)
br label %22
22: ; preds = %10, %6, %13
%23 = phi i32 [ 0, %13 ], [ -1, %6 ], [ -1, %10 ]
ret i32 %23
}