#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*/ assert(int);
int /*<<< orphan*/ memset(char *, char, int);
char *nvram_get_default(char const *);
int snprintf(char *, int, char *, char *);
int strlen(char *);
char *strsep(char **, char *);
int ezplib_get_rule_default(const char *rule_set, int nth, char *buf,
                            int bsize) {
  char tmp[8192];
  char *wordlist;
  char *str, *ptr;
  int ret;
  assert(strlen("|") == 1);
  if (!buf) {
    return -1;
  }
  memset(buf, '\0', bsize);
  if (!rule_set || !*rule_set) {
    return -1;
  }
  wordlist = nvram_get_default(rule_set);
  if (!wordlist) {
    return -3;
  }
  if (!*wordlist) {
    return -4;
  }
  ret = snprintf(tmp, 8192, "%s", wordlist);
  if (ret >= 8192) {
    return -2;
  }
  str = tmp;
  while (str) {
    ptr = strsep(&str, "|");
    if (!ptr) {
      return -4;
    }
    if (nth != 0) {
      nth--;
      continue;
    }
    ret = snprintf(buf, bsize, "%s", ptr);
    if (ret >= bsize) {
      return -2;
    }
    return ret;
  }
  if (nth >= 0) {
    return -4;
  }
  return 0;
}


x86 assembly (clang, -O0):
<ezplib_get_rule_default>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2050,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
lea    0xe80(%rip),%rdi
call   1030 <strlen@plt>
cmp    $0x1,%eax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
cmpq   $0x0,-0x20(%rbp)
jne    11ac <ezplib_get_rule_default+0x4c>
movl   $0xffffffff,-0x4(%rbp)
jmp    1333 <ezplib_get_rule_default+0x1d3>
mov    -0x20(%rbp),%rdi
mov    -0x24(%rbp),%edx
xor    %esi,%esi
call   1050 <memset@plt>
cmpq   $0x0,-0x10(%rbp)
je     11d2 <ezplib_get_rule_default+0x72>
mov    -0x10(%rbp),%rax
cmpb   $0x0,(%rax)
jne    11de <ezplib_get_rule_default+0x7e>
movl   $0xffffffff,-0x4(%rbp)
jmp    1333 <ezplib_get_rule_default+0x1d3>
mov    -0x10(%rbp),%rdi
call   1080 <nvram_get_default@plt>
mov    %rax,-0x2038(%rbp)
cmpq   $0x0,-0x2038(%rbp)
jne    1208 <ezplib_get_rule_default+0xa8>
movl   $0xfffffffd,-0x4(%rbp)
jmp    1333 <ezplib_get_rule_default+0x1d3>
mov    -0x2038(%rbp),%rax
cmpb   $0x0,(%rax)
jne    1224 <ezplib_get_rule_default+0xc4>
movl   $0xfffffffc,-0x4(%rbp)
jmp    1333 <ezplib_get_rule_default+0x1d3>
lea    -0x2030(%rbp),%rdi
mov    -0x2038(%rbp),%rcx
mov    $0x2000,%esi
lea    0xdc4(%rip),%rdx
call   1040 <snprintf@plt>
mov    %eax,-0x204c(%rbp)
cmpl   $0x2000,-0x204c(%rbp)
jl     1265 <ezplib_get_rule_default+0x105>
movl   $0xfffffffe,-0x4(%rbp)
jmp    1333 <ezplib_get_rule_default+0x1d3>
lea    -0x2030(%rbp),%rax
mov    %rax,-0x2040(%rbp)
cmpq   $0x0,-0x2040(%rbp)
je     1316 <ezplib_get_rule_default+0x1b6>
lea    -0x2040(%rbp),%rdi
lea    0xd71(%rip),%rsi
call   1070 <strsep@plt>
mov    %rax,-0x2048(%rbp)
cmpq   $0x0,-0x2048(%rbp)
jne    12b5 <ezplib_get_rule_default+0x155>
movl   $0xfffffffc,-0x4(%rbp)
jmp    1333 <ezplib_get_rule_default+0x1d3>
cmpl   $0x0,-0x14(%rbp)
je     12cd <ezplib_get_rule_default+0x16d>
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
jmp    1273 <ezplib_get_rule_default+0x113>
mov    -0x20(%rbp),%rdi
mov    -0x24(%rbp),%esi
mov    -0x2048(%rbp),%rcx
lea    0xd20(%rip),%rdx
call   1040 <snprintf@plt>
mov    %eax,-0x204c(%rbp)
mov    -0x204c(%rbp),%eax
cmp    -0x24(%rbp),%eax
jl     1308 <ezplib_get_rule_default+0x1a8>
movl   $0xfffffffe,-0x4(%rbp)
jmp    1333 <ezplib_get_rule_default+0x1d3>
mov    -0x204c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1333 <ezplib_get_rule_default+0x1d3>
cmpl   $0x0,-0x14(%rbp)
jl     132c <ezplib_get_rule_default+0x1cc>
movl   $0xfffffffc,-0x4(%rbp)
jmp    1333 <ezplib_get_rule_default+0x1d3>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x2050,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ezplib_get_rule_default>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x2010,%rsp
mov    %ecx,%r14d
mov    %rdx,%r15
mov    %esi,%ebp
mov    %rdi,%rbx
lea    0xe7f(%rip),%rdi
call   1030 <strlen@plt>
xor    %edi,%edi
cmp    $0x1,%eax
sete   %dil
call   1060 <assert@plt>
mov    $0xffffffff,%r12d
test   %r15,%r15
je     125f <ezplib_get_rule_default+0xff>
mov    %r15,%rdi
xor    %esi,%esi
mov    %r14d,%edx
call   1050 <memset@plt>
test   %rbx,%rbx
je     125f <ezplib_get_rule_default+0xff>
cmpb   $0x0,(%rbx)
je     125f <ezplib_get_rule_default+0xff>
mov    %rbx,%rdi
call   1080 <nvram_get_default@plt>
test   %rax,%rax
je     1251 <ezplib_get_rule_default+0xf1>
cmpb   $0x0,(%rax)
je     1259 <ezplib_get_rule_default+0xf9>
lea    0xe1f(%rip),%rdx
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x2000,%esi
mov    %rax,%rcx
call   1040 <snprintf@plt>
mov    $0xfffffffe,%r12d
cmp    $0x1fff,%eax
jg     125f <ezplib_get_rule_default+0xff>
mov    %rbx,0x8(%rsp)
add    $0x1,%ebp
lea    0xdec(%rip),%r12
lea    0x8(%rsp),%rbx
nopl   0x0(%rax)
mov    %rbx,%rdi
mov    %r12,%rsi
call   1070 <strsep@plt>
test   %rax,%rax
je     1259 <ezplib_get_rule_default+0xf9>
cmp    $0x1,%ebp
je     1272 <ezplib_get_rule_default+0x112>
add    $0xffffffff,%ebp
cmpq   $0x0,0x8(%rsp)
jne    1220 <ezplib_get_rule_default+0xc0>
xor    %eax,%eax
test   %ebp,%ebp
setle  %al
lea    -0x4(,%rax,4),%r12d
ff
jmp    125f <ezplib_get_rule_default+0xff>
mov    $0xfffffffd,%r12d
jmp    125f <ezplib_get_rule_default+0xff>
mov    $0xfffffffc,%r12d
mov    %r12d,%eax
add    $0x2010,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd89(%rip),%rdx
mov    %r15,%rdi
mov    %r14d,%esi
mov    %rax,%rcx
call   1040 <snprintf@plt>
cmp    %r14d,%eax
mov    $0xfffffffe,%r12d
cmovl  %eax,%r12d
jmp    125f <ezplib_get_rule_default+0xff>

