#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*/ AF_UNIX;
int /*<<< orphan*/ SOCK_DGRAM;
scalar_t__ close(int);
int create_filtered_socket();
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ putchar(char);
int /*<<< orphan*/ send_fd(int, int);
scalar_t__ socketpair(int /*<<< orphan*/, int /*<<< orphan*/,
                      int /*<<< orphan*/, int *);
int /*<<< orphan*/ stdout;
char *strerror(int /*<<< orphan*/);
void create_socket_tree(int parent, size_t depth) {
  int fds[2];
  size_t i;
  for (i = 0; i < 20; i++) {
    if (depth == 0) {
      putchar('.');
      fflush(stdout);
    }
    if (depth == (3 - 1)) {
      fds[0] = create_filtered_socket();
      send_fd(parent, fds[0]);
      if (close(fds[0]) < 0)
        do {
          printf("[-] "
                 "close"
                 ": %s"
                 "\n",
                 strerror(errno));
          exit(57);
        } while (0);
    } else {
      if (socketpair(AF_UNIX, SOCK_DGRAM, 0, fds) < 0)
        do {
          printf("[-] "
                 "socketpair"
                 ": %s"
                 "\n",
                 strerror(errno));
          exit(57);
        } while (0);
      create_socket_tree(fds[0], depth + 1);
      send_fd(parent, fds[0]);
      send_fd(parent, fds[1]);
      if (close(fds[0]) || close(fds[1]))
        do {
          printf("[-] "
                 "close"
                 ": %s"
                 "\n",
                 strerror(errno));
          exit(57);
        } while (0);
    }
  }
}


