#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*/ 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 *);
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ bzero(char *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int fork();
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ listen(int, int);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ sendMessage(int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
void start_server() {
  int sockfd, newsockfd, portno, clilen;
  struct sockaddr_in serv_addr, cli_addr;
  int pid;
  sockfd = socket(AF_INET, SOCK_STREAM, 0);
  if (sockfd < 0) {
    printf("Error! Can't open socket!\n");
    exit(1);
  }
  bzero((char *)&serv_addr, sizeof(serv_addr));
  portno = 5001;
  serv_addr.sin_family = AF_INET;
  serv_addr.sin_addr.s_addr = INADDR_ANY;
  serv_addr.sin_port = htons(portno);
  if (bind(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
    printf("Error! Can't build socket!\n");
    exit(1);
  }
  listen(sockfd, 5);
  clilen = sizeof(cli_addr);
  while (1) {
    newsockfd = accept(sockfd, (struct sockaddr *)&cli_addr, &clilen);
    if (newsockfd < 0) {
      printf("Error! Can't accept socket!\n");
      exit(1);
    }
    pid = fork();
    if (pid < 0) {
      printf("Error! Can't fork!\n");
      exit(1);
    }
    if (pid == 0) {
      close(sockfd);
      sendMessage(newsockfd);
      exit(0);
    } else {
      close(newsockfd);
    }
  }
}


x86 assembly (clang, -O0):
<start_server>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    0x2e29(%rip),%rax
mov    (%rax),%edi
mov    0x2e08(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
jge    11f4 <start_server+0x44>
lea    0xe1b(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
lea    -0x20(%rbp),%rdi
mov    $0xc,%esi
call   1090 <bzero@plt>
movl   $0x1389,-0xc(%rbp)
mov    0x2dd8(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    0x2dc4(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0xc(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x20(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   1080 <bind@plt>
cmp    $0x0,%rax
jge    125d <start_server+0xad>
lea    0xdcd(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x4(%rbp),%edi
mov    $0x5,%esi
call   1070 <listen@plt>
movl   $0xc,-0x10(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x30(%rbp),%rsi
lea    -0x10(%rbp),%rdx
call   10a0 <accept@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    12a4 <start_server+0xf4>
lea    0xda2(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    $0x0,%al
call   10c0 <fork@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jge    12ce <start_server+0x11e>
lea    0xd95(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
cmpl   $0x0,-0x34(%rbp)
jne    12ef <start_server+0x13f>
mov    -0x4(%rbp),%edi
call   1050 <close@plt>
mov    -0x8(%rbp),%edi
call   1060 <sendMessage@plt>
xor    %edi,%edi
call   10b0 <exit@plt>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
jmp    1271 <start_server+0xc1>

x86 assembly (clang, -O2):
<start_server>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    0x2e27(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2e05(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
test   %eax,%eax
jns    11e4 <start_server+0x34>
lea    0xe6b(%rip),%rdi
jmp    12a8 <start_server+0xf8>
mov    %eax,%r15d
lea    0x8(%rsp),%rbx
mov    %rbx,%rdi
mov    $0xc,%esi
call   1090 <bzero@plt>
mov    0x0(%rbp),%eax
mov    %eax,0x10(%rsp)
mov    0x2dd9(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
mov    $0x1389,%edi
call   1040 <htons@plt>
mov    %eax,0x8(%rsp)
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0xc,%edx
call   1080 <bind@plt>
test   %rax,%rax
jns    1239 <start_server+0x89>
lea    0xdf8(%rip),%rdi
jmp    12a8 <start_server+0xf8>
mov    %r15d,%edi
mov    $0x5,%esi
call   1070 <listen@plt>
movl   $0xc,0x4(%rsp)
lea    0x18(%rsp),%rsi
lea    0x4(%rsp),%rdx
mov    %r15d,%edi
call   10a0 <accept@plt>
test   %eax,%eax
js     1298 <start_server+0xe8>
mov    %eax,%ebp
lea    0x18(%rsp),%r14
lea    0x4(%rsp),%rbx
xor    %eax,%eax
call   10c0 <fork@plt>
test   %eax,%eax
js     12a1 <start_server+0xf1>
je     12b7 <start_server+0x107>
mov    %ebp,%edi
call   1050 <close@plt>
mov    %r15d,%edi
mov    %r14,%rsi
mov    %rbx,%rdx
call   10a0 <accept@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    1270 <start_server+0xc0>
lea    0xd74(%rip),%rdi
jmp    12a8 <start_server+0xf8>
lea    0xd58(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    %r15d,%edi
call   1050 <close@plt>
mov    %ebp,%edi
call   1060 <sendMessage@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 = [27 x i8] c"Error! Can't open socket!\0A\00"
@INADDR_ANY = global i32 0
@.str.1 = [28 x i8] c"Error! Can't build socket!\0A\00"
@.str.2 = [29 x i8] c"Error! Can't accept socket!\0A\00"
@.str.3 = [20 x i8] c"Error! Can't fork!\0A\00"
define void @start_server() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.sockaddr_in
%6 = alloca %struct.sockaddr_in
%7 = alloca i32
%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* %1
%11 = load i32, i32* %1
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %0
%14 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
%15 = call i32 @exit(i32 1)
unreachable
16: ; preds = %0
%17 = bitcast %struct.sockaddr_in* %5 to i8*
%18 = call i32 @bzero(i8* %17, i32 12)
store i32 5001, i32* %3
%19 = load i32, i32* @AF_INET
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 2
store i32 %19, i32* %20
%21 = load i32, i32* @INADDR_ANY
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 1
%23 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %22, i32 0, i32 0
store i32 %21, i32* %23
%24 = load i32, i32* %3
%25 = call i32 @htons(i32 %24)
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 0
store i32 %25, i32* %26
%27 = load i32, i32* %1
%28 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%29 = call i64 @bind(i32 %27, %struct.sockaddr* %28, i32 12)
%30 = icmp slt i64 %29, 0
br i1 %30, label %31, label %34
31: ; preds = %16
%32 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
%33 = call i32 @exit(i32 1)
unreachable
34: ; preds = %16
%35 = load i32, i32* %1
%36 = call i32 @listen(i32 %35, i32 5)
store i32 12, i32* %4
br label %37
37: ; preds = %34, %65
%38 = load i32, i32* %1
%39 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%40 = call i32 @accept(i32 %38, %struct.sockaddr* %39, i32* %4)
store i32 %40, i32* %2
%41 = load i32, i32* %2
%42 = icmp slt i32 %41, 0
br i1 %42, label %43, label %46
43: ; preds = %37
%44 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0))
%45 = call i32 @exit(i32 1)
unreachable
46: ; preds = %37
%47 = call i32 (...) @fork()
store i32 %47, i32* %7
%48 = load i32, i32* %7
%49 = icmp slt i32 %48, 0
br i1 %49, label %50, label %53
50: ; preds = %46
%51 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0))
%52 = call i32 @exit(i32 1)
unreachable
53: ; preds = %46
%54 = load i32, i32* %7
%55 = icmp eq i32 %54, 0
br i1 %55, label %56, label %62
56: ; preds = %53
%57 = load i32, i32* %1
%58 = call i32 @close(i32 %57)
%59 = load i32, i32* %2
%60 = call i32 @sendMessage(i32 %59)
%61 = call i32 @exit(i32 0)
unreachable
62: ; preds = %53
%63 = load i32, i32* %2
%64 = call i32 @close(i32 %63)
br label %65
65: ; preds = %62
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
@INADDR_ANY = global i32 0
@str = [19 x i8] c"Error! Can't fork!\00"
@str.4 = [28 x i8] c"Error! Can't accept socket!\00"
@str.5 = [27 x i8] c"Error! Can't build socket!\00"
@str.6 = [26 x i8] c"Error! Can't open socket!\00"
define void @start_server() {
%1 = alloca i32
%2 = alloca %struct.sockaddr_in
%3 = alloca %struct.sockaddr_in
%4 = bitcast i32* %1 to i8*
%5 = bitcast %struct.sockaddr_in* %2 to i8*
%6 = bitcast %struct.sockaddr_in* %3 to i8*
%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 = %0
%12 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str.6, i64 0, i64 0))
%13 = tail call i32 @exit(i32 1)
unreachable
14: ; preds = %0
%15 = call i32 @bzero(i8* %5, i32 12)
%16 = load i32, i32* @AF_INET
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 2
store i32 %16, i32* %17
%18 = load i32, i32* @INADDR_ANY
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1, i32 0
store i32 %18, i32* %19
%20 = tail call i32 @htons(i32 5001)
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0
store i32 %20, i32* %21
%22 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%23 = call i64 @bind(i32 %9, %struct.sockaddr* %22, i32 12)
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %28
25: ; preds = %14
%26 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str.5, i64 0, i64 0))
%27 = call i32 @exit(i32 1)
unreachable
28: ; preds = %14
%29 = call i32 @listen(i32 %9, i32 5)
store i32 12, i32* %1
%30 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%31 = call i32 @accept(i32 %9, %struct.sockaddr* %30, i32* %1)
%32 = icmp slt i32 %31, 0
br i1 %32, label %33, label %36
33: ; preds = %49, %28
%34 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @str.4, i64 0, i64 0))
%35 = call i32 @exit(i32 1)
unreachable
36: ; preds = %28, %49
%37 = phi i32 [ %51, %49 ], [ %31, %28 ]
%38 = call i32 (...) @fork()
%39 = icmp slt i32 %38, 0
br i1 %39, label %40, label %43
40: ; preds = %36
%41 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str, i64 0, i64 0))
%42 = call i32 @exit(i32 1)
unreachable
43: ; preds = %36
%44 = icmp eq i32 %38, 0
br i1 %44, label %45, label %49
45: ; preds = %43
%46 = call i32 @close(i32 %9)
%47 = call i32 @sendMessage(i32 %37)
%48 = call i32 @exit(i32 0)
unreachable
49: ; preds = %43
%50 = call i32 @close(i32 %37)
%51 = call i32 @accept(i32 %9, %struct.sockaddr* %30, i32* %1)
%52 = icmp slt i32 %51, 0
br i1 %52, label %33, label %36
}