#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 */
size_t atoi(char *);
int /*<<< orphan*/ free(char *);
scalar_t__ iptables_list_chain_with_line_numbers(char const *, char *, size_t,
                                                 size_t);
scalar_t__ malloc(size_t);
int /*<<< orphan*/ *strstr(char *, char const *);
char *strtok_r(char *, char const *, char **);
size_t iptables_find_rule_in_chain(const char *chain_name,
                                   const char *criteria_one, size_t use_xlock) {
  char *token;
  char *token_save;
  const char *s = "\n";
  if (chain_name && criteria_one) {
    size_t d_buf_sz = 524288;
    char *results = (char *)malloc(d_buf_sz + 1);
    *results = 0;
    size_t x;
    if (iptables_list_chain_with_line_numbers(chain_name, results, d_buf_sz,
                                              use_xlock) == 0) {
      token = strtok_r(results, s, &token_save);
      x = 0;
      while (token != NULL) {
        x = atoi(token);
        if (x > 0) {
          if (strstr(token, criteria_one) != NULL) {
            free(results);
            return x;
          }
        }
        token = strtok_r(NULL, s, &token_save);
      }
    }
    free(results);
  }
  return 0;
}


x86 assembly (clang, -O0):
<iptables_find_rule_in_chain>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
lea    0xe85(%rip),%rax
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     126e <iptables_find_rule_in_chain+0x10e>
cmpq   $0x0,-0x18(%rbp)
je     126e <iptables_find_rule_in_chain+0x10e>
movq   $0x80000,-0x40(%rbp)
mov    -0x40(%rbp),%rdi
add    $0x1,%rdi
call   1050 <malloc@plt>
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
mov    -0x48(%rbp),%rsi
mov    -0x40(%rbp),%rdx
mov    -0x20(%rbp),%rcx
call   1070 <iptables_list_chain_with_line_numbers@plt>
cmp    $0x0,%rax
jne    1265 <iptables_find_rule_in_chain+0x105>
mov    -0x48(%rbp),%rdi
mov    -0x38(%rbp),%rsi
lea    -0x30(%rbp),%rdx
call   1040 <strtok_r@plt>
mov    %rax,-0x28(%rbp)
movq   $0x0,-0x50(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     1260 <iptables_find_rule_in_chain+0x100>
mov    -0x28(%rbp),%rdi
call   1060 <atoi@plt>
mov    %rax,-0x50(%rbp)
cmpq   $0x0,-0x50(%rbp)
jbe    1246 <iptables_find_rule_in_chain+0xe6>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1080 <strstr@plt>
cmp    $0x0,%rax
je     1241 <iptables_find_rule_in_chain+0xe1>
mov    -0x48(%rbp),%rdi
call   1030 <free@plt>
mov    -0x50(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1276 <iptables_find_rule_in_chain+0x116>
jmp    1246 <iptables_find_rule_in_chain+0xe6>
mov    -0x38(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edi
lea    -0x30(%rbp),%rdx
call   1040 <strtok_r@plt>
mov    %rax,-0x28(%rbp)
jmp    11f1 <iptables_find_rule_in_chain+0x91>
jmp    1265 <iptables_find_rule_in_chain+0x105>
mov    -0x48(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<iptables_find_rule_in_chain>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
xor    %r13d,%r13d
test   %rdi,%rdi
je     11b4 <iptables_find_rule_in_chain+0x54>
mov    %rsi,%rbx
test   %rsi,%rsi
je     11b4 <iptables_find_rule_in_chain+0x54>
mov    %rdx,%r12
mov    %rdi,%rbp
mov    $0x80001,%edi
call   1050 <malloc@plt>
mov    %rax,%r14
movb   $0x0,(%rax)
mov    $0x80000,%edx
mov    %rbp,%rdi
mov    %rax,%rsi
mov    %r12,%rcx
call   1070 <iptables_list_chain_with_line_numbers@plt>
test   %rax,%rax
je     11c6 <iptables_find_rule_in_chain+0x66>
xor    %r13d,%r13d
mov    %r14,%rdi
call   1030 <free@plt>
mov    %r13,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe33(%rip),%rsi
mov    %rsp,%rdx
mov    %r14,%rdi
call   1040 <strtok_r@plt>
test   %rax,%rax
je     11a9 <iptables_find_rule_in_chain+0x49>
mov    %rax,%rbp
xor    %r13d,%r13d
mov    %rsp,%r12
jmp    1209 <iptables_find_rule_in_chain+0xa9>
nopl   0x0(%rax,%rax,1)
xor    %edi,%edi
lea    0xe07(%rip),%rsi
mov    %r12,%rdx
call   1040 <strtok_r@plt>
mov    %rax,%rbp
test   %rax,%rax
je     11ac <iptables_find_rule_in_chain+0x4c>
mov    %rbp,%rdi
call   1060 <atoi@plt>
test   %rax,%rax
je     11f0 <iptables_find_rule_in_chain+0x90>
mov    %rax,%r15
mov    %rbp,%rdi
mov    %rbx,%rsi
call   1080 <strstr@plt>
test   %rax,%rax
je     11f0 <iptables_find_rule_in_chain+0x90>
mov    %r15,%r13
jmp    11ac <iptables_find_rule_in_chain+0x4c>

LLVM IR (-O0):
@.str = [2 x i8] c"\0A\00"
define i64 @iptables_find_rule_in_chain(i8* %0, i8* %1, i64 %2) {
%4 = alloca i64
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i64
%12 = alloca i8*
%13 = alloca i64
store i8* %0, i8** %5
store i8* %1, i8** %6
store i64 %2, i64* %7
store i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8** %10
%14 = load i8*, i8** %5
%15 = icmp ne i8* %14, null
br i1 %15, label %16, label %60
16: ; preds = %3
%17 = load i8*, i8** %6
%18 = icmp ne i8* %17, null
br i1 %18, label %19, label %60
19: ; preds = %16
store i64 524288, i64* %11
%20 = load i64, i64* %11
%21 = add i64 %20, 1
%22 = call i64 @malloc(i64 %21)
%23 = inttoptr i64 %22 to i8*
store i8* %23, i8** %12
%24 = load i8*, i8** %12
store i8 0, i8* %24
%25 = load i8*, i8** %5
%26 = load i8*, i8** %12
%27 = load i64, i64* %11
%28 = load i64, i64* %7
%29 = call i64 @iptables_list_chain_with_line_numbers(i8* %25, i8* %26, i64 %27, i64 %28)
%30 = icmp eq i64 %29, 0
br i1 %30, label %31, label %57
31: ; preds = %19
%32 = load i8*, i8** %12
%33 = load i8*, i8** %10
%34 = call i8* @strtok_r(i8* %32, i8* %33, i8** %9)
store i8* %34, i8** %8
store i64 0, i64* %13
br label %35
35: ; preds = %53, %31
%36 = load i8*, i8** %8
%37 = icmp ne i8* %36, null
br i1 %37, label %38, label %56
38: ; preds = %35
%39 = load i8*, i8** %8
%40 = call i64 @atoi(i8* %39)
store i64 %40, i64* %13
%41 = load i64, i64* %13
%42 = icmp ugt i64 %41, 0
br i1 %42, label %43, label %53
43: ; preds = %38
%44 = load i8*, i8** %8
%45 = load i8*, i8** %6
%46 = call i32* @strstr(i8* %44, i8* %45)
%47 = icmp ne i32* %46, null
br i1 %47, label %48, label %52
48: ; preds = %43
%49 = load i8*, i8** %12
%50 = call i32 @free(i8* %49)
%51 = load i64, i64* %13
store i64 %51, i64* %4
br label %61
52: ; preds = %43
br label %53
53: ; preds = %52, %38
%54 = load i8*, i8** %10
%55 = call i8* @strtok_r(i8* null, i8* %54, i8** %9)
store i8* %55, i8** %8
br label %35
56: ; preds = %35
br label %57
57: ; preds = %56, %19
%58 = load i8*, i8** %12
%59 = call i32 @free(i8* %58)
br label %60
60: ; preds = %57, %16, %3
store i64 0, i64* %4
br label %61
61: ; preds = %60, %48
%62 = load i64, i64* %4
ret i64 %62
}

LLVM IR (-O2):
@.str = [2 x i8] c"\0A\00"
define i64 @iptables_find_rule_in_chain(i8* %0, i8* %1, i64 %2) {
%4 = alloca i8*
%5 = bitcast i8** %4 to i8*
%6 = icmp ne i8* %0, null
%7 = icmp ne i8* %1, null
%8 = and i1 %6, %7
br i1 %8, label %9, label %30
9: ; preds = %3
%10 = tail call i64 @malloc(i64 524289)
%11 = inttoptr i64 %10 to i8*
store i8 0, i8* %11
%12 = tail call i64 @iptables_list_chain_with_line_numbers(i8* %0, i8* %11, i64 524288, i64 %2)
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %27
14: ; preds = %9
%15 = call i8* @strtok_r(i8* %11, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8** %4)
%16 = icmp eq i8* %15, null
br i1 %16, label %27, label %17
17: ; preds = %14, %24
%18 = phi i8* [ %25, %24 ], [ %15, %14 ]
%19 = call i64 @atoi(i8* %18)
%20 = icmp eq i64 %19, 0
br i1 %20, label %24, label %21
21: ; preds = %17
%22 = call i32* @strstr(i8* %18, i8* %1)
%23 = icmp eq i32* %22, null
br i1 %23, label %24, label %27
24: ; preds = %21, %17
%25 = call i8* @strtok_r(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8** %4)
%26 = icmp eq i8* %25, null
br i1 %26, label %27, label %17
27: ; preds = %21, %24, %9, %14
%28 = phi i64 [ 0, %14 ], [ 0, %9 ], [ 0, %24 ], [ %19, %21 ]
%29 = call i32 @free(i8* %11)
br label %30
30: ; preds = %27, %3
%31 = phi i64 [ 0, %3 ], [ %28, %27 ]
ret i64 %31
}