#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*/ EXIT_SUCCESS;
int atoi(char *);
int /*<<< orphan*/ database_close();
int /*<<< orphan*/ database_create();
int /*<<< orphan*/ database_delete(int);
int /*<<< orphan*/ database_get(int);
int /*<<< orphan*/ database_list();
int /*<<< orphan*/ database_open(char *, char);
int /*<<< orphan*/ database_set(int, char *, char *);
int /*<<< orphan*/ database_write();
int /*<<< orphan*/ die(char *);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int main(int argc, char **argv) {
  if (argc < 3)
    die("USAGE: ex_17 <dbfile> <action> [action params]");
  char *filename = argv[1];
  char action = argv[2][0];
  database_open(filename, action);
  int id = 0;
  if (argc > 3)
    id = atoi(argv[3]);
  if (id >= 100)
    die("There's not that many records");
  switch (action) {
  case 'c':
    database_create();
    database_write();
    break;
  case 'g':
    if (argc != 4)
      die("Need an id to get");
    database_get(id);
    break;
  case 's':
    if (argc != 6)
      die("Need id, name, email to set");
    database_set(id, argv[4], argv[5]);
    database_write();
    break;
  case 'd':
    if (argc != 4)
      die("Need id to delete");
    database_delete(id);
    database_write();
    break;
  case 'l':
    database_list();
    break;
  default:
    die("Invalid action, only: c=create, g=get, s=set, d=delete, l=list");
  }
  database_close();
  exit(EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
jge    11dc <main+0x2c>
lea    0xe6d(%rip),%rdi
call   1090 <die@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
mov    (%rax),%al
mov    %al,-0x19(%rbp)
mov    -0x18(%rbp),%rdi
movsbl -0x19(%rbp),%esi
call   1060 <database_open@plt>
movl   $0x0,-0x20(%rbp)
cmpl   $0x3,-0x8(%rbp)
jle    1223 <main+0x73>
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
call   10a0 <atoi@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x64,-0x20(%rbp)
jl     1239 <main+0x89>
lea    0xe3f(%rip),%rdi
call   1090 <die@plt>
movsbl -0x19(%rbp),%eax
add    $0xffffff9d,%eax
mov    %eax,%ecx
mov    %rcx,-0x28(%rbp)
sub    $0x10,%eax
ja     1309 <main+0x159>
mov    -0x28(%rbp),%rax
lea    0xda6(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
mov    $0x0,%al
call   10c0 <database_create@plt>
mov    $0x0,%al
call   10d0 <database_write@plt>
jmp    1315 <main+0x165>
cmpl   $0x4,-0x8(%rbp)
je     128c <main+0xdc>
lea    0xe0a(%rip),%rdi
call   1090 <die@plt>
mov    -0x20(%rbp),%edi
call   1030 <database_get@plt>
jmp    1315 <main+0x165>
cmpl   $0x6,-0x8(%rbp)
je     12af <main+0xff>
lea    0xdf9(%rip),%rdi
call   1090 <die@plt>
mov    -0x20(%rbp),%edi
mov    -0x10(%rbp),%rax
mov    0x20(%rax),%rsi
mov    -0x10(%rbp),%rax
mov    0x28(%rax),%rdx
call   1040 <database_set@plt>
mov    $0x0,%al
call   10d0 <database_write@plt>
jmp    1315 <main+0x165>
cmpl   $0x4,-0x8(%rbp)
je     12e9 <main+0x139>
lea    0xddb(%rip),%rdi
call   1090 <die@plt>
mov    -0x20(%rbp),%edi
call   1070 <database_delete@plt>
mov    $0x0,%al
call   10d0 <database_write@plt>
jmp    1315 <main+0x165>
mov    $0x0,%al
call   1050 <database_list@plt>
jmp    1315 <main+0x165>
lea    0xdc1(%rip),%rdi
call   1090 <die@plt>
mov    $0x0,%al
call   1080 <database_close@plt>
mov    0x2cbd(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r15
mov    %edi,%ebp
cmp    $0x2,%edi
jg     11cd <main+0x1d>
lea    0xe7c(%rip),%rdi
call   1090 <die@plt>
mov    0x8(%r15),%rdi
mov    0x10(%r15),%rax
movsbl (%rax),%ebx
mov    %ebx,%esi
call   1060 <database_open@plt>
xor    %r14d,%r14d
cmp    $0x4,%ebp
jl     1204 <main+0x54>
mov    0x18(%r15),%rdi
call   10a0 <atoi@plt>
mov    %eax,%r14d
cmp    $0x64,%eax
jl     1204 <main+0x54>
lea    0xe74(%rip),%rdi
call   1090 <die@plt>
add    $0xffffff9d,%ebx
cmp    $0x10,%ebx
ja     1240 <main+0x90>
lea    0xded(%rip),%rax
movslq (%rax,%rbx,4),%rcx
add    %rax,%rcx
jmp    *%rcx
xor    %eax,%eax
call   10c0 <database_create@plt>
jmp    1293 <main+0xe3>
cmp    $0x4,%ebp
je     1236 <main+0x86>
lea    0xe8e(%rip),%rdi
call   1090 <die@plt>
mov    %r14d,%edi
call   1070 <database_delete@plt>
jmp    1293 <main+0xe3>
lea    0xe8a(%rip),%rdi
call   1090 <die@plt>
jmp    129a <main+0xea>
cmp    $0x4,%ebp
je     125f <main+0xaf>
lea    0xe37(%rip),%rdi
call   1090 <die@plt>
mov    %r14d,%edi
call   1030 <database_get@plt>
jmp    129a <main+0xea>
xor    %eax,%eax
call   1050 <database_list@plt>
jmp    129a <main+0xea>
cmp    $0x6,%ebp
je     1283 <main+0xd3>
lea    0xe25(%rip),%rdi
call   1090 <die@plt>
mov    0x20(%r15),%rsi
mov    0x28(%r15),%rdx
mov    %r14d,%edi
call   1040 <database_set@plt>
xor    %eax,%eax
call   10d0 <database_write@plt>
xor    %eax,%eax
call   1080 <database_close@plt>
mov    0x2d38(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@.str = [47 x i8] c"USAGE: ex_17 <dbfile> <action> [action params]\00"
@.str.1 = [30 x i8] c"There's not that many records\00"
@.str.2 = [18 x i8] c"Need an id to get\00"
@.str.3 = [28 x i8] c"Need id, name, email to set\00"
@.str.4 = [18 x i8] c"Need id to delete\00"
@.str.5 = [63 x i8] c"Invalid action, only: c=create, g=get, s=set, d=delete, l=list\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i8
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* %4
%10 = icmp slt i32 %9, 3
br i1 %10, label %11, label %13
11: ; preds = %2
%12 = call i32 @die(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str, i64 0, i64 0))
br label %13
13: ; preds = %11, %2
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 1
%16 = load i8*, i8** %15
store i8* %16, i8** %6
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 2
%19 = load i8*, i8** %18
%20 = getelementptr inbounds i8, i8* %19, i64 0
%21 = load i8, i8* %20
store i8 %21, i8* %7
%22 = load i8*, i8** %6
%23 = load i8, i8* %7
%24 = call i32 @database_open(i8* %22, i8 signext %23)
store i32 0, i32* %8
%25 = load i32, i32* %4
%26 = icmp sgt i32 %25, 3
br i1 %26, label %27, label %32
27: ; preds = %13
%28 = load i8**, i8*** %5
%29 = getelementptr inbounds i8*, i8** %28, i64 3
%30 = load i8*, i8** %29
%31 = call i32 @atoi(i8* %30)
store i32 %31, i32* %8
br label %32
32: ; preds = %27, %13
%33 = load i32, i32* %8
%34 = icmp sge i32 %33, 100
br i1 %34, label %35, label %37
35: ; preds = %32
%36 = call i32 @die(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0))
br label %37
37: ; preds = %35, %32
%38 = load i8, i8* %7
%39 = sext i8 %38 to i32
switch i32 %39, label %77 [
i32 99, label %40
i32 103, label %43
i32 115, label %51
i32 100, label %66
i32 108, label %75
]
40: ; preds = %37
%41 = call i32 (...) @database_create()
%42 = call i32 (...) @database_write()
br label %79
43: ; preds = %37
%44 = load i32, i32* %4
%45 = icmp ne i32 %44, 4
br i1 %45, label %46, label %48
46: ; preds = %43
%47 = call i32 @die(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
br label %48
48: ; preds = %46, %43
%49 = load i32, i32* %8
%50 = call i32 @database_get(i32 %49)
br label %79
51: ; preds = %37
%52 = load i32, i32* %4
%53 = icmp ne i32 %52, 6
br i1 %53, label %54, label %56
54: ; preds = %51
%55 = call i32 @die(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0))
br label %56
56: ; preds = %54, %51
%57 = load i32, i32* %8
%58 = load i8**, i8*** %5
%59 = getelementptr inbounds i8*, i8** %58, i64 4
%60 = load i8*, i8** %59
%61 = load i8**, i8*** %5
%62 = getelementptr inbounds i8*, i8** %61, i64 5
%63 = load i8*, i8** %62
%64 = call i32 @database_set(i32 %57, i8* %60, i8* %63)
%65 = call i32 (...) @database_write()
br label %79
66: ; preds = %37
%67 = load i32, i32* %4
%68 = icmp ne i32 %67, 4
br i1 %68, label %69, label %71
69: ; preds = %66
%70 = call i32 @die(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0))
br label %71
71: ; preds = %69, %66
%72 = load i32, i32* %8
%73 = call i32 @database_delete(i32 %72)
%74 = call i32 (...) @database_write()
br label %79
75: ; preds = %37
%76 = call i32 (...) @database_list()
br label %79
77: ; preds = %37
%78 = call i32 @die(i8* getelementptr inbounds ([63 x i8], [63 x i8]* @.str.5, i64 0, i64 0))
br label %79
79: ; preds = %77, %75, %71, %56, %48, %40
%80 = call i32 (...) @database_close()
%81 = load i32, i32* @EXIT_SUCCESS
%82 = call i32 @exit(i32 %81)
unreachable
}

LLVM IR (-O2):
@.str = [47 x i8] c"USAGE: ex_17 <dbfile> <action> [action params]\00"
@.str.1 = [30 x i8] c"There's not that many records\00"
@.str.2 = [18 x i8] c"Need an id to get\00"
@.str.3 = [28 x i8] c"Need id, name, email to set\00"
@.str.4 = [18 x i8] c"Need id to delete\00"
@.str.5 = [63 x i8] c"Invalid action, only: c=create, g=get, s=set, d=delete, l=list\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp slt i32 %0, 3
br i1 %3, label %4, label %6
4: ; preds = %2
%5 = tail call i32 @die(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str, i64 0, i64 0))
br label %6
6: ; preds = %4, %2
%7 = getelementptr inbounds i8*, i8** %1, i64 1
%8 = load i8*, i8** %7
%9 = getelementptr inbounds i8*, i8** %1, i64 2
%10 = load i8*, i8** %9
%11 = load i8, i8* %10
%12 = tail call i32 @database_open(i8* %8, i8 signext %11)
%13 = icmp sgt i32 %0, 3
br i1 %13, label %14, label %21
14: ; preds = %6
%15 = getelementptr inbounds i8*, i8** %1, i64 3
%16 = load i8*, i8** %15
%17 = tail call i32 @atoi(i8* %16)
%18 = icmp sgt i32 %17, 99
br i1 %18, label %19, label %21
19: ; preds = %14
%20 = tail call i32 @die(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0))
br label %21
21: ; preds = %6, %19, %14
%22 = phi i32 [ %17, %19 ], [ %17, %14 ], [ 0, %6 ]
%23 = sext i8 %11 to i32
switch i32 %23, label %53 [
i32 99, label %24
i32 103, label %27
i32 115, label %33
i32 100, label %44
i32 108, label %51
]
24: ; preds = %21
%25 = tail call i32 (...) @database_create()
%26 = tail call i32 (...) @database_write()
br label %55
27: ; preds = %21
%28 = icmp eq i32 %0, 4
br i1 %28, label %31, label %29
29: ; preds = %27
%30 = tail call i32 @die(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
br label %31
31: ; preds = %29, %27
%32 = tail call i32 @database_get(i32 %22)
br label %55
33: ; preds = %21
%34 = icmp eq i32 %0, 6
br i1 %34, label %37, label %35
35: ; preds = %33
%36 = tail call i32 @die(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0))
br label %37
37: ; preds = %35, %33
%38 = getelementptr inbounds i8*, i8** %1, i64 4
%39 = load i8*, i8** %38
%40 = getelementptr inbounds i8*, i8** %1, i64 5
%41 = load i8*, i8** %40
%42 = tail call i32 @database_set(i32 %22, i8* %39, i8* %41)
%43 = tail call i32 (...) @database_write()
br label %55
44: ; preds = %21
%45 = icmp eq i32 %0, 4
br i1 %45, label %48, label %46
46: ; preds = %44
%47 = tail call i32 @die(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0))
br label %48
48: ; preds = %46, %44
%49 = tail call i32 @database_delete(i32 %22)
%50 = tail call i32 (...) @database_write()
br label %55
51: ; preds = %21
%52 = tail call i32 (...) @database_list()
br label %55
53: ; preds = %21
%54 = tail call i32 @die(i8* getelementptr inbounds ([63 x i8], [63 x i8]* @.str.5, i64 0, i64 0))
br label %55
55: ; preds = %53, %51, %48, %37, %31, %24
%56 = tail call i32 (...) @database_close()
%57 = load i32, i32* @EXIT_SUCCESS
%58 = tail call i32 @exit(i32 %57)
unreachable
}