#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*/ close(int);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ printf(char *, ...);
char *xattrkv_get(int, char *);
int /*<<< orphan*/ xattrkv_keys(int, char ***, size_t *);
int xattrkv_new(char *);
int xattrkv_open(char *);
int /*<<< orphan*/ xattrkv_set(int, char *, char *);
int main(int argc, char *argv[]) {
  int db;
  db = xattrkv_new("testdb");
  if (db != -1) {
    xattrkv_set(db, "test", "this is a test");
    xattrkv_set(db, "testing", "this is a test");
    xattrkv_set(db, "testone", "this is a test");
    xattrkv_set(db, "testtwo", "this is a test");
  } else {
    printf("file exists, opening it instead!\n");
    db = xattrkv_open("testdb");
    char *val = xattrkv_get(db, "test");
    printf("got value from \"test\": %s\n", val);
    free(val);
  }
  char **keys;
  size_t nkeys;
  xattrkv_keys(db, &keys, &nkeys);
  printf("nkeys: %ld\n", nkeys);
  for (int i = 0; i < nkeys; i++) {
    printf("key: %s\n", keys[i]);
  }
  close(db);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe63(%rip),%rdi
call   1090 <xattrkv_new@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
je     120c <main+0x8c>
mov    -0x14(%rbp),%edi
lea    0xe58(%rip),%rsi
lea    0xe47(%rip),%rdx
call   1070 <xattrkv_set@plt>
mov    -0x14(%rbp),%edi
lea    0xe47(%rip),%rsi
lea    0xe31(%rip),%rdx
call   1070 <xattrkv_set@plt>
mov    -0x14(%rbp),%edi
lea    0xe39(%rip),%rsi
lea    0xe1b(%rip),%rdx
call   1070 <xattrkv_set@plt>
mov    -0x14(%rbp),%edi
lea    0xe2b(%rip),%rsi
lea    0xe05(%rip),%rdx
call   1070 <xattrkv_set@plt>
jmp    1257 <main+0xd7>
lea    0xe1b(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xddf(%rip),%rdi
call   10a0 <xattrkv_open@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
lea    0xdde(%rip),%rsi
call   1080 <xattrkv_get@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rsi
lea    0xe09(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x14(%rbp),%edi
lea    -0x28(%rbp),%rsi
lea    -0x30(%rbp),%rdx
call   1040 <xattrkv_keys@plt>
mov    -0x30(%rbp),%rsi
lea    0xdf9(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x0,-0x34(%rbp)
movslq -0x34(%rbp),%rax
cmp    -0x30(%rbp),%rax
jae    12b6 <main+0x136>
mov    -0x28(%rbp),%rax
movslq -0x34(%rbp),%rcx
mov    (%rax,%rcx,8),%rsi
lea    0xdd6(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    1280 <main+0x100>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
lea    0xe61(%rip),%rdi
call   10a0 <xattrkv_new@plt>
cmp    $0xffffffff,%eax
je     11fc <main+0x6c>
mov    %eax,%r14d
lea    0xe5e(%rip),%rsi
lea    0xe4d(%rip),%rbx
mov    %eax,%edi
mov    %rbx,%rdx
call   1080 <xattrkv_set@plt>
lea    0xe4b(%rip),%rsi
mov    %r14d,%edi
mov    %rbx,%rdx
call   1080 <xattrkv_set@plt>
lea    0xe41(%rip),%rsi
mov    %r14d,%edi
mov    %rbx,%rdx
call   1080 <xattrkv_set@plt>
lea    0xe37(%rip),%rsi
mov    %r14d,%edi
mov    %rbx,%rdx
call   1080 <xattrkv_set@plt>
jmp    1241 <main+0xb1>
lea    0xe5b(%rip),%rdi
call   1050 <puts@plt>
lea    0xdf1(%rip),%rdi
call   10b0 <xattrkv_open@plt>
mov    %eax,%r14d
lea    0xdf3(%rip),%rsi
mov    %eax,%edi
call   1090 <xattrkv_get@plt>
mov    %rax,%rbx
lea    0xdff(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
lea    0x8(%rsp),%rsi
mov    %rsp,%rdx
mov    %r14d,%edi
call   1040 <xattrkv_keys@plt>
mov    (%rsp),%rsi
lea    0xded(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
cmpq   $0x0,(%rsp)
je     129d <main+0x10d>
lea    0xde4(%rip),%rbx
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    0x8(%rsp),%rax
mov    (%rax,%rbp,8),%rsi
mov    %rbx,%rdi
xor    %eax,%eax
call   1060 <printf@plt>
add    $0x1,%rbp
cmp    %rbp,(%rsp)
ja     1280 <main+0xf0>
mov    %r14d,%edi
call   1070 <close@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [7 x i8] c"testdb\00"
@.str.1 = [5 x i8] c"test\00"
@.str.2 = [15 x i8] c"this is a test\00"
@.str.3 = [8 x i8] c"testing\00"
@.str.4 = [8 x i8] c"testone\00"
@.str.5 = [8 x i8] c"testtwo\00"
@.str.6 = [34 x i8] c"file exists, opening it instead!\0A\00"
@.str.7 = [27 x i8] c"got value from \22test\22: %s\0A\00"
@.str.8 = [12 x i8] c"nkeys: %ld\0A\00"
@.str.9 = [9 x i8] c"key: %s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8**
%9 = alloca i64
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = call i32 @xattrkv_new(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
store i32 %11, i32* %6
%12 = load i32, i32* %6
%13 = icmp ne i32 %12, -1
br i1 %13, label %14, label %23
14: ; preds = %2
%15 = load i32, i32* %6
%16 = call i32 @xattrkv_set(i32 %15, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
%17 = load i32, i32* %6
%18 = call i32 @xattrkv_set(i32 %17, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
%19 = load i32, i32* %6
%20 = call i32 @xattrkv_set(i32 %19, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
%21 = load i32, i32* %6
%22 = call i32 @xattrkv_set(i32 %21, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
br label %32
23: ; preds = %2
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.6, i64 0, i64 0))
%25 = call i32 @xattrkv_open(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
store i32 %25, i32* %6
%26 = load i32, i32* %6
%27 = call i8* @xattrkv_get(i32 %26, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
store i8* %27, i8** %7
%28 = load i8*, i8** %7
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.7, i64 0, i64 0), i8* %28)
%30 = load i8*, i8** %7
%31 = call i32 @free(i8* %30)
br label %32
32: ; preds = %23, %14
%33 = load i32, i32* %6
%34 = call i32 @xattrkv_keys(i32 %33, i8*** %8, i64* %9)
%35 = load i64, i64* %9
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.8, i64 0, i64 0), i64 %35)
store i32 0, i32* %10
br label %37
37: ; preds = %49, %32
%38 = load i32, i32* %10
%39 = sext i32 %38 to i64
%40 = load i64, i64* %9
%41 = icmp ult i64 %39, %40
br i1 %41, label %42, label %52
42: ; preds = %37
%43 = load i8**, i8*** %8
%44 = load i32, i32* %10
%45 = sext i32 %44 to i64
%46 = getelementptr inbounds i8*, i8** %43, i64 %45
%47 = load i8*, i8** %46
%48 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.9, i64 0, i64 0), i8* %47)
br label %49
49: ; preds = %42
%50 = load i32, i32* %10
%51 = add nsw i32 %50, 1
store i32 %51, i32* %10
br label %37
52: ; preds = %37
%53 = load i32, i32* %6
%54 = call i32 @close(i32 %53)
ret i32 0
}

LLVM IR (-O2):
@.str = [7 x i8] c"testdb\00"
@.str.1 = [5 x i8] c"test\00"
@.str.2 = [15 x i8] c"this is a test\00"
@.str.3 = [8 x i8] c"testing\00"
@.str.4 = [8 x i8] c"testone\00"
@.str.5 = [8 x i8] c"testtwo\00"
@.str.7 = [27 x i8] c"got value from \22test\22: %s\0A\00"
@.str.8 = [12 x i8] c"nkeys: %ld\0A\00"
@.str.9 = [9 x i8] c"key: %s\0A\00"
@str = [33 x i8] c"file exists, opening it instead!\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i8**
%4 = alloca i64
%5 = tail call i32 @xattrkv_new(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i32 %5, -1
br i1 %6, label %12, label %7
7: ; preds = %2
%8 = tail call i32 @xattrkv_set(i32 %5, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
%9 = tail call i32 @xattrkv_set(i32 %5, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
%10 = tail call i32 @xattrkv_set(i32 %5, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
%11 = tail call i32 @xattrkv_set(i32 %5, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
br label %18
12: ; preds = %2
%13 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @str, i64 0, i64 0))
%14 = tail call i32 @xattrkv_open(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%15 = tail call i8* @xattrkv_get(i32 %14, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%16 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.7, i64 0, i64 0), i8* %15)
%17 = tail call i32 @free(i8* %15)
br label %18
18: ; preds = %12, %7
%19 = phi i32 [ %5, %7 ], [ %14, %12 ]
%20 = bitcast i8*** %3 to i8*
%21 = bitcast i64* %4 to i8*
%22 = call i32 @xattrkv_keys(i32 %19, i8*** %3, i64* %4)
%23 = load i64, i64* %4
%24 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.8, i64 0, i64 0), i64 %23)
%25 = load i64, i64* %4
%26 = icmp eq i64 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %29, %18
%28 = call i32 @close(i32 %19)
ret i32 0
29: ; preds = %18, %29
%30 = phi i64 [ %35, %29 ], [ 0, %18 ]
%31 = load i8**, i8*** %3
%32 = getelementptr inbounds i8*, i8** %31, i64 %30
%33 = load i8*, i8** %32
%34 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.9, i64 0, i64 0), i8* %33)
%35 = add nuw i64 %30, 1
%36 = load i64, i64* %4
%37 = icmp ugt i64 %36, %35
br i1 %37, label %29, label %27
}