#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 */
typedef int /*<<< orphan*/ uint8_t;
typedef int /*<<< orphan*/ uint16_t;
struct timespec {
  int dummy;
};
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 ssize_t;
typedef int socklen_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ CLOCK_MONOTONIC_RAW;
int EXIT_FAILURE;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ IPPROTO_TCP;
int /*<<< orphan*/ SHUT_RDWR;
int /*<<< orphan*/ SOCK_STREAM;
int accept(int, struct sockaddr *, int *);
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ clock_gettime(int /*<<< orphan*/, struct timespec *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ free(int /*<<< orphan*/ *);
int fwrite(int /*<<< orphan*/ *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ htons(int /*<<< orphan*/);
int listen(int, int);
scalar_t__ malloc(int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ print_statistics(int, struct timespec, struct timespec);
int /*<<< orphan*/ printf(char *);
int recv(int, int /*<<< orphan*/ *, int, int /*<<< orphan*/);
int /*<<< orphan*/ shutdown(int, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int server_tcp(uint16_t listen_port, const char *file) {
  uint8_t *buffer;
  FILE *fp;
  int sock;
  int accepted;
  int received;
  ssize_t written;
  ssize_t total_bytes = 0;
  socklen_t client_addr_len;
  struct sockaddr_in sin;
  struct sockaddr client_addr;
  struct timespec start_time;
  struct timespec end_time;
  buffer = (uint8_t *)malloc(4096);
  if (!buffer) {
    perror("Allocate application receive buffer");
    return -EXIT_FAILURE;
  }
  fp = fopen(file, "w");
  if (!fp) {
    perror("Open file for writing");
    free(buffer);
    return -EXIT_FAILURE;
  }
  if ((sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1) {
    perror("Opening TCP socket");
    free(buffer);
    fclose(fp);
    return -EXIT_FAILURE;
  }
  memset(&sin, 0, sizeof(struct sockaddr_in));
  sin.sin_family = AF_INET;
  sin.sin_port = htons(listen_port);
  sin.sin_addr.s_addr = INADDR_ANY;
  if (bind(sock, (struct sockaddr *)&sin, sizeof(struct sockaddr_in)) == -1) {
    perror("TCP bind");
    free(buffer);
    fclose(fp);
    return -EXIT_FAILURE;
  }
  if (listen(sock, 1000) == -1) {
    perror("TCP listen");
    free(buffer);
    fclose(fp);
    return -EXIT_FAILURE;
  }
  client_addr_len = sizeof(struct sockaddr);
  accepted = accept(sock, &client_addr, &client_addr_len);
  if (accepted < 0) {
    perror("TCP accept");
    free(buffer);
    fclose(fp);
    return -EXIT_FAILURE;
  }
  clock_gettime(CLOCK_MONOTONIC_RAW, &start_time);
  while ((received = recv(accepted, buffer, 4096, 0)) > 0) {
    written = fwrite(buffer, sizeof(uint8_t), received, fp);
    total_bytes += received;
    if (written * sizeof(uint8_t) != received) {
      printf("Failed to write to the file the"
             " amount of data received from the network.\n");
      shutdown(accepted, SHUT_RDWR);
      shutdown(sock, SHUT_RDWR);
      close(accepted);
      close(sock);
      free(buffer);
      fclose(fp);
      return -EXIT_FAILURE;
    }
  }
  clock_gettime(CLOCK_MONOTONIC_RAW, &end_time);
  print_statistics(total_bytes, start_time, end_time);
  shutdown(accepted, SHUT_RDWR);
  shutdown(sock, SHUT_RDWR);
  close(accepted);
  close(sock);
  fclose(fp);
  free(buffer);
  return 0;
}


x86 assembly (clang, -O0):
<server_tcp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x34(%rbp)
mov    $0x1000,%edi
call   10c0 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    126e <server_tcp+0x4e>
lea    0xdaa(%rip),%rdi
call   1100 <perror@plt>
xor    %eax,%eax
mov    0x2d4c(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1566 <server_tcp+0x346>
mov    -0x10(%rbp),%rdi
lea    0xdab(%rip),%rsi
call   10f0 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    12b5 <server_tcp+0x95>
lea    0xd92(%rip),%rdi
call   1100 <perror@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
xor    %eax,%eax
mov    0x2d05(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1566 <server_tcp+0x346>
mov    0x2d24(%rip),%rax
mov    (%rax),%edi
mov    0x2cf3(%rip),%rax
mov    (%rax),%esi
mov    0x2d02(%rip),%rax
mov    (%rax),%edx
call   1140 <socket@plt>
mov    %eax,-0x24(%rbp)
cmp    $0xffffffff,%eax
jne    1312 <server_tcp+0xf2>
lea    0xd54(%rip),%rdi
call   1100 <perror@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rdi
call   1060 <fclose@plt>
xor    %eax,%eax
mov    0x2ca8(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1566 <server_tcp+0x346>
lea    -0x48(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   10a0 <memset@plt>
mov    0x2cb7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x40(%rbp)
mov    -0x8(%rbp),%edi
call   1080 <htons@plt>
mov    %eax,-0x44(%rbp)
mov    0x2c98(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x48(%rbp)
mov    -0x24(%rbp),%edi
lea    -0x48(%rbp),%rsi
mov    $0xc,%edx
call   10e0 <bind@plt>
cmp    $0xffffffff,%eax
jne    1390 <server_tcp+0x170>
lea    0xce9(%rip),%rdi
call   1100 <perror@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rdi
call   1060 <fclose@plt>
xor    %eax,%eax
mov    0x2c2a(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1566 <server_tcp+0x346>
mov    -0x24(%rbp),%edi
mov    $0x3e8,%esi
call   10d0 <listen@plt>
cmp    $0xffffffff,%eax
jne    13d7 <server_tcp+0x1b7>
lea    0xcab(%rip),%rdi
call   1100 <perror@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rdi
call   1060 <fclose@plt>
xor    %eax,%eax
mov    0x2be3(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1566 <server_tcp+0x346>
movl   $0x4,-0x38(%rbp)
mov    -0x24(%rbp),%edi
lea    -0x50(%rbp),%rsi
lea    -0x38(%rbp),%rdx
call   1110 <accept@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jge    142c <server_tcp+0x20c>
lea    0xc61(%rip),%rdi
call   1100 <perror@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rdi
call   1060 <fclose@plt>
xor    %eax,%eax
mov    0x2b8e(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1566 <server_tcp+0x346>
mov    0x2b95(%rip),%rax
mov    (%rax),%edi
lea    -0x58(%rbp),%rsi
call   1050 <clock_gettime@plt>
mov    -0x28(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    $0x1000,%edx
xor    %ecx,%ecx
call   1040 <recv@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0x0,%eax
jle    14fb <server_tcp+0x2db>
mov    -0x18(%rbp),%rdi
mov    -0x2c(%rbp),%edx
mov    -0x20(%rbp),%rcx
mov    $0x4,%esi
call   1120 <fwrite@plt>
mov    %eax,-0x30(%rbp)
mov    -0x2c(%rbp),%eax
add    -0x34(%rbp),%eax
mov    %eax,-0x34(%rbp)
movslq -0x30(%rbp),%rax
shl    $0x2,%rax
movslq -0x2c(%rbp),%rcx
cmp    %rcx,%rax
je     14f6 <server_tcp+0x2d6>
lea    0xbd4(%rip),%rdi
call   1090 <printf@plt>
mov    -0x28(%rbp),%edi
mov    0x2b47(%rip),%rax
mov    (%rax),%esi
call   1070 <shutdown@plt>
mov    -0x24(%rbp),%edi
mov    0x2b36(%rip),%rax
mov    (%rax),%esi
call   1070 <shutdown@plt>
mov    -0x28(%rbp),%edi
call   10b0 <close@plt>
mov    -0x24(%rbp),%edi
call   10b0 <close@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rdi
call   1060 <fclose@plt>
xor    %eax,%eax
mov    0x2ac4(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1566 <server_tcp+0x346>
jmp    143e <server_tcp+0x21e>
mov    0x2ac6(%rip),%rax
mov    (%rax),%edi
lea    -0x60(%rbp),%rsi
call   1050 <clock_gettime@plt>
mov    -0x34(%rbp),%edi
mov    -0x58(%rbp),%esi
mov    -0x60(%rbp),%edx
call   1130 <print_statistics@plt>
mov    -0x28(%rbp),%edi
mov    0x2acb(%rip),%rax
mov    (%rax),%esi
call   1070 <shutdown@plt>
mov    -0x24(%rbp),%edi
mov    0x2aba(%rip),%rax
mov    (%rax),%esi
call   1070 <shutdown@plt>
mov    -0x28(%rbp),%edi
call   10b0 <close@plt>
mov    -0x24(%rbp),%edi
call   10b0 <close@plt>
mov    -0x20(%rbp),%rdi
call   1060 <fclose@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<server_tcp>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rsi,%rbx
mov    %edi,%ebp
mov    $0x1000,%edi
call   10c0 <malloc@plt>
test   %rax,%rax
je     1417 <server_tcp+0x1f7>
mov    %rax,%r13
lea    0xdd4(%rip),%rsi
mov    %rbx,%rdi
call   10f0 <fopen@plt>
test   %rax,%rax
je     1425 <server_tcp+0x205>
mov    %rax,%r14
mov    0x2d75(%rip),%rax
mov    (%rax),%edi
mov    0x2d44(%rip),%rax
mov    (%rax),%esi
mov    0x2d53(%rip),%rax
mov    (%rax),%edx
call   1140 <socket@plt>
cmp    $0xffffffff,%eax
je     143b <server_tcp+0x21b>
mov    %eax,%r15d
lea    0x10(%rsp),%rbx
xor    %r12d,%r12d
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   10a0 <memset@plt>
mov    0x2d32(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
mov    %ebp,%edi
call   1090 <htons@plt>
mov    %eax,0x14(%rsp)
mov    0x2d12(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0xc,%edx
call   10e0 <bind@plt>
cmp    $0xffffffff,%eax
je     1444 <server_tcp+0x224>
mov    %r15d,%edi
mov    $0x3e8,%esi
call   10d0 <listen@plt>
cmp    $0xffffffff,%eax
je     1469 <server_tcp+0x249>
movl   $0x4,0xc(%rsp)
lea    0x20(%rsp),%rsi
lea    0xc(%rsp),%rdx
mov    %r15d,%edi
call   1110 <accept@plt>
test   %eax,%eax
js     1472 <server_tcp+0x252>
mov    %eax,%r12d
mov    0x2ca1(%rip),%rax
mov    (%rax),%edi
lea    0x8(%rsp),%rsi
call   1060 <clock_gettime@plt>
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r12d,%edi
mov    %r13,%rsi
mov    $0x1000,%edx
xor    %ecx,%ecx
call   1040 <recv@plt>
test   %eax,%eax
jle    13b5 <server_tcp+0x195>
mov    %eax,%ebp
mov    %r13,%rdi
mov    $0x4,%esi
mov    %eax,%edx
mov    %r14,%rcx
call   1120 <fwrite@plt>
add    %ebp,%ebx
cltq
shl    $0x2,%rax
mov    %ebp,%ecx
cmp    %rcx,%rax
je     1340 <server_tcp+0x120>
lea    0xcee(%rip),%rdi
call   1050 <puts@plt>
mov    0x2c64(%rip),%rbx
mov    (%rbx),%esi
mov    %r12d,%edi
call   1080 <shutdown@plt>
mov    (%rbx),%esi
mov    %r15d,%edi
call   1080 <shutdown@plt>
mov    %r12d,%edi
call   10b0 <close@plt>
mov    %r15d,%edi
call   10b0 <close@plt>
jmp    147e <server_tcp+0x25e>
mov    0x2c0c(%rip),%rax
mov    (%rax),%edi
mov    %rsp,%rsi
call   1060 <clock_gettime@plt>
mov    0x8(%rsp),%esi
mov    (%rsp),%edx
mov    %ebx,%edi
call   1130 <print_statistics@plt>
mov    0x2c15(%rip),%rbx
mov    (%rbx),%esi
mov    %r12d,%edi
call   1080 <shutdown@plt>
mov    (%rbx),%esi
mov    %r15d,%edi
call   1080 <shutdown@plt>
mov    %r12d,%edi
call   10b0 <close@plt>
mov    %r15d,%edi
call   10b0 <close@plt>
mov    %r14,%rdi
call   1070 <fclose@plt>
mov    %r13,%rdi
call   1030 <free@plt>
xor    %r12d,%r12d
jmp    149b <server_tcp+0x27b>
lea    0xbe2(%rip),%rdi
call   1100 <perror@plt>
jmp    148e <server_tcp+0x26e>
lea    0xbfa(%rip),%rdi
call   1100 <perror@plt>
mov    %r13,%rdi
call   1030 <free@plt>
jmp    148e <server_tcp+0x26e>
lea    0xbfa(%rip),%rdi
jmp    1479 <server_tcp+0x259>
lea    0xc04(%rip),%rdi
call   1100 <perror@plt>
mov    %r13,%rdi
call   1030 <free@plt>
mov    %r14,%rdi
call   1070 <fclose@plt>
mov    0x2b49(%rip),%rax
jmp    1498 <server_tcp+0x278>
lea    0xbe8(%rip),%rdi
jmp    1479 <server_tcp+0x259>
lea    0xbea(%rip),%rdi
call   1100 <perror@plt>
mov    %r13,%rdi
call   1030 <free@plt>
mov    %r14,%rdi
call   1070 <fclose@plt>
mov    0x2b1b(%rip),%rax
xor    %r12d,%r12d
sub    (%rax),%r12d
mov    %r12d,%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 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
%struct.timespec = type { i32 }
@.str = [36 x i8] c"Allocate application receive buffer\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [22 x i8] c"Open file for writing\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@.str.3 = [19 x i8] c"Opening TCP socket\00"
@INADDR_ANY = global i32 0
@.str.4 = [9 x i8] c"TCP bind\00"
@.str.5 = [11 x i8] c"TCP listen\00"
@.str.6 = [11 x i8] c"TCP accept\00"
@CLOCK_MONOTONIC_RAW = global i32 0
@.str.7 = [75 x i8] c"Failed to write to the file the amount of data received from the network.\0A\00"
@SHUT_RDWR = global i32 0
define i32 @server_tcp(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca %struct.sockaddr_in
%15 = alloca %struct.sockaddr
%16 = alloca %struct.timespec
%17 = alloca %struct.timespec
store i32 %0, i32* %4
store i8* %1, i8** %5
store i32 0, i32* %12
%18 = call i64 @malloc(i32 4096)
%19 = inttoptr i64 %18 to i32*
store i32* %19, i32** %6
%20 = load i32*, i32** %6
%21 = icmp ne i32* %20, null
br i1 %21, label %26, label %22
22: ; preds = %2
%23 = call i32 @perror(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0))
%24 = load i32, i32* @EXIT_FAILURE
%25 = sub nsw i32 0, %24
store i32 %25, i32* %3
br label %162
26: ; preds = %2
%27 = load i8*, i8** %5
%28 = call i32* @fopen(i8* %27, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %28, i32** %7
%29 = load i32*, i32** %7
%30 = icmp ne i32* %29, null
br i1 %30, label %37, label %31
31: ; preds = %26
%32 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
%33 = load i32*, i32** %6
%34 = call i32 @free(i32* %33)
%35 = load i32, i32* @EXIT_FAILURE
%36 = sub nsw i32 0, %35
store i32 %36, i32* %3
br label %162
37: ; preds = %26
%38 = load i32, i32* @AF_INET
%39 = load i32, i32* @SOCK_STREAM
%40 = load i32, i32* @IPPROTO_TCP
%41 = call i32 @socket(i32 %38, i32 %39, i32 %40)
store i32 %41, i32* %8
%42 = icmp eq i32 %41, -1
br i1 %42, label %43, label %51
43: ; preds = %37
%44 = call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0))
%45 = load i32*, i32** %6
%46 = call i32 @free(i32* %45)
%47 = load i32*, i32** %7
%48 = call i32 @fclose(i32* %47)
%49 = load i32, i32* @EXIT_FAILURE
%50 = sub nsw i32 0, %49
store i32 %50, i32* %3
br label %162
51: ; preds = %37
%52 = call i32 @memset(%struct.sockaddr_in* %14, i32 0, i32 12)
%53 = load i32, i32* @AF_INET
%54 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %14, i32 0, i32 2
store i32 %53, i32* %54
%55 = load i32, i32* %4
%56 = call i32 @htons(i32 %55)
%57 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %14, i32 0, i32 1
store i32 %56, i32* %57
%58 = load i32, i32* @INADDR_ANY
%59 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %14, i32 0, i32 0
%60 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %59, i32 0, i32 0
store i32 %58, i32* %60
%61 = load i32, i32* %8
%62 = bitcast %struct.sockaddr_in* %14 to %struct.sockaddr*
%63 = call i32 @bind(i32 %61, %struct.sockaddr* %62, i32 12)
%64 = icmp eq i32 %63, -1
br i1 %64, label %65, label %73
65: ; preds = %51
%66 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0))
%67 = load i32*, i32** %6
%68 = call i32 @free(i32* %67)
%69 = load i32*, i32** %7
%70 = call i32 @fclose(i32* %69)
%71 = load i32, i32* @EXIT_FAILURE
%72 = sub nsw i32 0, %71
store i32 %72, i32* %3
br label %162
73: ; preds = %51
%74 = load i32, i32* %8
%75 = call i32 @listen(i32 %74, i32 1000)
%76 = icmp eq i32 %75, -1
br i1 %76, label %77, label %85
77: ; preds = %73
%78 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.5, i64 0, i64 0))
%79 = load i32*, i32** %6
%80 = call i32 @free(i32* %79)
%81 = load i32*, i32** %7
%82 = call i32 @fclose(i32* %81)
%83 = load i32, i32* @EXIT_FAILURE
%84 = sub nsw i32 0, %83
store i32 %84, i32* %3
br label %162
85: ; preds = %73
store i32 4, i32* %13
%86 = load i32, i32* %8
%87 = call i32 @accept(i32 %86, %struct.sockaddr* %15, i32* %13)
store i32 %87, i32* %9
%88 = load i32, i32* %9
%89 = icmp slt i32 %88, 0
br i1 %89, label %90, label %98
90: ; preds = %85
%91 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.6, i64 0, i64 0))
%92 = load i32*, i32** %6
%93 = call i32 @free(i32* %92)
%94 = load i32*, i32** %7
%95 = call i32 @fclose(i32* %94)
%96 = load i32, i32* @EXIT_FAILURE
%97 = sub nsw i32 0, %96
store i32 %97, i32* %3
br label %162
98: ; preds = %85
%99 = load i32, i32* @CLOCK_MONOTONIC_RAW
%100 = call i32 @clock_gettime(i32 %99, %struct.timespec* %16)
br label %101
101: ; preds = %138, %98
%102 = load i32, i32* %9
%103 = load i32*, i32** %6
%104 = call i32 @recv(i32 %102, i32* %103, i32 4096, i32 0)
store i32 %104, i32* %10
%105 = icmp sgt i32 %104, 0
br i1 %105, label %106, label %139
106: ; preds = %101
%107 = load i32*, i32** %6
%108 = load i32, i32* %10
%109 = load i32*, i32** %7
%110 = call i32 @fwrite(i32* %107, i32 4, i32 %108, i32* %109)
store i32 %110, i32* %11
%111 = load i32, i32* %10
%112 = load i32, i32* %12
%113 = add nsw i32 %112, %111
store i32 %113, i32* %12
%114 = load i32, i32* %11
%115 = sext i32 %114 to i64
%116 = mul i64 %115, 4
%117 = load i32, i32* %10
%118 = sext i32 %117 to i64
%119 = icmp ne i64 %116, %118
br i1 %119, label %120, label %138
120: ; preds = %106
%121 = call i32 @printf(i8* getelementptr inbounds ([75 x i8], [75 x i8]* @.str.7, i64 0, i64 0))
%122 = load i32, i32* %9
%123 = load i32, i32* @SHUT_RDWR
%124 = call i32 @shutdown(i32 %122, i32 %123)
%125 = load i32, i32* %8
%126 = load i32, i32* @SHUT_RDWR
%127 = call i32 @shutdown(i32 %125, i32 %126)
%128 = load i32, i32* %9
%129 = call i32 @close(i32 %128)
%130 = load i32, i32* %8
%131 = call i32 @close(i32 %130)
%132 = load i32*, i32** %6
%133 = call i32 @free(i32* %132)
%134 = load i32*, i32** %7
%135 = call i32 @fclose(i32* %134)
%136 = load i32, i32* @EXIT_FAILURE
%137 = sub nsw i32 0, %136
store i32 %137, i32* %3
br label %162
138: ; preds = %106
br label %101
139: ; preds = %101
%140 = load i32, i32* @CLOCK_MONOTONIC_RAW
%141 = call i32 @clock_gettime(i32 %140, %struct.timespec* %17)
%142 = load i32, i32* %12
%143 = getelementptr inbounds %struct.timespec, %struct.timespec* %16, i32 0, i32 0
%144 = load i32, i32* %143
%145 = getelementptr inbounds %struct.timespec, %struct.timespec* %17, i32 0, i32 0
%146 = load i32, i32* %145
%147 = call i32 @print_statistics(i32 %142, i32 %144, i32 %146)
%148 = load i32, i32* %9
%149 = load i32, i32* @SHUT_RDWR
%150 = call i32 @shutdown(i32 %148, i32 %149)
%151 = load i32, i32* %8
%152 = load i32, i32* @SHUT_RDWR
%153 = call i32 @shutdown(i32 %151, i32 %152)
%154 = load i32, i32* %9
%155 = call i32 @close(i32 %154)
%156 = load i32, i32* %8
%157 = call i32 @close(i32 %156)
%158 = load i32*, i32** %7
%159 = call i32 @fclose(i32* %158)
%160 = load i32*, i32** %6
%161 = call i32 @free(i32* %160)
store i32 0, i32* %3
br label %162
162: ; preds = %139, %120, %90, %77, %65, %43, %31, %22
%163 = load i32, i32* %3
ret i32 %163
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
%struct.timespec = type { i32 }
@.str = [36 x i8] c"Allocate application receive buffer\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [22 x i8] c"Open file for writing\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@.str.3 = [19 x i8] c"Opening TCP socket\00"
@INADDR_ANY = global i32 0
@.str.4 = [9 x i8] c"TCP bind\00"
@.str.5 = [11 x i8] c"TCP listen\00"
@.str.6 = [11 x i8] c"TCP accept\00"
@CLOCK_MONOTONIC_RAW = global i32 0
@SHUT_RDWR = global i32 0
@str = [74 x i8] c"Failed to write to the file the amount of data received from the network.\00"
define i32 @server_tcp(i32 %0, i8* nocapture %1) {
%3 = alloca i32
%4 = alloca %struct.sockaddr_in
%5 = alloca %struct.sockaddr
%6 = alloca %struct.timespec
%7 = alloca %struct.timespec
%8 = bitcast i32* %3 to i8*
%9 = bitcast %struct.sockaddr_in* %4 to i8*
%10 = bitcast %struct.sockaddr* %5 to i8*
%11 = bitcast %struct.timespec* %6 to i8*
%12 = bitcast %struct.timespec* %7 to i8*
%13 = tail call i64 @malloc(i32 4096)
%14 = inttoptr i64 %13 to i32*
%15 = icmp eq i64 %13, 0
br i1 %15, label %16, label %20
16: ; preds = %2
%17 = tail call i32 @perror(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0))
%18 = load i32, i32* @EXIT_FAILURE
%19 = sub nsw i32 0, %18
br label %117
20: ; preds = %2
%21 = tail call i32* @fopen(i8* %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%22 = icmp eq i32* %21, null
br i1 %22, label %23, label %28
23: ; preds = %20
%24 = tail call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.2, i64 0, i64 0))
%25 = tail call i32 @free(i32* %14)
%26 = load i32, i32* @EXIT_FAILURE
%27 = sub nsw i32 0, %26
br label %117
28: ; preds = %20
%29 = load i32, i32* @AF_INET
%30 = load i32, i32* @SOCK_STREAM
%31 = load i32, i32* @IPPROTO_TCP
%32 = tail call i32 @socket(i32 %29, i32 %30, i32 %31)
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %40
34: ; preds = %28
%35 = tail call i32 @perror(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0))
%36 = tail call i32 @free(i32* %14)
%37 = tail call i32 @fclose(i32* %21)
%38 = load i32, i32* @EXIT_FAILURE
%39 = sub nsw i32 0, %38
br label %117
40: ; preds = %28
%41 = call i32 @memset(%struct.sockaddr_in* %4, i32 0, i32 12)
%42 = load i32, i32* @AF_INET
%43 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 2
store i32 %42, i32* %43
%44 = call i32 @htons(i32 %0)
%45 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1
store i32 %44, i32* %45
%46 = load i32, i32* @INADDR_ANY
%47 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0, i32 0
store i32 %46, i32* %47
%48 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%49 = call i32 @bind(i32 %32, %struct.sockaddr* %48, i32 12)
%50 = icmp eq i32 %49, -1
br i1 %50, label %51, label %57
51: ; preds = %40
%52 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0))
%53 = call i32 @free(i32* %14)
%54 = call i32 @fclose(i32* %21)
%55 = load i32, i32* @EXIT_FAILURE
%56 = sub nsw i32 0, %55
br label %117
57: ; preds = %40
%58 = call i32 @listen(i32 %32, i32 1000)
%59 = icmp eq i32 %58, -1
br i1 %59, label %60, label %66
60: ; preds = %57
%61 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.5, i64 0, i64 0))
%62 = call i32 @free(i32* %14)
%63 = call i32 @fclose(i32* %21)
%64 = load i32, i32* @EXIT_FAILURE
%65 = sub nsw i32 0, %64
br label %117
66: ; preds = %57
store i32 4, i32* %3
%67 = call i32 @accept(i32 %32, %struct.sockaddr* %5, i32* %3)
%68 = icmp slt i32 %67, 0
br i1 %68, label %69, label %75
69: ; preds = %66
%70 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.6, i64 0, i64 0))
%71 = call i32 @free(i32* %14)
%72 = call i32 @fclose(i32* %21)
%73 = load i32, i32* @EXIT_FAILURE
%74 = sub nsw i32 0, %73
br label %117
75: ; preds = %66
%76 = load i32, i32* @CLOCK_MONOTONIC_RAW
%77 = call i32 @clock_gettime(i32 %76, %struct.timespec* %6)
br label %78
78: ; preds = %82, %75
%79 = phi i32 [ 0, %75 ], [ %84, %82 ]
%80 = call i32 @recv(i32 %67, i32* %14, i32 4096, i32 0)
%81 = icmp sgt i32 %80, 0
br i1 %81, label %82, label %101
82: ; preds = %78
%83 = call i32 @fwrite(i32* %14, i32 4, i32 %80, i32* %21)
%84 = add nsw i32 %80, %79
%85 = sext i32 %83 to i64
%86 = shl nsw i64 %85, 2
%87 = zext i32 %80 to i64
%88 = icmp eq i64 %86, %87
br i1 %88, label %78, label %89
89: ; preds = %82
%90 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([74 x i8], [74 x i8]* @str, i64 0, i64 0))
%91 = load i32, i32* @SHUT_RDWR
%92 = call i32 @shutdown(i32 %67, i32 %91)
%93 = load i32, i32* @SHUT_RDWR
%94 = call i32 @shutdown(i32 %32, i32 %93)
%95 = call i32 @close(i32 %67)
%96 = call i32 @close(i32 %32)
%97 = call i32 @free(i32* %14)
%98 = call i32 @fclose(i32* %21)
%99 = load i32, i32* @EXIT_FAILURE
%100 = sub nsw i32 0, %99
br label %117
101: ; preds = %78
%102 = load i32, i32* @CLOCK_MONOTONIC_RAW
%103 = call i32 @clock_gettime(i32 %102, %struct.timespec* %7)
%104 = getelementptr inbounds %struct.timespec, %struct.timespec* %6, i64 0, i32 0
%105 = load i32, i32* %104
%106 = getelementptr inbounds %struct.timespec, %struct.timespec* %7, i64 0, i32 0
%107 = load i32, i32* %106
%108 = call i32 @print_statistics(i32 %79, i32 %105, i32 %107)
%109 = load i32, i32* @SHUT_RDWR
%110 = call i32 @shutdown(i32 %67, i32 %109)
%111 = load i32, i32* @SHUT_RDWR
%112 = call i32 @shutdown(i32 %32, i32 %111)
%113 = call i32 @close(i32 %67)
%114 = call i32 @close(i32 %32)
%115 = call i32 @fclose(i32* %21)
%116 = call i32 @free(i32* %14)
br label %117
117: ; preds = %101, %89, %69, %60, %51, %34, %23, %16
%118 = phi i32 [ %39, %34 ], [ %56, %51 ], [ %65, %60 ], [ %74, %69 ], [ %100, %89 ], [ 0, %101 ], [ %27, %23 ], [ %19, %16 ]
ret i32 %118
}