#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 handle_request(int, struct sockaddr_in *);
int /*<<< orphan*/ htons(int);
int listen(int, int);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int);
int control_loop(int port) {
  int socketfd, newsockfd;
  int err = 0;
  socklen_t clilen;
  struct sockaddr_in serv_addr, cli_addr;
  socketfd = socket(AF_INET, SOCK_STREAM, 6);
  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;
  err = bind(socketfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr));
  if (err < 0) {
    perror("Failed to bind to the socket");
    exit(EXIT_FAILURE);
  }
  while (true) {
    err = listen(socketfd, 5);
    if (err != 0) {
      perror("Failed to listen on the socket");
      close(socketfd);
      return err;
    }
    clilen = sizeof(cli_addr);
    newsockfd = accept(socketfd, (struct sockaddr *)&cli_addr, &clilen);
    if (newsockfd == -1) {
      perror("Failed to accept connection");
      err = newsockfd;
    }
    err = handle_request(newsockfd, &cli_addr);
    if (err != 0) {
      break;
    }
  }
  close(socketfd);
  return err;
}


x86 assembly (clang, -O0):
<control_loop>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
movl   $0x0,-0x14(%rbp)
mov    0x2e2f(%rip),%rax
mov    (%rax),%edi
mov    0x2e0e(%rip),%rax
mov    (%rax),%esi
mov    $0x6,%edx
call   10c0 <socket@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    11f5 <control_loop+0x55>
lea    0xe1e(%rip),%rdi
call   1080 <perror@plt>
mov    0x2dda(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
lea    -0x28(%rbp),%rdi
mov    $0xc,%esi
call   1090 <bzero@plt>
mov    0x2dde(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x8(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x24(%rbp)
mov    0x2dbf(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0xc,%edx
call   1070 <bind@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    125e <control_loop+0xbe>
lea    0xdcb(%rip),%rdi
call   1080 <perror@plt>
mov    0x2d71(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
jmp    1263 <control_loop+0xc3>
mov    -0xc(%rbp),%edi
mov    $0x5,%esi
call   1060 <listen@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     129c <control_loop+0xfc>
lea    0xdaf(%rip),%rdi
call   1080 <perror@plt>
mov    -0xc(%rbp),%edi
call   1040 <close@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1303 <control_loop+0x163>
movl   $0xc,-0x18(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x38(%rbp),%rsi
lea    -0x18(%rbp),%rdx
call   10a0 <accept@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
jne    12d2 <control_loop+0x132>
lea    0xd8b(%rip),%rdi
call   1080 <perror@plt>
mov    -0x10(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%edi
lea    -0x38(%rbp),%rsi
call   1050 <handle_request@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     12f0 <control_loop+0x150>
jmp    12f5 <control_loop+0x155>
jmp    1263 <control_loop+0xc3>
mov    -0xc(%rbp),%edi
call   1040 <close@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<control_loop>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x30,%rsp
mov    %edi,%ebp
mov    0x2e33(%rip),%r15
mov    (%r15),%edi
mov    0x2e11(%rip),%rax
mov    (%rax),%esi
mov    $0x6,%edx
call   10c0 <socket@plt>
test   %eax,%eax
js     129c <control_loop+0xfc>
mov    %eax,%ebx
lea    0x10(%rsp),%r14
mov    %r14,%rdi
mov    $0xc,%esi
call   1090 <bzero@plt>
mov    (%r15),%eax
mov    %eax,0x18(%rsp)
mov    %ebp,%edi
call   1030 <htons@plt>
mov    %eax,0x14(%rsp)
mov    0x2de0(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0xc,%edx
call   1070 <bind@plt>
test   %eax,%eax
js     12a5 <control_loop+0x105>
lea    0x20(%rsp),%r14
lea    0xc(%rsp),%r15
lea    0xe24(%rip),%r12
jmp    123e <control_loop+0x9e>
mov    %ebp,%edi
mov    %r14,%rsi
call   1050 <handle_request@plt>
test   %eax,%eax
jne    1274 <control_loop+0xd4>
mov    %ebx,%edi
mov    $0x5,%esi
call   1060 <listen@plt>
test   %eax,%eax
jne    128c <control_loop+0xec>
movl   $0xc,0xc(%rsp)
mov    %ebx,%edi
mov    %r14,%rsi
mov    %r15,%rdx
call   10a0 <accept@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    1230 <control_loop+0x90>
mov    %r12,%rdi
call   1080 <perror@plt>
jmp    1230 <control_loop+0x90>
mov    %eax,%ebp
mov    %ebx,%edi
call   1040 <close@plt>
mov    %ebp,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %eax,%ebp
lea    0xd9e(%rip),%rdi
call   1080 <perror@plt>
jmp    1276 <control_loop+0xd6>
lea    0xd5d(%rip),%rdi
jmp    12ac <control_loop+0x10c>
lea    0xd6a(%rip),%rdi
call   1080 <perror@plt>
mov    0x2d10(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>

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 i32 @control_loop(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.sockaddr_in
%9 = alloca %struct.sockaddr_in
store i32 %0, i32* %3
store i32 0, i32* %6
%10 = load i32, i32* @AF_INET
%11 = load i32, i32* @SOCK_STREAM
%12 = call i32 @socket(i32 %10, i32 %11, i32 6)
store i32 %12, i32* %4
%13 = load i32, i32* %4
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %19
15: ; preds = %1
%16 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
%17 = load i32, i32* @EXIT_FAILURE
%18 = call i32 @exit(i32 %17)
unreachable
19: ; preds = %1
%20 = bitcast %struct.sockaddr_in* %8 to i8*
%21 = call i32 @bzero(i8* %20, i32 12)
%22 = load i32, i32* @AF_INET
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 2
store i32 %22, i32* %23
%24 = load i32, i32* %3
%25 = call i32 @htons(i32 %24)
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 1
store i32 %25, i32* %26
%27 = load i32, i32* @INADDR_ANY
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 0
%29 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %28, i32 0, i32 0
store i32 %27, i32* %29
%30 = load i32, i32* %4
%31 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%32 = call i32 @bind(i32 %30, %struct.sockaddr* %31, i32 12)
store i32 %32, i32* %6
%33 = load i32, i32* %6
%34 = icmp slt i32 %33, 0
br i1 %34, label %35, label %39
35: ; preds = %19
%36 = call i32 @perror(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
%37 = load i32, i32* @EXIT_FAILURE
%38 = call i32 @exit(i32 %37)
unreachable
39: ; preds = %19
br label %40
40: ; preds = %39, %65
%41 = load i32, i32* %4
%42 = call i32 @listen(i32 %41, i32 5)
store i32 %42, i32* %6
%43 = load i32, i32* %6
%44 = icmp ne i32 %43, 0
br i1 %44, label %45, label %50
45: ; preds = %40
%46 = call i32 @perror(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0))
%47 = load i32, i32* %4
%48 = call i32 @close(i32 %47)
%49 = load i32, i32* %6
store i32 %49, i32* %2
br label %70
50: ; preds = %40
store i32 12, i32* %7
%51 = load i32, i32* %4
%52 = bitcast %struct.sockaddr_in* %9 to %struct.sockaddr*
%53 = call i32 @accept(i32 %51, %struct.sockaddr* %52, i32* %7)
store i32 %53, i32* %5
%54 = load i32, i32* %5
%55 = icmp eq i32 %54, -1
br i1 %55, label %56, label %59
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* %5
store i32 %58, i32* %6
br label %59
59: ; preds = %56, %50
%60 = load i32, i32* %5
%61 = call i32 @handle_request(i32 %60, %struct.sockaddr_in* %9)
store i32 %61, i32* %6
%62 = load i32, i32* %6
%63 = icmp ne i32 %62, 0
br i1 %63, label %64, label %65
64: ; preds = %59
br label %66
65: ; preds = %59
br label %40
66: ; preds = %64
%67 = load i32, i32* %4
%68 = call i32 @close(i32 %67)
%69 = load i32, i32* %6
store i32 %69, i32* %2
br label %70
70: ; preds = %66, %45
%71 = load i32, i32* %2
ret i32 %71
}

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 i32 @control_loop(i32 %0) {
%2 = alloca i32
%3 = alloca %struct.sockaddr_in
%4 = alloca %struct.sockaddr_in
%5 = bitcast i32* %2 to i8*
%6 = bitcast %struct.sockaddr_in* %3 to i8*
%7 = bitcast %struct.sockaddr_in* %4 to i8*
%8 = load i32, i32* @AF_INET
%9 = load i32, i32* @SOCK_STREAM
%10 = tail call i32 @socket(i32 %8, i32 %9, i32 6)
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %16
12: ; preds = %1
%13 = tail call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
%14 = load i32, i32* @EXIT_FAILURE
%15 = tail call i32 @exit(i32 %14)
unreachable
16: ; preds = %1
%17 = call i32 @bzero(i8* %6, i32 12)
%18 = load i32, i32* @AF_INET
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %18, i32* %19
%20 = tail call i32 @htons(i32 %0)
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %20, i32* %21
%22 = load i32, i32* @INADDR_ANY
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0, i32 0
store i32 %22, i32* %23
%24 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%25 = call i32 @bind(i32 %10, %struct.sockaddr* %24, i32 12)
%26 = icmp slt i32 %25, 0
br i1 %26, label %29, label %27
27: ; preds = %16
%28 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
br label %33
29: ; preds = %16
%30 = call i32 @perror(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
%31 = load i32, i32* @EXIT_FAILURE
%32 = call i32 @exit(i32 %31)
unreachable
33: ; preds = %27, %43
%34 = call i32 @listen(i32 %10, i32 5)
%35 = icmp eq i32 %34, 0
br i1 %35, label %38, label %36
36: ; preds = %33
%37 = call i32 @perror(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0))
br label %46
38: ; preds = %33
store i32 12, i32* %2
%39 = call i32 @accept(i32 %10, %struct.sockaddr* %28, i32* %2)
%40 = icmp eq i32 %39, -1
br i1 %40, label %41, label %43
41: ; preds = %38
%42 = call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0))
br label %43
43: ; preds = %41, %38
%44 = call i32 @handle_request(i32 %39, %struct.sockaddr_in* %4)
%45 = icmp eq i32 %44, 0
br i1 %45, label %33, label %46
46: ; preds = %43, %36
%47 = phi i32 [ %34, %36 ], [ %44, %43 ]
%48 = call i32 @close(i32 %10)
ret i32 %47
}