#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*/ self;
typedef int /*<<< orphan*/ client_info;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ abort();
int accept(int, struct sockaddr *, int *);
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ bzero(struct sockaddr_in *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int, int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ execl(char *, char *, char *, char *, int /*<<< orphan*/);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int /*<<< orphan*/ htons(int);
char *inet_ntoa(TYPE_1__);
scalar_t__ listen(int, int);
int ntohs(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main() {
  int sd;
  struct sockaddr_in self;
  if ((sd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
    perror("Socket");
    exit(errno);
  }
  bzero(&self, sizeof(self));
  self.sin_family = AF_INET;
  self.sin_addr.s_addr = INADDR_ANY;
  self.sin_port = htons(9999);
  if (bind(sd, (struct sockaddr *)&self, sizeof(self)) != 0) {
    perror("Bind");
    abort();
  }
  if (listen(sd, 20) != 0) {
    perror("Listen");
    abort();
  }
  while (1) {
    static int client;
    int sd_copy, size;
    struct sockaddr_in client_info;
    size = sizeof(client_info);
    client = accept(sd, (struct sockaddr *)&client_info, &size);
    if (client < 0)
      perror("Accept");
    else {
      printf("Conectado por: %s:%d\n", inet_ntoa(client_info.sin_addr),
             ntohs(client_info.sin_port));
      if (fork() != 0) {
        close(client);
      } else {
        close(sd);
        dup2(client, 0);
        dup2(client, 1);
        dup2(client, 2);
        execl("/bin/ls", "/bin/ls", "-al", "/sbin", 0);
      }
    }
  }
  close(sd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2dd2(%rip),%rax
mov    (%rax),%edi
mov    0x2db1(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1120 <socket@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jge    124e <main+0x4e>
lea    0xdc5(%rip),%rdi
call   10c0 <perror@plt>
mov    0x2d81(%rip),%rax
mov    (%rax),%edi
call   10f0 <exit@plt>
lea    -0x18(%rbp),%rdi
mov    $0xc,%esi
call   10d0 <bzero@plt>
mov    0x2d85(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    0x2d71(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    $0x270f,%edi
call   1050 <htons@plt>
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   10b0 <bind@plt>
cmp    $0x0,%rax
je     12af <main+0xaf>
lea    0xd64(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x0,%al
call   1030 <abort@plt>
mov    -0x8(%rbp),%edi
mov    $0x14,%esi
call   1090 <listen@plt>
cmp    $0x0,%rax
je     12d9 <main+0xd9>
lea    0xd3f(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x0,%al
call   1030 <abort@plt>
jmp    12de <main+0xde>
movl   $0xc,-0x20(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x30(%rbp),%rsi
lea    -0x20(%rbp),%rdx
call   10e0 <accept@plt>
mov    %eax,0x2db9(%rip)
cmpl   $0x0,0x2db2(%rip)
jge    1319 <main+0x119>
lea    0xd04(%rip),%rdi
call   10c0 <perror@plt>
jmp    13ba <main+0x1ba>
mov    -0x2c(%rbp),%edi
call   1040 <inet_ntoa@plt>
mov    %rax,-0x38(%rbp)
mov    -0x30(%rbp),%edi
call   10a0 <ntohs@plt>
mov    -0x38(%rbp),%rsi
mov    %eax,%edx
lea    0xce0(%rip),%rdi
call   1070 <printf@plt>
mov    $0x0,%al
call   1110 <fork@plt>
cmp    $0x0,%rax
je     1360 <main+0x160>
mov    0x2d5e(%rip),%edi
call   1080 <close@plt>
jmp    13b5 <main+0x1b5>
mov    -0x8(%rbp),%edi
call   1080 <close@plt>
mov    0x2d46(%rip),%edi
xor    %esi,%esi
call   1060 <dup2@plt>
mov    0x2d39(%rip),%edi
mov    $0x1,%esi
call   1060 <dup2@plt>
mov    0x2d29(%rip),%edi
mov    $0x2,%esi
call   1060 <dup2@plt>
lea    0xc94(%rip),%rsi
lea    0xc95(%rip),%rdx
lea    0xc92(%rip),%rcx
xor    %r8d,%r8d
mov    %rsi,%rdi
call   1100 <execl@plt>
jmp    13ba <main+0x1ba>
jmp    12de <main+0xde>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    0x2dd3(%rip),%r14
mov    (%r14),%edi
mov    0x2db1(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1120 <socket@plt>
test   %eax,%eax
js     1370 <main+0x170>
mov    %eax,%ebx
lea    0x8(%rsp),%rbp
mov    %rbp,%rdi
mov    $0xc,%esi
call   10d0 <bzero@plt>
mov    (%r14),%eax
mov    %eax,0x10(%rsp)
mov    0x2d8e(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
mov    $0x270f,%edi
call   1050 <htons@plt>
mov    %eax,0x8(%rsp)
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0xc,%edx
call   10b0 <bind@plt>
test   %rax,%rax
jne    138a <main+0x18a>
mov    %ebx,%edi
mov    $0x14,%esi
call   1090 <listen@plt>
test   %rax,%rax
jne    1393 <main+0x193>
lea    0x18(%rsp),%r14
lea    0x4(%rsp),%r15
lea    0xd76(%rip),%r13
lea    0xd85(%rip),%rbp
jmp    12bc <main+0xbc>
nopl   (%rax)
lea    0xd5c(%rip),%rdi
call   10c0 <perror@plt>
movl   $0xc,0x4(%rsp)
mov    %ebx,%edi
mov    %r14,%rsi
mov    %r15,%rdx
call   10e0 <accept@plt>
mov    %eax,0x2ddd(%rip)
test   %eax,%eax
js     12b0 <main+0xb0>
mov    0x1c(%rsp),%edi
call   1040 <inet_ntoa@plt>
mov    %rax,%r12
mov    0x18(%rsp),%edi
call   10a0 <ntohs@plt>
mov    %r13,%rdi
mov    %r12,%rsi
mov    %eax,%edx
call   1070 <printf@plt>
xor    %eax,%eax
call   1110 <fork@plt>
test   %rax,%rax
jne    1360 <main+0x160>
mov    %ebx,%edi
call   1080 <close@plt>
mov    0x2d9e(%rip),%edi
xor    %esi,%esi
call   1060 <dup2@plt>
mov    0x2d91(%rip),%edi
mov    $0x1,%esi
call   1060 <dup2@plt>
mov    0x2d81(%rip),%edi
mov    $0x2,%esi
call   1060 <dup2@plt>
mov    %rbp,%rdi
mov    %rbp,%rsi
lea    0xcee(%rip),%rdx
lea    0xceb(%rip),%rcx
xor    %r8d,%r8d
call   1100 <execl@plt>
jmp    12bc <main+0xbc>
xchg   %ax,%ax
mov    0x2d4e(%rip),%edi
call   1080 <close@plt>
jmp    12bc <main+0xbc>
lea    0xc89(%rip),%rdi
call   10c0 <perror@plt>
mov    0x2c45(%rip),%rax
mov    (%rax),%edi
call   10f0 <exit@plt>
lea    0xc76(%rip),%rdi
jmp    139a <main+0x19a>
lea    0xc72(%rip),%rdi
call   10c0 <perror@plt>
xor    %eax,%eax
call   1030 <abort@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 = [7 x i8] c"Socket\00"
@errno = global i32 0
@INADDR_ANY = global i32 0
@.str.1 = [5 x i8] c"Bind\00"
@.str.2 = [7 x i8] c"Listen\00"
@main.client = internal global i32 0
@.str.3 = [7 x i8] c"Accept\00"
@.str.4 = [22 x i8] c"Conectado por: %s:%d\0A\00"
@.str.5 = [8 x i8] c"/bin/ls\00"
@.str.6 = [4 x i8] c"-al\00"
@.str.7 = [6 x i8] c"/sbin\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.sockaddr_in
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.sockaddr_in
store i32 0, i32* %1
%7 = load i32, i32* @AF_INET
%8 = load i32, i32* @SOCK_STREAM
%9 = call i32 @socket(i32 %7, i32 %8, i32 0)
store i32 %9, i32* %2
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %15
11: ; preds = %0
%12 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%13 = load i32, i32* @errno
%14 = call i32 @exit(i32 %13)
unreachable
15: ; preds = %0
%16 = call i32 @bzero(%struct.sockaddr_in* %3, i32 12)
%17 = load i32, i32* @AF_INET
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 2
store i32 %17, i32* %18
%19 = load i32, i32* @INADDR_ANY
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 1
%21 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %20, i32 0, i32 0
store i32 %19, i32* %21
%22 = call i32 @htons(i32 9999)
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 0
store i32 %22, i32* %23
%24 = load i32, i32* %2
%25 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%26 = call i64 @bind(i32 %24, %struct.sockaddr* %25, i32 12)
%27 = icmp ne i64 %26, 0
br i1 %27, label %28, label %31
28: ; preds = %15
%29 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%30 = call i32 (...) @abort()
unreachable
31: ; preds = %15
%32 = load i32, i32* %2
%33 = call i64 @listen(i32 %32, i32 20)
%34 = icmp ne i64 %33, 0
br i1 %34, label %35, label %38
35: ; preds = %31
%36 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%37 = call i32 (...) @abort()
unreachable
38: ; preds = %31
br label %39
39: ; preds = %38, %72
store i32 12, i32* %5
%40 = load i32, i32* %2
%41 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%42 = call i32 @accept(i32 %40, %struct.sockaddr* %41, i32* %5)
store i32 %42, i32* @main.client
%43 = load i32, i32* @main.client
%44 = icmp slt i32 %43, 0
br i1 %44, label %45, label %47
45: ; preds = %39
%46 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
br label %72
47: ; preds = %39
%48 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
%49 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %48, i32 0, i32 0
%50 = load i32, i32* %49
%51 = call i8* @inet_ntoa(i32 %50)
%52 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
%53 = load i32, i32* %52
%54 = call i32 @ntohs(i32 %53)
%55 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0), i8* %51, i32 %54)
%56 = call i64 (...) @fork()
%57 = icmp ne i64 %56, 0
br i1 %57, label %58, label %61
58: ; preds = %47
%59 = load i32, i32* @main.client
%60 = call i32 @close(i32 %59)
br label %71
61: ; preds = %47
%62 = load i32, i32* %2
%63 = call i32 @close(i32 %62)
%64 = load i32, i32* @main.client
%65 = call i32 @dup2(i32 %64, i32 0)
%66 = load i32, i32* @main.client
%67 = call i32 @dup2(i32 %66, i32 1)
%68 = load i32, i32* @main.client
%69 = call i32 @dup2(i32 %68, i32 2)
%70 = call i32 @execl(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.7, i64 0, i64 0), i32 0)
br label %71
71: ; preds = %61, %58
br label %72
72: ; preds = %71, %45
br label %39
}

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
@.str = [7 x i8] c"Socket\00"
@errno = global i32 0
@INADDR_ANY = global i32 0
@.str.1 = [5 x i8] c"Bind\00"
@.str.2 = [7 x i8] c"Listen\00"
@main.client = internal unnamed_addr global i32 0
@.str.3 = [7 x i8] c"Accept\00"
@.str.4 = [22 x i8] c"Conectado por: %s:%d\0A\00"
@.str.5 = [8 x i8] c"/bin/ls\00"
@.str.6 = [4 x i8] c"-al\00"
@.str.7 = [6 x i8] c"/sbin\00"
define i32 @main() {
%1 = alloca %struct.sockaddr_in
%2 = alloca i32
%3 = alloca %struct.sockaddr_in
%4 = bitcast %struct.sockaddr_in* %1 to i8*
%5 = load i32, i32* @AF_INET
%6 = load i32, i32* @SOCK_STREAM
%7 = tail call i32 @socket(i32 %5, i32 %6, i32 0)
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %13
9: ; preds = %0
%10 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%11 = load i32, i32* @errno
%12 = tail call i32 @exit(i32 %11)
unreachable
13: ; preds = %0
%14 = call i32 @bzero(%struct.sockaddr_in* %1, i32 12)
%15 = load i32, i32* @AF_INET
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 2
store i32 %15, i32* %16
%17 = load i32, i32* @INADDR_ANY
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1, i32 0
store i32 %17, i32* %18
%19 = tail call i32 @htons(i32 9999)
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0
store i32 %19, i32* %20
%21 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%22 = call i64 @bind(i32 %7, %struct.sockaddr* %21, i32 12)
%23 = icmp eq i64 %22, 0
br i1 %23, label %27, label %24
24: ; preds = %13
%25 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%26 = call i32 (...) @abort()
unreachable
27: ; preds = %13
%28 = call i64 @listen(i32 %7, i32 20)
%29 = icmp eq i64 %28, 0
br i1 %29, label %30, label %36
30: ; preds = %27
%31 = bitcast i32* %2 to i8*
%32 = bitcast %struct.sockaddr_in* %3 to i8*
%33 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%34 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1, i32 0
%35 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
br label %39
36: ; preds = %27
%37 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%38 = call i32 (...) @abort()
unreachable
39: ; preds = %30, %64
store i32 12, i32* %2
%40 = call i32 @accept(i32 %7, %struct.sockaddr* %33, i32* %2)
store i32 %40, i32* @main.client
%41 = icmp slt i32 %40, 0
br i1 %41, label %42, label %44
42: ; preds = %39
%43 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
br label %64
44: ; preds = %39
%45 = load i32, i32* %34
%46 = call i8* @inet_ntoa(i32 %45)
%47 = load i32, i32* %35
%48 = call i32 @ntohs(i32 %47)
%49 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0), i8* %46, i32 %48)
%50 = call i64 (...) @fork()
%51 = icmp eq i64 %50, 0
br i1 %51, label %55, label %52
52: ; preds = %44
%53 = load i32, i32* @main.client
%54 = call i32 @close(i32 %53)
br label %64
55: ; preds = %44
%56 = call i32 @close(i32 %7)
%57 = load i32, i32* @main.client
%58 = call i32 @dup2(i32 %57, i32 0)
%59 = load i32, i32* @main.client
%60 = call i32 @dup2(i32 %59, i32 1)
%61 = load i32, i32* @main.client
%62 = call i32 @dup2(i32 %61, i32 2)
%63 = call i32 @execl(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.7, i64 0, i64 0), i32 0)
br label %64
64: ; preds = %52, %55, %42
br label %39
}