#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 {
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int socklen_t;
typedef int /*<<< orphan*/ serv_addr;
typedef int /*<<< orphan*/ cli_addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_STREAM;
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 /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
int listen(int, int);
scalar_t__ listen_loop(int, char *);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
void server_loop(int port, char *handle) {
  int socketfd, newsockfd, ret;
  socklen_t clilen;
  struct sockaddr_in serv_addr, cli_addr;
  socketfd = socket(AF_INET, SOCK_STREAM, 0);
  if (socketfd < 0) {
    perror("Couldn't open socket!");
    exit(EXIT_FAILURE);
  }
  bzero((char *)&serv_addr, sizeof(serv_addr));
  serv_addr.sin_family = AF_INET;
  serv_addr.sin_port = htons(port);
  serv_addr.sin_addr.s_addr = INADDR_ANY;
  ret = bind(socketfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr));
  if (ret < 0) {
    perror("Failed to bind to the socket");
    exit(EXIT_FAILURE);
  }
  while (true) {
    ret = listen(socketfd, 5);
    if (ret < 0) {
      perror("Failed to listen on the socket");
      exit(EXIT_FAILURE);
    }
    clilen = sizeof(cli_addr);
    newsockfd = accept(socketfd, (struct sockaddr *)&cli_addr, &clilen);
    if (newsockfd < 0) {
      perror("Failed to accept connection");
      exit(EXIT_FAILURE);
    }
    if (listen_loop(newsockfd, handle)) {
      return;
    }
  }
  close(socketfd);
}


