#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*/ socklen_t;
typedef int /*<<< orphan*/ serv_addr;
typedef int /*<<< orphan*/ cli_addr;

/* 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*/ chat(int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ listen(int, int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char const *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  int sockfd, newsockfd, clilen;
  struct sockaddr_in serv_addr, cli_addr;
  int pid;
  sockfd = socket(AF_INET, SOCK_STREAM, 0);
  if (sockfd < 0)
    perror((const char *)"ERROR opening socket");
  memset(&serv_addr, 0, sizeof(serv_addr));
  serv_addr.sin_family = AF_INET;
  serv_addr.sin_addr.s_addr = INADDR_ANY;
  serv_addr.sin_port = htons(40000);
  if (bind(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
    perror((const char *)"ERROR on binding");
  listen(sockfd, 5);
  clilen = sizeof(cli_addr);
  while (1) {
    newsockfd =
        accept(sockfd, (struct sockaddr *)&cli_addr, (socklen_t *)&clilen);
    if (newsockfd < 0)
      perror((const char *)"ERROR on accept");
    pid = fork();
    if (pid < 0) {
      perror((const char *)"ERROR on fork");
    }
    if (pid == 0) {
      close(sockfd);
      chat(newsockfd);
      exit(0);
    } else {
      close(newsockfd);
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e1b(%rip),%rax
mov    (%rax),%edi
mov    0x2dfa(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11f8 <main+0x48>
lea    0xe0d(%rip),%rdi
call   1090 <perror@plt>
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    0x2dd9(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2dc5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    $0x9c40,%edi
call   1040 <htons@plt>
mov    %eax,-0x28(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0xc,%edx
call   1080 <bind@plt>
cmp    $0x0,%rax
jge    1254 <main+0xa4>
lea    0xdc6(%rip),%rdi
call   1090 <perror@plt>
mov    -0x14(%rbp),%edi
mov    $0x5,%esi
call   1070 <listen@plt>
movl   $0xc,-0x1c(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x38(%rbp),%rsi
lea    -0x1c(%rbp),%rdx
call   10a0 <accept@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    1291 <main+0xe1>
lea    0xd9a(%rip),%rdi
call   1090 <perror@plt>
mov    $0x0,%al
call   10c0 <fork@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jge    12b1 <main+0x101>
lea    0xd8a(%rip),%rdi
call   1090 <perror@plt>
cmpl   $0x0,-0x3c(%rbp)
jne    12d2 <main+0x122>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
mov    -0x18(%rbp),%edi
call   1030 <chat@plt>
xor    %edi,%edi
call   10b0 <exit@plt>
mov    -0x18(%rbp),%edi
call   1060 <close@plt>
jmp    1268 <main+0xb8>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    0x2e23(%rip),%r14
mov    (%r14),%edi
mov    0x2e01(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    11ea <main+0x3a>
lea    0xe1b(%rip),%rdi
call   1090 <perror@plt>
lea    0x8(%rsp),%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    (%r14),%eax
mov    %eax,0x10(%rsp)
mov    0x2dd4(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
mov    $0x9c40,%edi
call   1040 <htons@plt>
mov    %eax,0x8(%rsp)
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0xc,%edx
call   1080 <bind@plt>
test   %rax,%rax
jns    1240 <main+0x90>
lea    0xdda(%rip),%rdi
call   1090 <perror@plt>
mov    %ebx,%edi
mov    $0x5,%esi
call   1070 <listen@plt>
movl   $0xc,0x4(%rsp)
lea    0x18(%rsp),%r14
lea    0x4(%rsp),%r15
lea    0xdc1(%rip),%r12
lea    0xdca(%rip),%r13
jmp    1287 <main+0xd7>
mov    %r13,%rdi
call   1090 <perror@plt>
cs nopw 0x0(%rax,%rax,1)
mov    %ebp,%edi
call   1060 <close@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    %r15,%rdx
call   10a0 <accept@plt>
mov    %eax,%ebp
test   %eax,%eax
js     12a9 <main+0xf9>
xor    %eax,%eax
call   10c0 <fork@plt>
test   %eax,%eax
js     126e <main+0xbe>
jne    1280 <main+0xd0>
jmp    12b3 <main+0x103>
mov    %r12,%rdi
call   1090 <perror@plt>
jmp    129a <main+0xea>
mov    %ebx,%edi
call   1060 <close@plt>
mov    %ebp,%edi
call   1030 <chat@plt>
xor    %edi,%edi
call   10b0 <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 = [21 x i8] c"ERROR opening socket\00"
@INADDR_ANY = global i32 0
@.str.1 = [17 x i8] c"ERROR on binding\00"
@.str.2 = [16 x i8] c"ERROR on accept\00"
@.str.3 = [14 x i8] c"ERROR on fork\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 i32
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 = load i32, i32* %6
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %2
%18 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
br label %19
19: ; preds = %17, %2
%20 = call i32 @memset(%struct.sockaddr_in* %9, i32 0, i32 12)
%21 = load i32, i32* @AF_INET
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 2
store i32 %21, i32* %22
%23 = load i32, i32* @INADDR_ANY
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 1
%25 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %24, i32 0, i32 0
store i32 %23, i32* %25
%26 = call i32 @htons(i32 40000)
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 0
store i32 %26, i32* %27
%28 = load i32, i32* %6
%29 = bitcast %struct.sockaddr_in* %9 to %struct.sockaddr*
%30 = call i64 @bind(i32 %28, %struct.sockaddr* %29, i32 12)
%31 = icmp slt i64 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %19
%33 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
br label %34
34: ; preds = %32, %19
%35 = load i32, i32* %6
%36 = call i32 @listen(i32 %35, i32 5)
store i32 12, i32* %8
br label %37
37: ; preds = %34, %63
%38 = load i32, i32* %6
%39 = bitcast %struct.sockaddr_in* %10 to %struct.sockaddr*
%40 = call i32 @accept(i32 %38, %struct.sockaddr* %39, i32* %8)
store i32 %40, i32* %7
%41 = load i32, i32* %7
%42 = icmp slt i32 %41, 0
br i1 %42, label %43, label %45
43: ; preds = %37
%44 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
br label %45
45: ; preds = %43, %37
%46 = call i32 (...) @fork()
store i32 %46, i32* %11
%47 = load i32, i32* %11
%48 = icmp slt i32 %47, 0
br i1 %48, label %49, label %51
49: ; preds = %45
%50 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
br label %51
51: ; preds = %49, %45
%52 = load i32, i32* %11
%53 = icmp eq i32 %52, 0
br i1 %53, label %54, label %60
54: ; preds = %51
%55 = load i32, i32* %6
%56 = call i32 @close(i32 %55)
%57 = load i32, i32* %7
%58 = call i32 @chat(i32 %57)
%59 = call i32 @exit(i32 0)
unreachable
60: ; preds = %51
%61 = load i32, i32* %7
%62 = call i32 @close(i32 %61)
br label %63
63: ; preds = %60
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 = [21 x i8] c"ERROR opening socket\00"
@INADDR_ANY = global i32 0
@.str.1 = [17 x i8] c"ERROR on binding\00"
@.str.2 = [16 x i8] c"ERROR on accept\00"
@.str.3 = [14 x i8] c"ERROR on fork\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i32
%4 = alloca %struct.sockaddr_in
%5 = alloca %struct.sockaddr_in
%6 = bitcast i32* %3 to i8*
%7 = bitcast %struct.sockaddr_in* %4 to i8*
%8 = bitcast %struct.sockaddr_in* %5 to i8*
%9 = load i32, i32* @AF_INET
%10 = load i32, i32* @SOCK_STREAM
%11 = tail call i32 @socket(i32 %9, i32 %10, i32 0)
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %2
%14 = tail call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
br label %15
15: ; preds = %13, %2
%16 = call i32 @memset(%struct.sockaddr_in* %4, i32 0, i32 12)
%17 = load i32, i32* @AF_INET
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 2
store i32 %17, i32* %18
%19 = load i32, i32* @INADDR_ANY
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1, i32 0
store i32 %19, i32* %20
%21 = call i32 @htons(i32 40000)
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0
store i32 %21, i32* %22
%23 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%24 = call i64 @bind(i32 %11, %struct.sockaddr* %23, i32 12)
%25 = icmp slt i64 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %15
%27 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
br label %28
28: ; preds = %26, %15
%29 = call i32 @listen(i32 %11, i32 5)
store i32 12, i32* %3
%30 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
br label %31
31: ; preds = %47, %28
%32 = call i32 @accept(i32 %11, %struct.sockaddr* %30, i32* %3)
%33 = icmp slt i32 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %31
%35 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
br label %36
36: ; preds = %34, %31
%37 = call i32 (...) @fork()
%38 = icmp slt i32 %37, 0
br i1 %38, label %39, label %41
39: ; preds = %36
%40 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
br label %47
41: ; preds = %36
%42 = icmp eq i32 %37, 0
br i1 %42, label %43, label %47
43: ; preds = %41
%44 = call i32 @close(i32 %11)
%45 = call i32 @chat(i32 %32)
%46 = call i32 @exit(i32 0)
unreachable
47: ; preds = %39, %41
%48 = call i32 @close(i32 %32)
br label %31
}