#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 *);
int strlen(char *);
int /*<<< orphan*/ strncat(char *, char *, int);
size_t iptables_list_all(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", "iptables", "-w -L -n");
  else
    snprintf(cmd, 100, "%s %s", "iptables", "-L -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_all>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2c0,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    $0x80000,%edi
call   10a0 <malloc@plt>
mov    %rax,-0x98(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     11ee <iptables_list_all+0x5e>
lea    -0x90(%rbp),%rdi
mov    $0x64,%esi
lea    0xe2a(%rip),%rdx
lea    0xe29(%rip),%rcx
lea    0xe2b(%rip),%r8
call   1060 <snprintf@plt>
jmp    1214 <iptables_list_all+0x84>
lea    -0x90(%rbp),%rdi
mov    $0x64,%esi
lea    0xdff(%rip),%rdx
lea    0xdfe(%rip),%rcx
lea    0xe03(%rip),%r8
call   1060 <snprintf@plt>
lea    -0x90(%rbp),%rdi
lea    0xdf6(%rip),%rsi
mov    $0x0,%al
call   10b0 <popen@plt>
mov    %rax,-0xa0(%rbp)
cmp    $0x0,%rax
jne    1253 <iptables_list_all+0xc3>
mov    -0x98(%rbp),%rdi
call   1030 <free@plt>
movq   $0x1,-0x8(%rbp)
jmp    135e <iptables_list_all+0x1ce>
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     12c9 <iptables_list_all+0x139>
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    125d <iptables_list_all+0xcd>
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    -0x18(%rbp),%rax
jbe    1320 <iptables_list_all+0x190>
movq   $0x1,-0x2a8(%rbp)
jmp    133b <iptables_list_all+0x1ab>
mov    -0x10(%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    $0x2c0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<iptables_list_all>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x270,%rsp
mov    %rdx,%rbx
mov    %rsi,%r15
mov    %rdi,%r14
mov    $0x80000,%edi
call   10a0 <malloc@plt>
mov    %rax,%r13
test   %rbx,%rbx
lea    0xe52(%rip),%rax
lea    0xe48(%rip),%r8
cmove  %rax,%r8
lea    0xe2e(%rip),%rdx
lea    0xe2d(%rip),%rcx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x64,%esi
call   1060 <snprintf@plt>
lea    0xe28(%rip),%rsi
mov    %rbx,%rdi
xor    %eax,%eax
call   10b0 <popen@plt>
test   %rax,%rax
je     129d <iptables_list_all+0x10d>
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     125f <iptables_list_all+0xcf>
lea    0x70(%rsp),%rbx
nopw   0x0(%rax,%rax,1)
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    1230 <iptables_list_all+0xa0>
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     128b <iptables_list_all+0xfb>
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    12aa <iptables_list_all+0x11a>
mov    %r13,%rdi
call   1030 <free@plt>
mov    $0x1,%ebx
mov    %rbx,%rax
add    $0x270,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

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

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