x86 assembly (clang, -O0):
<create_socket_tree>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x20(%rbp)
cmpq   $0x14,-0x20(%rbp)
jae    132c <create_socket_tree+0x18c>
cmpq   $0x0,-0x10(%rbp)
jne    11e5 <create_socket_tree+0x45>
mov    $0x2e,%edi
call   1030 <putchar@plt>
mov    0x2dea(%rip),%rax
mov    (%rax),%edi
call   1090 <fflush@plt>
cmpq   $0x2,-0x10(%rbp)
jne    1248 <create_socket_tree+0xa8>
mov    $0x0,%al
call   1040 <create_filtered_socket@plt>
mov    %eax,-0x18(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x18(%rbp),%esi
call   1050 <send_fd@plt>
mov    -0x18(%rbp),%edi
call   1070 <close@plt>
cmp    $0x0,%rax
jge    1243 <create_socket_tree+0xa3>
jmp    121c <create_socket_tree+0x7c>
mov    0x2dad(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    %rax,%rsi
lea    0xdcc(%rip),%rdi
call   1060 <printf@plt>
mov    $0x39,%edi
call   10b0 <exit@plt>
jmp    1316 <create_socket_tree+0x176>
mov    0x2d99(%rip),%rax
mov    (%rax),%edi
mov    0x2d88(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rcx
xor    %edx,%edx
call   1080 <socketpair@plt>
cmp    $0x0,%rax
jge    129b <create_socket_tree+0xfb>
jmp    1274 <create_socket_tree+0xd4>
mov    0x2d55(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    %rax,%rsi
lea    0xd83(%rip),%rdi
call   1060 <printf@plt>
mov    $0x39,%edi
call   10b0 <exit@plt>
mov    -0x18(%rbp),%edi
mov    -0x10(%rbp),%rsi
add    $0x1,%rsi
call   10a0 <create_socket_tree@plt>
mov    -0x4(%rbp),%edi
mov    -0x18(%rbp),%esi
call   1050 <send_fd@plt>
mov    -0x4(%rbp),%edi
mov    -0x14(%rbp),%esi
call   1050 <send_fd@plt>
mov    -0x18(%rbp),%edi
call   1070 <close@plt>
cmp    $0x0,%rax
jne    12e5 <create_socket_tree+0x145>
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
cmp    $0x0,%rax
je     1311 <create_socket_tree+0x171>
jmp    12ea <create_socket_tree+0x14a>
mov    0x2cdf(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    %rax,%rsi
lea    0xcfe(%rip),%rdi
call   1060 <printf@plt>
mov    $0x39,%edi
call   10b0 <exit@plt>
jmp    1316 <create_socket_tree+0x176>
jmp    131b <create_socket_tree+0x17b>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    11b7 <create_socket_tree+0x17>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_socket_tree>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %edi,%r12d
lea    0x1(%rsi),%r14
mov    $0x14,%ebp
mov    0x2e1f(%rip),%r13
mov    %rsp,%r15
jmp    11ff <create_socket_tree+0x5f>
cs nopw 0x0(%rax,%rax,1)
xor    %eax,%eax
call   1040 <create_filtered_socket@plt>
mov    %eax,(%rsp)
mov    %r12d,%edi
mov    %eax,%esi
call   1050 <send_fd@plt>
mov    (%rsp),%edi
call   1070 <close@plt>
test   %rax,%rax
js     127f <create_socket_tree+0xdf>
add    $0xffffffffffffffff,%rbp
je     1296 <create_socket_tree+0xf6>
cmp    $0x2,%rbx
je     11d0 <create_socket_tree+0x30>
test   %rbx,%rbx
jne    1222 <create_socket_tree+0x82>
mov    $0x2e,%edi
call   1030 <putchar@plt>
mov    0x2dad(%rip),%rax
mov    (%rax),%edi
call   1090 <fflush@plt>
mov    0x2dbf(%rip),%rax
mov    (%rax),%edi
mov    0x0(%r13),%esi
xor    %edx,%edx
mov    %r15,%rcx
call   1080 <socketpair@plt>
test   %rax,%rax
js     12a5 <create_socket_tree+0x105>
mov    (%rsp),%edi
mov    %r14,%rsi
call   10a0 <create_socket_tree@plt>
mov    (%rsp),%esi
mov    %r12d,%edi
call   1050 <send_fd@plt>
mov    0x4(%rsp),%esi
mov    %r12d,%edi
call   1050 <send_fd@plt>
mov    (%rsp),%edi
call   1070 <close@plt>
test   %rax,%rax
jne    127f <create_socket_tree+0xdf>
mov    0x4(%rsp),%edi
call   1070 <close@plt>
test   %rax,%rax
je     11f5 <create_socket_tree+0x55>
mov    0x2d4a(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
lea    0xd6c(%rip),%rdi
jmp    12ba <create_socket_tree+0x11a>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d24(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
lea    0xd55(%rip),%rdi
mov    %rax,%rsi
call   1060 <printf@plt>
mov    $0x39,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@stdout = global i32 0
@.str = [15 x i8] c"[-] close: %s\0A\00"
@errno = global i32 0
@AF_UNIX = global i32 0
@SOCK_DGRAM = global i32 0
@.str.1 = [20 x i8] c"[-] socketpair: %s\0A\00"
define void @create_socket_tree(i32 %0, i64 %1) {
%3 = alloca i32
%4 = alloca i64
%5 = alloca [2 x i32]
%6 = alloca i64
store i32 %0, i32* %3
store i64 %1, i64* %4
store i64 0, i64* %6
br label %7
7: ; preds = %83, %2
%8 = load i64, i64* %6
%9 = icmp ult i64 %8, 20
br i1 %9, label %10, label %86
10: ; preds = %7
%11 = load i64, i64* %4
%12 = icmp eq i64 %11, 0
br i1 %12, label %13, label %17
13: ; preds = %10
%14 = call i32 @putchar(i8 signext 46)
%15 = load i32, i32* @stdout
%16 = call i32 @fflush(i32 %15)
br label %17
17: ; preds = %13, %10
%18 = load i64, i64* %4
%19 = icmp eq i64 %18, 2
br i1 %19, label %20, label %39
20: ; preds = %17
%21 = call i32 (...) @create_filtered_socket()
%22 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 0
store i32 %21, i32* %22
%23 = load i32, i32* %3
%24 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 0
%25 = load i32, i32* %24
%26 = call i32 @send_fd(i32 %23, i32 %25)
%27 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 0
%28 = load i32, i32* %27
%29 = call i64 @close(i32 %28)
%30 = icmp slt i64 %29, 0
br i1 %30, label %31, label %38
31: ; preds = %20
br label %32
32: ; preds = %31
%33 = load i32, i32* @errno
%34 = call i8* @strerror(i32 %33)
%35 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %34)
%36 = call i32 @exit(i32 57)
unreachable
37: ; No predecessors!
br label %38
38: ; preds = %37, %20
br label %82
39: ; preds = %17
%40 = load i32, i32* @AF_UNIX
%41 = load i32, i32* @SOCK_DGRAM
%42 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 0
%43 = call i64 @socketpair(i32 %40, i32 %41, i32 0, i32* %42)
%44 = icmp slt i64 %43, 0
br i1 %44, label %45, label %52
45: ; preds = %39
br label %46
46: ; preds = %45
%47 = load i32, i32* @errno
%48 = call i8* @strerror(i32 %47)
%49 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8* %48)
%50 = call i32 @exit(i32 57)
unreachable
51: ; No predecessors!
br label %52
52: ; preds = %51, %39
%53 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 0
%54 = load i32, i32* %53
%55 = load i64, i64* %4
%56 = add i64 %55, 1
%57 = load i32, i32* %3
%58 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 0
%59 = load i32, i32* %58
%60 = call i32 @send_fd(i32 %57, i32 %59)
%61 = load i32, i32* %3
%62 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 1
%63 = load i32, i32* %62
%64 = call i32 @send_fd(i32 %61, i32 %63)
%65 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 0
%66 = load i32, i32* %65
%67 = call i64 @close(i32 %66)
%68 = icmp ne i64 %67, 0
br i1 %68, label %74, label %69
69: ; preds = %52
%70 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 1
%71 = load i32, i32* %70
%72 = call i64 @close(i32 %71)
%73 = icmp ne i64 %72, 0
br i1 %73, label %74, label %81
74: ; preds = %69, %52
br label %75
75: ; preds = %74
%76 = load i32, i32* @errno
%77 = call i8* @strerror(i32 %76)
%78 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %77)
%79 = call i32 @exit(i32 57)
unreachable
80: ; No predecessors!
br label %81
81: ; preds = %80, %69
br label %82
82: ; preds = %81, %38
br label %83
83: ; preds = %82
%84 = load i64, i64* %6
%85 = add i64 %84, 1
store i64 %85, i64* %6
br label %7
86: ; preds = %7
ret void
}

LLVM IR (-O2):
@stdout = global i32 0
@.str = [15 x i8] c"[-] close: %s\0A\00"
@errno = global i32 0
@AF_UNIX = global i32 0
@SOCK_DGRAM = global i32 0
@.str.1 = [20 x i8] c"[-] socketpair: %s\0A\00"
define void @create_socket_tree(i32 %0, i64 %1) {
%3 = alloca [2 x i32]
%4 = bitcast [2 x i32]* %3 to i8*
%5 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%6 = add i64 %1, 1
%7 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
br label %8
8: ; preds = %2, %53
%9 = phi i64 [ 0, %2 ], [ %54, %53 ]
switch i64 %1, label %25 [
i64 0, label %10
i64 2, label %14
]
10: ; preds = %8
%11 = call i32 @putchar(i8 signext 46)
%12 = load i32, i32* @stdout
%13 = call i32 @fflush(i32 %12)
br label %25
14: ; preds = %8
%15 = call i32 (...) @create_filtered_socket()
store i32 %15, i32* %5
%16 = call i32 @send_fd(i32 %0, i32 %15)
%17 = load i32, i32* %5
%18 = call i64 @close(i32 %17)
%19 = icmp slt i64 %18, 0
br i1 %19, label %20, label %53
20: ; preds = %14
%21 = load i32, i32* @errno
%22 = call i8* @strerror(i32 %21)
%23 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %22)
%24 = call i32 @exit(i32 57)
unreachable
25: ; preds = %10, %8
%26 = load i32, i32* @AF_UNIX
%27 = load i32, i32* @SOCK_DGRAM
%28 = call i64 @socketpair(i32 %26, i32 %27, i32 0, i32* %5)
%29 = icmp slt i64 %28, 0
br i1 %29, label %30, label %35
30: ; preds = %25
%31 = load i32, i32* @errno
%32 = call i8* @strerror(i32 %31)
%33 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8* %32)
%34 = call i32 @exit(i32 57)
unreachable
35: ; preds = %25
%36 = load i32, i32* %5
%37 = load i32, i32* %5
%38 = call i32 @send_fd(i32 %0, i32 %37)
%39 = load i32, i32* %7
%40 = call i32 @send_fd(i32 %0, i32 %39)
%41 = load i32, i32* %5
%42 = call i64 @close(i32 %41)
%43 = icmp eq i64 %42, 0
br i1 %43, label %44, label %48
44: ; preds = %35
%45 = load i32, i32* %7
%46 = call i64 @close(i32 %45)
%47 = icmp eq i64 %46, 0
br i1 %47, label %53, label %48
48: ; preds = %35, %44
%49 = load i32, i32* @errno
%50 = call i8* @strerror(i32 %49)
%51 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %50)
%52 = call i32 @exit(i32 57)
unreachable
53: ; preds = %14, %44
%54 = add nuw nsw i64 %9, 1
%55 = icmp eq i64 %54, 20
br i1 %55, label %56, label %8
56: ; preds = %53
ret void
}