#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 */
scalar_t__ ENOENT;
scalar_t__ ENOMEM;
int /*<<< orphan*/ EX_DATAERR;
int /*<<< orphan*/ EX_OSERR;
int /*<<< orphan*/ bzero(char *, size_t);
int /*<<< orphan*/ err(int /*<<< orphan*/, char *, ...);
scalar_t__ errno;
int /*<<< orphan*/ free(char *);
char *malloc(size_t);
int /*<<< orphan*/ printf(char *, char *);
scalar_t__ strcmp(char const *, char *);
char *strsep(char **, char *);
int sysctlbyname(char *, char *, size_t *, int /*<<< orphan*/ *,
                 int /*<<< orphan*/);
void ddb_list_scripts(const char *scriptname) {
  char *buffer, *line, *nextline;
  char *line_script, *line_scriptname;
  size_t buflen, len;
  int ret;
repeat:
  if (sysctlbyname("debug.ddb.scripting.scripts", NULL, &buflen, NULL, 0) < 0)
    err(EX_OSERR, "sysctl: %s", "debug.ddb.scripting.scripts");
  if (buflen == 0)
    return;
  buffer = malloc(buflen);
  if (buffer == NULL)
    err(EX_OSERR, "malloc");
  bzero(buffer, buflen);
  len = buflen;
  ret = sysctlbyname("debug.ddb.scripting.scripts", buffer, &len, NULL, 0);
  if (ret < 0 && errno != ENOMEM)
    err(EX_OSERR, "sysctl: %s", "debug.ddb.scripting.scripts");
  if (ret < 0) {
    free(buffer);
    goto repeat;
  }
  if (scriptname == NULL) {
    printf("%s", buffer);
    free(buffer);
    return;
  }
  nextline = buffer;
  while ((line = strsep(&nextline, "\n")) != NULL) {
    line_script = line;
    line_scriptname = strsep(&line_script, "=");
    if (line_script == NULL)
      continue;
    if (strcmp(scriptname, line_scriptname) != 0)
      continue;
    printf("%s\n", line_script);
    break;
  }
  if (line == NULL) {
    errno = ENOENT;
    err(EX_DATAERR, "%s", scriptname);
  }
  free(buffer);
}