LLVM IR (-O0):
@.str = [2 x i8] c"|\00"
@.str.1 = [3 x i8] c"%s\00"
define i32 @ezplib_get_rule_default(i8* %0, i32 %1, i8* %2, i32 %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
%10 = alloca [8192 x i8]
%11 = alloca i8*
%12 = alloca i8*
%13 = alloca i8*
%14 = alloca i32
store i8* %0, i8** %6
store i32 %1, i32* %7
store i8* %2, i8** %8
store i32 %3, i32* %9
%15 = call i32 @strlen(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%16 = icmp eq i32 %15, 1
%17 = zext i1 %16 to i32
%18 = call i32 @assert(i32 %17)
%19 = load i8*, i8** %8
%20 = icmp ne i8* %19, null
br i1 %20, label %22, label %21
21: ; preds = %4
store i32 -1, i32* %5
br label %83
22: ; preds = %4
%23 = load i8*, i8** %8
%24 = load i32, i32* %9
%25 = call i32 @memset(i8* %23, i8 signext 0, i32 %24)
%26 = load i8*, i8** %6
%27 = icmp ne i8* %26, null
br i1 %27, label %28, label %32
28: ; preds = %22
%29 = load i8*, i8** %6
%30 = load i8, i8* %29
%31 = icmp ne i8 %30, 0
br i1 %31, label %33, label %32
32: ; preds = %28, %22
store i32 -1, i32* %5
br label %83
33: ; preds = %28
%34 = load i8*, i8** %6
%35 = call i8* @nvram_get_default(i8* %34)
store i8* %35, i8** %11
%36 = load i8*, i8** %11
%37 = icmp ne i8* %36, null
br i1 %37, label %39, label %38
38: ; preds = %33
store i32 -3, i32* %5
br label %83
39: ; preds = %33
%40 = load i8*, i8** %11
%41 = load i8, i8* %40
%42 = icmp ne i8 %41, 0
br i1 %42, label %44, label %43
43: ; preds = %39
store i32 -4, i32* %5
br label %83
44: ; preds = %39
%45 = getelementptr inbounds [8192 x i8], [8192 x i8]* %10, i64 0, i64 0
%46 = load i8*, i8** %11
%47 = call i32 @snprintf(i8* %45, i32 8192, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %46)
store i32 %47, i32* %14
%48 = load i32, i32* %14
%49 = icmp sge i32 %48, 8192
br i1 %49, label %50, label %51
50: ; preds = %44
store i32 -2, i32* %5
br label %83
51: ; preds = %44
%52 = getelementptr inbounds [8192 x i8], [8192 x i8]* %10, i64 0, i64 0
store i8* %52, i8** %12
br label %53
53: ; preds = %64, %51
%54 = load i8*, i8** %12
%55 = icmp ne i8* %54, null
br i1 %55, label %56, label %78
56: ; preds = %53
%57 = call i8* @strsep(i8** %12, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i8* %57, i8** %13
%58 = load i8*, i8** %13
%59 = icmp ne i8* %58, null
br i1 %59, label %61, label %60
60: ; preds = %56
store i32 -4, i32* %5
br label %83
61: ; preds = %56
%62 = load i32, i32* %7
%63 = icmp ne i32 %62, 0
br i1 %63, label %64, label %67
64: ; preds = %61
%65 = load i32, i32* %7
%66 = add nsw i32 %65, -1
store i32 %66, i32* %7
br label %53
67: ; preds = %61
%68 = load i8*, i8** %8
%69 = load i32, i32* %9
%70 = load i8*, i8** %13
%71 = call i32 @snprintf(i8* %68, i32 %69, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %70)
store i32 %71, i32* %14
%72 = load i32, i32* %14
%73 = load i32, i32* %9
%74 = icmp sge i32 %72, %73
br i1 %74, label %75, label %76
75: ; preds = %67
store i32 -2, i32* %5
br label %83
76: ; preds = %67
%77 = load i32, i32* %14
store i32 %77, i32* %5
br label %83
78: ; preds = %53
%79 = load i32, i32* %7
%80 = icmp sge i32 %79, 0
br i1 %80, label %81, label %82
81: ; preds = %78
store i32 -4, i32* %5
br label %83
82: ; preds = %78
store i32 0, i32* %5
br label %83
83: ; preds = %82, %81, %76, %75, %60, %50, %43, %38, %32, %21
%84 = load i32, i32* %5
ret i32 %84
}

LLVM IR (-O2):
@.str = [2 x i8] c"|\00"
@.str.1 = [3 x i8] c"%s\00"
define i32 @ezplib_get_rule_default(i8* %0, i32 %1, i8* %2, i32 %3) {
%5 = alloca [8192 x i8]
%6 = alloca i8*
%7 = getelementptr inbounds [8192 x i8], [8192 x i8]* %5, i64 0, i64 0
%8 = bitcast i8** %6 to i8*
%9 = tail call i32 @strlen(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%10 = icmp eq i32 %9, 1
%11 = zext i1 %10 to i32
%12 = tail call i32 @assert(i32 %11)
%13 = icmp eq i8* %2, null
br i1 %13, label %47, label %14
14: ; preds = %4
%15 = tail call i32 @memset(i8* %2, i8 signext 0, i32 %3)
%16 = icmp eq i8* %0, null
br i1 %16, label %47, label %17
17: ; preds = %14
%18 = load i8, i8* %0
%19 = icmp eq i8 %18, 0
br i1 %19, label %47, label %20
20: ; preds = %17
%21 = tail call i8* @nvram_get_default(i8* %0)
%22 = icmp eq i8* %21, null
br i1 %22, label %47, label %23
23: ; preds = %20
%24 = load i8, i8* %21
%25 = icmp eq i8 %24, 0
br i1 %25, label %47, label %26
26: ; preds = %23
%27 = call i32 @snprintf(i8* %7, i32 8192, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %21)
%28 = icmp sgt i32 %27, 8191
br i1 %28, label %47, label %29
29: ; preds = %26
store i8* %7, i8** %6
br label %30
30: ; preds = %29, %36
%31 = phi i32 [ %1, %29 ], [ %37, %36 ]
%32 = call i8* @strsep(i8** %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%33 = icmp eq i8* %32, null
br i1 %33, label %47, label %34
34: ; preds = %30
%35 = icmp eq i32 %31, 0
br i1 %35, label %40, label %36
36: ; preds = %34
%37 = add nsw i32 %31, -1
%38 = load i8*, i8** %6
%39 = icmp eq i8* %38, null
br i1 %39, label %44, label %30
40: ; preds = %34
%41 = call i32 @snprintf(i8* %2, i32 %3, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %32)
%42 = icmp slt i32 %41, %3
%43 = select i1 %42, i32 %41, i32 -2
br label %47
44: ; preds = %36
%45 = icmp sgt i32 %31, 0
%46 = select i1 %45, i32 -4, i32 0
br label %47
47: ; preds = %30, %44, %40, %26, %23, %20, %14, %17, %4
%48 = phi i32 [ -1, %4 ], [ -1, %17 ], [ -1, %14 ], [ -3, %20 ], [ -4, %23 ], [ -2, %26 ], [ %43, %40 ], [ %46, %44 ], [ -4, %30 ]
ret i32 %48
}