#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 */

/* Type definitions */

/* Variables and functions */
int /*<<< orphan*/ SIGPIPE;
int /*<<< orphan*/ SIG_IGN;
int acepta_cliente(int);
int /*<<< orphan*/ atiende_cliente(int);
scalar_t__ close(int);
int /*<<< orphan*/ error_fatal(char *);
int espera_recepcion(int, int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int inicia_socket_servidor(char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ signal(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int main() {
  int s_servidor;
  int s_cliente;
  int continuar = 1;
  signal(SIGPIPE, SIG_IGN);
  s_servidor = inicia_socket_servidor("cpr");
  while (continuar) {
    printf("Esperando conexiones...\n");
    if (espera_recepcion(s_servidor, 30) == 1) {
      s_cliente = acepta_cliente(s_servidor);
      if (s_cliente >= 0) {
        atiende_cliente(s_cliente);
        if (close(s_cliente) < 0)
          fprintf(stderr, "Error cerrando socket de cliente\n");
        printf("Conexion con el cliente cerrada\n");
      }
    } else
      continuar = 0;
  }
  fprintf(stderr, "*** no oigo nada ***\n");
  if (close(s_servidor) < 0)
    error_fatal("close");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x10(%rbp)
mov    0x2e23(%rip),%rax
mov    (%rax),%edi
mov    0x2e32(%rip),%rax
mov    (%rax),%esi
call   1060 <signal@plt>
lea    0xe3c(%rip),%rdi
call   1090 <inicia_socket_servidor@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x10(%rbp)
je     1259 <main+0xc9>
lea    0xe27(%rip),%rdi
call   1040 <printf@plt>
mov    -0x8(%rbp),%edi
mov    $0x1e,%esi
call   1070 <espera_recepcion@plt>
cmp    $0x1,%eax
jne    124d <main+0xbd>
mov    -0x8(%rbp),%edi
call   10a0 <acepta_cliente@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jl     1248 <main+0xb8>
mov    -0xc(%rbp),%edi
call   1030 <atiende_cliente@plt>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
cmp    $0x0,%rax
jge    123c <main+0xac>
mov    0x2daa(%rip),%rax
mov    (%rax),%edi
lea    0xde6(%rip),%rsi
call   1080 <fprintf@plt>
lea    0xdfc(%rip),%rdi
call   1040 <printf@plt>
jmp    1254 <main+0xc4>
movl   $0x0,-0x10(%rbp)
jmp    11cc <main+0x3c>
mov    0x2d78(%rip),%rax
mov    (%rax),%edi
lea    0xdf7(%rip),%rsi
call   1080 <fprintf@plt>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
cmp    $0x0,%rax
jge    128c <main+0xfc>
lea    0xdef(%rip),%rdi
call   10b0 <error_fatal@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    0x2e2e(%rip),%rax
mov    (%rax),%edi
mov    0x2e3d(%rip),%rax
mov    (%rax),%esi
call   1060 <signal@plt>
lea    0xe47(%rip),%rdi
call   1090 <inicia_socket_servidor@plt>
mov    %eax,%ebx
lea    0xe7b(%rip),%rdi
call   1040 <puts@plt>
mov    %ebx,%edi
mov    $0x1e,%esi
call   1070 <espera_recepcion@plt>
cmp    $0x1,%eax
jne    124f <main+0xbf>
mov    0x2df4(%rip),%r13
lea    0xe19(%rip),%r14
lea    0xe68(%rip),%r15
lea    0xe49(%rip),%r12
jmp    1221 <main+0x91>
nopl   0x0(%rax,%rax,1)
mov    %r15,%rdi
call   1040 <puts@plt>
mov    %r12,%rdi
call   1040 <puts@plt>
mov    %ebx,%edi
mov    $0x1e,%esi
call   1070 <espera_recepcion@plt>
cmp    $0x1,%eax
jne    124f <main+0xbf>
mov    %ebx,%edi
call   10a0 <acepta_cliente@plt>
test   %eax,%eax
js     1208 <main+0x78>
mov    %eax,%ebp
mov    %eax,%edi
call   1030 <atiende_cliente@plt>
mov    %ebp,%edi
call   1050 <close@plt>
test   %rax,%rax
jns    1200 <main+0x70>
mov    0x0(%r13),%edi
mov    %r14,%rsi
call   1080 <fprintf@plt>
jmp    1200 <main+0x70>
mov    0x2d82(%rip),%rax
mov    (%rax),%edi
lea    0xdc7(%rip),%rsi
call   1080 <fprintf@plt>
mov    %ebx,%edi
call   1050 <close@plt>
test   %rax,%rax
jns    127c <main+0xec>
lea    0xdc5(%rip),%rdi
call   10b0 <error_fatal@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@SIGPIPE = global i32 0
@SIG_IGN = global i32 0
@.str = [4 x i8] c"cpr\00"
@.str.1 = [25 x i8] c"Esperando conexiones...\0A\00"
@stderr = global i32 0
@.str.2 = [34 x i8] c"Error cerrando socket de cliente\0A\00"
@.str.3 = [33 x i8] c"Conexion con el cliente cerrada\0A\00"
@.str.4 = [22 x i8] c"*** no oigo nada ***\0A\00"
@.str.5 = [6 x i8] c"close\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 0, i32* %1
store i32 1, i32* %4
%5 = load i32, i32* @SIGPIPE
%6 = load i32, i32* @SIG_IGN
%7 = call i32 @signal(i32 %5, i32 %6)
%8 = call i32 @inicia_socket_servidor(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
store i32 %8, i32* %2
br label %9
9: ; preds = %35, %0
%10 = load i32, i32* %4
%11 = icmp ne i32 %10, 0
br i1 %11, label %12, label %36
12: ; preds = %9
%13 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
%14 = load i32, i32* %2
%15 = call i32 @espera_recepcion(i32 %14, i32 30)
%16 = icmp eq i32 %15, 1
br i1 %16, label %17, label %34
17: ; preds = %12
%18 = load i32, i32* %2
%19 = call i32 @acepta_cliente(i32 %18)
store i32 %19, i32* %3
%20 = load i32, i32* %3
%21 = icmp sge i32 %20, 0
br i1 %21, label %22, label %33
22: ; preds = %17
%23 = load i32, i32* %3
%24 = call i32 @atiende_cliente(i32 %23)
%25 = load i32, i32* %3
%26 = call i64 @close(i32 %25)
%27 = icmp slt i64 %26, 0
br i1 %27, label %28, label %31
28: ; preds = %22
%29 = load i32, i32* @stderr
%30 = call i32 @fprintf(i32 %29, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0))
br label %31
31: ; preds = %28, %22
%32 = call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0))
br label %33
33: ; preds = %31, %17
br label %35
34: ; preds = %12
store i32 0, i32* %4
br label %35
35: ; preds = %34, %33
br label %9
36: ; preds = %9
%37 = load i32, i32* @stderr
%38 = call i32 @fprintf(i32 %37, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0))
%39 = load i32, i32* %2
%40 = call i64 @close(i32 %39)
%41 = icmp slt i64 %40, 0
br i1 %41, label %42, label %44
42: ; preds = %36
%43 = call i32 @error_fatal(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
br label %44
44: ; preds = %42, %36
ret i32 0
}

LLVM IR (-O2):
@SIGPIPE = global i32 0
@SIG_IGN = global i32 0
@.str = [4 x i8] c"cpr\00"
@stderr = global i32 0
@.str.2 = [34 x i8] c"Error cerrando socket de cliente\0A\00"
@.str.4 = [22 x i8] c"*** no oigo nada ***\0A\00"
@.str.5 = [6 x i8] c"close\00"
@str = [24 x i8] c"Esperando conexiones...\00"
@str.6 = [32 x i8] c"Conexion con el cliente cerrada\00"
define i32 @main() {
%1 = load i32, i32* @SIGPIPE
%2 = load i32, i32* @SIG_IGN
%3 = tail call i32 @signal(i32 %1, i32 %2)
%4 = tail call i32 @inicia_socket_servidor(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str, i64 0, i64 0))
%6 = tail call i32 @espera_recepcion(i32 %4, i32 30)
%7 = icmp eq i32 %6, 1
br i1 %7, label %8, label %24
8: ; preds = %0, %20
%9 = tail call i32 @acepta_cliente(i32 %4)
%10 = icmp sgt i32 %9, -1
br i1 %10, label %11, label %20
11: ; preds = %8
%12 = tail call i32 @atiende_cliente(i32 %9)
%13 = tail call i64 @close(i32 %9)
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %11
%16 = load i32, i32* @stderr
%17 = tail call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.2, i64 0, i64 0))
br label %18
18: ; preds = %15, %11
%19 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @str.6, i64 0, i64 0))
br label %20
20: ; preds = %8, %18
%21 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str, i64 0, i64 0))
%22 = tail call i32 @espera_recepcion(i32 %4, i32 30)
%23 = icmp eq i32 %22, 1
br i1 %23, label %8, label %24
24: ; preds = %20, %0
%25 = load i32, i32* @stderr
%26 = tail call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0))
%27 = tail call i64 @close(i32 %4)
%28 = icmp slt i64 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %24
%30 = tail call i32 @error_fatal(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
br label %31
31: ; preds = %29, %24
ret i32 0
}