#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*/ soma;
typedef int /*<<< orphan*/ num;
typedef int /*<<< orphan*/ me;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOMAXCONN;
int accept(int, struct sockaddr *, int *);
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ bzero(char *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fork();
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ listen(int, int /*<<< orphan*/);
int /*<<< orphan*/ puts(char *);
int /*<<< orphan*/ read(int, int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ write(int, int *, int);
void main(void) {
  struct sockaddr_in me, from;
  int newSock, sock, adl;
  int num, soma;
  adl = sizeof(me);
  sock = socket(AF_INET, SOCK_STREAM, 0);
  bzero((char *)&me, adl);
  me.sin_family = AF_INET;
  me.sin_addr.s_addr = htonl(INADDR_ANY);
  me.sin_port = htons(60112);
  if (-1 == bind(sock, (struct sockaddr *)&me, adl)) {
    puts("Porta ocupada");
    close(sock);
    exit(1);
  }
  listen(sock, SOMAXCONN);
  for (;;) {
    newSock = accept(sock, (struct sockaddr *)&from, &adl);
    if (!fork()) {
      close(sock);
      do {
        soma = 0;
        do {
          read(newSock, &num, sizeof(num));
          soma = soma + num;
        } while (num);
        write(newSock, &soma, sizeof(soma));
      } while (soma);
      close(newSock);
      exit(0);
    }
    close(newSock);
  }
  close(sock);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0xc,-0x2c(%rbp)
mov    0x2e02(%rip),%rax
mov    (%rax),%edi
mov    0x2dd9(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
mov    %eax,-0x28(%rbp)
lea    -0x10(%rbp),%rdi
mov    -0x2c(%rbp),%esi
call   10b0 <bzero@plt>
mov    0x2dda(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x8(%rbp)
mov    0x2dc6(%rip),%rax
mov    (%rax),%edi
call   1060 <htonl@plt>
mov    %eax,-0xc(%rbp)
mov    $0xead0,%edi
call   1050 <htons@plt>
mov    %eax,-0x10(%rbp)
mov    -0x28(%rbp),%edi
lea    -0x10(%rbp),%rsi
mov    -0x2c(%rbp),%edx
call   10a0 <bind@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
cmp    %ecx,%eax
jne    126d <main+0x9d>
lea    0xdaa(%rip),%rdi
call   1030 <puts@plt>
mov    -0x28(%rbp),%edi
call   1070 <close@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    -0x28(%rbp),%edi
mov    0x2d59(%rip),%rax
mov    (%rax),%esi
call   1090 <listen@plt>
mov    -0x28(%rbp),%edi
lea    -0x20(%rbp),%rsi
lea    -0x2c(%rbp),%rdx
call   10c0 <accept@plt>
mov    %eax,-0x24(%rbp)
mov    $0x0,%al
call   10e0 <fork@plt>
cmp    $0x0,%eax
jne    12fe <main+0x12e>
mov    -0x28(%rbp),%edi
call   1070 <close@plt>
movl   $0x0,-0x34(%rbp)
mov    -0x24(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x4,%edx
call   1080 <read@plt>
mov    -0x34(%rbp),%eax
add    -0x30(%rbp),%eax
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x30(%rbp)
jne    12b0 <main+0xe0>
mov    -0x24(%rbp),%edi
lea    -0x34(%rbp),%rsi
mov    $0x4,%edx
call   1040 <write@plt>
cmpl   $0x0,-0x34(%rbp)
jne    12a9 <main+0xd9>
mov    -0x24(%rbp),%edi
call   1070 <close@plt>
xor    %edi,%edi
call   10d0 <exit@plt>
mov    -0x24(%rbp),%edi
call   1070 <close@plt>
jmp    127e <main+0xae>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x30,%rsp
movl   $0xc,0x4(%rsp)
mov    0x2dfd(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2dd3(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
mov    %eax,%r14d
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0xc,%esi
call   10b0 <bzero@plt>
mov    0x0(%rbp),%eax
mov    %eax,0x18(%rsp)
mov    0x2dbf(%rip),%rax
mov    (%rax),%edi
call   1060 <htonl@plt>
mov    %eax,0x14(%rsp)
mov    $0xead0,%edi
call   1050 <htons@plt>
mov    %eax,0x10(%rsp)
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0xc,%edx
call   10a0 <bind@plt>
cmp    $0xffffffff,%eax
jne    126d <main+0x9d>
lea    0xdaa(%rip),%rdi
call   1030 <puts@plt>
mov    %r14d,%edi
call   1070 <close@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    0x2d5c(%rip),%rax
mov    (%rax),%esi
mov    %r14d,%edi
call   1090 <listen@plt>
lea    0x20(%rsp),%rsi
lea    0x4(%rsp),%rdx
mov    %r14d,%edi
call   10c0 <accept@plt>
mov    %eax,%ebp
xor    %ebx,%ebx
xor    %eax,%eax
call   10e0 <fork@plt>
test   %eax,%eax
je     12d2 <main+0x102>
lea    0x20(%rsp),%r15
lea    0x4(%rsp),%r12
nopl   0x0(%rax)
mov    %ebp,%edi
call   1070 <close@plt>
mov    %r14d,%edi
mov    %r15,%rsi
mov    %r12,%rdx
call   10c0 <accept@plt>
mov    %eax,%ebp
xor    %eax,%eax
call   10e0 <fork@plt>
test   %eax,%eax
jne    12b0 <main+0xe0>
mov    %r14d,%edi
call   1070 <close@plt>
lea    0xc(%rsp),%r15
lea    0x8(%rsp),%r14
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0x4,%edx
call   1080 <read@plt>
mov    0xc(%rsp),%eax
add    %eax,%ebx
test   %eax,%eax
jne    12f0 <main+0x120>
mov    %ebx,0x8(%rsp)
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0x4,%edx
call   1040 <write@plt>
test   %ebx,%ebx
mov    $0x0,%ebx
jne    12f0 <main+0x120>
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
@INADDR_ANY = global i32 0
@.str = [14 x i8] c"Porta ocupada\00"
@SOMAXCONN = global i32 0
define void @main() {
%1 = alloca %struct.sockaddr_in
%2 = alloca %struct.sockaddr_in
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i32 12, i32* %5
%8 = load i32, i32* @AF_INET
%9 = load i32, i32* @SOCK_STREAM
%10 = call i32 @socket(i32 %8, i32 %9, i32 0)
store i32 %10, i32* %4
%11 = bitcast %struct.sockaddr_in* %1 to i8*
%12 = load i32, i32* %5
%13 = call i32 @bzero(i8* %11, i32 %12)
%14 = load i32, i32* @AF_INET
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i32 0, i32 2
store i32 %14, i32* %15
%16 = load i32, i32* @INADDR_ANY
%17 = call i32 @htonl(i32 %16)
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i32 0, i32 1
%19 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %18, i32 0, i32 0
store i32 %17, i32* %19
%20 = call i32 @htons(i32 60112)
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i32 0, i32 0
store i32 %20, i32* %21
%22 = load i32, i32* %4
%23 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%24 = load i32, i32* %5
%25 = call i32 @bind(i32 %22, %struct.sockaddr* %23, i32 %24)
%26 = icmp eq i32 -1, %25
br i1 %26, label %27, label %32
27: ; preds = %0
%28 = call i32 @puts(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%29 = load i32, i32* %4
%30 = call i32 @close(i32 %29)
%31 = call i32 @exit(i32 1)
unreachable
32: ; preds = %0
%33 = load i32, i32* %4
%34 = load i32, i32* @SOMAXCONN
%35 = call i32 @listen(i32 %33, i32 %34)
br label %36
36: ; preds = %65, %32
%37 = load i32, i32* %4
%38 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%39 = call i32 @accept(i32 %37, %struct.sockaddr* %38, i32* %5)
store i32 %39, i32* %3
%40 = call i32 (...) @fork()
%41 = icmp ne i32 %40, 0
br i1 %41, label %65, label %42
42: ; preds = %36
%43 = load i32, i32* %4
%44 = call i32 @close(i32 %43)
br label %45
45: ; preds = %58, %42
store i32 0, i32* %7
br label %46
46: ; preds = %52, %45
%47 = load i32, i32* %3
%48 = call i32 @read(i32 %47, i32* %6, i32 4)
%49 = load i32, i32* %7
%50 = load i32, i32* %6
%51 = add nsw i32 %49, %50
store i32 %51, i32* %7
br label %52
52: ; preds = %46
%53 = load i32, i32* %6
%54 = icmp ne i32 %53, 0
br i1 %54, label %46, label %55
55: ; preds = %52
%56 = load i32, i32* %3
%57 = call i32 @write(i32 %56, i32* %7, i32 4)
br label %58
58: ; preds = %55
%59 = load i32, i32* %7
%60 = icmp ne i32 %59, 0
br i1 %60, label %45, label %61
61: ; preds = %58
%62 = load i32, i32* %3
%63 = call i32 @close(i32 %62)
%64 = call i32 @exit(i32 0)
unreachable
65: ; preds = %36
%66 = load i32, i32* %3
%67 = call i32 @close(i32 %66)
br label %36
}

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 = [14 x i8] c"Porta ocupada\00"
@SOMAXCONN = global i32 0
define void @main() {
%1 = alloca %struct.sockaddr_in
%2 = alloca %struct.sockaddr_in
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = bitcast %struct.sockaddr_in* %1 to i8*
%7 = bitcast %struct.sockaddr_in* %2 to i8*
%8 = bitcast i32* %3 to i8*
%9 = bitcast i32* %4 to i8*
%10 = bitcast i32* %5 to i8*
store i32 12, i32* %3
%11 = load i32, i32* @AF_INET
%12 = load i32, i32* @SOCK_STREAM
%13 = tail call i32 @socket(i32 %11, i32 %12, i32 0)
%14 = call i32 @bzero(i8* %6, 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 = tail call i32 @htonl(i32 %17)
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1, i32 0
store i32 %18, i32* %19
%20 = tail call i32 @htons(i32 60112)
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0
store i32 %20, i32* %21
%22 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%23 = call i32 @bind(i32 %13, %struct.sockaddr* %22, i32 12)
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %29
25: ; preds = %0
%26 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%27 = call i32 @close(i32 %13)
%28 = call i32 @exit(i32 1)
unreachable
29: ; preds = %0
%30 = load i32, i32* @SOMAXCONN
%31 = call i32 @listen(i32 %13, i32 %30)
%32 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%33 = call i32 @accept(i32 %13, %struct.sockaddr* %32, i32* %3)
%34 = call i32 (...) @fork()
%35 = icmp eq i32 %34, 0
br i1 %35, label %36, label %53
36: ; preds = %53, %29
%37 = phi i32 [ %33, %29 ], [ %56, %53 ]
%38 = call i32 @close(i32 %13)
br label %39
39: ; preds = %45, %36
%40 = phi i32 [ 0, %36 ], [ %46, %45 ]
%41 = call i32 @read(i32 %37, i32* %4, i32 4)
%42 = load i32, i32* %4
%43 = add nsw i32 %42, %40
%44 = icmp eq i32 %42, 0
br i1 %44, label %47, label %45
45: ; preds = %39, %47
%46 = phi i32 [ %43, %39 ], [ 0, %47 ]
br label %39
47: ; preds = %39
store i32 %43, i32* %5
%48 = call i32 @write(i32 %37, i32* %5, i32 4)
%49 = icmp eq i32 %43, 0
br i1 %49, label %50, label %45
50: ; preds = %47
%51 = call i32 @close(i32 %37)
%52 = call i32 @exit(i32 0)
unreachable
53: ; preds = %29, %53
%54 = phi i32 [ %56, %53 ], [ %33, %29 ]
%55 = call i32 @close(i32 %54)
%56 = call i32 @accept(i32 %13, %struct.sockaddr* %32, i32* %3)
%57 = call i32 (...) @fork()
%58 = icmp eq i32 %57, 0
br i1 %58, label %36, label %53
}