#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*/ pthread_t;
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*/ free(int /*<<< orphan*/ *);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ listen(int, int);
void *malloc(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
int pthread_create(int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                   void *(*)(void *), int *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int initSocketListener(int port, void *(*processFunc)(void *arg)) {
  int sockfd;
  struct sockaddr_in serv_addr, cli_addr;
  sockfd = socket(AF_INET, SOCK_STREAM, 0);
  if (sockfd < 0) {
    perror("ERROR - Couldn't open socket.\n");
    return (1);
  }
  bzero((char *)&serv_addr, sizeof(serv_addr));
  serv_addr.sin_family = AF_INET;
  serv_addr.sin_addr.s_addr = INADDR_ANY;
  serv_addr.sin_port = htons(port);
  if (bind(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) {
    perror("ERROR - Couldn't bind.\n");
    return (2);
  } else {
    printf("Listening on port %d...\n", port);
  }
  listen(sockfd, 3);
  int clilen = sizeof(cli_addr);
  while (1) {
    int *newsockfd = malloc(sizeof(int));
    *newsockfd = accept(sockfd, (struct sockaddr *)&cli_addr, &clilen);
    if (newsockfd < 0) {
      perror("ERROR - Error on accept.\n");
      return (3);
    } else {
      pthread_t *thr = malloc(sizeof(pthread_t));
      int err = pthread_create(thr, NULL, processFunc, newsockfd);
      free(thr);
      if (err != 0) {
        perror("ERROR - Couldn't create a processing thread.\n");
        return (4);
      }
    }
  }
  return (0);
}


x86 assembly (clang, -O0):
<initSocketListener>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e22(%rip),%rax
mov    (%rax),%edi
mov    0x2e01(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11fd <initSocketListener+0x4d>
lea    0xe14(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1328 <initSocketListener+0x178>
lea    -0x20(%rbp),%rdi
mov    $0xc,%esi
call   10b0 <bzero@plt>
mov    0x2dd6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    0x2dc2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x20(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   1080 <bind@plt>
cmp    $0x0,%rax
jge    1261 <initSocketListener+0xb1>
lea    0xdcf(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x2,-0x4(%rbp)
jmp    1328 <initSocketListener+0x178>
mov    -0x8(%rbp),%esi
lea    0xdcc(%rip),%rdi
call   1050 <printf@plt>
mov    -0x14(%rbp),%edi
mov    $0x3,%esi
call   1070 <listen@plt>
movl   $0xc,-0x34(%rbp)
mov    $0x4,%edi
call   1060 <malloc@plt>
mov    %rax,-0x40(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x30(%rbp),%rsi
lea    -0x34(%rbp),%rdx
call   10c0 <accept@plt>
mov    %eax,%ecx
mov    -0x40(%rbp),%rax
mov    %ecx,(%rax)
cmpq   $0x0,-0x40(%rbp)
jae    12cd <initSocketListener+0x11d>
lea    0xd94(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x3,-0x4(%rbp)
jmp    1328 <initSocketListener+0x178>
mov    $0x4,%edi
call   1060 <malloc@plt>
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    -0x40(%rbp),%rcx
xor    %eax,%eax
mov    %eax,%esi
call   1090 <pthread_create@plt>
mov    %eax,-0x4c(%rbp)
mov    -0x48(%rbp),%rdi
call   1030 <free@plt>
cmpl   $0x0,-0x4c(%rbp)
je     131e <initSocketListener+0x16e>
lea    0xd5d(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x4,-0x4(%rbp)
jmp    1328 <initSocketListener+0x178>
jmp    1323 <initSocketListener+0x173>
jmp    1284 <initSocketListener+0xd4>
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<initSocketListener>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rsi,%r15
mov    %edi,%r14d
mov    0x2e1d(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2dfb(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
test   %eax,%eax
jns    11f8 <initSocketListener+0x48>
lea    0xe17(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%eax
jmp    12e9 <initSocketListener+0x139>
mov    %eax,%r13d
lea    0x8(%rsp),%rbx
mov    %rbx,%rdi
mov    $0xc,%esi
call   10b0 <bzero@plt>
mov    0x0(%rbp),%eax
mov    %eax,0x10(%rsp)
mov    0x2dc5(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
mov    %r14d,%edi
call   1040 <htons@plt>
mov    %eax,0x8(%rsp)
mov    %r13d,%edi
mov    %rbx,%rsi
mov    $0xc,%edx
call   1080 <bind@plt>
test   %rax,%rax
jns    1258 <initSocketListener+0xa8>
lea    0xdd6(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x2,%eax
jmp    12e9 <initSocketListener+0x139>
lea    0xdd8(%rip),%rdi
mov    %r14d,%esi
call   1050 <printf@plt>
mov    %r13d,%edi
mov    $0x3,%esi
call   1070 <listen@plt>
movl   $0xc,0x4(%rsp)
lea    0x18(%rsp),%r14
lea    0x4(%rsp),%r12
cs nopw 0x0(%rax,%rax,1)
mov    $0x4,%edi
call   1060 <malloc@plt>
mov    %rax,%rbx
mov    %r13d,%edi
mov    %r14,%rsi
mov    %r12,%rdx
call   10c0 <accept@plt>
mov    %eax,(%rbx)
mov    $0x4,%edi
call   1060 <malloc@plt>
mov    %rax,%rbp
mov    %rax,%rdi
xor    %esi,%esi
mov    %r15,%rdx
mov    %rbx,%rcx
call   1090 <pthread_create@plt>
mov    %eax,%ebx
mov    %rbp,%rdi
call   1030 <free@plt>
test   %ebx,%ebx
je     1290 <initSocketListener+0xe0>
lea    0xd71(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x4,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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 = [31 x i8] c"ERROR - Couldn't open socket.\0A\00"
@INADDR_ANY = global i32 0
@.str.1 = [24 x i8] c"ERROR - Couldn't bind.\0A\00"
@.str.2 = [25 x i8] c"Listening on port %d...\0A\00"
@.str.3 = [26 x i8] c"ERROR - Error on accept.\0A\00"
@.str.4 = [46 x i8] c"ERROR - Couldn't create a processing thread.\0A\00"
define i32 @initSocketListener(i32 %0, i8* (i8*)* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8* (i8*)*
%6 = alloca i32
%7 = alloca %struct.sockaddr_in
%8 = alloca %struct.sockaddr_in
%9 = alloca i32
%10 = alloca i32*
%11 = alloca i32*
%12 = alloca i32
store i32 %0, i32* %4
store i8* (i8*)* %1, i8* (i8*)** %5
%13 = load i32, i32* @AF_INET
%14 = load i32, i32* @SOCK_STREAM
%15 = call i32 @socket(i32 %13, i32 %14, i32 0)
store i32 %15, i32* %6
%16 = load i32, i32* %6
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %2
%19 = call i32 @perror(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %69
20: ; preds = %2
%21 = bitcast %struct.sockaddr_in* %7 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* %7, i32 0, i32 2
store i32 %23, i32* %24
%25 = load i32, i32* @INADDR_ANY
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 1
%27 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %26, i32 0, i32 0
store i32 %25, i32* %27
%28 = load i32, i32* %4
%29 = call i32 @htons(i32 %28)
%30 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
store i32 %29, i32* %30
%31 = load i32, i32* %6
%32 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%33 = call i64 @bind(i32 %31, %struct.sockaddr* %32, i32 12)
%34 = icmp slt i64 %33, 0
br i1 %34, label %35, label %37
35: ; preds = %20
%36 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
store i32 2, i32* %3
br label %69
37: ; preds = %20
%38 = load i32, i32* %4
%39 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i32 %38)
br label %40
40: ; preds = %37
%41 = load i32, i32* %6
%42 = call i32 @listen(i32 %41, i32 3)
store i32 12, i32* %9
br label %43
43: ; preds = %40, %68
%44 = call i8* @malloc(i32 4)
%45 = bitcast i8* %44 to i32*
store i32* %45, i32** %10
%46 = load i32, i32* %6
%47 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%48 = call i32 @accept(i32 %46, %struct.sockaddr* %47, i32* %9)
%49 = load i32*, i32** %10
store i32 %48, i32* %49
%50 = load i32*, i32** %10
%51 = icmp ult i32* %50, null
br i1 %51, label %52, label %54
52: ; preds = %43
%53 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0))
store i32 3, i32* %3
br label %69
54: ; preds = %43
%55 = call i8* @malloc(i32 4)
%56 = bitcast i8* %55 to i32*
store i32* %56, i32** %11
%57 = load i32*, i32** %11
%58 = load i8* (i8*)*, i8* (i8*)** %5
%59 = load i32*, i32** %10
%60 = call i32 @pthread_create(i32* %57, i32* null, i8* (i8*)* %58, i32* %59)
store i32 %60, i32* %12
%61 = load i32*, i32** %11
%62 = call i32 @free(i32* %61)
%63 = load i32, i32* %12
%64 = icmp ne i32 %63, 0
br i1 %64, label %65, label %67
65: ; preds = %54
%66 = call i32 @perror(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.4, i64 0, i64 0))
store i32 4, i32* %3
br label %69
67: ; preds = %54
br label %68
68: ; preds = %67
br label %43
69: ; preds = %65, %52, %35, %18
%70 = load i32, i32* %3
ret i32 %70
}

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 = [31 x i8] c"ERROR - Couldn't open socket.\0A\00"
@INADDR_ANY = global i32 0
@.str.1 = [24 x i8] c"ERROR - Couldn't bind.\0A\00"
@.str.2 = [25 x i8] c"Listening on port %d...\0A\00"
@.str.4 = [46 x i8] c"ERROR - Couldn't create a processing thread.\0A\00"
define i32 @initSocketListener(i32 %0, i8* (i8*)* %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca %struct.sockaddr_in
%5 = alloca i32
%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 0)
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = tail call i32 @perror(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0))
br label %43
14: ; preds = %2
%15 = call i32 @bzero(i8* %6, i32 12)
%16 = load i32, i32* @AF_INET
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %16, i32* %17
%18 = load i32, i32* @INADDR_ANY
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1, i32 0
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 0
store i32 %20, i32* %21
%22 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%23 = call i64 @bind(i32 %10, %struct.sockaddr* %22, i32 12)
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %14
%26 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
br label %43
27: ; preds = %14
%28 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i32 %0)
%29 = call i32 @listen(i32 %10, i32 3)
%30 = bitcast i32* %5 to i8*
store i32 12, i32* %5
%31 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
br label %32
32: ; preds = %32, %27
%33 = call dereferenceable_or_null(4) i8* @malloc(i32 4)
%34 = bitcast i8* %33 to i32*
%35 = call i32 @accept(i32 %10, %struct.sockaddr* %31, i32* %5)
store i32 %35, i32* %34
%36 = call dereferenceable_or_null(4) i8* @malloc(i32 4)
%37 = bitcast i8* %36 to i32*
%38 = call i32 @pthread_create(i32* %37, i32* null, i8* (i8*)* %1, i32* %34)
%39 = call i32 @free(i32* %37)
%40 = icmp eq i32 %38, 0
br i1 %40, label %32, label %41
41: ; preds = %32
%42 = call i32 @perror(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.4, i64 0, i64 0))
br label %43
43: ; preds = %41, %25, %12
%44 = phi i32 [ 1, %12 ], [ 2, %25 ], [ 4, %41 ]
ret i32 %44
}