#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 */

/* Variables and functions */
int /*<<< orphan*/ db_get_error(char **, int);
int db_sql_exec_fn(char **, int, char *, int, int);
int db_sql_fetch_int(char **, int *, char *, int);
int /*<<< orphan*/ free(char *);
int db_sql_add_playlist_item(char **pe, int playlistid, int songid) {
  int result;
  int playlist_type;
  int count;
  result = db_sql_fetch_int(
      pe, &playlist_type, "select type from playlists where id=%d", playlistid);
  if (result != 0x00) {
    if (result == 0x05) {
      if (pe) {
        free(*pe);
      };
      db_get_error(pe, 0x06);
      return 0x06;
    }
    return result;
  }
  if (playlist_type == 1) {
    db_get_error(pe, 0x04);
    return 0x04;
  }
  result = db_sql_fetch_int(pe, &count,
                            "select count(*) from songs where "
                            "id=%d",
                            songid);
  if (result != 0x00) {
    if (result == 0x05) {
      if (pe) {
        free(*pe);
      };
      db_get_error(pe, 0x07);
      return 0x07;
    }
    return result;
  }
  result = db_sql_exec_fn(pe, 9,
                          "insert into playlistitems "
                          "(playlistid, songid) values (%d,%d)",
                          playlistid, songid);
  return result;
}


x86 assembly (clang, -O0):
<db_sql_add_playlist_item>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%ecx
lea    -0x20(%rbp),%rsi
lea    0xe9c(%rip),%rdx
call   1050 <db_sql_fetch_int@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     11bc <db_sql_add_playlist_item+0x7c>
cmpl   $0x5,-0x1c(%rbp)
jne    11b1 <db_sql_add_playlist_item+0x71>
cmpq   $0x0,-0x10(%rbp)
je     1197 <db_sql_add_playlist_item+0x57>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1030 <free@plt>
mov    -0x10(%rbp),%rdi
mov    $0x6,%esi
call   1060 <db_get_error@plt>
movl   $0x6,-0x4(%rbp)
jmp    126f <db_sql_add_playlist_item+0x12f>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    126f <db_sql_add_playlist_item+0x12f>
cmpl   $0x1,-0x20(%rbp)
jne    11e0 <db_sql_add_playlist_item+0xa0>
mov    -0x10(%rbp),%rdi
mov    $0x4,%esi
call   1060 <db_get_error@plt>
movl   $0x4,-0x4(%rbp)
jmp    126f <db_sql_add_playlist_item+0x12f>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%ecx
lea    -0x24(%rbp),%rsi
lea    0xe35(%rip),%rdx
call   1050 <db_sql_fetch_int@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     124a <db_sql_add_playlist_item+0x10a>
cmpl   $0x5,-0x1c(%rbp)
jne    123f <db_sql_add_playlist_item+0xff>
cmpq   $0x0,-0x10(%rbp)
je     1225 <db_sql_add_playlist_item+0xe5>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1030 <free@plt>
mov    -0x10(%rbp),%rdi
mov    $0x7,%esi
call   1060 <db_get_error@plt>
movl   $0x7,-0x4(%rbp)
jmp    126f <db_sql_add_playlist_item+0x12f>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    126f <db_sql_add_playlist_item+0x12f>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%ecx
mov    -0x18(%rbp),%r8d
mov    $0x9,%esi
lea    0xded(%rip),%rdx
call   1040 <db_sql_exec_fn@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<db_sql_add_playlist_item>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%r14d
mov    %esi,%r15d
mov    %rdi,%rbx
lea    0xea9(%rip),%rdx
mov    %rsp,%rsi
mov    %r15d,%ecx
call   1050 <db_sql_fetch_int@plt>
test   %eax,%eax
je     1192 <db_sql_add_playlist_item+0x52>
mov    %eax,%ebp
cmp    $0x5,%eax
jne    120b <db_sql_add_playlist_item+0xcb>
test   %rbx,%rbx
je     117e <db_sql_add_playlist_item+0x3e>
mov    (%rbx),%rdi
call   1030 <free@plt>
mov    $0x6,%ebp
mov    %rbx,%rdi
mov    $0x6,%esi
call   1060 <db_get_error@plt>
jmp    120b <db_sql_add_playlist_item+0xcb>
cmpl   $0x1,(%rsp)
jne    11ac <db_sql_add_playlist_item+0x6c>
mov    $0x4,%ebp
mov    %rbx,%rdi
mov    $0x4,%esi
call   1060 <db_get_error@plt>
jmp    120b <db_sql_add_playlist_item+0xcb>
lea    0xe74(%rip),%rdx
lea    0x4(%rsp),%rsi
mov    %rbx,%rdi
mov    %r14d,%ecx
call   1050 <db_sql_fetch_int@plt>
test   %eax,%eax
je     11ef <db_sql_add_playlist_item+0xaf>
mov    %eax,%ebp
cmp    $0x5,%eax
jne    120b <db_sql_add_playlist_item+0xcb>
test   %rbx,%rbx
je     11db <db_sql_add_playlist_item+0x9b>
mov    (%rbx),%rdi
call   1030 <free@plt>
mov    $0x7,%ebp
mov    %rbx,%rdi
mov    $0x7,%esi
call   1060 <db_get_error@plt>
jmp    120b <db_sql_add_playlist_item+0xcb>
lea    0xe58(%rip),%rdx
mov    %rbx,%rdi
mov    $0x9,%esi
mov    %r15d,%ecx
mov    %r14d,%r8d
call   1040 <db_sql_exec_fn@plt>
mov    %eax,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [39 x i8] c"select type from playlists where id=%d\00"
@.str.1 = [39 x i8] c"select count(*) from songs where id=%d\00"
@.str.2 = [62 x i8] c"insert into playlistitems (playlistid, songid) values (%d,%d)\00"
define i32 @db_sql_add_playlist_item(i8** %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i8** %0, i8*** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
%11 = load i8**, i8*** %5
%12 = load i32, i32* %6
%13 = call i32 @db_sql_fetch_int(i8** %11, i32* %9, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0), i32 %12)
store i32 %13, i32* %8
%14 = load i32, i32* %8
%15 = icmp ne i32 %14, 0
br i1 %15, label %16, label %31
16: ; preds = %3
%17 = load i32, i32* %8
%18 = icmp eq i32 %17, 5
br i1 %18, label %19, label %29
19: ; preds = %16
%20 = load i8**, i8*** %5
%21 = icmp ne i8** %20, null
br i1 %21, label %22, label %26
22: ; preds = %19
%23 = load i8**, i8*** %5
%24 = load i8*, i8** %23
%25 = call i32 @free(i8* %24)
br label %26
26: ; preds = %22, %19
%27 = load i8**, i8*** %5
%28 = call i32 @db_get_error(i8** %27, i32 6)
store i32 6, i32* %4
br label %64
29: ; preds = %16
%30 = load i32, i32* %8
store i32 %30, i32* %4
br label %64
31: ; preds = %3
%32 = load i32, i32* %9
%33 = icmp eq i32 %32, 1
br i1 %33, label %34, label %37
34: ; preds = %31
%35 = load i8**, i8*** %5
%36 = call i32 @db_get_error(i8** %35, i32 4)
store i32 4, i32* %4
br label %64
37: ; preds = %31
%38 = load i8**, i8*** %5
%39 = load i32, i32* %7
%40 = call i32 @db_sql_fetch_int(i8** %38, i32* %10, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i32 %39)
store i32 %40, i32* %8
%41 = load i32, i32* %8
%42 = icmp ne i32 %41, 0
br i1 %42, label %43, label %58
43: ; preds = %37
%44 = load i32, i32* %8
%45 = icmp eq i32 %44, 5
br i1 %45, label %46, label %56
46: ; preds = %43
%47 = load i8**, i8*** %5
%48 = icmp ne i8** %47, null
br i1 %48, label %49, label %53
49: ; preds = %46
%50 = load i8**, i8*** %5
%51 = load i8*, i8** %50
%52 = call i32 @free(i8* %51)
br label %53
53: ; preds = %49, %46
%54 = load i8**, i8*** %5
%55 = call i32 @db_get_error(i8** %54, i32 7)
store i32 7, i32* %4
br label %64
56: ; preds = %43
%57 = load i32, i32* %8
store i32 %57, i32* %4
br label %64
58: ; preds = %37
%59 = load i8**, i8*** %5
%60 = load i32, i32* %6
%61 = load i32, i32* %7
%62 = call i32 @db_sql_exec_fn(i8** %59, i32 9, i8* getelementptr inbounds ([62 x i8], [62 x i8]* @.str.2, i64 0, i64 0), i32 %60, i32 %61)
store i32 %62, i32* %8
%63 = load i32, i32* %8
store i32 %63, i32* %4
br label %64
64: ; preds = %58, %56, %53, %34, %29, %26
%65 = load i32, i32* %4
ret i32 %65
}

