#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 socklen_t;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int accept(int, struct sockaddr *, int *);
int /*<<< orphan*/ bind(int, struct sockaddr const *, int);
int /*<<< orphan*/ check_errno(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ finalaze_socket(int);
scalar_t__ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, int);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_addr(char const *);
char *inet_ntoa(TYPE_1__);
int /*<<< orphan*/ listen(int, int);
scalar_t__ ntohs(int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ transfer_data(int);
void run_server(const char *IP_ADDR, int PORT_NUMBER, int MAX_BACKLOG) {
  int socket_d = socket(AF_INET, SOCK_STREAM, 0);
  if (socket_d == -1) {
    check_errno("socket");
  }
  struct sockaddr_in local_addr;
  local_addr.sin_family = AF_INET;
  local_addr.sin_port = htons(PORT_NUMBER);
  local_addr.sin_addr.s_addr = inet_addr(IP_ADDR);
  if (!bind(socket_d, (const struct sockaddr *)&local_addr,
            sizeof(struct sockaddr_in))) {
    check_errno("bind");
  }
  while (!listen(socket_d, MAX_BACKLOG)) {
    struct sockaddr_in remote_addr;
    socklen_t addr_len = sizeof(struct sockaddr_in);
    int rs = accept(socket_d, (struct sockaddr *)&remote_addr, &addr_len);
    if (rs == -1) {
      check_errno("accept");
    }
    if (fork() == 0) {
      close(socket_d);
      fprintf(stderr, "Connected: %s:%d\n", inet_ntoa(remote_addr.sin_addr),
              (int)ntohs(remote_addr.sin_port));
      transfer_data(rs);
      finalaze_socket(rs);
      fprintf(stderr, "Closed: %s:%d\n", inet_ntoa(remote_addr.sin_addr),
              (int)ntohs(remote_addr.sin_port));
      exit(0);
    }
    close(rs);
  }
  close(socket_d);
  check_errno("listen");
}


x86 assembly (clang, -O0):
<run_server>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    0x2ddf(%rip),%rax
mov    (%rax),%edi
mov    0x2dbe(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1110 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1234 <run_server+0x44>
lea    0xdd1(%rip),%rdi
call   10e0 <check_errno@plt>
mov    0x2dad(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0xc(%rbp),%edi
call   1050 <htons@plt>
mov    %eax,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
call   1070 <inet_addr@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   10b0 <bind@plt>
cmp    $0x0,%eax
jne    127d <run_server+0x8d>
lea    0xd8f(%rip),%rdi
call   10e0 <check_errno@plt>
jmp    1282 <run_server+0x92>
mov    -0x14(%rbp),%edi
mov    -0x10(%rbp),%esi
call   1090 <listen@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    12a2 <run_server+0xb2>
jmp    1379 <run_server+0x189>
movl   $0xc,-0x34(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x30(%rbp),%rsi
lea    -0x34(%rbp),%rdx
call   10d0 <accept@plt>
mov    %eax,-0x38(%rbp)
cmpl   $0xffffffff,-0x38(%rbp)
jne    12d2 <run_server+0xe2>
lea    0xd3f(%rip),%rdi
call   10e0 <check_errno@plt>
mov    $0x0,%al
call   1100 <fork@plt>
cmp    $0x0,%rax
jne    136c <run_server+0x17c>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
mov    0x2ce6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x54(%rbp)
mov    -0x2c(%rbp),%edi
call   1040 <inet_ntoa@plt>
mov    %rax,-0x50(%rbp)
mov    -0x30(%rbp),%edi
call   10a0 <ntohs@plt>
mov    -0x54(%rbp),%edi
mov    -0x50(%rbp),%rdx
mov    %eax,%ecx
lea    0xcf8(%rip),%rsi
call   1080 <fprintf@plt>
mov    -0x38(%rbp),%edi
call   1030 <transfer_data@plt>
mov    -0x38(%rbp),%edi
call   10c0 <finalaze_socket@plt>
mov    0x2ca1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
mov    -0x2c(%rbp),%edi
call   1040 <inet_ntoa@plt>
mov    %rax,-0x40(%rbp)
mov    -0x30(%rbp),%edi
call   10a0 <ntohs@plt>
mov    -0x44(%rbp),%edi
mov    -0x40(%rbp),%rdx
mov    %eax,%ecx
lea    0xcc5(%rip),%rsi
call   1080 <fprintf@plt>
xor    %edi,%edi
call   10f0 <exit@plt>
mov    -0x38(%rbp),%edi
call   1060 <close@plt>
jmp    1282 <run_server+0x92>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
lea    0xcac(%rip),%rdi
call   10e0 <check_errno@plt>
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<run_server>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %edx,%r14d
mov    %esi,%ebx
mov    %rdi,%r15
mov    0x2ddb(%rip),%r12
mov    (%r12),%edi
mov    0x2db8(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1110 <socket@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    1234 <run_server+0x44>
lea    0xdd1(%rip),%rdi
call   10e0 <check_errno@plt>
mov    (%r12),%eax
mov    %eax,0x20(%rsp)
mov    %ebx,%edi
call   1050 <htons@plt>
mov    %eax,0x18(%rsp)
mov    %r15,%rdi
call   1070 <inet_addr@plt>
mov    %eax,0x1c(%rsp)
lea    0x18(%rsp),%rsi
mov    %ebp,%edi
mov    $0xc,%edx
call   10b0 <bind@plt>
test   %eax,%eax
jne    1274 <run_server+0x84>
lea    0xd98(%rip),%rdi
call   10e0 <check_errno@plt>
mov    %ebp,%edi
mov    %r14d,%esi
call   1090 <listen@plt>
test   %eax,%eax
jne    12e5 <run_server+0xf5>
lea    0x8(%rsp),%r15
lea    0x4(%rsp),%r12
lea    0xd79(%rip),%r13
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
movl   $0xc,0x4(%rsp)
mov    %ebp,%edi
mov    %r15,%rsi
mov    %r12,%rdx
call   10d0 <accept@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
jne    12c4 <run_server+0xd4>
mov    %r13,%rdi
call   10e0 <check_errno@plt>
xor    %eax,%eax
call   1100 <fork@plt>
test   %rax,%rax
je     1307 <run_server+0x117>
mov    %ebx,%edi
call   1060 <close@plt>
mov    %ebp,%edi
mov    %r14d,%esi
call   1090 <listen@plt>
test   %eax,%eax
je     12a0 <run_server+0xb0>
mov    %ebp,%edi
call   1060 <close@plt>
lea    0xd41(%rip),%rdi
call   10e0 <check_errno@plt>
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %ebp,%edi
call   1060 <close@plt>
mov    0x2cc3(%rip),%r15
mov    (%r15),%r14d
mov    0xc(%rsp),%edi
call   1040 <inet_ntoa@plt>
mov    %rax,%rbp
mov    0x8(%rsp),%edi
call   10a0 <ntohs@plt>
lea    0xcdf(%rip),%rsi
mov    %r14d,%edi
mov    %rbp,%rdx
mov    %eax,%ecx
call   1080 <fprintf@plt>
mov    %ebx,%edi
call   1030 <transfer_data@plt>
mov    %ebx,%edi
call   10c0 <finalaze_socket@plt>
mov    (%r15),%ebx
mov    0xc(%rsp),%edi
call   1040 <inet_ntoa@plt>
mov    %rax,%rbp
mov    0x8(%rsp),%edi
call   10a0 <ntohs@plt>
lea    0xcb7(%rip),%rsi
mov    %ebx,%edi
mov    %rbp,%rdx
mov    %eax,%ecx
call   1080 <fprintf@plt>
xor    %edi,%edi
call   10f0 <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 = [7 x i8] c"socket\00"
@.str.1 = [5 x i8] c"bind\00"
@.str.2 = [7 x i8] c"accept\00"
@stderr = global i32 0
@.str.3 = [18 x i8] c"Connected: %s:%d\0A\00"
@.str.4 = [15 x i8] c"Closed: %s:%d\0A\00"
@.str.5 = [7 x i8] c"listen\00"
define void @run_server(i8* %0, i32 %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.sockaddr_in
%9 = alloca %struct.sockaddr_in
%10 = alloca i32
%11 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32 %2, i32* %6
%12 = load i32, i32* @AF_INET
%13 = load i32, i32* @SOCK_STREAM
%14 = call i32 @socket(i32 %12, i32 %13, i32 0)
store i32 %14, i32* %7
%15 = load i32, i32* %7
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %19
17: ; preds = %3
%18 = call i32 @check_errno(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %19
19: ; preds = %17, %3
%20 = load i32, i32* @AF_INET
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 2
store i32 %20, i32* %21
%22 = load i32, i32* %5
%23 = call i32 @htons(i32 %22)
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 0
store i32 %23, i32* %24
%25 = load i8*, i8** %4
%26 = call i32 @inet_addr(i8* %25)
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 1
%28 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %27, i32 0, i32 0
store i32 %26, i32* %28
%29 = load i32, i32* %7
%30 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%31 = call i32 @bind(i32 %29, %struct.sockaddr* %30, i32 12)
%32 = icmp ne i32 %31, 0
br i1 %32, label %35, label %33
33: ; preds = %19
%34 = call i32 @check_errno(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %35
35: ; preds = %33, %19
br label %36
36: ; preds = %81, %35
%37 = load i32, i32* %7
%38 = load i32, i32* %6
%39 = call i32 @listen(i32 %37, i32 %38)
%40 = icmp ne i32 %39, 0
%41 = xor i1 %40, true
br i1 %41, label %42, label %84
42: ; preds = %36
store i32 12, i32* %10
%43 = load i32, i32* %7
%44 = bitcast %struct.sockaddr_in* %9 to %struct.sockaddr*
%45 = call i32 @accept(i32 %43, %struct.sockaddr* %44, i32* %10)
store i32 %45, i32* %11
%46 = load i32, i32* %11
%47 = icmp eq i32 %46, -1
br i1 %47, label %48, label %50
48: ; preds = %42
%49 = call i32 @check_errno(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
br label %50
50: ; preds = %48, %42
%51 = call i64 (...) @fork()
%52 = icmp eq i64 %51, 0
br i1 %52, label %53, label %81
53: ; preds = %50
%54 = load i32, i32* %7
%55 = call i32 @close(i32 %54)
%56 = load i32, i32* @stderr
%57 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 1
%58 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %57, i32 0, i32 0
%59 = load i32, i32* %58
%60 = call i8* @inet_ntoa(i32 %59)
%61 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 0
%62 = load i32, i32* %61
%63 = call i64 @ntohs(i32 %62)
%64 = trunc i64 %63 to i32
%65 = call i32 @fprintf(i32 %56, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i8* %60, i32 %64)
%66 = load i32, i32* %11
%67 = call i32 @transfer_data(i32 %66)
%68 = load i32, i32* %11
%69 = call i32 @finalaze_socket(i32 %68)
%70 = load i32, i32* @stderr
%71 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 1
%72 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %71, i32 0, i32 0
%73 = load i32, i32* %72
%74 = call i8* @inet_ntoa(i32 %73)
%75 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 0
%76 = load i32, i32* %75
%77 = call i64 @ntohs(i32 %76)
%78 = trunc i64 %77 to i32
%79 = call i32 @fprintf(i32 %70, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0), i8* %74, i32 %78)
%80 = call i32 @exit(i32 0)
unreachable
81: ; preds = %50
%82 = load i32, i32* %11
%83 = call i32 @close(i32 %82)
br label %36
84: ; preds = %36
%85 = load i32, i32* %7
%86 = call i32 @close(i32 %85)
%87 = call i32 @check_errno(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0))
ret void
}

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 = [7 x i8] c"socket\00"
@.str.1 = [5 x i8] c"bind\00"
@.str.2 = [7 x i8] c"accept\00"
@stderr = global i32 0
@.str.3 = [18 x i8] c"Connected: %s:%d\0A\00"
@.str.4 = [15 x i8] c"Closed: %s:%d\0A\00"
@.str.5 = [7 x i8] c"listen\00"
define void @run_server(i8* %0, i32 %1, i32 %2) {
%4 = alloca %struct.sockaddr_in
%5 = alloca %struct.sockaddr_in
%6 = alloca i32
%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 eq i32 %9, -1
br i1 %10, label %11, label %13
11: ; preds = %3
%12 = tail call i32 @check_errno(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %13
13: ; preds = %11, %3
%14 = bitcast %struct.sockaddr_in* %4 to i8*
%15 = load i32, i32* @AF_INET
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 2
store i32 %15, i32* %16
%17 = tail call i32 @htons(i32 %1)
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0
store i32 %17, i32* %18
%19 = tail call i32 @inet_addr(i8* %0)
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1, i32 0
store i32 %19, i32* %20
%21 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%22 = call i32 @bind(i32 %9, %struct.sockaddr* %21, i32 12)
%23 = icmp eq i32 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %13
%25 = call i32 @check_errno(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %26
26: ; preds = %24, %13
%27 = call i32 @listen(i32 %9, i32 %2)
%28 = icmp eq i32 %27, 0
br i1 %28, label %29, label %66
29: ; preds = %26
%30 = bitcast %struct.sockaddr_in* %5 to i8*
%31 = bitcast i32* %6 to i8*
%32 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
br label %33
33: ; preds = %29, %62
store i32 12, i32* %6
%34 = call i32 @accept(i32 %9, %struct.sockaddr* %32, i32* %6)
%35 = icmp eq i32 %34, -1
br i1 %35, label %36, label %38
36: ; preds = %33
%37 = call i32 @check_errno(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
br label %38
38: ; preds = %36, %33
%39 = call i64 (...) @fork()
%40 = icmp eq i64 %39, 0
br i1 %40, label %41, label %62
41: ; preds = %38
%42 = call i32 @close(i32 %9)
%43 = load i32, i32* @stderr
%44 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 1, i32 0
%45 = load i32, i32* %44
%46 = call i8* @inet_ntoa(i32 %45)
%47 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 0
%48 = load i32, i32* %47
%49 = call i64 @ntohs(i32 %48)
%50 = trunc i64 %49 to i32
%51 = call i32 @fprintf(i32 %43, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i8* %46, i32 %50)
%52 = call i32 @transfer_data(i32 %34)
%53 = call i32 @finalaze_socket(i32 %34)
%54 = load i32, i32* @stderr
%55 = load i32, i32* %44
%56 = call i8* @inet_ntoa(i32 %55)
%57 = load i32, i32* %47
%58 = call i64 @ntohs(i32 %57)
%59 = trunc i64 %58 to i32
%60 = call i32 @fprintf(i32 %54, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0), i8* %56, i32 %59)
%61 = call i32 @exit(i32 0)
unreachable
62: ; preds = %38
%63 = call i32 @close(i32 %34)
%64 = call i32 @listen(i32 %9, i32 %2)
%65 = icmp eq i32 %64, 0
br i1 %65, label %33, label %66
66: ; preds = %62, %26
%67 = call i32 @close(i32 %9)
%68 = call i32 @check_errno(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0))
ret void
}