#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 atoi(char *);
int /*<<< orphan*/ databaseClose(struct connection *);
int /*<<< orphan*/ databaseCreate(struct connection *);
int /*<<< orphan*/ databaseDelete(struct connection *, int);
int /*<<< orphan*/ databaseGet(struct connection *, int);
int /*<<< orphan*/ databaseList(struct connection *);
struct connection *databaseOpen(char *, char);
int /*<<< orphan*/ databaseSet(struct connection *, int, char *, char *);
int /*<<< orphan*/ databaseWrite(struct connection *);
int /*<<< orphan*/ die(char *);
int main(int argc, char *argv[]) {
  if (argc < 3 || argv[1] == "--help")
    die("Usage: ex17 <dfile> <action> [action params]");
  char *filename = argv[1];
  char action = argv[2][0];
  struct connection *conn = databaseOpen(filename, action);
  int id = 0;
  if (argc > 3)
    id = atoi(argv[3]);
  if (id >= 100)
    die("Out of bounds");
  switch (action) {
  case 'c':
    databaseCreate(conn);
    databaseWrite(conn);
    break;
  case 'g':
    if (argc != 4)
      die("Need an id");
    databaseGet(conn, id);
    break;
  case 's':
    if (argc != 6)
      die("Need id, name, and email to set");
    databaseSet(conn, id, argv[4], argv[5]);
    databaseWrite(conn);
    break;
  case 'd':
    if (argc != 4)
      die("Need id to delete");
    databaseDelete(conn, id);
    databaseWrite(conn);
    break;
  case 'l':
    databaseList(conn);
    break;
  default:
    die("Invalid action, valid actions are: g=get, s=set, d=del, l=list");
  }
  databaseClose(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)
jl     11d5 <main+0x35>
mov    -0x10(%rbp),%rax
lea    0xe79(%rip),%rcx
cmp    %rcx,0x8(%rax)
jne    11e1 <main+0x41>
lea    0xe6f(%rip),%rdi
call   10a0 <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 <databaseOpen@plt>
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
cmpl   $0x3,-0x8(%rbp)
jle    122c <main+0x8c>
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
call   10b0 <atoi@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x64,-0x2c(%rbp)
jl     1242 <main+0xa2>
lea    0xe3b(%rip),%rdi
call   10a0 <die@plt>
movsbl -0x19(%rbp),%eax
add    $0xffffff9d,%eax
mov    %eax,%ecx
mov    %rcx,-0x38(%rbp)
sub    $0x10,%eax
ja     1328 <main+0x188>
mov    -0x38(%rbp),%rax
lea    0xd9d(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
mov    -0x28(%rbp),%rdi
call   1090 <databaseCreate@plt>
mov    -0x28(%rbp),%rdi
call   10c0 <databaseWrite@plt>
jmp    1334 <main+0x194>
cmpl   $0x4,-0x8(%rbp)
je     1299 <main+0xf9>
lea    0xdf2(%rip),%rdi
call   10a0 <die@plt>
mov    -0x28(%rbp),%rdi
mov    -0x2c(%rbp),%esi
call   1050 <databaseGet@plt>
jmp    1334 <main+0x194>
cmpl   $0x6,-0x8(%rbp)
je     12c0 <main+0x120>
lea    0xdd6(%rip),%rdi
call   10a0 <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   1080 <databaseSet@plt>
mov    -0x28(%rbp),%rdi
call   10c0 <databaseWrite@plt>
jmp    1334 <main+0x194>
cmpl   $0x4,-0x8(%rbp)
je     1300 <main+0x160>
lea    0xdb6(%rip),%rdi
call   10a0 <die@plt>
mov    -0x28(%rbp),%rdi
mov    -0x2c(%rbp),%esi
call   1040 <databaseDelete@plt>
mov    -0x28(%rbp),%rdi
call   10c0 <databaseWrite@plt>
jmp    1334 <main+0x194>
mov    -0x28(%rbp),%rdi
call   1030 <databaseList@plt>
jmp    1334 <main+0x194>
lea    0xd94(%rip),%rdi
call   10a0 <die@plt>
mov    -0x28(%rbp),%rdi
call   1070 <databaseClose@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,%rbx
mov    %edi,%r12d
cmp    $0x3,%edi
jl     11c3 <main+0x23>
mov    0x8(%rbx),%rdi
lea    0xe86(%rip),%rax
cmp    %rax,%rdi
jne    11d3 <main+0x33>
lea    0xe81(%rip),%rdi
call   10a0 <die@plt>
mov    0x8(%rbx),%rdi
mov    0x10(%rbx),%rax
movsbl (%rax),%ebp
mov    %ebp,%esi
call   1060 <databaseOpen@plt>
mov    %rax,%r14
xor    %r15d,%r15d
cmp    $0x4,%r12d
jl     120a <main+0x6a>
mov    0x18(%rbx),%rdi
call   10b0 <atoi@plt>
mov    %eax,%r15d
cmp    $0x64,%eax
jl     120a <main+0x6a>
lea    0xe73(%rip),%rdi
call   10a0 <die@plt>
add    $0xffffff9d,%ebp
cmp    $0x10,%ebp
ja     124b <main+0xab>
lea    0xde7(%rip),%rax
movslq (%rax,%rbp,4),%rcx
add    %rax,%rcx
jmp    *%rcx
mov    %r14,%rdi
call   1090 <databaseCreate@plt>
jmp    12a7 <main+0x107>
cmp    $0x4,%r12d
je     123e <main+0x9e>
lea    0xe78(%rip),%rdi
call   10a0 <die@plt>
mov    %r14,%rdi
mov    %r15d,%esi
call   1040 <databaseDelete@plt>
jmp    12a7 <main+0x107>
lea    0xe71(%rip),%rdi
call   10a0 <die@plt>
jmp    12af <main+0x10f>
cmp    $0x4,%r12d
je     126b <main+0xcb>
lea    0xe20(%rip),%rdi
call   10a0 <die@plt>
mov    %r14,%rdi
mov    %r15d,%esi
call   1050 <databaseGet@plt>
jmp    12af <main+0x10f>
mov    %r14,%rdi
call   1030 <databaseList@plt>
jmp    12af <main+0x10f>
cmp    $0x6,%r12d
je     1294 <main+0xf4>
lea    0xe02(%rip),%rdi
call   10a0 <die@plt>
mov    0x20(%rbx),%rdx
mov    0x28(%rbx),%rcx
mov    %r14,%rdi
mov    %r15d,%esi
call   1080 <databaseSet@plt>
mov    %r14,%rdi
call   10c0 <databaseWrite@plt>
mov    %r14,%rdi
call   1070 <databaseClose@plt>
xor    %eax,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.connection = type { i32 }
@.str = [7 x i8] c"--help\00"
@.str.1 = [45 x i8] c"Usage: ex17 <dfile> <action> [action params]\00"
@.str.2 = [14 x i8] c"Out of bounds\00"
@.str.3 = [11 x i8] c"Need an id\00"
@.str.4 = [32 x i8] c"Need id, name, and email to set\00"
@.str.5 = [18 x i8] c"Need id to delete\00"
@.str.6 = [63 x i8] c"Invalid action, valid actions are: 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 %17, label %12
12: ; preds = %2
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 1
%15 = load i8*, i8** %14
%16 = icmp eq i8* %15, getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0)
br i1 %16, label %17, label %19
17: ; preds = %12, %2
%18 = call i32 @die(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0))
br label %19
19: ; preds = %17, %12
%20 = load i8**, i8*** %5
%21 = getelementptr inbounds i8*, i8** %20, i64 1
%22 = load i8*, i8** %21
store i8* %22, i8** %6
%23 = load i8**, i8*** %5
%24 = getelementptr inbounds i8*, i8** %23, i64 2
%25 = load i8*, i8** %24
%26 = getelementptr inbounds i8, i8* %25, i64 0
%27 = load i8, i8* %26
store i8 %27, i8* %7
%28 = load i8*, i8** %6
%29 = load i8, i8* %7
%30 = call %struct.connection* @databaseOpen(i8* %28, i8 signext %29)
store %struct.connection* %30, %struct.connection** %8
store i32 0, i32* %9
%31 = load i32, i32* %4
%32 = icmp sgt i32 %31, 3
br i1 %32, label %33, label %38
33: ; preds = %19
%34 = load i8**, i8*** %5
%35 = getelementptr inbounds i8*, i8** %34, i64 3
%36 = load i8*, i8** %35
%37 = call i32 @atoi(i8* %36)
store i32 %37, i32* %9
br label %38
38: ; preds = %33, %19
%39 = load i32, i32* %9
%40 = icmp sge i32 %39, 100
br i1 %40, label %41, label %43
41: ; preds = %38
%42 = call i32 @die(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
br label %43
43: ; preds = %41, %38
%44 = load i8, i8* %7
%45 = sext i8 %44 to i32
switch i32 %45, label %91 [
i32 99, label %46
i32 103, label %51
i32 115, label %60
i32 100, label %77
i32 108, label %88
]
46: ; preds = %43
%47 = load %struct.connection*, %struct.connection** %8
%48 = call i32 @databaseCreate(%struct.connection* %47)
%49 = load %struct.connection*, %struct.connection** %8
%50 = call i32 @databaseWrite(%struct.connection* %49)
br label %93
51: ; preds = %43
%52 = load i32, i32* %4
%53 = icmp ne i32 %52, 4
br i1 %53, label %54, label %56
54: ; preds = %51
%55 = call i32 @die(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
br label %56
56: ; preds = %54, %51
%57 = load %struct.connection*, %struct.connection** %8
%58 = load i32, i32* %9
%59 = call i32 @databaseGet(%struct.connection* %57, i32 %58)
br label %93
60: ; preds = %43
%61 = load i32, i32* %4
%62 = icmp ne i32 %61, 6
br i1 %62, label %63, label %65
63: ; preds = %60
%64 = call i32 @die(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.4, i64 0, i64 0))
br label %65
65: ; preds = %63, %60
%66 = load %struct.connection*, %struct.connection** %8
%67 = load i32, i32* %9
%68 = load i8**, i8*** %5
%69 = getelementptr inbounds i8*, i8** %68, i64 4
%70 = load i8*, i8** %69
%71 = load i8**, i8*** %5
%72 = getelementptr inbounds i8*, i8** %71, i64 5
%73 = load i8*, i8** %72
%74 = call i32 @databaseSet(%struct.connection* %66, i32 %67, i8* %70, i8* %73)
%75 = load %struct.connection*, %struct.connection** %8
%76 = call i32 @databaseWrite(%struct.connection* %75)
br label %93
77: ; preds = %43
%78 = load i32, i32* %4
%79 = icmp ne i32 %78, 4
br i1 %79, label %80, label %82
80: ; preds = %77
%81 = call i32 @die(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.5, i64 0, i64 0))
br label %82
82: ; preds = %80, %77
%83 = load %struct.connection*, %struct.connection** %8
%84 = load i32, i32* %9
%85 = call i32 @databaseDelete(%struct.connection* %83, i32 %84)
%86 = load %struct.connection*, %struct.connection** %8
%87 = call i32 @databaseWrite(%struct.connection* %86)
br label %93
88: ; preds = %43
%89 = load %struct.connection*, %struct.connection** %8
%90 = call i32 @databaseList(%struct.connection* %89)
br label %93
91: ; preds = %43
%92 = call i32 @die(i8* getelementptr inbounds ([63 x i8], [63 x i8]* @.str.6, i64 0, i64 0))
br label %93
93: ; preds = %91, %88, %82, %65, %56, %46
%94 = load %struct.connection*, %struct.connection** %8
%95 = call i32 @databaseClose(%struct.connection* %94)
ret i32 0
}