x86 assembly (clang, -O0):
<ddb_list_scripts>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
lea    0xe6d(%rip),%rdi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x38(%rbp),%rdx
xor    %r8d,%r8d
mov    %rcx,%rsi
call   10a0 <sysctlbyname@plt>
cmp    $0x0,%eax
jge    11cd <ddb_list_scripts+0x4d>
mov    0x2e12(%rip),%rax
mov    (%rax),%edi
lea    0xe5d(%rip),%rsi
lea    0xe3a(%rip),%rdx
mov    $0x0,%al
call   1060 <err@plt>
cmpq   $0x0,-0x38(%rbp)
jne    11dd <ddb_list_scripts+0x5d>
jmp    1383 <ddb_list_scripts+0x203>
mov    -0x38(%rbp),%rdi
call   1070 <malloc@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    120c <ddb_list_scripts+0x8c>
mov    0x2dcc(%rip),%rax
mov    (%rax),%edi
lea    0xe22(%rip),%rsi
mov    $0x0,%al
call   1060 <err@plt>
mov    -0x10(%rbp),%rdi
mov    -0x38(%rbp),%rsi
call   1090 <bzero@plt>
mov    -0x38(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x10(%rbp),%rsi
lea    0xdd4(%rip),%rdi
lea    -0x40(%rbp),%rdx
xor    %eax,%eax
mov    %eax,%ecx
xor    %r8d,%r8d
call   10a0 <sysctlbyname@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0x0,-0x44(%rbp)
jge    1281 <ddb_list_scripts+0x101>
mov    0x2d80(%rip),%rax
mov    (%rax),%rax
mov    0x2d7e(%rip),%rcx
cmp    (%rcx),%rax
je     1281 <ddb_list_scripts+0x101>
mov    0x2d5e(%rip),%rax
mov    (%rax),%edi
lea    0xda9(%rip),%rsi
lea    0xd86(%rip),%rdx
mov    $0x0,%al
call   1060 <err@plt>
cmpl   $0x0,-0x44(%rbp)
jge    1299 <ddb_list_scripts+0x119>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
jmp    118c <ddb_list_scripts+0xc>
cmpq   $0x0,-0x8(%rbp)
jne    12c2 <ddb_list_scripts+0x142>
mov    -0x10(%rbp),%rsi
lea    0xd75(%rip),%rdi
call   1040 <printf@plt>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
jmp    1383 <ddb_list_scripts+0x203>
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
lea    -0x20(%rbp),%rdi
lea    0xd5d(%rip),%rsi
call   1080 <strsep@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     1340 <ddb_list_scripts+0x1c0>
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
lea    -0x28(%rbp),%rdi
lea    0xd33(%rip),%rsi
call   1080 <strsep@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1314 <ddb_list_scripts+0x194>
jmp    12ca <ddb_list_scripts+0x14a>
mov    -0x8(%rbp),%rdi
mov    -0x30(%rbp),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%rax
je     1330 <ddb_list_scripts+0x1b0>
jmp    12ca <ddb_list_scripts+0x14a>
mov    -0x28(%rbp),%rsi
lea    0xcf5(%rip),%rdi
call   1040 <printf@plt>
cmpq   $0x0,-0x18(%rbp)
jne    137a <ddb_list_scripts+0x1fa>
mov    0x2c66(%rip),%rax
mov    (%rax),%rcx
mov    0x2c74(%rip),%rax
mov    %rcx,(%rax)
mov    0x2c82(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xcb1(%rip),%rsi
mov    $0x0,%al
call   1060 <err@plt>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ddb_list_scripts>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rdi,0x18(%rsp)
lea    0xe66(%rip),%r15
lea    0x8(%rsp),%r12
mov    0x2e22(%rip),%r13
lea    0x20(%rsp),%r14
jmp    11b8 <ddb_list_scripts+0x38>
nopl   (%rax)
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r15,%rdi
xor    %esi,%esi
mov    %r12,%rdx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   10a0 <sysctlbyname@plt>
test   %eax,%eax
jns    11e3 <ddb_list_scripts+0x63>
mov    0x0(%r13),%edi
lea    0xe43(%rip),%rsi
mov    %r15,%rdx
xor    %eax,%eax
call   1060 <err@plt>
mov    0x8(%rsp),%rbp
test   %rbp,%rbp
je     134a <ddb_list_scripts+0x1ca>
mov    %rbp,%rdi
call   1070 <malloc@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    1218 <ddb_list_scripts+0x98>
mov    0x0(%r13),%edi
lea    0xe1b(%rip),%rsi
xor    %eax,%eax
call   1060 <err@plt>
mov    0x8(%rsp),%rbp
mov    %rbx,%rdi
mov    %rbp,%rsi
call   1090 <bzero@plt>
mov    %rbp,0x20(%rsp)
mov    %r15,%rdi
mov    %rbx,%rsi
mov    %r14,%rdx
xor    %ecx,%ecx
xor    %r8d,%r8d
call   10a0 <sysctlbyname@plt>
test   %eax,%eax
jns    1273 <ddb_list_scripts+0xf3>
mov    0x2d8a(%rip),%rax
mov    (%rax),%rax
mov    0x2d88(%rip),%rcx
cmp    (%rcx),%rax
je     11b0 <ddb_list_scripts+0x30>
mov    0x0(%r13),%edi
lea    0xdb8(%rip),%rsi
mov    %r15,%rdx
xor    %eax,%eax
call   1060 <err@plt>
jmp    11b0 <ddb_list_scripts+0x30>
mov    0x18(%rsp),%rbp
test   %rbp,%rbp
je     1333 <ddb_list_scripts+0x1b3>
mov    %rbx,0x10(%rsp)
lea    0xda5(%rip),%rsi
lea    0x10(%rsp),%rdi
call   1080 <strsep@plt>
test   %rax,%rax
je     1303 <ddb_list_scripts+0x183>
lea    0xd8b(%rip),%r14
mov    %rsp,%r12
lea    0xd85(%rip),%r15
lea    0x10(%rsp),%r13
jmp    12d0 <ddb_list_scripts+0x150>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %r13,%rdi
mov    %r15,%rsi
call   1080 <strsep@plt>
test   %rax,%rax
je     1303 <ddb_list_scripts+0x183>
mov    %rax,(%rsp)
mov    %r12,%rdi
mov    %r14,%rsi
call   1080 <strsep@plt>
cmpq   $0x0,(%rsp)
je     12c0 <ddb_list_scripts+0x140>
mov    %rbp,%rdi
mov    %rax,%rsi
call   1050 <strcmp@plt>
test   %rax,%rax
jne    12c0 <ddb_list_scripts+0x140>
mov    (%rsp),%rsi
lea    0xd2f(%rip),%rdi
jmp    133d <ddb_list_scripts+0x1bd>
mov    0x2cae(%rip),%rax
mov    (%rax),%rax
mov    0x2cbc(%rip),%rcx
mov    %rax,(%rcx)
mov    0x2cca(%rip),%rax
mov    (%rax),%edi
lea    0xcfd(%rip),%rsi
mov    %rbp,%rdx
xor    %eax,%eax
call   1060 <err@plt>
jmp    1342 <ddb_list_scripts+0x1c2>
lea    0xcea(%rip),%rdi
mov    %rbx,%rsi
call   1040 <printf@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [28 x i8] c"debug.ddb.scripting.scripts\00"
@EX_OSERR = global i32 0
@.str.1 = [11 x i8] c"sysctl: %s\00"
@.str.2 = [7 x i8] c"malloc\00"
@errno = global i64 0
@ENOMEM = global i64 0
@.str.3 = [3 x i8] c"%s\00"
@.str.4 = [2 x i8] c"\0A\00"
@.str.5 = [2 x i8] c"=\00"
@.str.6 = [4 x i8] c"%s\0A\00"
@ENOENT = global i64 0
@EX_DATAERR = global i32 0
define void @ddb_list_scripts(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i64
%10 = alloca i32
store i8* %0, i8** %2
br label %11
11: ; preds = %48, %1
%12 = call i32 @sysctlbyname(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i8* null, i64* %8, i32* null, i32 0)
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %11
%15 = load i32, i32* @EX_OSERR
%16 = call i32 (i32, i8*, ...) @err(i32 %15, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
br label %17
17: ; preds = %14, %11
%18 = load i64, i64* %8
%19 = icmp eq i64 %18, 0
br i1 %19, label %20, label %21
20: ; preds = %17
br label %90
21: ; preds = %17
%22 = load i64, i64* %8
%23 = call i8* @malloc(i64 %22)
store i8* %23, i8** %3
%24 = load i8*, i8** %3
%25 = icmp eq i8* %24, null
br i1 %25, label %26, label %29
26: ; preds = %21
%27 = load i32, i32* @EX_OSERR
%28 = call i32 (i32, i8*, ...) @err(i32 %27, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
br label %29
29: ; preds = %26, %21
%30 = load i8*, i8** %3
%31 = load i64, i64* %8
%32 = call i32 @bzero(i8* %30, i64 %31)
%33 = load i64, i64* %8
store i64 %33, i64* %9
%34 = load i8*, i8** %3
%35 = call i32 @sysctlbyname(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i8* %34, i64* %9, i32* null, i32 0)
store i32 %35, i32* %10
%36 = load i32, i32* %10
%37 = icmp slt i32 %36, 0
br i1 %37, label %38, label %45
38: ; preds = %29
%39 = load i64, i64* @errno
%40 = load i64, i64* @ENOMEM
%41 = icmp ne i64 %39, %40
br i1 %41, label %42, label %45
42: ; preds = %38
%43 = load i32, i32* @EX_OSERR
%44 = call i32 (i32, i8*, ...) @err(i32 %43, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
br label %45
45: ; preds = %42, %38, %29
%46 = load i32, i32* %10
%47 = icmp slt i32 %46, 0
br i1 %47, label %48, label %51
48: ; preds = %45
%49 = load i8*, i8** %3
%50 = call i32 @free(i8* %49)
br label %11
51: ; preds = %45
%52 = load i8*, i8** %2
%53 = icmp eq i8* %52, null
br i1 %53, label %54, label %59
54: ; preds = %51
%55 = load i8*, i8** %3
%56 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %55)
%57 = load i8*, i8** %3
%58 = call i32 @free(i8* %57)
br label %90
59: ; preds = %51
%60 = load i8*, i8** %3
store i8* %60, i8** %5
br label %61
61: ; preds = %75, %69, %59
%62 = call i8* @strsep(i8** %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
store i8* %62, i8** %4
%63 = icmp ne i8* %62, null
br i1 %63, label %64, label %79
64: ; preds = %61
%65 = load i8*, i8** %4
store i8* %65, i8** %6
%66 = call i8* @strsep(i8** %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
store i8* %66, i8** %7
%67 = load i8*, i8** %6
%68 = icmp eq i8* %67, null
br i1 %68, label %69, label %70
69: ; preds = %64
br label %61
70: ; preds = %64
%71 = load i8*, i8** %2
%72 = load i8*, i8** %7
%73 = call i64 @strcmp(i8* %71, i8* %72)
%74 = icmp ne i64 %73, 0
br i1 %74, label %75, label %76
75: ; preds = %70
br label %61
76: ; preds = %70
%77 = load i8*, i8** %6
%78 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0), i8* %77)
br label %79
79: ; preds = %76, %61
%80 = load i8*, i8** %4
%81 = icmp eq i8* %80, null
br i1 %81, label %82, label %87
82: ; preds = %79
%83 = load i64, i64* @ENOENT
store i64 %83, i64* @errno
%84 = load i32, i32* @EX_DATAERR
%85 = load i8*, i8** %2
%86 = call i32 (i32, i8*, ...) @err(i32 %84, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %85)
br label %87
87: ; preds = %82, %79
%88 = load i8*, i8** %3
%89 = call i32 @free(i8* %88)
br label %90
90: ; preds = %87, %54, %20
ret void
}

LLVM IR (-O2):
@.str = [28 x i8] c"debug.ddb.scripting.scripts\00"
@EX_OSERR = global i32 0
@.str.1 = [11 x i8] c"sysctl: %s\00"
@.str.2 = [7 x i8] c"malloc\00"
@errno = global i64 0
@ENOMEM = global i64 0
@.str.3 = [3 x i8] c"%s\00"
@.str.4 = [2 x i8] c"\0A\00"
@.str.5 = [2 x i8] c"=\00"
@.str.6 = [4 x i8] c"%s\0A\00"
@ENOENT = global i64 0
@EX_DATAERR = global i32 0
define void @ddb_list_scripts(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i64
%6 = bitcast i8** %2 to i8*
%7 = bitcast i8** %3 to i8*
%8 = bitcast i64* %4 to i8*
%9 = bitcast i64* %5 to i8*
br label %10
10: ; preds = %38, %1
%11 = call i32 @sysctlbyname(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i8* null, i64* %4, i32* null, i32 0)
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %10
%14 = load i32, i32* @EX_OSERR
%15 = call i32 (i32, i8*, ...) @err(i32 %14, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
br label %16
16: ; preds = %13, %10
%17 = load i64, i64* %4
%18 = icmp eq i64 %17, 0
br i1 %18, label %67, label %19
19: ; preds = %16
%20 = call i8* @malloc(i64 %17)
%21 = icmp eq i8* %20, null
br i1 %21, label %22, label %26
22: ; preds = %19
%23 = load i32, i32* @EX_OSERR
%24 = call i32 (i32, i8*, ...) @err(i32 %23, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%25 = load i64, i64* %4
br label %26
26: ; preds = %22, %19
%27 = phi i64 [ %25, %22 ], [ %17, %19 ]
%28 = call i32 @bzero(i8* %20, i64 %27)
store i64 %27, i64* %5
%29 = call i32 @sysctlbyname(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i8* %20, i64* %5, i32* null, i32 0)
%30 = icmp slt i32 %29, 0
br i1 %30, label %31, label %40
31: ; preds = %26
%32 = load i64, i64* @errno
%33 = load i64, i64* @ENOMEM
%34 = icmp eq i64 %32, %33
br i1 %34, label %38, label %35
35: ; preds = %31
%36 = load i32, i32* @EX_OSERR
%37 = call i32 (i32, i8*, ...) @err(i32 %36, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
br label %38
38: ; preds = %31, %35
%39 = call i32 @free(i8* %20)
br label %10
40: ; preds = %26
%41 = icmp eq i8* %0, null
br i1 %41, label %42, label %44
42: ; preds = %40
%43 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %20)
br label %65
44: ; preds = %40
store i8* %20, i8** %2
%45 = call i8* @strsep(i8** %2, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%46 = icmp eq i8* %45, null
br i1 %46, label %61, label %47
47: ; preds = %44, %52
%48 = phi i8* [ %53, %52 ], [ %45, %44 ]
store i8* %48, i8** %3
%49 = call i8* @strsep(i8** %3, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
%50 = load i8*, i8** %3
%51 = icmp eq i8* %50, null
br i1 %51, label %52, label %55
52: ; preds = %47, %55
%53 = call i8* @strsep(i8** %2, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%54 = icmp eq i8* %53, null
br i1 %54, label %61, label %47
55: ; preds = %47
%56 = call i64 @strcmp(i8* %0, i8* %49)
%57 = icmp eq i64 %56, 0
br i1 %57, label %58, label %52
58: ; preds = %55
%59 = load i8*, i8** %3
%60 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0), i8* %59)
br label %65
61: ; preds = %52, %44
%62 = load i64, i64* @ENOENT
store i64 %62, i64* @errno
%63 = load i32, i32* @EX_DATAERR
%64 = call i32 (i32, i8*, ...) @err(i32 %63, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %0)
br label %65
65: ; preds = %61, %58, %42
%66 = call i32 @free(i8* %20)
br label %67
67: ; preds = %16, %65
ret void
}