#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 Connection {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ Database_close(struct Connection *);
int /*<<< orphan*/ Database_create(struct Connection *);
int /*<<< orphan*/ Database_get(struct Connection *, int);
int /*<<< orphan*/ Database_list(struct Connection *);
struct Connection *Database_open(char *, char);
int /*<<< orphan*/ Database_set(struct Connection *, int, char *, char *);
int /*<<< orphan*/ Database_write(struct Connection *);
int atoi(char *);
int /*<<< orphan*/ die(char *);
int main(int argc, char *argv[]) {
  if (argc < 3)
    die("USAGE: ex17 <dbfile> <action> [action params]");
  char *filename = argv[1];
  char action = argv[2][0];
  struct Connection *conn = 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(conn);
    Database_write(conn);
    break;
  case 'g':
    if (argc != 4)
      die("Need an id to get");
    Database_get(conn, id);
    break;
  case 's':
    if (argc != 6)
      die("Need id, name, email to set");
    Database_set(conn, id, argv[4], argv[5]);
    Database_write(conn);
    break;
  case 'd':
    if (argc != 4)
      die("Need id to delete");
    Database_set(conn, id, argv[4], argv[5]);
    Database_write(conn);
    break;
  case 'l':
    Database_list(conn);
    break;
  default:
    die("Invalid action, only: c=create, g=get, s=set, d=del, l=list");
  }
  Database_close(conn);
  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)
