#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_zero;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ sai;

/* Variables and functions */
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int accept(int, struct sockaddr *, unsigned int *);
int bind(int, struct sockaddr *, int);
int handle_client(struct sockaddr_in, int, int);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
char *inet_ntoa(TYPE_1__);
int listen(int, int);
int /*<<< orphan*/ memset(int /*<<< orphan*/ *, char, int);
int /*<<< orphan*/ printf(char *, ...);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main() {
  struct sockaddr_in sai;
  int socketfd = socket(PF_INET, SOCK_STREAM, 0);
  if (socketfd == -1) {
    printf("There was an error creating the specified socket.");
    return 1;
  }
  sai.sin_family = PF_INET;
  sai.sin_port = htons(45287);
  sai.sin_addr.s_addr = htonl(INADDR_ANY);
  memset(&(sai.sin_zero), '\0', 8);
  int bind_error_code = bind(socketfd, (struct sockaddr *)&sai, sizeof(sai));
  if (bind_error_code != 0) {
    printf("There was an error binding to the specified socket on the "
           "specified port.");
    return 2;
  }
  int listen_error_code = listen(socketfd, 100);
  if (listen_error_code != 0) {
    printf("There was an error listening with the specified socket on the "
           "specified port.");
    return 3;
  }
  printf("Accepting connections.\n");
  while (1) {
    struct sockaddr_in client_addr;
    unsigned int size = sizeof(struct sockaddr_in);
    int newsfd =
        accept(socketfd, (struct sockaddr *)&client_addr.sin_addr, &size);
    if (newsfd == -1)
      printf("There was an error while the client was trying to connect.\n");
    else
      printf("New connection recieved from: %s\n",
             inet_ntoa(client_addr.sin_addr));
    int handle_ret = handle_client(client_addr, newsfd, socketfd);
    if (handle_ret != 1 && handle_ret != 0)
      printf("There was an error handling the client. :(\n\n");
    if (handle_ret == 1)
      return 0;
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e1a(%rip),%rax
mov    (%rax),%edi
mov    0x2e19(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11ef <main+0x4f>
lea    0xe24(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    134a <main+0x1aa>
mov    0x2dda(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
mov    $0xb0e7,%edi
call   1050 <htons@plt>
mov    %eax,-0x10(%rbp)
mov    0x2dd9(%rip),%rax
mov    (%rax),%edi
call   1070 <htonl@plt>
mov    %eax,-0x18(%rbp)
lea    -0x18(%rbp),%rdi
add    $0x4,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1080 <memset@plt>
mov    -0x1c(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x10,%edx
call   10a0 <bind@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
je     1265 <main+0xc5>
lea    0xde0(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x2,-0x4(%rbp)
jmp    134a <main+0x1aa>
mov    -0x1c(%rbp),%edi
mov    $0x64,%esi
call   1090 <listen@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     1299 <main+0xf9>
lea    0xdf6(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x3,-0x4(%rbp)
jmp    134a <main+0x1aa>
lea    0xe2a(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x10,-0x3c(%rbp)
mov    -0x1c(%rbp),%edi
lea    -0x38(%rbp),%rsi
lea    -0x3c(%rbp),%rdx
call   10b0 <accept@plt>
mov    %eax,-0x40(%rbp)
cmpl   $0xffffffff,-0x40(%rbp)
jne    12de <main+0x13e>
lea    0xe10(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    12f7 <main+0x157>
mov    -0x38(%rbp),%edi
call   1040 <inet_ntoa@plt>
mov    %rax,%rsi
lea    0xe2e(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x40(%rbp),%edx
mov    -0x1c(%rbp),%ecx
mov    -0x38(%rbp),%rdi
mov    -0x30(%rbp),%rsi
call   1030 <handle_client@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0x1,-0x44(%rbp)
je     132f <main+0x18f>
cmpl   $0x0,-0x44(%rbp)
je     132f <main+0x18f>
lea    0xe18(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
cmpl   $0x1,-0x44(%rbp)
jne    1345 <main+0x1a5>
movl   $0x0,-0x4(%rbp)
jmp    134a <main+0x1aa>
jmp    12a7 <main+0x107>
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    0x2e0b(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2e09(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
cmp    $0xffffffff,%eax
je     1244 <main+0x94>
mov    %eax,%ebx
mov    0x0(%rbp),%eax
mov    %eax,0x14(%rsp)
mov    $0xb0e7,%edi
call   1060 <htons@plt>
mov    %eax,0x10(%rsp)
mov    0x2ded(%rip),%rax
mov    (%rax),%edi
call   1080 <htonl@plt>
mov    %eax,0x8(%rsp)
lea    0xc(%rsp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1090 <memset@plt>
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0x10,%edx
call   10b0 <bind@plt>
test   %eax,%eax
je     125c <main+0xac>
lea    0xdff(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
mov    $0x2,%eax
jmp    1327 <main+0x177>
lea    0xdb5(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
mov    $0x1,%eax
jmp    1327 <main+0x177>
mov    %ebx,%edi
mov    $0x64,%esi
call   10a0 <listen@plt>
test   %eax,%eax
je     1284 <main+0xd4>
lea    0xe09(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
mov    $0x3,%eax
jmp    1327 <main+0x177>
lea    0xe61(%rip),%rdi
call   1040 <puts@plt>
lea    0x18(%rsp),%r14
lea    0x4(%rsp),%r15
lea    0xe29(%rip),%r13
jmp    12b5 <main+0x105>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
cmp    $0x1,%ebp
je     1325 <main+0x175>
movl   $0x10,0x4(%rsp)
mov    %ebx,%edi
mov    %r14,%rsi
mov    %r15,%rdx
call   10c0 <accept@plt>
mov    %eax,%r12d
cmp    $0xffffffff,%eax
je     12f0 <main+0x140>
mov    0x18(%rsp),%edi
call   1050 <inet_ntoa@plt>
mov    %r13,%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
jmp    12fc <main+0x14c>
nopw   0x0(%rax,%rax,1)
lea    0xe38(%rip),%rdi
call   1040 <puts@plt>
mov    0x18(%rsp),%rdi
mov    0x20(%rsp),%rsi
mov    %r12d,%edx
mov    %ebx,%ecx
call   1030 <handle_client@plt>
mov    %eax,%ebp
cmp    $0x2,%eax
jb     12b0 <main+0x100>
lea    0xde5(%rip),%rdi
call   1040 <puts@plt>
jmp    12b0 <main+0x100>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [50 x i8] c"There was an error creating the specified socket.\00"
@INADDR_ANY = global i32 0
@.str.1 = [74 x i8] c"There was an error binding to the specified socket on the specified port.\00"
@.str.2 = [78 x i8] c"There was an error listening with the specified socket on the specified port.\00"
@.str.3 = [24 x i8] c"Accepting connections.\0A\00"
@.str.4 = [60 x i8] c"There was an error while the client was trying to connect.\0A\00"
@.str.5 = [34 x i8] c"New connection recieved from: %s\0A\00"
@.str.6 = [45 x i8] c"There was an error handling the client. :(\0A\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca %struct.sockaddr_in
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.sockaddr_in
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32 0, i32* %1
%10 = load i32, i32* @PF_INET
%11 = load i32, i32* @SOCK_STREAM
%12 = call i32 @socket(i32 %10, i32 %11, i32 0)
store i32 %12, i32* %3
%13 = load i32, i32* %3
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %17
15: ; preds = %0
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %1
br label %80
17: ; preds = %0
%18 = load i32, i32* @PF_INET
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i32 0, i32 3
store i32 %18, i32* %19
%20 = call i32 @htons(i32 45287)
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i32 0, i32 2
store i32 %20, i32* %21
%22 = load i32, i32* @INADDR_ANY
%23 = call i32 @htonl(i32 %22)
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i32 0, i32 0
%25 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %24, i32 0, i32 0
store i32 %23, i32* %25
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i32 0, i32 1
%27 = call i32 @memset(i32* %26, i8 signext 0, i32 8)
%28 = load i32, i32* %3
%29 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%30 = call i32 @bind(i32 %28, %struct.sockaddr* %29, i32 16)
store i32 %30, i32* %4
%31 = load i32, i32* %4
%32 = icmp ne i32 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %17
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([74 x i8], [74 x i8]* @.str.1, i64 0, i64 0))
store i32 2, i32* %1
br label %80
35: ; preds = %17
%36 = load i32, i32* %3
%37 = call i32 @listen(i32 %36, i32 100)
store i32 %37, i32* %5
%38 = load i32, i32* %5
%39 = icmp ne i32 %38, 0
br i1 %39, label %40, label %42
40: ; preds = %35
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([78 x i8], [78 x i8]* @.str.2, i64 0, i64 0))
store i32 3, i32* %1
br label %80
42: ; preds = %35
%43 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0))
br label %44
44: ; preds = %42, %79
store i32 16, i32* %7
%45 = load i32, i32* %3
%46 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
%47 = bitcast %struct.TYPE_2__* %46 to %struct.sockaddr*
%48 = call i32 @accept(i32 %45, %struct.sockaddr* %47, i32* %7)
store i32 %48, i32* %8
%49 = load i32, i32* %8
%50 = icmp eq i32 %49, -1
br i1 %50, label %51, label %53
51: ; preds = %44
%52 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([60 x i8], [60 x i8]* @.str.4, i64 0, i64 0))
br label %59
53: ; preds = %44
%54 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
%55 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %54, i32 0, i32 0
%56 = load i32, i32* %55
%57 = call i8* @inet_ntoa(i32 %56)
%58 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.5, i64 0, i64 0), i8* %57)
br label %59
59: ; preds = %53, %51
%60 = load i32, i32* %8
%61 = load i32, i32* %3
%62 = bitcast %struct.sockaddr_in* %6 to { i64, i64 }*
%63 = getelementptr inbounds { i64, i64 }, { i64, i64 }* %62, i32 0, i32 0
%64 = load i64, i64* %63
%65 = getelementptr inbounds { i64, i64 }, { i64, i64 }* %62, i32 0, i32 1
%66 = load i64, i64* %65
%67 = call i32 @handle_client(i64 %64, i64 %66, i32 %60, i32 %61)
store i32 %67, i32* %9
%68 = load i32, i32* %9
%69 = icmp ne i32 %68, 1
br i1 %69, label %70, label %75
70: ; preds = %59
%71 = load i32, i32* %9
%72 = icmp ne i32 %71, 0
br i1 %72, label %73, label %75
73: ; preds = %70
%74 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.6, i64 0, i64 0))
br label %75
75: ; preds = %73, %70, %59
%76 = load i32, i32* %9
%77 = icmp eq i32 %76, 1
br i1 %77, label %78, label %79
78: ; preds = %75
store i32 0, i32* %1
br label %80
79: ; preds = %75
br label %44
80: ; preds = %78, %40, %33, %15
%81 = load i32, i32* %1
ret i32 %81
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [50 x i8] c"There was an error creating the specified socket.\00"
@INADDR_ANY = global i32 0
@.str.1 = [74 x i8] c"There was an error binding to the specified socket on the specified port.\00"
@.str.2 = [78 x i8] c"There was an error listening with the specified socket on the specified port.\00"
@.str.5 = [34 x i8] c"New connection recieved from: %s\0A\00"
@str = [23 x i8] c"Accepting connections.\00"
@str.7 = [44 x i8] c"There was an error handling the client. :(\0A\00"
@str.8 = [59 x i8] c"There was an error while the client was trying to connect.\00"
define i32 @main() {
%1 = alloca %struct.sockaddr_in
%2 = alloca { i64, i64 }
%3 = alloca i32
%4 = bitcast %struct.sockaddr_in* %1 to i8*
%5 = load i32, i32* @PF_INET
%6 = load i32, i32* @SOCK_STREAM
%7 = tail call i32 @socket(i32 %5, i32 %6, i32 0)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %11
9: ; preds = %0
%10 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([50 x i8], [50 x i8]* @.str, i64 0, i64 0))
br label %57
11: ; preds = %0
%12 = load i32, i32* @PF_INET
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 3
store i32 %12, i32* %13
%14 = tail call i32 @htons(i32 45287)
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 2
store i32 %14, i32* %15
%16 = load i32, i32* @INADDR_ANY
%17 = tail call i32 @htonl(i32 %16)
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0, i32 0
store i32 %17, i32* %18
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1
%20 = call i32 @memset(i32* %19, i8 signext 0, i32 8)
%21 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%22 = call i32 @bind(i32 %7, %struct.sockaddr* %21, i32 16)
%23 = icmp eq i32 %22, 0
br i1 %23, label %26, label %24
24: ; preds = %11
%25 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([74 x i8], [74 x i8]* @.str.1, i64 0, i64 0))
br label %57
26: ; preds = %11
%27 = call i32 @listen(i32 %7, i32 100)
%28 = icmp eq i32 %27, 0
br i1 %28, label %31, label %29
29: ; preds = %26
%30 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([78 x i8], [78 x i8]* @.str.2, i64 0, i64 0))
br label %57
31: ; preds = %26
%32 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str, i64 0, i64 0))
%33 = bitcast { i64, i64 }* %2 to i8*
%34 = bitcast i32* %3 to i8*
%35 = bitcast { i64, i64 }* %2 to %struct.sockaddr*
%36 = bitcast { i64, i64 }* %2 to i32*
%37 = getelementptr inbounds { i64, i64 }, { i64, i64 }* %2, i64 0, i32 0
%38 = getelementptr inbounds { i64, i64 }, { i64, i64 }* %2, i64 0, i32 1
br label %39
39: ; preds = %55, %31
store i32 16, i32* %3
%40 = call i32 @accept(i32 %7, %struct.sockaddr* %35, i32* %3)
%41 = icmp eq i32 %40, -1
br i1 %41, label %42, label %44
42: ; preds = %39
%43 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([59 x i8], [59 x i8]* @str.8, i64 0, i64 0))
br label %48
44: ; preds = %39
%45 = load i32, i32* %36
%46 = call i8* @inet_ntoa(i32 %45)
%47 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @.str.5, i64 0, i64 0), i8* %46)
br label %48
48: ; preds = %44, %42
%49 = load i64, i64* %37
%50 = load i64, i64* %38
%51 = call i32 @handle_client(i64 %49, i64 %50, i32 %40, i32 %7)
%52 = icmp ult i32 %51, 2
br i1 %52, label %55, label %53
53: ; preds = %48
%54 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([44 x i8], [44 x i8]* @str.7, i64 0, i64 0))
br label %55
55: ; preds = %53, %48
%56 = icmp eq i32 %51, 1
br i1 %56, label %57, label %39
57: ; preds = %55, %24, %29, %9
%58 = phi i32 [ 1, %9 ], [ 2, %24 ], [ 3, %29 ], [ 0, %55 ]
ret i32 %58
}