#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 */
typedef int /*<<< orphan*/ buff;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
scalar_t__ malloc(int);
int /*<<< orphan*/ memcpy(char *, char *, size_t);
int /*<<< orphan*/ pclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ snprintf(char *, int, char *, char *, char *, char const *,
                            char *);
int strlen(char *);
int /*<<< orphan*/ strncat(char *, char *, int);
size_t iptables_list_chain(const char *chain_name, char *dst, size_t sz_dst,
                           size_t use_xlock) {
  char cmd[100];
  char *dest = (char *)malloc(524288);
  if (use_xlock)
    snprintf(cmd, 100, "%s %s %s %s", "iptables", "-w -L", chain_name, "-n");
  else
    snprintf(cmd, 100, "%s %s %s %s", "iptables", "-L", chain_name, "-n");
  FILE *in;
  extern FILE *popen();
  char buff[512];
  if (!(in = popen(cmd, "r"))) {
    free(dest);
    return 1;
  }
  *dest = 0;
  while (fgets(buff, sizeof(buff), in) != NULL) {
    strncat(dest, buff, 524288 - strlen(dest) - 1);
  }
  size_t rc = 0;
  size_t dest_len = strlen(dest);
  dest[dest_len] = '\0';
  if (dest_len + 1 > sz_dst) {
    rc = 1;
  } else {
    memcpy(dst, dest, dest_len + 1);
  }
  free(dest);
  pclose(in);
  return rc;
}