cmpl   $0x3,-0x8(%rbp)
jge    11bc <main+0x2c>
lea    0xe8d(%rip),%rdi
call   1070 <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   10a0 <Database_open@plt>
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
cmpl   $0x3,-0x8(%rbp)
jle    1207 <main+0x77>
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
call   1080 <atoi@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x64,-0x2c(%rbp)
jl     121d <main+0x8d>
lea    0xe5a(%rip),%rdi
call   1070 <die@plt>
movsbl -0x19(%rbp),%eax
add    $0xffffff9d,%eax
mov    %eax,%ecx
mov    %rcx,-0x38(%rbp)
sub    $0x10,%eax
ja     1313 <main+0x183>
mov    -0x38(%rbp),%rax
lea    0xdc2(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
mov    -0x28(%rbp),%rdi
call   1050 <Database_create@plt>
mov    -0x28(%rbp),%rdi
call   1030 <Database_write@plt>
jmp    131f <main+0x18f>
cmpl   $0x4,-0x8(%rbp)
je     1274 <main+0xe4>
lea    0xe22(%rip),%rdi
call   1070 <die@plt>
mov    -0x28(%rbp),%rdi
mov    -0x2c(%rbp),%esi
call   1090 <Database_get@plt>
jmp    131f <main+0x18f>
cmpl   $0x6,-0x8(%rbp)
je     129b <main+0x10b>
lea    0xe0d(%rip),%rdi
call   1070 <die@plt>
mov    -0x28(%rbp),%rdi
mov    -0x2c(%rbp),%esi
mov    -0x10(%rbp),%rax
mov    0x20(%rax),%rdx
mov    -0x10(%rbp),%rax
mov    0x28(%rax),%rcx
call   1060 <Database_set@plt>
mov    -0x28(%rbp),%rdi
call   1030 <Database_write@plt>
jmp    131f <main+0x18f>
cmpl   $0x4,-0x8(%rbp)
je     12db <main+0x14b>
lea    0xde9(%rip),%rdi
call   1070 <die@plt>
mov    -0x28(%rbp),%rdi
mov    -0x2c(%rbp),%esi
mov    -0x10(%rbp),%rax
mov    0x20(%rax),%rdx
mov    -0x10(%rbp),%rax
mov    0x28(%rax),%rcx
call   1060 <Database_set@plt>
mov    -0x28(%rbp),%rdi
call   1030 <Database_write@plt>
jmp    131f <main+0x18f>
mov    -0x28(%rbp),%rdi
call   1040 <Database_list@plt>
jmp    131f <main+0x18f>
lea    0xdb7(%rip),%rdi
call   1070 <die@plt>
mov    -0x28(%rbp),%rdi
call   10b0 <Database_close@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%r12
mov    %edi,%ebp
cmp    $0x2,%edi
jg     11ae <main+0x1e>
lea    0xe9b(%rip),%rdi
call   1070 <die@plt>
mov    0x8(%r12),%rdi
mov    0x10(%r12),%rax
movsbl (%rax),%ebx
mov    %ebx,%esi
call   10a0 <Database_open@plt>
mov    %rax,%r14
xor    %r15d,%r15d
cmp    $0x4,%ebp
jl     11eb <main+0x5b>
mov    0x18(%r12),%rdi
call   1080 <atoi@plt>
mov    %eax,%r15d
cmp    $0x64,%eax
jl     11eb <main+0x5b>
lea    0xe8c(%rip),%rdi
call   1070 <die@plt>
add    $0xffffff9d,%ebx
cmp    $0x10,%ebx
ja     121b <main+0x8b>
lea    0xe06(%rip),%rax
movslq (%rax,%rbx,4),%rcx
add    %rax,%rcx
jmp    *%rcx
mov    %r14,%rdi
call   1050 <Database_create@plt>
jmp    1277 <main+0xe7>
cmp    $0x4,%ebp
je     1262 <main+0xd2>
lea    0xea6(%rip),%rdi
jmp    125d <main+0xcd>
lea    0xeaf(%rip),%rdi
call   1070 <die@plt>
jmp    127f <main+0xef>
cmp    $0x4,%ebp
je     123a <main+0xaa>
lea    0xe5c(%rip),%rdi
call   1070 <die@plt>
mov    %r14,%rdi
mov    %r15d,%esi
call   1090 <Database_get@plt>
jmp    127f <main+0xef>
mov    %r14,%rdi
call   1040 <Database_list@plt>
jmp    127f <main+0xef>
cmp    $0x6,%ebp
je     1262 <main+0xd2>
lea    0xe46(%rip),%rdi
call   1070 <die@plt>
mov    0x20(%r12),%rdx
mov    0x28(%r12),%rcx
mov    %r14,%rdi
mov    %r15d,%esi
call   1060 <Database_set@plt>
mov    %r14,%rdi
call   1030 <Database_write@plt>
mov    %r14,%rdi
call   10b0 <Database_close@plt>
xor    %eax,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.Connection = type { i32 }
@.str = [46 x i8] c"USAGE: ex17 <dbfile> <action> [action params]\00"
@.str.1 = [31 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 = [60 x i8] c"Invalid action, only: c=create, g=get, s=set, d=del, l=list\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i8
%8 = alloca %struct.Connection*
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* %4
%11 = icmp slt i32 %10, 3
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = call i32 @die(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str, i64 0, i64 0))
br label %14
14: ; preds = %12, %2
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 1
%17 = load i8*, i8** %16
store i8* %17, i8** %6
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 2
%20 = load i8*, i8** %19
%21 = getelementptr inbounds i8, i8* %20, i64 0
%22 = load i8, i8* %21
store i8 %22, i8* %7
%23 = load i8*, i8** %6
%24 = load i8, i8* %7
%25 = call %struct.Connection* @Database_open(i8* %23, i8 signext %24)
store %struct.Connection* %25, %struct.Connection** %8
store i32 0, i32* %9
%26 = load i32, i32* %4
%27 = icmp sgt i32 %26, 3
br i1 %27, label %28, label %33
28: ; preds = %14
%29 = load i8**, i8*** %5
%30 = getelementptr inbounds i8*, i8** %29, i64 3
%31 = load i8*, i8** %30
%32 = call i32 @atoi(i8* %31)
store i32 %32, i32* %9
br label %33
33: ; preds = %28, %14
%34 = load i32, i32* %9
%35 = icmp sge i32 %34, 100
br i1 %35, label %36, label %38
36: ; preds = %33
%37 = call i32 @die(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0))
br label %38
38: ; preds = %36, %33
%39 = load i8, i8* %7
%40 = sext i8 %39 to i32
switch i32 %40, label %92 [
i32 99, label %41
i32 103, label %46
i32 115, label %55
i32 100, label %72
i32 108, label %89
]
41: ; preds = %38
%42 = load %struct.Connection*, %struct.Connection** %8
%43 = call i32 @Database_create(%struct.Connection* %42)
%44 = load %struct.Connection*, %struct.Connection** %8
%45 = call i32 @Database_write(%struct.Connection* %44)
br label %94
46: ; preds = %38
%47 = load i32, i32* %4
%48 = icmp ne i32 %47, 4
br i1 %48, label %49, label %51
49: ; preds = %46
%50 = call i32 @die(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
br label %51
51: ; preds = %49, %46
%52 = load %struct.Connection*, %struct.Connection** %8
%53 = load i32, i32* %9
%54 = call i32 @Database_get(%struct.Connection* %52, i32 %53)
br label %94
55: ; preds = %38
%56 = load i32, i32* %4
%57 = icmp ne i32 %56, 6
br i1 %57, label %58, label %60
58: ; preds = %55
%59 = call i32 @die(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0))
br label %60
60: ; preds = %58, %55
%61 = load %struct.Connection*, %struct.Connection** %8
%62 = load i32, i32* %9
%63 = load i8**, i8*** %5
%64 = getelementptr inbounds i8*, i8** %63, i64 4
%65 = load i8*, i8** %64
%66 = load i8**, i8*** %5
%67 = getelementptr inbounds i8*, i8** %66, i64 5
%68 = load i8*, i8** %67
%69 = call i32 @Database_set(%struct.Connection* %61, i32 %62, i8* %65, i8* %68)
%70 = load %struct.Connection*, %struct.Connection** %8
%71 = call i32 @Database_write(%struct.Connection* %70)
br label %94
72: ; preds = %38
%73 = load i32, i32* %4
%74 = icmp ne i32 %73, 4
br i1 %74, label %75, label %77
75: ; preds = %72
%76 = call i32 @die(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0))
br label %77
77: ; preds = %75, %72
%78 = load %struct.Connection*, %struct.Connection** %8
%79 = load i32, i32* %9
%80 = load i8**, i8*** %5
%81 = getelementptr inbounds i8*, i8** %80, i64 4
%82 = load i8*, i8** %81
%83 = load i8**, i8*** %5
%84 = getelementptr inbounds i8*, i8** %83, i64 5
%85 = load i8*, i8** %84
%86 = call i32 @Database_set(%struct.Connection* %78, i32 %79, i8* %82, i8* %85)
%87 = load %struct.Connection*, %struct.Connection** %8
%88 = call i32 @Database_write(%struct.Connection* %87)
br label %94
89: ; preds = %38
%90 = load %struct.Connection*, %struct.Connection** %8
%91 = call i32 @Database_list(%struct.Connection* %90)
br label %94
92: ; preds = %38
%93 = call i32 @die(i8* getelementptr inbounds ([60 x i8], [60 x i8]* @.str.5, i64 0, i64 0))
br label %94
94: ; preds = %92, %89, %77, %60, %51, %41
%95 = load %struct.Connection*, %struct.Connection** %8
%96 = call i32 @Database_close(%struct.Connection* %95)
ret i32 0
}