LLVM IR (-O2):
%struct.connection = type { i32 }
@.str = [7 x i8] c"--help\00"
@.str.1 = [45 x i8] c"Usage: ex17 <dfile> <action> [action params]\00"
@.str.2 = [14 x i8] c"Out of bounds\00"
@.str.3 = [11 x i8] c"Need an id\00"
@.str.4 = [32 x i8] c"Need id, name, and email to set\00"
@.str.5 = [18 x i8] c"Need id to delete\00"
@.str.6 = [63 x i8] c"Invalid action, valid actions are: 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 %8, label %4
4: ; preds = %2
%5 = getelementptr inbounds i8*, i8** %1, i64 1
%6 = load i8*, i8** %5
%7 = icmp eq i8* %6, getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0)
br i1 %7, label %8, label %12
8: ; preds = %4, %2
%9 = tail call i32 @die(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0))
%10 = getelementptr inbounds i8*, i8** %1, i64 1
%11 = load i8*, i8** %10
br label %12
12: ; preds = %8, %4
%13 = phi i8* [ %11, %8 ], [ %6, %4 ]
%14 = getelementptr inbounds i8*, i8** %1, i64 2
%15 = load i8*, i8** %14
%16 = load i8, i8* %15
%17 = tail call %struct.connection* @databaseOpen(i8* %13, i8 signext %16)
%18 = icmp sgt i32 %0, 3
br i1 %18, label %19, label %26
19: ; preds = %12
%20 = getelementptr inbounds i8*, i8** %1, i64 3
%21 = load i8*, i8** %20
%22 = tail call i32 @atoi(i8* %21)
%23 = icmp sgt i32 %22, 99
br i1 %23, label %24, label %26
24: ; preds = %19
%25 = tail call i32 @die(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
br label %26
26: ; preds = %12, %24, %19
%27 = phi i32 [ %22, %24 ], [ %22, %19 ], [ 0, %12 ]
%28 = sext i8 %16 to i32
switch i32 %28, label %58 [
i32 99, label %29
i32 103, label %32
i32 115, label %38
i32 100, label %49
i32 108, label %56
]
29: ; preds = %26
%30 = tail call i32 @databaseCreate(%struct.connection* %17)
%31 = tail call i32 @databaseWrite(%struct.connection* %17)
br label %60
32: ; preds = %26
%33 = icmp eq i32 %0, 4
br i1 %33, label %36, label %34
34: ; preds = %32
%35 = tail call i32 @die(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0))
br label %36
36: ; preds = %34, %32
%37 = tail call i32 @databaseGet(%struct.connection* %17, i32 %27)
br label %60
38: ; preds = %26
%39 = icmp eq i32 %0, 6
br i1 %39, label %42, label %40
40: ; preds = %38
%41 = tail call i32 @die(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.4, i64 0, i64 0))
br label %42
42: ; preds = %40, %38
%43 = getelementptr inbounds i8*, i8** %1, i64 4
%44 = load i8*, i8** %43
%45 = getelementptr inbounds i8*, i8** %1, i64 5
%46 = load i8*, i8** %45
%47 = tail call i32 @databaseSet(%struct.connection* %17, i32 %27, i8* %44, i8* %46)
%48 = tail call i32 @databaseWrite(%struct.connection* %17)
br label %60
49: ; preds = %26
%50 = icmp eq i32 %0, 4
br i1 %50, label %53, label %51
51: ; preds = %49
%52 = tail call i32 @die(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.5, i64 0, i64 0))
br label %53
53: ; preds = %51, %49
%54 = tail call i32 @databaseDelete(%struct.connection* %17, i32 %27)
%55 = tail call i32 @databaseWrite(%struct.connection* %17)
br label %60
56: ; preds = %26
%57 = tail call i32 @databaseList(%struct.connection* %17)
br label %60
58: ; preds = %26
%59 = tail call i32 @die(i8* getelementptr inbounds ([63 x i8], [63 x i8]* @.str.6, i64 0, i64 0))
br label %60
60: ; preds = %58, %56, %53, %42, %36, %29
%61 = tail call i32 @databaseClose(%struct.connection* %17)
ret i32 0
}