LLVM IR (-O2):
@.str = [39 x i8] c"select type from playlists where id=%d\00"
@.str.1 = [39 x i8] c"select count(*) from songs where id=%d\00"
@.str.2 = [62 x i8] c"insert into playlistitems (playlistid, songid) values (%d,%d)\00"
define i32 @db_sql_add_playlist_item(i8** %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = bitcast i32* %4 to i8*
%7 = bitcast i32* %5 to i8*
%8 = call i32 @db_sql_fetch_int(i8** %0, i32* %4, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0), i32 %1)
switch i32 %8, label %32 [
i32 0, label %16
i32 5, label %9
]
9: ; preds = %3
%10 = icmp eq i8** %0, null
br i1 %10, label %14, label %11
11: ; preds = %9
%12 = load i8*, i8** %0
%13 = call i32 @free(i8* %12)
br label %14
14: ; preds = %11, %9
%15 = call i32 @db_get_error(i8** %0, i32 6)
br label %32
16: ; preds = %3
%17 = load i32, i32* %4
%18 = icmp eq i32 %17, 1
br i1 %18, label %19, label %21
19: ; preds = %16
%20 = call i32 @db_get_error(i8** %0, i32 4)
br label %32
21: ; preds = %16
%22 = call i32 @db_sql_fetch_int(i8** %0, i32* %5, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i32 %2)
switch i32 %22, label %32 [
i32 0, label %30
i32 5, label %23
]
23: ; preds = %21
%24 = icmp eq i8** %0, null
br i1 %24, label %28, label %25
25: ; preds = %23
%26 = load i8*, i8** %0
%27 = call i32 @free(i8* %26)
br label %28
28: ; preds = %25, %23
%29 = call i32 @db_get_error(i8** %0, i32 7)
br label %32
30: ; preds = %21
%31 = call i32 @db_sql_exec_fn(i8** %0, i32 9, i8* getelementptr inbounds ([62 x i8], [62 x i8]* @.str.2, i64 0, i64 0), i32 %1, i32 %2)
br label %32
32: ; preds = %21, %3, %30, %28, %19, %14
%33 = phi i32 [ 6, %14 ], [ 4, %19 ], [ 7, %28 ], [ %31, %30 ], [ %8, %3 ], [ %22, %21 ]
ret i32 %33
}