#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*/ server;
typedef int /*<<< orphan*/ buff;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_STREAM;
int accept(int, struct sockaddr *, int /*<<< orphan*/);
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ listen(int, int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int recv(int, char *, int, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stdout;
int main(int argc, char **argv) {
  int sock, mysock, rval;
  struct sockaddr_in server, client;
  char buff[256];
  if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
    perror("Falha ao criar socket.");
    exit(1);
  }
  server.sin_family = AF_INET;
  server.sin_addr.s_addr = INADDR_ANY;
  server.sin_port = htons(5000);
  if (bind(sock, (struct sockaddr *)&server, sizeof(server))) {
    perror("Funcao bind() falhou.");
    exit(1);
  }
  listen(sock, 5);
  while (1) {
    if ((mysock = accept(sock, (struct sockaddr *)0, 0)) < 0)
      perror("Tentativa de aceitar conexao falhou.");
    else {
      memset(buff, 0, sizeof(buff));
      if ((rval = recv(mysock, buff, sizeof(buff), 0)) < 0)
        perror("Nao foi possivel ler o stream do socket.");
      else if (rval == 0)
        perror("Finalizando conexao!");
      else
        fprintf(stdout, "MSG: %s\n", buff);
      fprintf(stdout, "Mensagem recebida (rval = %d)\n", rval);
      close(mysock);
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x140,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e18(%rip),%rax
mov    (%rax),%edi
mov    0x2df7(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    1204 <main+0x54>
lea    0xe0b(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    0x2ddd(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2dc9(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    $0x1388,%edi
call   1040 <htons@plt>
mov    %eax,-0x28(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0xc,%edx
call   1090 <bind@plt>
cmp    $0x0,%rax
je     125a <main+0xaa>
lea    0xdcc(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x14(%rbp),%edi
mov    $0x5,%esi
call   1080 <listen@plt>
mov    -0x14(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   10b0 <accept@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jge    1292 <main+0xe2>
lea    0xda5(%rip),%rdi
call   10a0 <perror@plt>
jmp    1338 <main+0x188>
lea    -0x140(%rbp),%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1050 <memset@plt>
mov    -0x18(%rbp),%edi
lea    -0x140(%rbp),%rsi
mov    $0x100,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jge    12d8 <main+0x128>
lea    0xd84(%rip),%rdi
call   10a0 <perror@plt>
jmp    1316 <main+0x166>
cmpl   $0x0,-0x1c(%rbp)
jne    12f3 <main+0x143>
lea    0xd92(%rip),%rdi
call   10a0 <perror@plt>
jmp    1311 <main+0x161>
mov    0x2cce(%rip),%rax
mov    (%rax),%edi
lea    -0x140(%rbp),%rdx
lea    0xd86(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
jmp    1316 <main+0x166>
mov    0x2cab(%rip),%rax
mov    (%rax),%edi
mov    -0x1c(%rbp),%edx
lea    0xd70(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    -0x18(%rbp),%edi
call   1060 <close@plt>
jmp    1267 <main+0xb7>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x118,%rsp
mov    0x2e20(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2dfe(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
test   %eax,%eax
js     12e0 <main+0x130>
mov    %eax,%ebx
mov    0x0(%rbp),%eax
mov    %eax,0x8(%rsp)
mov    0x2ded(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x4(%rsp)
mov    $0x1388,%edi
call   1040 <htons@plt>
mov    %eax,(%rsp)
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   1090 <bind@plt>
test   %rax,%rax
jne    12e9 <main+0x139>
mov    %ebx,%edi
mov    $0x5,%esi
call   1080 <listen@plt>
lea    0x10(%rsp),%r15
mov    0x2d92(%rip),%r14
lea    0xe5c(%rip),%r13
jmp    124c <main+0x9c>
nop
lea    0xde6(%rip),%rdi
call   10a0 <perror@plt>
mov    %ebx,%edi
xor    %esi,%esi
xor    %edx,%edx
call   10b0 <accept@plt>
test   %eax,%eax
js     1240 <main+0x90>
mov    %eax,%ebp
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1050 <memset@plt>
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0x100,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,%r12d
test   %eax,%eax
jns    12a0 <main+0xf0>
lea    0xdc7(%rip),%rdi
call   10a0 <perror@plt>
jmp    12c4 <main+0x114>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
jne    12b0 <main+0x100>
lea    0xdd2(%rip),%rdi
call   10a0 <perror@plt>
jmp    12c4 <main+0x114>
mov    (%r14),%edi
lea    0xdd6(%rip),%rsi
mov    %r15,%rdx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    (%r14),%edi
mov    %r13,%rsi
mov    %r12d,%edx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    %ebp,%edi
call   1060 <close@plt>
jmp    124c <main+0x9c>
lea    0xd19(%rip),%rdi
jmp    12f0 <main+0x140>
lea    0xd27(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10c0 <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
@.str = [23 x i8] c"Falha ao criar socket.\00"
@INADDR_ANY = global i32 0
@.str.1 = [22 x i8] c"Funcao bind() falhou.\00"
@.str.2 = [37 x i8] c"Tentativa de aceitar conexao falhou.\00"
@.str.3 = [41 x i8] c"Nao foi possivel ler o stream do socket.\00"
@.str.4 = [21 x i8] c"Finalizando conexao!\00"
@stdout = global i32 0
@.str.5 = [9 x i8] c"MSG: %s\0A\00"
@.str.6 = [31 x i8] c"Mensagem recebida (rval = %d)\0A\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 %struct.sockaddr_in
%10 = alloca %struct.sockaddr_in
%11 = alloca [256 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = load i32, i32* @AF_INET
%13 = load i32, i32* @SOCK_STREAM
%14 = call i32 @socket(i32 %12, i32 %13, i32 0)
store i32 %14, i32* %6
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %2
%17 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %2
%20 = load i32, i32* @AF_INET
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 2
store i32 %20, i32* %21
%22 = load i32, i32* @INADDR_ANY
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 1
%24 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %23, i32 0, i32 0
store i32 %22, i32* %24
%25 = call i32 @htons(i32 5000)
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 0
store i32 %25, i32* %26
%27 = load i32, i32* %6
%28 = bitcast %struct.sockaddr_in* %9 to %struct.sockaddr*
%29 = call i64 @bind(i32 %27, %struct.sockaddr* %28, i32 12)
%30 = icmp ne i64 %29, 0
br i1 %30, label %31, label %34
31: ; preds = %19
%32 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
%33 = call i32 @exit(i32 1)
unreachable
34: ; preds = %19
%35 = load i32, i32* %6
%36 = call i32 @listen(i32 %35, i32 5)
br label %37
37: ; preds = %34, %68
%38 = load i32, i32* %6
%39 = call i32 @accept(i32 %38, %struct.sockaddr* null, i32 0)
store i32 %39, i32* %7
%40 = icmp slt i32 %39, 0
br i1 %40, label %41, label %43
41: ; preds = %37
%42 = call i32 @perror(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0))
br label %68
43: ; preds = %37
%44 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 0
%45 = call i32 @memset(i8* %44, i32 0, i32 256)
%46 = load i32, i32* %7
%47 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 0
%48 = call i32 @recv(i32 %46, i8* %47, i32 256, i32 0)
store i32 %48, i32* %8
%49 = icmp slt i32 %48, 0
br i1 %49, label %50, label %52
50: ; preds = %43
%51 = call i32 @perror(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.3, i64 0, i64 0))
br label %62
52: ; preds = %43
%53 = load i32, i32* %8
%54 = icmp eq i32 %53, 0
br i1 %54, label %55, label %57
55: ; preds = %52
%56 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0))
br label %61
57: ; preds = %52
%58 = load i32, i32* @stdout
%59 = getelementptr inbounds [256 x i8], [256 x i8]* %11, i64 0, i64 0
%60 = call i32 (i32, i8*, ...) @fprintf(i32 %58, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0), i8* %59)
br label %61
61: ; preds = %57, %55
br label %62
62: ; preds = %61, %50
%63 = load i32, i32* @stdout
%64 = load i32, i32* %8
%65 = call i32 (i32, i8*, ...) @fprintf(i32 %63, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.6, i64 0, i64 0), i32 %64)
%66 = load i32, i32* %7
%67 = call i32 @close(i32 %66)
br label %68
68: ; preds = %62, %41
br label %37
}

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 = [23 x i8] c"Falha ao criar socket.\00"
@INADDR_ANY = global i32 0
@.str.1 = [22 x i8] c"Funcao bind() falhou.\00"
@.str.2 = [37 x i8] c"Tentativa de aceitar conexao falhou.\00"
@.str.3 = [41 x i8] c"Nao foi possivel ler o stream do socket.\00"
@.str.4 = [21 x i8] c"Finalizando conexao!\00"
@stdout = global i32 0
@.str.5 = [9 x i8] c"MSG: %s\0A\00"
@.str.6 = [31 x i8] c"Mensagem recebida (rval = %d)\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca [256 x i8]
%5 = bitcast %struct.sockaddr_in* %3 to i8*
%6 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%7 = load i32, i32* @AF_INET
%8 = load i32, i32* @SOCK_STREAM
%9 = tail call i32 @socket(i32 %7, i32 %8, i32 0)
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %2
%12 = tail call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
%13 = tail call i32 @exit(i32 1)
unreachable
14: ; preds = %2
%15 = load i32, i32* @AF_INET
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %15, i32* %16
%17 = load i32, i32* @INADDR_ANY
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1, i32 0
store i32 %17, i32* %18
%19 = tail call i32 @htons(i32 5000)
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %19, i32* %20
%21 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%22 = call i64 @bind(i32 %9, %struct.sockaddr* %21, i32 12)
%23 = icmp eq i64 %22, 0
br i1 %23, label %27, label %24
24: ; preds = %14
%25 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
%26 = call i32 @exit(i32 1)
unreachable
27: ; preds = %14
%28 = call i32 @listen(i32 %9, i32 5)
br label %29
29: ; preds = %51, %27
%30 = call i32 @accept(i32 %9, %struct.sockaddr* null, i32 0)
%31 = icmp slt i32 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %29
%33 = call i32 @perror(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.2, i64 0, i64 0))
br label %51
34: ; preds = %29
%35 = call i32 @memset(i8* %6, i32 0, i32 256)
%36 = call i32 @recv(i32 %30, i8* %6, i32 256, i32 0)
%37 = icmp slt i32 %36, 0
br i1 %37, label %38, label %40
38: ; preds = %34
%39 = call i32 @perror(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.3, i64 0, i64 0))
br label %47
40: ; preds = %34
%41 = icmp eq i32 %36, 0
br i1 %41, label %42, label %44
42: ; preds = %40
%43 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0))
br label %47
44: ; preds = %40
%45 = load i32, i32* @stdout
%46 = call i32 (i32, i8*, ...) @fprintf(i32 %45, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0), i8* %6)
br label %47
47: ; preds = %42, %44, %38
%48 = load i32, i32* @stdout
%49 = call i32 (i32, i8*, ...) @fprintf(i32 %48, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.6, i64 0, i64 0), i32 %36)
%50 = call i32 @close(i32 %30)
br label %51
51: ; preds = %47, %32
br label %29
}