#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_delete(struct Connection *, int);
int /*<<< orphan*/ Database_find(struct Connection *, char *);
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];
  char *name = {0};
  struct Connection *conn = Database_open(filename, action);
  int id = 0;
  if (argc > 3)
    id = atoi(argv[3]);
  if (argc > 3 && action == 'f')
    name = 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 'f':
    if (argc != 4)
      die("Need a name to search for");
    Database_find(conn, name);
    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_delete(conn, id);
    Database_write(conn);
    break;
  case 'l':
    Database_list(conn);
    break;
  default:
    die("Invalid action: 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    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)
movq   $0x0,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
movsbl -0x19(%rbp),%esi
call   10c0 <Database_open@plt>
mov    %rax,-0x30(%rbp)
movl   $0x0,-0x34(%rbp)
cmpl   $0x3,-0x8(%rbp)
jle    122f <main+0x7f>
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
call   10a0 <atoi@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x3,-0x8(%rbp)
jle    1252 <main+0xa2>
movsbl -0x19(%rbp),%eax
cmp    $0x66,%eax
jne    1252 <main+0xa2>
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x28(%rbp)
cmpl   $0x64,-0x34(%rbp)
jl     1268 <main+0xb8>
lea    0xe0f(%rip),%rdi
call   1090 <die@plt>
movsbl -0x19(%rbp),%eax
add    $0xffffff9d,%eax
mov    %eax,%ecx
mov    %rcx,-0x40(%rbp)
sub    $0x10,%eax
ja     1376 <main+0x1c6>
mov    -0x40(%rbp),%rax
lea    0xd77(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
mov    -0x30(%rbp),%rdi
call   1060 <Database_create@plt>
mov    -0x30(%rbp),%rdi
call   1030 <Database_write@plt>
jmp    1382 <main+0x1d2>
cmpl   $0x4,-0x8(%rbp)
je     12bf <main+0x10f>
lea    0xdd7(%rip),%rdi
call   1090 <die@plt>
mov    -0x30(%rbp),%rdi
mov    -0x34(%rbp),%esi
call   10b0 <Database_get@plt>
jmp    1382 <main+0x1d2>
cmpl   $0x4,-0x8(%rbp)
je     12e6 <main+0x136>
lea    0xdc2(%rip),%rdi
call   1090 <die@plt>
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1050 <Database_find@plt>
jmp    1382 <main+0x1d2>
cmpl   $0x6,-0x8(%rbp)
je     130e <main+0x15e>
lea    0xdb4(%rip),%rdi
call   1090 <die@plt>
mov    -0x30(%rbp),%rdi
mov    -0x34(%rbp),%esi
mov    -0x10(%rbp),%rax
mov    0x20(%rax),%rdx
mov    -0x10(%rbp),%rax
mov    0x28(%rax),%rcx
call   1080 <Database_set@plt>
mov    -0x30(%rbp),%rdi
call   1030 <Database_write@plt>
jmp    1382 <main+0x1d2>
cmpl   $0x4,-0x8(%rbp)
je     134e <main+0x19e>
lea    0xd90(%rip),%rdi
call   1090 <die@plt>
mov    -0x30(%rbp),%rdi
mov    -0x34(%rbp),%esi
call   1070 <Database_delete@plt>
mov    -0x30(%rbp),%rdi
call   1030 <Database_write@plt>
jmp    1382 <main+0x1d2>
mov    -0x30(%rbp),%rdi
call   1040 <Database_list@plt>
jmp    1382 <main+0x1d2>
lea    0xd6e(%rip),%rdi
call   1090 <die@plt>
mov    -0x30(%rbp),%rdi
call   10d0 <Database_close@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r12
mov    %edi,%ebp
cmp    $0x2,%edi
jg     11d1 <main+0x21>
lea    0xe78(%rip),%rdi
call   1090 <die@plt>
mov    0x8(%r12),%rdi
mov    0x10(%r12),%rax
movsbl (%rax),%ebx
mov    %ebx,%esi
call   10c0 <Database_open@plt>
mov    %rax,%r15
cmp    $0x4,%ebp
jl     121f <main+0x6f>
mov    0x18(%r12),%r14
mov    %r14,%rdi
call   10a0 <atoi@plt>
mov    %eax,%esi
xor    %r13d,%r13d
cmp    $0x66,%bl
cmove  %r14,%r13
cmp    $0x64,%eax
jl     1224 <main+0x74>
lea    0xe60(%rip),%rdi
mov    %esi,%r14d
call   1090 <die@plt>
mov    %r14d,%esi
jmp    1224 <main+0x74>
xor    %r13d,%r13d
xor    %esi,%esi
add    $0xffffff9d,%ebx
cmp    $0x10,%ebx
ja     1268 <main+0xb8>
lea    0xdcd(%rip),%rax
movslq (%rax,%rbx,4),%rcx
add    %rax,%rcx
jmp    *%rcx
mov    %r15,%rdi
call   1060 <Database_create@plt>
jmp    12da <main+0x12a>
cmp    $0x4,%ebp
je     125e <main+0xae>
lea    0xe84(%rip),%rdi
mov    %esi,%ebx
call   1090 <die@plt>
mov    %ebx,%esi
mov    %r15,%rdi
call   1070 <Database_delete@plt>
jmp    12da <main+0x12a>
lea    0xe7c(%rip),%rdi
call   1090 <die@plt>
jmp    12ec <main+0x13c>
cmp    $0x4,%ebp
je     1287 <main+0xd7>
lea    0xe21(%rip),%rdi
call   1090 <die@plt>
mov    %r15,%rdi
mov    %r13,%rsi
call   1050 <Database_find@plt>
jmp    12ec <main+0x13c>
cmp    $0x4,%ebp
je     12a9 <main+0xf9>
lea    0xdf1(%rip),%rdi
mov    %esi,%ebx
call   1090 <die@plt>
mov    %ebx,%esi
mov    %r15,%rdi
call   10b0 <Database_get@plt>
jmp    12ec <main+0x13c>
cmp    $0x6,%ebp
je     12c8 <main+0x118>
lea    0xdfe(%rip),%rdi
mov    %esi,%ebx
call   1090 <die@plt>
mov    %ebx,%esi
mov    0x20(%r12),%rdx
mov    0x28(%r12),%rcx
mov    %r15,%rdi
call   1080 <Database_set@plt>
mov    %r15,%rdi
call   1030 <Database_write@plt>
jmp    12ec <main+0x13c>
mov    %r15,%rdi
call   1040 <Database_list@plt>
mov    %r15,%rdi
call   10d0 <Database_close@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
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 = [26 x i8] c"Need a name to search for\00"
@.str.4 = [28 x i8] c"Need id, name, email to set\00"
@.str.5 = [18 x i8] c"Need id to delete\00"
@.str.6 = [54 x i8] c"Invalid action: 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 i8*
%9 = alloca %struct.Connection*
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* %4
%12 = icmp slt i32 %11, 3
br i1 %12, label %13, label %15
13: ; preds = %2
%14 = call i32 @die(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str, i64 0, i64 0))
br label %15
15: ; preds = %13, %2
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 1
%18 = load i8*, i8** %17
store i8* %18, i8** %6
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 2
%21 = load i8*, i8** %20
%22 = getelementptr inbounds i8, i8* %21, i64 0
%23 = load i8, i8* %22
store i8 %23, i8* %7
store i8* null, i8** %8
%24 = load i8*, i8** %6
%25 = load i8, i8* %7
%26 = call %struct.Connection* @Database_open(i8* %24, i8 signext %25)
store %struct.Connection* %26, %struct.Connection** %9
store i32 0, i32* %10
%27 = load i32, i32* %4
%28 = icmp sgt i32 %27, 3
br i1 %28, label %29, label %34
29: ; preds = %15
%30 = load i8**, i8*** %5
%31 = getelementptr inbounds i8*, i8** %30, i64 3
%32 = load i8*, i8** %31
%33 = call i32 @atoi(i8* %32)
store i32 %33, i32* %10
br label %34
34: ; preds = %29, %15
%35 = load i32, i32* %4
%36 = icmp sgt i32 %35, 3
br i1 %36, label %37, label %45
37: ; preds = %34
%38 = load i8, i8* %7
%39 = sext i8 %38 to i32
%40 = icmp eq i32 %39, 102
br i1 %40, label %41, label %45
41: ; preds = %37
%42 = load i8**, i8*** %5
%43 = getelementptr inbounds i8*, i8** %42, i64 3
%44 = load i8*, i8** %43
store i8* %44, i8** %8
br label %45
45: ; preds = %41, %37, %34
%46 = load i32, i32* %10
%47 = icmp sge i32 %46, 100
br i1 %47, label %48, label %50
48: ; preds = %45
%49 = call i32 @die(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0))
br label %50
50: ; preds = %48, %45
%51 = load i8, i8* %7
%52 = sext i8 %51 to i32
switch i32 %52, label %107 [
i32 99, label %53
i32 103, label %58
i32 102, label %67
i32 115, label %76
i32 100, label %93
i32 108, label %104
]
53: ; preds = %50
%54 = load %struct.Connection*, %struct.Connection** %9
%55 = call i32 @Database_create(%struct.Connection* %54)
%56 = load %struct.Connection*, %struct.Connection** %9
%57 = call i32 @Database_write(%struct.Connection* %56)
br label %109
58: ; preds = %50
%59 = load i32, i32* %4
%60 = icmp ne i32 %59, 4
br i1 %60, label %61, label %63
61: ; preds = %58
%62 = call i32 @die(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
br label %63
63: ; preds = %61, %58
%64 = load %struct.Connection*, %struct.Connection** %9
%65 = load i32, i32* %10
%66 = call i32 @Database_get(%struct.Connection* %64, i32 %65)
br label %109
67: ; preds = %50
%68 = load i32, i32* %4
%69 = icmp ne i32 %68, 4
br i1 %69, label %70, label %72
70: ; preds = %67
%71 = call i32 @die(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0))
br label %72
72: ; preds = %70, %67
%73 = load %struct.Connection*, %struct.Connection** %9
%74 = load i8*, i8** %8
%75 = call i32 @Database_find(%struct.Connection* %73, i8* %74)
br label %109
76: ; preds = %50
%77 = load i32, i32* %4
%78 = icmp ne i32 %77, 6
br i1 %78, label %79, label %81
79: ; preds = %76
%80 = call i32 @die(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0))
br label %81
81: ; preds = %79, %76
%82 = load %struct.Connection*, %struct.Connection** %9
%83 = load i32, i32* %10
%84 = load i8**, i8*** %5
%85 = getelementptr inbounds i8*, i8** %84, i64 4
%86 = load i8*, i8** %85
%87 = load i8**, i8*** %5
%88 = getelementptr inbounds i8*, i8** %87, i64 5
%89 = load i8*, i8** %88
%90 = call i32 @Database_set(%struct.Connection* %82, i32 %83, i8* %86, i8* %89)
%91 = load %struct.Connection*, %struct.Connection** %9
%92 = call i32 @Database_write(%struct.Connection* %91)
br label %109
93: ; preds = %50
%94 = load i32, i32* %4
%95 = icmp ne i32 %94, 4
br i1 %95, label %96, label %98
96: ; preds = %93
%97 = call i32 @die(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.5, i64 0, i64 0))
br label %98
98: ; preds = %96, %93
%99 = load %struct.Connection*, %struct.Connection** %9
%100 = load i32, i32* %10
%101 = call i32 @Database_delete(%struct.Connection* %99, i32 %100)
%102 = load %struct.Connection*, %struct.Connection** %9
%103 = call i32 @Database_write(%struct.Connection* %102)
br label %109
104: ; preds = %50
%105 = load %struct.Connection*, %struct.Connection** %9
%106 = call i32 @Database_list(%struct.Connection* %105)
br label %109
107: ; preds = %50
%108 = call i32 @die(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.6, i64 0, i64 0))
br label %109
109: ; preds = %107, %104, %98, %81, %72, %63, %53
%110 = load %struct.Connection*, %struct.Connection** %9
%111 = call i32 @Database_close(%struct.Connection* %110)
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 = [26 x i8] c"Need a name to search for\00"
@.str.4 = [28 x i8] c"Need id, name, email to set\00"
@.str.5 = [18 x i8] c"Need id to delete\00"
@.str.6 = [54 x i8] c"Invalid action: 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 %23
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 eq i8 %11, 102
%19 = select i1 %18, i8* %16, i8* null
%20 = icmp sgt i32 %17, 99
br i1 %20, label %21, label %23
21: ; preds = %14
%22 = tail call i32 @die(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0))
br label %23
23: ; preds = %6, %21, %14
%24 = phi i8* [ %19, %21 ], [ %19, %14 ], [ null, %6 ]
%25 = phi i32 [ %17, %21 ], [ %17, %14 ], [ 0, %6 ]
%26 = sext i8 %11 to i32
switch i32 %26, label %62 [
i32 99, label %27
i32 103, label %30
i32 102, label %36
i32 115, label %42
i32 100, label %53
i32 108, label %60
]
27: ; preds = %23
%28 = tail call i32 @Database_create(%struct.Connection* %12)
%29 = tail call i32 @Database_write(%struct.Connection* %12)
br label %64
30: ; preds = %23
%31 = icmp eq i32 %0, 4
br i1 %31, label %34, label %32
32: ; preds = %30
%33 = tail call i32 @die(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
br label %34
34: ; preds = %32, %30
%35 = tail call i32 @Database_get(%struct.Connection* %12, i32 %25)
br label %64
36: ; preds = %23
%37 = icmp eq i32 %0, 4
br i1 %37, label %40, label %38
38: ; preds = %36
%39 = tail call i32 @die(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0))
br label %40
40: ; preds = %38, %36
%41 = tail call i32 @Database_find(%struct.Connection* %12, i8* %24)
br label %64
42: ; preds = %23
%43 = icmp eq i32 %0, 6
br i1 %43, label %46, label %44
44: ; preds = %42
%45 = tail call i32 @die(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0))
br label %46
46: ; preds = %44, %42
%47 = getelementptr inbounds i8*, i8** %1, i64 4
%48 = load i8*, i8** %47
%49 = getelementptr inbounds i8*, i8** %1, i64 5
%50 = load i8*, i8** %49
%51 = tail call i32 @Database_set(%struct.Connection* %12, i32 %25, i8* %48, i8* %50)
%52 = tail call i32 @Database_write(%struct.Connection* %12)
br label %64
53: ; preds = %23
%54 = icmp eq i32 %0, 4
br i1 %54, label %57, label %55
55: ; preds = %53
%56 = tail call i32 @die(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.5, i64 0, i64 0))
br label %57
57: ; preds = %55, %53
%58 = tail call i32 @Database_delete(%struct.Connection* %12, i32 %25)
%59 = tail call i32 @Database_write(%struct.Connection* %12)
br label %64
60: ; preds = %23
%61 = tail call i32 @Database_list(%struct.Connection* %12)
br label %64
62: ; preds = %23
%63 = tail call i32 @die(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.6, i64 0, i64 0))
br label %64
64: ; preds = %62, %60, %57, %46, %40, %34, %27
%65 = tail call i32 @Database_close(%struct.Connection* %12)
ret i32 0
}