x86 assembly (clang, -O0):
<iptables_list_chain>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2d0,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    $0x80000,%edi
call   10a0 <malloc@plt>
mov    %rax,-0x98(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     1201 <iptables_list_chain+0x71>
lea    -0x90(%rbp),%rdi
mov    -0x10(%rbp),%r9
mov    $0x64,%esi
lea    0xe22(%rip),%rdx
lea    0xe27(%rip),%rcx
lea    0xe29(%rip),%r8
lea    0xe28(%rip),%rax
mov    %rax,(%rsp)
call   1060 <snprintf@plt>
jmp    1236 <iptables_list_chain+0xa6>
lea    -0x90(%rbp),%rdi
mov    -0x10(%rbp),%r9
mov    $0x64,%esi
lea    0xde8(%rip),%rdx
lea    0xded(%rip),%rcx
lea    0xdf2(%rip),%r8
lea    0xdee(%rip),%rax
mov    %rax,(%rsp)
call   1060 <snprintf@plt>
lea    -0x90(%rbp),%rdi
lea    0xdda(%rip),%rsi
mov    $0x0,%al
call   10b0 <popen@plt>
mov    %rax,-0xa0(%rbp)
cmp    $0x0,%rax
jne    1275 <iptables_list_chain+0xe5>
mov    -0x98(%rbp),%rdi
call   1030 <free@plt>
movq   $0x1,-0x8(%rbp)
jmp    1380 <iptables_list_chain+0x1f0>
mov    -0x98(%rbp),%rax
movb   $0x0,(%rax)
lea    -0x2a0(%rbp),%rdi
mov    -0xa0(%rbp),%rdx
mov    $0x200,%esi
call   1080 <fgets@plt>
cmp    $0x0,%rax
je     12eb <iptables_list_chain+0x15b>
mov    -0x98(%rbp),%rax
mov    %rax,-0x2c0(%rbp)
lea    -0x2a0(%rbp),%rax
mov    %rax,-0x2b8(%rbp)
mov    -0x98(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x2c0(%rbp),%rdi
mov    -0x2b8(%rbp),%rsi
mov    $0x80000,%edx
sub    %eax,%edx
sub    $0x1,%edx
call   1070 <strncat@plt>
jmp    127f <iptables_list_chain+0xef>
movq   $0x0,-0x2a8(%rbp)
mov    -0x98(%rbp),%rdi
call   1040 <strlen@plt>
cltq
mov    %rax,-0x2b0(%rbp)
mov    -0x98(%rbp),%rax
mov    -0x2b0(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x2b0(%rbp),%rax
add    $0x1,%rax
cmp    -0x20(%rbp),%rax
jbe    1342 <iptables_list_chain+0x1b2>
movq   $0x1,-0x2a8(%rbp)
jmp    135d <iptables_list_chain+0x1cd>
mov    -0x18(%rbp),%rdi
mov    -0x98(%rbp),%rsi
mov    -0x2b0(%rbp),%rdx
add    $0x1,%rdx
call   1090 <memcpy@plt>
mov    -0x98(%rbp),%rdi
call   1030 <free@plt>
mov    -0xa0(%rbp),%rdi
call   1050 <pclose@plt>
mov    -0x2a8(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x2d0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<iptables_list_chain>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x280,%rsp
mov    %rcx,%rbx
mov    %rdx,%r15
mov    %rsi,%r14
mov    %rdi,%r12
mov    $0x80000,%edi
call   10a0 <malloc@plt>
mov    %rax,%r13
test   %rbx,%rbx
lea    0xe55(%rip),%rax
lea    0xe4b(%rip),%r8
cmove  %rax,%r8
lea    0xe46(%rip),%rax
mov    %rax,(%rsp)
lea    0xe20(%rip),%rdx
lea    0xe25(%rip),%rcx
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x64,%esi
mov    %r12,%r9
call   1060 <snprintf@plt>
lea    0xe1b(%rip),%rsi
mov    %rbx,%rdi
xor    %eax,%eax
call   10b0 <popen@plt>
test   %rax,%rax
je     12ad <iptables_list_chain+0x11d>
mov    %rax,%r12
movb   $0x0,0x0(%r13)
lea    0x80(%rsp),%rdi
mov    $0x200,%esi
mov    %rax,%rdx
call   1080 <fgets@plt>
test   %rax,%rax
je     126f <iptables_list_chain+0xdf>
lea    0x80(%rsp),%rbx
mov    %r13,%rdi
call   1040 <strlen@plt>
mov    $0x7ffff,%edx
sub    %eax,%edx
mov    %r13,%rdi
mov    %rbx,%rsi
call   1070 <strncat@plt>
mov    %rbx,%rdi
mov    $0x200,%esi
mov    %r12,%rdx
call   1080 <fgets@plt>
test   %rax,%rax
jne    1240 <iptables_list_chain+0xb0>
mov    %r13,%rdi
call   1040 <strlen@plt>
movslq %eax,%rdx
movb   $0x0,0x0(%r13,%rdx,1)
add    $0x1,%rdx
mov    $0x1,%ebx
cmp    %r15,%rdx
ja     129b <iptables_list_chain+0x10b>
mov    %r14,%rdi
mov    %r13,%rsi
call   1090 <memcpy@plt>
xor    %ebx,%ebx
mov    %r13,%rdi
call   1030 <free@plt>
mov    %r12,%rdi
call   1050 <pclose@plt>
jmp    12ba <iptables_list_chain+0x12a>
mov    %r13,%rdi
call   1030 <free@plt>
mov    $0x1,%ebx
mov    %rbx,%rax
add    $0x280,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [12 x i8] c"%s %s %s %s\00"
@.str.1 = [9 x i8] c"iptables\00"
@.str.2 = [6 x i8] c"-w -L\00"
@.str.3 = [3 x i8] c"-n\00"
@.str.4 = [3 x i8] c"-L\00"
@.str.5 = [2 x i8] c"r\00"
define i64 @iptables_list_chain(i8* %0, i8* %1, i64 %2, i64 %3) {
%5 = alloca i64
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i64
%10 = alloca [100 x i8]
%11 = alloca i8*
%12 = alloca i32*
%13 = alloca [512 x i8]
%14 = alloca i64
%15 = alloca i64
store i8* %0, i8** %6
store i8* %1, i8** %7
store i64 %2, i64* %8
store i64 %3, i64* %9
%16 = call i64 @malloc(i32 524288)
%17 = inttoptr i64 %16 to i8*
store i8* %17, i8** %11
%18 = load i64, i64* %9
%19 = icmp ne i64 %18, 0
br i1 %19, label %20, label %24
20: ; preds = %4
%21 = getelementptr inbounds [100 x i8], [100 x i8]* %10, i64 0, i64 0
%22 = load i8*, i8** %6
%23 = call i32 @snprintf(i8* %21, i32 100, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %22, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
br label %28
24: ; preds = %4
%25 = getelementptr inbounds [100 x i8], [100 x i8]* %10, i64 0, i64 0
%26 = load i8*, i8** %6
%27 = call i32 @snprintf(i8* %25, i32 100, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* %26, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
br label %28
28: ; preds = %24, %20
%29 = getelementptr inbounds [100 x i8], [100 x i8]* %10, i64 0, i64 0
%30 = call i32* (i8*, i8*, ...) bitcast (i32* (...)* @popen to i32* (i8*, i8*, ...)*)(i8* %29, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
store i32* %30, i32** %12
%31 = icmp ne i32* %30, null
br i1 %31, label %35, label %32
32: ; preds = %28
%33 = load i8*, i8** %11
%34 = call i32 @free(i8* %33)
store i64 1, i64* %5
br label %74
35: ; preds = %28
%36 = load i8*, i8** %11
store i8 0, i8* %36
br label %37
37: ; preds = %42, %35
%38 = getelementptr inbounds [512 x i8], [512 x i8]* %13, i64 0, i64 0
%39 = load i32*, i32** %12
%40 = call i32* @fgets(i8* %38, i32 512, i32* %39)
%41 = icmp ne i32* %40, null
br i1 %41, label %42, label %50
42: ; preds = %37
%43 = load i8*, i8** %11
%44 = getelementptr inbounds [512 x i8], [512 x i8]* %13, i64 0, i64 0
%45 = load i8*, i8** %11
%46 = call i32 @strlen(i8* %45)
%47 = sub nsw i32 524288, %46
%48 = sub nsw i32 %47, 1
%49 = call i32 @strncat(i8* %43, i8* %44, i32 %48)
br label %37
50: ; preds = %37
store i64 0, i64* %14
%51 = load i8*, i8** %11
%52 = call i32 @strlen(i8* %51)
%53 = sext i32 %52 to i64
store i64 %53, i64* %15
%54 = load i8*, i8** %11
%55 = load i64, i64* %15
%56 = getelementptr inbounds i8, i8* %54, i64 %55
store i8 0, i8* %56
%57 = load i64, i64* %15
%58 = add i64 %57, 1
%59 = load i64, i64* %8
%60 = icmp ugt i64 %58, %59
br i1 %60, label %61, label %62
61: ; preds = %50
store i64 1, i64* %14
br label %68
62: ; preds = %50
%63 = load i8*, i8** %7
%64 = load i8*, i8** %11
%65 = load i64, i64* %15
%66 = add i64 %65, 1
%67 = call i32 @memcpy(i8* %63, i8* %64, i64 %66)
br label %68
68: ; preds = %62, %61
%69 = load i8*, i8** %11
%70 = call i32 @free(i8* %69)
%71 = load i32*, i32** %12
%72 = call i32 @pclose(i32* %71)
%73 = load i64, i64* %14
store i64 %73, i64* %5
br label %74
74: ; preds = %68, %32
%75 = load i64, i64* %5
ret i64 %75
}

LLVM IR (-O2):
@.str = [12 x i8] c"%s %s %s %s\00"
@.str.1 = [9 x i8] c"iptables\00"
@.str.2 = [6 x i8] c"-w -L\00"
@.str.3 = [3 x i8] c"-n\00"
@.str.4 = [3 x i8] c"-L\00"
@.str.5 = [2 x i8] c"r\00"
define i64 @iptables_list_chain(i8* %0, i8* %1, i64 %2, i64 %3) {
%5 = alloca [100 x i8]
%6 = alloca [512 x i8]
%7 = getelementptr inbounds [100 x i8], [100 x i8]* %5, i64 0, i64 0
%8 = tail call i64 @malloc(i32 524288)
%9 = inttoptr i64 %8 to i8*
%10 = icmp eq i64 %3, 0
%11 = select i1 %10, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0)
%12 = call i32 @snprintf(i8* %7, i32 100, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i8* %11, i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%13 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%14 = call i32* (i8*, i8*, ...) bitcast (i32* (...)* @popen to i32* (i8*, i8*, ...)*)(i8* %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
%15 = icmp eq i32* %14, null
br i1 %15, label %16, label %18
16: ; preds = %4
%17 = call i32 @free(i8* %9)
br label %39
18: ; preds = %4
store i8 0, i8* %9
%19 = call i32* @fgets(i8* %13, i32 512, i32* %14)
%20 = icmp eq i32* %19, null
br i1 %20, label %27, label %21
21: ; preds = %18, %21
%22 = call i32 @strlen(i8* %9)
%23 = sub i32 524287, %22
%24 = call i32 @strncat(i8* %9, i8* %13, i32 %23)
%25 = call i32* @fgets(i8* %13, i32 512, i32* %14)
%26 = icmp eq i32* %25, null
br i1 %26, label %27, label %21
27: ; preds = %21, %18
%28 = call i32 @strlen(i8* %9)
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds i8, i8* %9, i64 %29
store i8 0, i8* %30
%31 = add nsw i64 %29, 1
%32 = icmp ugt i64 %31, %2
br i1 %32, label %35, label %33
33: ; preds = %27
%34 = call i32 @memcpy(i8* %1, i8* %9, i64 %31)
br label %35
35: ; preds = %27, %33
%36 = phi i64 [ 0, %33 ], [ 1, %27 ]
%37 = call i32 @free(i8* %9)
%38 = call i32 @pclose(i32* %14)
br label %39
39: ; preds = %35, %16
%40 = phi i64 [ %36, %35 ], [ 1, %16 ]
ret i64 %40
}