#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_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_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    11cc <main+0x2c>
lea    0xe7d(%rip),%rdi
call   1080 <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   10b0 <Database_open@plt>
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
cmpl   $0x3,-0x8(%rbp)
jle    1217 <main+0x77>
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
call   1090 <atoi@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x64,-0x2c(%rbp)
jl     122d <main+0x8d>
lea    0xe4a(%rip),%rdi
call   1080 <die@plt>
movsbl -0x19(%rbp),%eax
add    $0xffffff9d,%eax
mov    %eax,%ecx
mov    %rcx,-0x38(%rbp)
sub    $0x10,%eax
ja     1313 <main+0x173>
mov    -0x38(%rbp),%rax
lea    0xdb2(%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+0x17f>
cmpl   $0x4,-0x8(%rbp)
je     1284 <main+0xe4>
lea    0xe12(%rip),%rdi
call   1080 <die@plt>
mov    -0x28(%rbp),%rdi
mov    -0x2c(%rbp),%esi
call   10a0 <Database_get@plt>
jmp    131f <main+0x17f>
cmpl   $0x6,-0x8(%rbp)
je     12ab <main+0x10b>
lea    0xdfd(%rip),%rdi
call   1080 <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   1070 <Database_set@plt>
mov    -0x28(%rbp),%rdi
call   1030 <Database_write@plt>
jmp    131f <main+0x17f>
cmpl   $0x4,-0x8(%rbp)
je     12eb <main+0x14b>
lea    0xdd9(%rip),%rdi
call   1080 <die@plt>
mov    -0x28(%rbp),%rdi
mov    -0x2c(%rbp),%esi
call   1060 <Database_delete@plt>
mov    -0x28(%rbp),%rdi
call   1030 <Database_write@plt>
jmp    131f <main+0x17f>
mov    -0x28(%rbp),%rdi
call   1040 <Database_list@plt>
jmp    131f <main+0x17f>
lea    0xdb7(%rip),%rdi
call   1080 <die@plt>
mov    -0x28(%rbp),%rdi
call   10c0 <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     11be <main+0x1e>
lea    0xe8b(%rip),%rdi
call   1080 <die@plt>
mov    0x8(%r12),%rdi
mov    0x10(%r12),%rax
movsbl (%rax),%ebx
mov    %ebx,%esi
call   10b0 <Database_open@plt>
mov    %rax,%r14
xor    %r15d,%r15d
cmp    $0x4,%ebp
jl     11fb <main+0x5b>
mov    0x18(%r12),%rdi
call   1090 <atoi@plt>
mov    %eax,%r15d
cmp    $0x64,%eax
jl     11fb <main+0x5b>
lea    0xe7c(%rip),%rdi
call   1080 <die@plt>
add    $0xffffff9d,%ebx
cmp    $0x10,%ebx
ja     123b <main+0x9b>
lea    0xdf6(%rip),%rax
movslq (%rax,%rbx,4),%rcx
add    %rax,%rcx
jmp    *%rcx
mov    %r14,%rdi
call   1050 <Database_create@plt>
jmp    1297 <main+0xf7>
cmp    $0x4,%ebp
je     122e <main+0x8e>
lea    0xe96(%rip),%rdi
call   1080 <die@plt>
mov    %r14,%rdi
mov    %r15d,%esi
call   1060 <Database_delete@plt>
jmp    1297 <main+0xf7>
lea    0xe8f(%rip),%rdi
call   1080 <die@plt>
jmp    129f <main+0xff>
cmp    $0x4,%ebp
je     125a <main+0xba>
lea    0xe3c(%rip),%rdi
call   1080 <die@plt>
mov    %r14,%rdi
mov    %r15d,%esi
call   10a0 <Database_get@plt>
jmp    129f <main+0xff>
mov    %r14,%rdi
call   1040 <Database_list@plt>
jmp    129f <main+0xff>
cmp    $0x6,%ebp
je     1282 <main+0xe2>
lea    0xe26(%rip),%rdi
call   1080 <die@plt>
mov    0x20(%r12),%rdx
mov    0x28(%r12),%rcx
mov    %r14,%rdi
mov    %r15d,%esi
call   1070 <Database_set@plt>
mov    %r14,%rdi
call   1030 <Database_write@plt>
mov    %r14,%rdi
call   10c0 <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 = [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 %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 %86 [
i32 99, label %41
i32 103, label %46
i32 115, label %55
i32 100, label %72
i32 108, label %83
]
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 %88
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 %88
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 %88
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 = call i32 @Database_delete(%struct.Connection* %78, i32 %79)
%81 = load %struct.Connection*, %struct.Connection** %8
%82 = call i32 @Database_write(%struct.Connection* %81)
br label %88
83: ; preds = %38
%84 = load %struct.Connection*, %struct.Connection** %8
%85 = call i32 @Database_list(%struct.Connection* %84)
br label %88
86: ; preds = %38
%87 = call i32 @die(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.5, i64 0, i64 0))
br label %88
88: ; preds = %86, %83, %77, %60, %51, %41
%89 = load %struct.Connection*, %struct.Connection** %8
%90 = call i32 @Database_close(%struct.Connection* %89)
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 = [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 %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 %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(%struct.Connection* %12)
%26 = tail call i32 @Database_write(%struct.Connection* %12)
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(%struct.Connection* %12, 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(%struct.Connection* %12, i32 %22, i8* %39, i8* %41)
%43 = tail call i32 @Database_write(%struct.Connection* %12)
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(%struct.Connection* %12, i32 %22)
%50 = tail call i32 @Database_write(%struct.Connection* %12)
br label %55
51: ; preds = %21
%52 = tail call i32 @Database_list(%struct.Connection* %12)
br label %55
53: ; preds = %21
%54 = tail call i32 @die(i8* getelementptr inbounds ([54 x i8], [54 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(%struct.Connection* %12)
ret i32 0
}