#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;
};
typedef int /*<<< orphan*/ sockaddr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SIGCHLD;
int /*<<< orphan*/ SIG_IGN;
int /*<<< orphan*/ SOCK_STREAM;
int accept(int, struct sockaddr *, int *);
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int fork();
int /*<<< orphan*/ htons(int);
scalar_t__ listen(int, int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ procesar(int);
int /*<<< orphan*/ signal(int /*<<< orphan*/, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main() {
  struct sockaddr_in sockaddr, clientaddr;
  int s;
  int ns;
  int pid;
  int len;
  int portno = 51717;
  if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0)
    perror("Error al crear socket\n");
  memset(&sockaddr, 0, sizeof(sockaddr));
  sockaddr.sin_family = AF_INET;
  sockaddr.sin_port = htons(portno);
  sockaddr.sin_addr.s_addr = INADDR_ANY;
  len = sizeof(sockaddr);
  if (bind(s, (struct sockaddr *)&sockaddr, len) < 0)
    perror("Error en el bind\n");
  signal(SIGCHLD, SIG_IGN);
  if (listen(s, 10) < 0)
    perror("Eror en el listen\n");
  printf("Socket disponible\n");
  while (1) {
    len = sizeof(sockaddr);
    if ((ns = accept(s, (struct sockaddr *)&sockaddr, &len)) < 0)
      perror("Error en el accept\n");
    printf("Conexion establecida\n");
    if ((pid = fork()) < 0) {
      perror("Error en fork\n");
      exit(1);
    }
    if (pid == 0) {
      printf("Atendiendo conexion\n");
      procesar(ns);
      exit(0);
    } else
      close(ns);
  }
  return (0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0xca05,-0x34(%rbp)
mov    0x2dfb(%rip),%rax
mov    (%rax),%edi
mov    0x2dca(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jge    1217 <main+0x47>
lea    0xdee(%rip),%rdi
call   10b0 <perror@plt>
lea    -0x10(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    0x2dba(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x8(%rbp)
mov    -0x34(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0xc(%rbp)
mov    0x2d9b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
movl   $0xc,-0x30(%rbp)
mov    -0x24(%rbp),%edi
lea    -0x10(%rbp),%rsi
mov    -0x30(%rbp),%edx
call   10a0 <bind@plt>
cmp    $0x0,%rax
jge    1276 <main+0xa6>
lea    0xda6(%rip),%rdi
call   10b0 <perror@plt>
mov    0x2d5b(%rip),%rax
mov    (%rax),%edi
mov    0x2d4a(%rip),%rax
mov    (%rax),%esi
call   1080 <signal@plt>
mov    -0x24(%rbp),%edi
mov    $0xa,%esi
call   1090 <listen@plt>
cmp    $0x0,%rax
jge    12b0 <main+0xe0>
lea    0xd7e(%rip),%rdi
call   10b0 <perror@plt>
lea    0xd85(%rip),%rdi
call   1040 <printf@plt>
movl   $0xc,-0x30(%rbp)
mov    -0x24(%rbp),%edi
lea    -0x10(%rbp),%rsi
lea    -0x30(%rbp),%rdx
call   10c0 <accept@plt>
mov    %eax,-0x28(%rbp)
cmp    $0x0,%eax
jge    12eb <main+0x11b>
lea    0xd69(%rip),%rdi
call   10b0 <perror@plt>
lea    0xd71(%rip),%rdi
call   1040 <printf@plt>
mov    $0x0,%al
call   10e0 <fork@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0x0,%eax
jge    1320 <main+0x150>
lea    0xd68(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
cmpl   $0x0,-0x2c(%rbp)
jne    1345 <main+0x175>
lea    0xd57(%rip),%rdi
call   1040 <printf@plt>
mov    -0x28(%rbp),%edi
call   1060 <procesar@plt>
xor    %edi,%edi
call   10d0 <exit@plt>
mov    -0x28(%rbp),%edi
call   1070 <close@plt>
jmp    12bc <main+0xec>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    0x2e03(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2dd1(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
mov    %eax,%r14d
test   %eax,%eax
jns    120b <main+0x3b>
lea    0xdfa(%rip),%rdi
call   10b0 <perror@plt>
lea    0x8(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    0x0(%rbp),%eax
mov    %eax,0x10(%rsp)
mov    $0xca05,%edi
call   1040 <htons@plt>
mov    %eax,0xc(%rsp)
mov    0x2da5(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0xc,%edx
call   10a0 <bind@plt>
test   %rax,%rax
jns    1262 <main+0x92>
lea    0xdba(%rip),%rdi
call   10b0 <perror@plt>
mov    0x2d6f(%rip),%rax
mov    (%rax),%edi
mov    0x2d5e(%rip),%rax
mov    (%rax),%esi
call   1080 <signal@plt>
mov    %r14d,%edi
mov    $0xa,%esi
call   1090 <listen@plt>
test   %rax,%rax
jns    1297 <main+0xc7>
lea    0xd97(%rip),%rdi
call   10b0 <perror@plt>
lea    0xdc1(%rip),%rdi
call   1030 <puts@plt>
lea    0x8(%rsp),%r12
lea    0x4(%rsp),%r13
lea    0xd88(%rip),%r15
lea    0xdb6(%rip),%rbx
nopl   0x0(%rax,%rax,1)
movl   $0xc,0x4(%rsp)
mov    %r14d,%edi
mov    %r12,%rsi
mov    %r13,%rdx
call   10c0 <accept@plt>
mov    %eax,%ebp
test   %eax,%eax
js     12fa <main+0x12a>
mov    %rbx,%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   10e0 <fork@plt>
test   %eax,%eax
js     1304 <main+0x134>
je     131a <main+0x14a>
mov    %ebp,%edi
call   1070 <close@plt>
jmp    12c0 <main+0xf0>
mov    %r15,%rdi
call   10b0 <perror@plt>
jmp    12dc <main+0x10c>
lea    0xd45(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
lea    0xd65(%rip),%rdi
call   1030 <puts@plt>
mov    %ebp,%edi
call   1060 <procesar@plt>
xor    %edi,%edi
call   10d0 <exit@plt>

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 = [23 x i8] c"Error al crear socket\0A\00"
@INADDR_ANY = global i32 0
@.str.1 = [18 x i8] c"Error en el bind\0A\00"
@SIGCHLD = global i32 0
@SIG_IGN = global i32 0
@.str.2 = [19 x i8] c"Eror en el listen\0A\00"
@.str.3 = [19 x i8] c"Socket disponible\0A\00"
@.str.4 = [20 x i8] c"Error en el accept\0A\00"
@.str.5 = [22 x i8] c"Conexion establecida\0A\00"
@.str.6 = [15 x i8] c"Error en fork\0A\00"
@.str.7 = [21 x i8] c"Atendiendo conexion\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca %struct.sockaddr_in
%3 = alloca %struct.sockaddr_in
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32 0, i32* %1
store i32 51717, i32* %8
%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 = %0
%14 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
br label %15
15: ; preds = %13, %0
%16 = call i32 @memset(%struct.sockaddr_in* %2, i32 0, i32 12)
%17 = load i32, i32* @AF_INET
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i32 0, i32 2
store i32 %17, i32* %18
%19 = load i32, i32* %8
%20 = call i32 @htons(i32 %19)
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i32 0, i32 1
store i32 %20, i32* %21
%22 = load i32, i32* @INADDR_ANY
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i32 0, i32 0
%24 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %23, i32 0, i32 0
store i32 %22, i32* %24
store i32 12, i32* %7
%25 = load i32, i32* %4
%26 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%27 = load i32, i32* %7
%28 = call i64 @bind(i32 %25, %struct.sockaddr* %26, i32 %27)
%29 = icmp slt i64 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %15
%31 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
br label %32
32: ; preds = %30, %15
%33 = load i32, i32* @SIGCHLD
%34 = load i32, i32* @SIG_IGN
%35 = call i32 @signal(i32 %33, i32 %34)
%36 = load i32, i32* %4
%37 = call i64 @listen(i32 %36, i32 10)
%38 = icmp slt i64 %37, 0
br i1 %38, label %39, label %41
39: ; preds = %32
%40 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
br label %41
41: ; preds = %39, %32
%42 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0))
br label %43
43: ; preds = %41, %68
store i32 12, i32* %7
%44 = load i32, i32* %4
%45 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%46 = call i32 @accept(i32 %44, %struct.sockaddr* %45, i32* %7)
store i32 %46, i32* %5
%47 = icmp slt i32 %46, 0
br i1 %47, label %48, label %50
48: ; preds = %43
%49 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0))
br label %50
50: ; preds = %48, %43
%51 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0))
%52 = call i32 (...) @fork()
store i32 %52, i32* %6
%53 = icmp slt i32 %52, 0
br i1 %53, label %54, label %57
54: ; preds = %50
%55 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.6, i64 0, i64 0))
%56 = call i32 @exit(i32 1)
unreachable
57: ; preds = %50
%58 = load i32, i32* %6
%59 = icmp eq i32 %58, 0
br i1 %59, label %60, label %65
60: ; preds = %57
%61 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.7, i64 0, i64 0))
%62 = load i32, i32* %5
%63 = call i32 @procesar(i32 %62)
%64 = call i32 @exit(i32 0)
unreachable
65: ; preds = %57
%66 = load i32, i32* %5
%67 = call i32 @close(i32 %66)
br label %68
68: ; preds = %65
br label %43
}

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 = [23 x i8] c"Error al crear socket\0A\00"
@INADDR_ANY = global i32 0
@.str.1 = [18 x i8] c"Error en el bind\0A\00"
@SIGCHLD = global i32 0
@SIG_IGN = global i32 0
@.str.2 = [19 x i8] c"Eror en el listen\0A\00"
@.str.4 = [20 x i8] c"Error en el accept\0A\00"
@.str.6 = [15 x i8] c"Error en fork\0A\00"
@str = [18 x i8] c"Socket disponible\00"
@str.8 = [21 x i8] c"Conexion establecida\00"
@str.9 = [20 x i8] c"Atendiendo conexion\00"
define i32 @main() {
%1 = alloca %struct.sockaddr_in
%2 = alloca i32
%3 = bitcast %struct.sockaddr_in* %1 to i8*
%4 = bitcast i32* %2 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 %11
9: ; preds = %0
%10 = tail call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
br label %11
11: ; preds = %9, %0
%12 = call i32 @memset(%struct.sockaddr_in* %1, i32 0, i32 12)
%13 = load i32, i32* @AF_INET
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 2
store i32 %13, i32* %14
%15 = call i32 @htons(i32 51717)
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1
store i32 %15, i32* %16
%17 = load i32, i32* @INADDR_ANY
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0, i32 0
store i32 %17, i32* %18
%19 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%20 = call i64 @bind(i32 %7, %struct.sockaddr* %19, i32 12)
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %11
%23 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
br label %24
24: ; preds = %22, %11
%25 = load i32, i32* @SIGCHLD
%26 = load i32, i32* @SIG_IGN
%27 = call i32 @signal(i32 %25, i32 %26)
%28 = call i64 @listen(i32 %7, i32 10)
%29 = icmp slt i64 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %24
%31 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0))
br label %32
32: ; preds = %30, %24
%33 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str, i64 0, i64 0))
br label %34
34: ; preds = %52, %32
store i32 12, i32* %2
%35 = call i32 @accept(i32 %7, %struct.sockaddr* %19, i32* %2)
%36 = icmp slt i32 %35, 0
br i1 %36, label %37, label %39
37: ; preds = %34
%38 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0))
br label %39
39: ; preds = %37, %34
%40 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str.8, i64 0, i64 0))
%41 = call i32 (...) @fork()
%42 = icmp slt i32 %41, 0
br i1 %42, label %43, label %46
43: ; preds = %39
%44 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.6, i64 0, i64 0))
%45 = call i32 @exit(i32 1)
unreachable
46: ; preds = %39
%47 = icmp eq i32 %41, 0
br i1 %47, label %48, label %52
48: ; preds = %46
%49 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str.9, i64 0, i64 0))
%50 = call i32 @procesar(i32 %35)
%51 = call i32 @exit(i32 0)
unreachable
52: ; preds = %46
%53 = call i32 @close(i32 %35)
br label %34
}