#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 *, char const *);
int strlen(char *);
int /*<<< orphan*/ strncat(char *, char *, int);
size_t iptables_list_chain_table(const char *chain_name, const char *table_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 %s", "iptables", "-w -L", chain_name,
             "-n -t", table_name);
  else
    snprintf(cmd, 100, "%s %s %s %s %s", "iptables", "-L", chain_name, "-n -t",
             table_name);
  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_table>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2e0,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8,-0x30(%rbp)
mov    $0x80000,%edi
call   10a0 <malloc@plt>
mov    %rax,-0xa8(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     120e <iptables_list_chain_table+0x7e>
lea    -0xa0(%rbp),%rdi
mov    -0x10(%rbp),%r9
mov    -0x18(%rbp),%rax
mov    $0x64,%esi
lea    0xe1a(%rip),%rdx
lea    0xe22(%rip),%rcx
lea    0xe24(%rip),%r8
lea    0xe23(%rip),%r10
mov    %r10,(%rsp)
mov    %rax,0x8(%rsp)
call   1060 <snprintf@plt>
jmp    124c <iptables_list_chain_table+0xbc>
lea    -0xa0(%rbp),%rdi
mov    -0x10(%rbp),%r9
mov    -0x18(%rbp),%rax
mov    $0x64,%esi
lea    0xdd7(%rip),%rdx
lea    0xddf(%rip),%rcx
lea    0xde4(%rip),%r8
lea    0xde0(%rip),%r10
mov    %r10,(%rsp)
mov    %rax,0x8(%rsp)
call   1060 <snprintf@plt>
lea    -0xa0(%rbp),%rdi
lea    0xdca(%rip),%rsi
mov    $0x0,%al
call   10b0 <popen@plt>
mov    %rax,-0xb0(%rbp)
cmp    $0x0,%rax
jne    128b <iptables_list_chain_table+0xfb>
mov    -0xa8(%rbp),%rdi
call   1030 <free@plt>
movq   $0x1,-0x8(%rbp)
jmp    1396 <iptables_list_chain_table+0x206>
mov    -0xa8(%rbp),%rax
movb   $0x0,(%rax)
lea    -0x2b0(%rbp),%rdi
mov    -0xb0(%rbp),%rdx
mov    $0x200,%esi
call   1080 <fgets@plt>
cmp    $0x0,%rax
je     1301 <iptables_list_chain_table+0x171>
mov    -0xa8(%rbp),%rax
mov    %rax,-0x2d0(%rbp)
lea    -0x2b0(%rbp),%rax
mov    %rax,-0x2c8(%rbp)
mov    -0xa8(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x2d0(%rbp),%rdi
mov    -0x2c8(%rbp),%rsi
mov    $0x80000,%edx
sub    %eax,%edx
sub    $0x1,%edx
call   1070 <strncat@plt>
jmp    1295 <iptables_list_chain_table+0x105>
movq   $0x0,-0x2b8(%rbp)
mov    -0xa8(%rbp),%rdi
call   1040 <strlen@plt>
cltq
mov    %rax,-0x2c0(%rbp)
mov    -0xa8(%rbp),%rax
mov    -0x2c0(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x2c0(%rbp),%rax
add    $0x1,%rax
cmp    -0x28(%rbp),%rax
jbe    1358 <iptables_list_chain_table+0x1c8>
movq   $0x1,-0x2b8(%rbp)
jmp    1373 <iptables_list_chain_table+0x1e3>
mov    -0x20(%rbp),%rdi
mov    -0xa8(%rbp),%rsi
mov    -0x2c0(%rbp),%rdx
add    $0x1,%rdx
call   1090 <memcpy@plt>
mov    -0xa8(%rbp),%rdi
call   1030 <free@plt>
mov    -0xb0(%rbp),%rdi
call   1050 <pclose@plt>
mov    -0x2b8(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x2e0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<iptables_list_chain_table>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x278,%rsp
mov    %r8,%rbx
mov    %rcx,%r15
mov    %rdx,%r14
mov    %rsi,%r12
mov    %rdi,%rbp
mov    $0x80000,%edi
call   10a0 <malloc@plt>
mov    %rax,%r13
test   %rbx,%rbx
lea    0xe54(%rip),%rax
lea    0xe4a(%rip),%r8
cmove  %rax,%r8
lea    0xe45(%rip),%rax
lea    0xe20(%rip),%rdx
lea    0xe28(%rip),%rcx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x64,%esi
mov    %rbp,%r9
push   %r12
push   %rax
call   1060 <snprintf@plt>
add    $0x10,%rsp
lea    0xe1c(%rip),%rsi
mov    %rbx,%rdi
xor    %eax,%eax
call   10b0 <popen@plt>
test   %rax,%rax
je     12ad <iptables_list_chain_table+0x11d>
mov    %rax,%r12
movb   $0x0,0x0(%r13)
lea    0x70(%rsp),%rdi
mov    $0x200,%esi
mov    %rax,%rdx
call   1080 <fgets@plt>
test   %rax,%rax
je     126f <iptables_list_chain_table+0xdf>
lea    0x70(%rsp),%rbx
nop
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_table+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_table+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_table+0x12a>
mov    %r13,%rdi
call   1030 <free@plt>
mov    $0x1,%ebx
mov    %rbx,%rax
add    $0x278,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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