#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 id, name, email to set");
    Database_get(conn, id);
    break;
  case 's':
    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, only: c=create, g=get, s=set, d=fel, 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     12fd <main+0x15d>
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    1309 <main+0x169>
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    1309 <main+0x169>
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    1309 <main+0x169>
cmpl   $0x4,-0x8(%rbp)
je     12d5 <main+0x135>
lea    0xddd(%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    1309 <main+0x169>
mov    -0x28(%rbp),%rdi
call   1040 <Database_list@plt>
jmp    1309 <main+0x169>
lea    0xdbb(%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,%rbx
mov    %edi,%r12d
cmp    $0x2,%edi
jg     11bf <main+0x1f>
lea    0xe8a(%rip),%rdi
call   1080 <die@plt>
mov    0x8(%rbx),%rdi
mov    0x10(%rbx),%rax
movsbl (%rax),%ebp
mov    %ebp,%esi
call   10b0 <Database_open@plt>
mov    %rax,%r14
xor    %r15d,%r15d
cmp    $0x4,%r12d
jl     11fa <main+0x5a>
mov    0x18(%rbx),%rdi
call   1090 <atoi@plt>
mov    %eax,%r15d
cmp    $0x64,%eax
jl     11fa <main+0x5a>
lea    0xe7d(%rip),%rdi
call   1080 <die@plt>
add    $0xffffff9d,%ebp
cmp    $0x10,%ebp
ja     121c <main+0x7c>
lea    0xdf7(%rip),%rax
movslq (%rax,%rbp,4),%rcx
add    %rax,%rcx
jmp    *%rcx
mov    %r14,%rdi
call   1050 <Database_create@plt>
jmp    1285 <main+0xe5>
lea    0xe9c(%rip),%rdi
call   1080 <die@plt>
jmp    128d <main+0xed>
cmp    $0x4,%r12d
je     123c <main+0x9c>
lea    0xe5a(%rip),%rdi
call   1080 <die@plt>
mov    %r14,%rdi
mov    %r15d,%esi
call   10a0 <Database_get@plt>
jmp    128d <main+0xed>
cmp    $0x4,%r12d
je     125b <main+0xbb>
lea    0xe57(%rip),%rdi
call   1080 <die@plt>
mov    %r14,%rdi
mov    %r15d,%esi
call   1060 <Database_delete@plt>
jmp    1285 <main+0xe5>
mov    %r14,%rdi
call   1040 <Database_list@plt>
jmp    128d <main+0xed>
mov    0x20(%rbx),%rdx
mov    0x28(%rbx),%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 = [28 x i8] c"Need id, name, email to set\00"
@.str.3 = [18 x i8] c"Need id to delete\00"
@.str.4 = [60 x i8] c"Invalid action, only: c=create, g=get, s=set, d=fel, 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 %81 [
i32 99, label %41
i32 103, label %46
i32 115, label %55
i32 100, label %67
i32 108, label %78
]
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 %83
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 ([28 x i8], [28 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 %83
55: ; preds = %38
%56 = load %struct.Connection*, %struct.Connection** %8
%57 = load i32, i32* %9
%58 = load i8**, i8*** %5
%59 = getelementptr inbounds i8*, i8** %58, i64 4
%60 = load i8*, i8** %59
%61 = load i8**, i8*** %5
%62 = getelementptr inbounds i8*, i8** %61, i64 5
%63 = load i8*, i8** %62
%64 = call i32 @Database_set(%struct.Connection* %56, i32 %57, i8* %60, i8* %63)
%65 = load %struct.Connection*, %struct.Connection** %8
%66 = call i32 @Database_write(%struct.Connection* %65)
br label %83
67: ; preds = %38
%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 ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0))
br label %72
72: ; preds = %70, %67
%73 = load %struct.Connection*, %struct.Connection** %8
%74 = load i32, i32* %9
%75 = call i32 @Database_delete(%struct.Connection* %73, i32 %74)
%76 = load %struct.Connection*, %struct.Connection** %8
%77 = call i32 @Database_write(%struct.Connection* %76)
br label %83
78: ; preds = %38
%79 = load %struct.Connection*, %struct.Connection** %8
%80 = call i32 @Database_list(%struct.Connection* %79)
br label %83
81: ; preds = %38
%82 = call i32 @die(i8* getelementptr inbounds ([60 x i8], [60 x i8]* @.str.4, i64 0, i64 0))
br label %83
83: ; preds = %81, %78, %72, %55, %51, %41
%84 = load %struct.Connection*, %struct.Connection** %8
%85 = call i32 @Database_close(%struct.Connection* %84)
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 = [28 x i8] c"Need id, name, email to set\00"
@.str.3 = [18 x i8] c"Need id to delete\00"
@.str.4 = [60 x i8] c"Invalid action, only: c=create, g=get, s=set, d=fel, 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 %49 [
i32 99, label %24
i32 103, label %27
i32 115, label %33
i32 100, label %40
i32 108, label %47
]
24: ; preds = %21
%25 = tail call i32 @Database_create(%struct.Connection* %12)
%26 = tail call i32 @Database_write(%struct.Connection* %12)
br label %51
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 ([28 x i8], [28 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 %51
33: ; preds = %21
%34 = getelementptr inbounds i8*, i8** %1, i64 4
%35 = load i8*, i8** %34
%36 = getelementptr inbounds i8*, i8** %1, i64 5
%37 = load i8*, i8** %36
%38 = tail call i32 @Database_set(%struct.Connection* %12, i32 %22, i8* %35, i8* %37)
%39 = tail call i32 @Database_write(%struct.Connection* %12)
br label %51
40: ; preds = %21
%41 = icmp eq i32 %0, 4
br i1 %41, label %44, label %42
42: ; preds = %40
%43 = tail call i32 @die(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0))
br label %44
44: ; preds = %42, %40
%45 = tail call i32 @Database_delete(%struct.Connection* %12, i32 %22)
%46 = tail call i32 @Database_write(%struct.Connection* %12)
br label %51
47: ; preds = %21
%48 = tail call i32 @Database_list(%struct.Connection* %12)
br label %51
49: ; preds = %21
%50 = tail call i32 @die(i8* getelementptr inbounds ([60 x i8], [60 x i8]* @.str.4, i64 0, i64 0))
br label %51
51: ; preds = %49, %47, %44, %33, %31, %24
%52 = tail call i32 @Database_close(%struct.Connection* %12)
ret i32 0
}