x86 assembly (clang, -O0):
<server_loop>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e42(%rip),%rax
mov    (%rax),%edi
mov    0x2e21(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11df <server_loop+0x4f>
lea    0xe34(%rip),%rdi
call   1070 <perror@plt>
mov    0x2df0(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
lea    -0x30(%rbp),%rdi
mov    $0xc,%esi
call   1080 <bzero@plt>
mov    0x2df4(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x4(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x2c(%rbp)
mov    0x2dd5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0xc,%edx
call   1060 <bind@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1248 <server_loop+0xb8>
lea    0xde1(%rip),%rdi
call   1070 <perror@plt>
mov    0x2d87(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
jmp    124d <server_loop+0xbd>
mov    -0x14(%rbp),%edi
mov    $0x5,%esi
call   1050 <listen@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1281 <server_loop+0xf1>
lea    0xdc5(%rip),%rdi
call   1070 <perror@plt>
mov    0x2d4e(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
movl   $0xc,-0x20(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x40(%rbp),%rsi
lea    -0x20(%rbp),%rdx
call   1090 <accept@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    12bf <server_loop+0x12f>
lea    0xda6(%rip),%rdi
call   1070 <perror@plt>
mov    0x2d10(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    -0x18(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   1030 <listen_loop@plt>
cmp    $0x0,%rax
je     12db <server_loop+0x14b>
add    $0x40,%rsp
pop    %rbp
ret
jmp    124d <server_loop+0xbd>

x86 assembly (clang, -O2):
<server_loop>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x30,%rsp
mov    %rsi,%rbx
mov    %edi,%r14d
mov    0x2e3f(%rip),%r12
mov    (%r12),%edi
mov    0x2e1c(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
test   %eax,%eax
js     1288 <server_loop+0xf8>
mov    %eax,%ebp
lea    0x10(%rsp),%r15
mov    %r15,%rdi
mov    $0xc,%esi
call   1080 <bzero@plt>
mov    (%r12),%eax
mov    %eax,0x18(%rsp)
mov    %r14d,%edi
call   1040 <htons@plt>
mov    %eax,0x14(%rsp)
mov    0x2dec(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0xc,%edx
call   1060 <bind@plt>
test   %eax,%eax
js     1291 <server_loop+0x101>
lea    0x20(%rsp),%r14
lea    0xc(%rsp),%r15
nopl   0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    $0x5,%esi
call   1050 <listen@plt>
test   %eax,%eax
js     1265 <server_loop+0xd5>
movl   $0xc,0xc(%rsp)
mov    %ebp,%edi
mov    %r14,%rsi
mov    %r15,%rdx
call   1090 <accept@plt>
test   %eax,%eax
js     126e <server_loop+0xde>
mov    %eax,%edi
mov    %rbx,%rsi
call   1030 <listen_loop@plt>
test   %rax,%rax
je     1220 <server_loop+0x90>
add    $0x30,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdc7(%rip),%rdi
jmp    1275 <server_loop+0xe5>
lea    0xddd(%rip),%rdi
call   1070 <perror@plt>
mov    0x2d47(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
lea    0xd71(%rip),%rdi
jmp    1275 <server_loop+0xe5>
lea    0xd7e(%rip),%rdi
jmp    1275 <server_loop+0xe5>

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [22 x i8] c"Couldn't open socket!\00"
@EXIT_FAILURE = global i32 0
@INADDR_ANY = global i32 0
@.str.1 = [29 x i8] c"Failed to bind to the socket\00"
@.str.2 = [31 x i8] c"Failed to listen on the socket\00"
@.str.3 = [28 x i8] c"Failed to accept connection\00"
define void @server_loop(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca %struct.sockaddr_in
%10 = alloca %struct.sockaddr_in
store i32 %0, i32* %3
store i8* %1, i8** %4
%11 = load i32, i32* @AF_INET
%12 = load i32, i32* @SOCK_STREAM
%13 = call i32 @socket(i32 %11, i32 %12, i32 0)
store i32 %13, i32* %5
%14 = load i32, i32* %5
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %20
16: ; preds = %2
%17 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
%18 = load i32, i32* @EXIT_FAILURE
%19 = call i32 @exit(i32 %18)
unreachable
20: ; preds = %2
%21 = bitcast %struct.sockaddr_in* %9 to i8*
%22 = call i32 @bzero(i8* %21, i32 12)
%23 = load i32, i32* @AF_INET
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 2
store i32 %23, i32* %24
%25 = load i32, i32* %3
%26 = call i32 @htons(i32 %25)
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 1
store i32 %26, i32* %27
%28 = load i32, i32* @INADDR_ANY
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 0
%30 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %29, i32 0, i32 0
store i32 %28, i32* %30
%31 = load i32, i32* %5
%32 = bitcast %struct.sockaddr_in* %9 to %struct.sockaddr*
%33 = call i32 @bind(i32 %31, %struct.sockaddr* %32, i32 12)
store i32 %33, i32* %7
%34 = load i32, i32* %7
%35 = icmp slt i32 %34, 0
br i1 %35, label %36, label %40
36: ; preds = %20
%37 = call i32 @perror(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
%38 = load i32, i32* @EXIT_FAILURE
%39 = call i32 @exit(i32 %38)
unreachable
40: ; preds = %20
br label %41
41: ; preds = %40, %66
%42 = load i32, i32* %5
%43 = call i32 @listen(i32 %42, i32 5)
store i32 %43, i32* %7
%44 = load i32, i32* %7
%45 = icmp slt i32 %44, 0
br i1 %45, label %46, label %50
46: ; preds = %41
%47 = call i32 @perror(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0))
%48 = load i32, i32* @EXIT_FAILURE
%49 = call i32 @exit(i32 %48)
unreachable
50: ; preds = %41
store i32 12, i32* %8
%51 = load i32, i32* %5
%52 = bitcast %struct.sockaddr_in* %10 to %struct.sockaddr*
%53 = call i32 @accept(i32 %51, %struct.sockaddr* %52, i32* %8)
store i32 %53, i32* %6
%54 = load i32, i32* %6
%55 = icmp slt i32 %54, 0
br i1 %55, label %56, label %60
56: ; preds = %50
%57 = call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0))
%58 = load i32, i32* @EXIT_FAILURE
%59 = call i32 @exit(i32 %58)
unreachable
60: ; preds = %50
%61 = load i32, i32* %6
%62 = load i8*, i8** %4
%63 = call i64 @listen_loop(i32 %61, i8* %62)
%64 = icmp ne i64 %63, 0
br i1 %64, label %65, label %66
65: ; preds = %60
ret void
66: ; preds = %60
br label %41
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [22 x i8] c"Couldn't open socket!\00"
@EXIT_FAILURE = global i32 0
@INADDR_ANY = global i32 0
@.str.1 = [29 x i8] c"Failed to bind to the socket\00"
@.str.2 = [31 x i8] c"Failed to listen on the socket\00"
@.str.3 = [28 x i8] c"Failed to accept connection\00"
define void @server_loop(i32 %0, i8* %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 %17
13: ; preds = %2
%14 = tail call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
%15 = load i32, i32* @EXIT_FAILURE
%16 = tail call i32 @exit(i32 %15)
unreachable
17: ; preds = %2
%18 = call i32 @bzero(i8* %7, i32 12)
%19 = load i32, i32* @AF_INET
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 2
store i32 %19, i32* %20
%21 = tail call i32 @htons(i32 %0)
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1
store i32 %21, i32* %22
%23 = load i32, i32* @INADDR_ANY
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0, i32 0
store i32 %23, i32* %24
%25 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%26 = call i32 @bind(i32 %11, %struct.sockaddr* %25, i32 12)
%27 = icmp slt i32 %26, 0
br i1 %27, label %30, label %28
28: ; preds = %17
%29 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
br label %34
30: ; preds = %17
%31 = call i32 @perror(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
%32 = load i32, i32* @EXIT_FAILURE
%33 = call i32 @exit(i32 %32)
unreachable
34: ; preds = %28, %48
%35 = call i32 @listen(i32 %11, i32 5)
%36 = icmp slt i32 %35, 0
br i1 %36, label %37, label %41
37: ; preds = %34
%38 = call i32 @perror(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0))
%39 = load i32, i32* @EXIT_FAILURE
%40 = call i32 @exit(i32 %39)
unreachable
41: ; preds = %34
store i32 12, i32* %3
%42 = call i32 @accept(i32 %11, %struct.sockaddr* %29, i32* %3)
%43 = icmp slt i32 %42, 0
br i1 %43, label %44, label %48
44: ; preds = %41
%45 = call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0))
%46 = load i32, i32* @EXIT_FAILURE
%47 = call i32 @exit(i32 %46)
unreachable
48: ; preds = %41
%49 = call i64 @listen_loop(i32 %42, i8* %1)
%50 = icmp eq i64 %49, 0
br i1 %50, label %34, label %51
51: ; preds = %48
ret void
}