#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 *, struct Connection *);
int main(int argc, char *argv[]) {
  if (argc < 3)
    die("USAGE: ex17 <dbfile> <action> [action params]", NULL);
  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.", conn);
  switch (action) {
  case 'c':
    Database_create(conn);
    Database_write(conn);
    break;
  case 'g':
    if (argc != 4)
      die("Need an id to get", conn);
    Database_get(conn, id);
    break;
  case 's':
    if (argc != 6)
      die("Need id, name, email to set", conn);
    Database_set(conn, id, argv[4], argv[5]);
    Database_write(conn);
    break;
  case 'd':
    if (argc != 4)
      die("Need id to delete", conn);
    Database_delete(conn, id);
    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", conn);
  }
  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    11d0 <main+0x30>
lea    0xe7d(%rip),%rdi
xor    %eax,%eax
mov    %eax,%esi
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    121b <main+0x7b>
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
call   1090 <atoi@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x64,-0x2c(%rbp)
jl     1235 <main+0x95>
mov    -0x28(%rbp),%rsi
lea    0xe42(%rip),%rdi
call   1080 <die@plt>
movsbl -0x19(%rbp),%eax
add    $0xffffff9d,%eax
mov    %eax,%ecx
mov    %rcx,-0x38(%rbp)
sub    $0x10,%eax
ja     1327 <main+0x187>
mov    -0x38(%rbp),%rax
lea    0xdaa(%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    1337 <main+0x197>
cmpl   $0x4,-0x8(%rbp)
je     1290 <main+0xf0>
mov    -0x28(%rbp),%rsi
lea    0xe06(%rip),%rdi
call   1080 <die@plt>
mov    -0x28(%rbp),%rdi
mov    -0x2c(%rbp),%esi
call   10a0 <Database_get@plt>
jmp    1337 <main+0x197>
cmpl   $0x6,-0x8(%rbp)
je     12bb <main+0x11b>
mov    -0x28(%rbp),%rsi
lea    0xded(%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    1337 <main+0x197>
cmpl   $0x4,-0x8(%rbp)
je     12ff <main+0x15f>
mov    -0x28(%rbp),%rsi
lea    0xdc5(%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    1337 <main+0x197>
mov    -0x28(%rbp),%rdi
call   1040 <Database_list@plt>
jmp    1337 <main+0x197>
mov    -0x28(%rbp),%rsi
lea    0xd9f(%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,%ebx
cmp    $0x2,%edi
jg     11c0 <main+0x20>
lea    0xe8b(%rip),%rdi
xor    %esi,%esi
call   1080 <die@plt>
mov    0x8(%r12),%rdi
mov    0x10(%r12),%rax
movsbl (%rax),%ebp
mov    %ebp,%esi
call   10b0 <Database_open@plt>
mov    %rax,%r15
xor    %r14d,%r14d
cmp    $0x4,%ebx
jl     1200 <main+0x60>
mov    0x18(%r12),%rdi
call   1090 <atoi@plt>
mov    %eax,%r14d
cmp    $0x64,%eax
jl     1200 <main+0x60>
lea    0xe7a(%rip),%rdi
mov    %r15,%rsi
call   1080 <die@plt>
add    $0xffffff9d,%ebp
cmp    $0x10,%ebp
ja     1246 <main+0xa6>
lea    0xdf1(%rip),%rax
movslq (%rax,%rbp,4),%rcx
add    %rax,%rcx
jmp    *%rcx
mov    %r15,%rdi
call   1050 <Database_create@plt>
jmp    12ab <main+0x10b>
cmp    $0x4,%ebx
je     1239 <main+0x99>
lea    0xe8e(%rip),%rdi
mov    %r15,%rsi
call   1080 <die@plt>
mov    %r15,%rdi
mov    %r14d,%esi
call   1060 <Database_delete@plt>
jmp    12ab <main+0x10b>
lea    0xe84(%rip),%rdi
mov    %r15,%rsi
call   1080 <die@plt>
jmp    12b3 <main+0x113>
cmp    $0x4,%ebx
je     126b <main+0xcb>
lea    0xe2e(%rip),%rdi
mov    %r15,%rsi
call   1080 <die@plt>
mov    %r15,%rdi
mov    %r14d,%esi
call   10a0 <Database_get@plt>
jmp    12b3 <main+0x113>
mov    %r15,%rdi
call   1040 <Database_list@plt>
jmp    12b3 <main+0x113>
cmp    $0x6,%ebx
je     1296 <main+0xf6>
lea    0xe15(%rip),%rdi
mov    %r15,%rsi
call   1080 <die@plt>
mov    0x20(%r12),%rdx
mov    0x28(%r12),%rcx
mov    %r15,%rdi
mov    %r14d,%esi
call   1070 <Database_set@plt>
mov    %r15,%rdi
call   1030 <Database_write@plt>
mov    %r15,%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 = [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), %struct.Connection* null)
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 %39
36: ; preds = %33
%37 = load %struct.Connection*, %struct.Connection** %8
%38 = call i32 @die(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), %struct.Connection* %37)
br label %39
39: ; preds = %36, %33
%40 = load i8, i8* %7
%41 = sext i8 %40 to i32
switch i32 %41, label %90 [
i32 99, label %42
i32 103, label %47
i32 115, label %57
i32 100, label %75
i32 108, label %87
]
42: ; preds = %39
%43 = load %struct.Connection*, %struct.Connection** %8
%44 = call i32 @Database_create(%struct.Connection* %43)
%45 = load %struct.Connection*, %struct.Connection** %8
%46 = call i32 @Database_write(%struct.Connection* %45)
br label %93
47: ; preds = %39
%48 = load i32, i32* %4
%49 = icmp ne i32 %48, 4
br i1 %49, label %50, label %53
50: ; preds = %47
%51 = load %struct.Connection*, %struct.Connection** %8
%52 = call i32 @die(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), %struct.Connection* %51)
br label %53
53: ; preds = %50, %47
%54 = load %struct.Connection*, %struct.Connection** %8
%55 = load i32, i32* %9
%56 = call i32 @Database_get(%struct.Connection* %54, i32 %55)
br label %93
57: ; preds = %39
%58 = load i32, i32* %4
%59 = icmp ne i32 %58, 6
br i1 %59, label %60, label %63
60: ; preds = %57
%61 = load %struct.Connection*, %struct.Connection** %8
%62 = call i32 @die(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), %struct.Connection* %61)
br label %63
63: ; preds = %60, %57
%64 = load %struct.Connection*, %struct.Connection** %8
%65 = load i32, i32* %9
%66 = load i8**, i8*** %5
%67 = getelementptr inbounds i8*, i8** %66, i64 4
%68 = load i8*, i8** %67
%69 = load i8**, i8*** %5
%70 = getelementptr inbounds i8*, i8** %69, i64 5
%71 = load i8*, i8** %70
%72 = call i32 @Database_set(%struct.Connection* %64, i32 %65, i8* %68, i8* %71)
%73 = load %struct.Connection*, %struct.Connection** %8
%74 = call i32 @Database_write(%struct.Connection* %73)
br label %93
75: ; preds = %39
%76 = load i32, i32* %4
%77 = icmp ne i32 %76, 4
br i1 %77, label %78, label %81
78: ; preds = %75
%79 = load %struct.Connection*, %struct.Connection** %8
%80 = call i32 @die(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0), %struct.Connection* %79)
br label %81
81: ; preds = %78, %75
%82 = load %struct.Connection*, %struct.Connection** %8
%83 = load i32, i32* %9
%84 = call i32 @Database_delete(%struct.Connection* %82, i32 %83)
%85 = load %struct.Connection*, %struct.Connection** %8
%86 = call i32 @Database_write(%struct.Connection* %85)
br label %93
87: ; preds = %39
%88 = load %struct.Connection*, %struct.Connection** %8
%89 = call i32 @Database_list(%struct.Connection* %88)
br label %93
90: ; preds = %39
%91 = load %struct.Connection*, %struct.Connection** %8
%92 = call i32 @die(i8* getelementptr inbounds ([60 x i8], [60 x i8]* @.str.5, i64 0, i64 0), %struct.Connection* %91)
br label %93
93: ; preds = %90, %87, %81, %63, %53, %42
%94 = load %struct.Connection*, %struct.Connection** %8
%95 = call i32 @Database_close(%struct.Connection* %94)
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), %struct.Connection* null)
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), %struct.Connection* %12)
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), %struct.Connection* %12)
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), %struct.Connection* %12)
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), %struct.Connection* %12)
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 ([60 x i8], [60 x i8]* @.str.5, i64 0, i64 0), %struct.Connection* %12)
br label %55
55: ; preds = %53, %51, %48, %37, %31, %24
%56 = tail call i32 @Database_close(%struct.Connection* %12)
ret i32 0
}