LLVM IR (-O2):
%struct.Connection = type { i32 }
@.str = [46 x i8] c"USAGE: ex17 <dbfile> <action> [action params]\00"
@.str.1 = [31 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 = [60 x i8] c"Invalid action, only: c=create, g=get, s=set, d=del, l=list\00"
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 ([46 x i8], [46 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 %struct.Connection* @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 ([31 x i8], [31 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 %57 [
i32 99, label %24
i32 103, label %27
i32 115, label %33
i32 100, label %44
i32 108, label %55
]
24: ; preds = %21
%25 = tail call i32 @Database_create(%struct.Connection* %12)
%26 = tail call i32 @Database_write(%struct.Connection* %12)
br label %59
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(%struct.Connection* %12, i32 %22)
br label %59
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(%struct.Connection* %12, i32 %22, i8* %39, i8* %41)
%43 = tail call i32 @Database_write(%struct.Connection* %12)
br label %59
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 = getelementptr inbounds i8*, i8** %1, i64 4
%50 = load i8*, i8** %49
%51 = getelementptr inbounds i8*, i8** %1, i64 5
%52 = load i8*, i8** %51
%53 = tail call i32 @Database_set(%struct.Connection* %12, i32 %22, i8* %50, i8* %52)
%54 = tail call i32 @Database_write(%struct.Connection* %12)
br label %59
55: ; preds = %21
%56 = tail call i32 @Database_list(%struct.Connection* %12)
br label %59
57: ; preds = %21
%58 = tail call i32 @die(i8* getelementptr inbounds ([60 x i8], [60 x i8]* @.str.5, i64 0, i64 0))
br label %59
59: ; preds = %57, %55, %48, %37, %31, %24
%60 = tail call i32 @Database_close(%struct.Connection* %12)
ret i32 0
}