#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*/ sin;
typedef int /*<<< orphan*/ cin;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int INET_ADDRSTRLEN;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ SEEK_CUR;
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ SOCK_STREAM;
int accept(int, struct sockaddr *, int *);
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
unsigned int crc32_le(unsigned int, char *, int);
scalar_t__ fork();
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_ntop(int /*<<< orphan*/, TYPE_1__ *, char *, int);
scalar_t__ listen(int, int);
unsigned int lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int open(char *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int strtol(char *, char **, int /*<<< orphan*/);
int write(int, ...);
int main(int argc, char **argv) {
  int fd, sockfd, port, result, bytes_read, bytes_written, bytes_out;
  unsigned int size, crc32_calc = 0;
  char *endptr, ip_addr[INET_ADDRSTRLEN], buf[1024];
  struct sockaddr_in sin, cin;
  socklen_t cin_size = sizeof(cin);
  if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
    printf("Error creating socket.\n");
    return 1;
  }
  port = strtol(argv[1], &endptr, 0);
  if (*endptr) {
    printf("Invalid port number.\n");
    return 1;
  }
  memset(&sin, 0, sizeof(sin));
  sin.sin_family = AF_INET;
  sin.sin_addr.s_addr = htonl(INADDR_ANY);
  sin.sin_port = htons(port);
  printf("Bound to port %d, waiting for connection...\n", port);
  if (bind(sockfd, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
    printf("Error binding to port.\n");
    return 1;
  }
  if (listen(sockfd, 1) < 0) {
    printf("Error listening for connections.\n");
    return 1;
  }
  while (1) {
    if ((result = accept(sockfd, (struct sockaddr *)&cin, &cin_size)) < 0) {
      printf("Error accepting new connection.\n");
      return 1;
    }
    inet_ntop(AF_INET, &cin.sin_addr, ip_addr, cin_size);
    printf("Received connection from %s, serving file.\n", ip_addr);
    if (fork() == 0) {
      if ((fd = open(argv[2], O_RDONLY, 0)) < 0) {
        printf("Error opening file.\n");
        return 1;
      }
      lseek(fd, 0, SEEK_END);
      size = lseek(fd, 0, SEEK_CUR);
      lseek(fd, 0, SEEK_SET);
      write(result, &size, 4);
      while (1) {
        bytes_read = read(fd, buf, sizeof(buf));
        if (bytes_read == 0)
          break;
        else if (bytes_read < 0) {
          printf("Error reading from file.\n");
          return 1;
        }
        bytes_out = 0;
        while (bytes_read) {
          bytes_written = write(result, buf + bytes_out, bytes_read);
          if (bytes_written < 0) {
            printf("Error writing to socket.\n");
            return 1;
          }
          crc32_calc = crc32_le(crc32_calc, buf + bytes_out, bytes_written);
          bytes_read -= bytes_written;
          bytes_out += bytes_written;
        }
      }
      write(result, &crc32_calc, 4);
      close(fd);
      return 0;
    } else
      close(result);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x480,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x34(%rbp)
mov    0x2d71(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x48(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x480(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x50(%rbp)
movl   $0xc,-0x474(%rbp)
mov    0x2d70(%rip),%rax
mov    (%rax),%edi
mov    0x2d3f(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1130 <socket@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jge    12b2 <main+0xa2>
lea    0xd6b(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x478(%rbp)
jmp    162d <main+0x41d>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    -0x40(%rbp),%rsi
xor    %edx,%edx
call   10d0 <strtol@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x40(%rbp),%rax
cmpb   $0x0,(%rax)
je     12f9 <main+0xe9>
lea    0xd3c(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x478(%rbp)
jmp    162d <main+0x41d>
lea    -0x460(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   10a0 <memset@plt>
mov    0x2ccd(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x458(%rbp)
mov    0x2ca6(%rip),%rax
mov    (%rax),%edi
call   1090 <htonl@plt>
mov    %eax,-0x460(%rbp)
mov    -0x1c(%rbp),%edi
call   1060 <htons@plt>
mov    %eax,-0x45c(%rbp)
mov    -0x1c(%rbp),%esi
lea    0xce7(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x18(%rbp),%edi
lea    -0x460(%rbp),%rsi
mov    $0xc,%edx
call   10f0 <bind@plt>
cmp    $0x0,%rax
jge    1390 <main+0x180>
lea    0xce8(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x478(%rbp)
jmp    162d <main+0x41d>
mov    -0x18(%rbp),%edi
mov    $0x1,%esi
call   10e0 <listen@plt>
cmp    $0x0,%rax
jge    13cb <main+0x1bb>
lea    0xcc5(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x478(%rbp)
jmp    162d <main+0x41d>
jmp    13d0 <main+0x1c0>
jmp    13d5 <main+0x1c5>
mov    -0x18(%rbp),%edi
lea    -0x470(%rbp),%rsi
lea    -0x474(%rbp),%rdx
call   1110 <accept@plt>
mov    %eax,-0x20(%rbp)
cmp    $0x0,%eax
jge    141b <main+0x20b>
lea    0xc97(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x478(%rbp)
jmp    162d <main+0x41d>
mov    -0x480(%rbp),%rdx
mov    0x2bb7(%rip),%rax
mov    (%rax),%edi
lea    -0x470(%rbp),%rsi
mov    -0x474(%rbp),%ecx
call   1030 <inet_ntop@plt>
mov    -0x480(%rbp),%rsi
lea    0xc6b(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    $0x0,%al
call   1120 <fork@plt>
cmp    $0x0,%rax
jne    1620 <main+0x410>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
mov    0x2b5e(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1100 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    14ab <main+0x29b>
lea    0xc54(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x478(%rbp)
jmp    162d <main+0x41d>
mov    -0x14(%rbp),%edi
mov    0x2b33(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1080 <lseek@plt>
mov    -0x14(%rbp),%edi
mov    0x2b10(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1080 <lseek@plt>
mov    %eax,-0x30(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2ad2(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1080 <lseek@plt>
mov    -0x20(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x4,%edx
mov    $0x0,%al
call   1050 <write@plt>
jmp    14ff <main+0x2ef>
mov    -0x14(%rbp),%edi
lea    -0x450(%rbp),%rsi
mov    $0x400,%edx
call   10c0 <read@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jne    1525 <main+0x315>
jmp    15ef <main+0x3df>
cmpl   $0x0,-0x24(%rbp)
jge    1553 <main+0x343>
lea    0xbc1(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x478(%rbp)
jmp    162d <main+0x41d>
jmp    1558 <main+0x348>
movl   $0x0,-0x2c(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     15ea <main+0x3da>
mov    -0x20(%rbp),%edi
lea    -0x450(%rbp),%rsi
movslq -0x2c(%rbp),%rax
add    %rax,%rsi
mov    -0x24(%rbp),%edx
mov    $0x0,%al
call   1050 <write@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jge    15b5 <main+0x3a5>
lea    0xb79(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x478(%rbp)
jmp    162d <main+0x41d>
mov    -0x34(%rbp),%edi
lea    -0x450(%rbp),%rsi
movslq -0x2c(%rbp),%rax
add    %rax,%rsi
mov    -0x28(%rbp),%edx
call   1040 <crc32_le@plt>
mov    %eax,-0x34(%rbp)
mov    -0x28(%rbp),%ecx
mov    -0x24(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x24(%rbp)
mov    -0x28(%rbp),%eax
add    -0x2c(%rbp),%eax
mov    %eax,-0x2c(%rbp)
jmp    155f <main+0x34f>
jmp    14fa <main+0x2ea>
mov    -0x20(%rbp),%edi
lea    -0x34(%rbp),%rsi
mov    $0x4,%edx
mov    $0x0,%al
call   1050 <write@plt>
mov    -0x14(%rbp),%edi
call   10b0 <close@plt>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x478(%rbp)
jmp    162d <main+0x41d>
mov    -0x20(%rbp),%edi
call   10b0 <close@plt>
jmp    13d0 <main+0x1c0>
mov    -0x48(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x448,%rsp
mov    %rsi,%r13
mov    %rsp,%rbx
movl   $0x0,-0x3c(%rbp)
mov    0x2d60(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%r14
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r14
mov    %r14,%rsp
movl   $0xc,-0x34(%rbp)
mov    0x2d77(%rip),%rax
mov    (%rax),%edi
mov    0x2d46(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1140 <socket@plt>
test   %eax,%eax
js     12b9 <main+0x99>
mov    %eax,%r12d
mov    %rbx,-0x30(%rbp)
mov    0x8(%r13),%rdi
lea    -0x60(%rbp),%rsi
xor    %edx,%edx
call   10e0 <strtol@plt>
mov    %eax,%r15d
mov    -0x60(%rbp),%rax
cmpb   $0x0,(%rax)
je     12e0 <main+0xc0>
lea    0xe4f(%rip),%rdi
call   1050 <puts@plt>
mov    $0x1,%r13d
mov    -0x30(%rbp),%rbx
jmp    12cb <main+0xab>
lea    0xe4c(%rip),%rdi
call   1050 <puts@plt>
mov    $0x1,%r13d
mov    %rbx,%rsp
mov    %r13d,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %r13,-0x48(%rbp)
lea    -0x58(%rbp),%r13
mov    %r13,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   10b0 <memset@plt>
mov    0x2ce2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x50(%rbp)
mov    0x2cbe(%rip),%rax
mov    (%rax),%edi
call   10a0 <htonl@plt>
mov    %eax,-0x58(%rbp)
mov    %r15d,%edi
call   1070 <htons@plt>
mov    %eax,-0x54(%rbp)
lea    0xcda(%rip),%rdi
mov    %r15d,%esi
xor    %eax,%eax
call   1080 <printf@plt>
mov    %r12d,%edi
mov    %r13,%rsi
mov    $0xc,%edx
call   1100 <bind@plt>
test   %rax,%rax
js     13e8 <main+0x1c8>
mov    $0x1,%r13d
mov    %r12d,%edi
mov    $0x1,%esi
call   10f0 <listen@plt>
test   %rax,%rax
js     13f4 <main+0x1d4>
lea    -0x70(%rbp),%rsi
lea    -0x34(%rbp),%rdx
mov    %r12d,%edi
call   1120 <accept@plt>
test   %eax,%eax
js     13dc <main+0x1bc>
mov    %eax,%ebx
lea    -0x70(%rbp),%r13
lea    0xca7(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
mov    0x2c49(%rip),%rax
mov    (%rax),%edi
mov    -0x34(%rbp),%ecx
mov    %r13,%rsi
mov    %r14,%rdx
call   1030 <inet_ntop@plt>
mov    %r15,%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1080 <printf@plt>
xor    %eax,%eax
call   1130 <fork@plt>
test   %rax,%rax
je     1405 <main+0x1e5>
mov    %ebx,%edi
call   10c0 <close@plt>
mov    %r12d,%edi
mov    %r13,%rsi
lea    -0x34(%rbp),%rdx
call   1120 <accept@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    1390 <main+0x170>
lea    0xcbc(%rip),%rdi
jmp    12a8 <main+0x88>
lea    0xcf1(%rip),%rdi
jmp    12a8 <main+0x88>
lea    0xcc4(%rip),%rdi
call   1050 <puts@plt>
jmp    12b3 <main+0x93>
mov    -0x48(%rbp),%rax
mov    0x10(%rax),%rdi
mov    0x2bbc(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1110 <open@plt>
test   %eax,%eax
js     1534 <main+0x314>
mov    %eax,%r14d
mov    0x2bb9(%rip),%rax
mov    (%rax),%edx
mov    %r14d,%edi
xor    %esi,%esi
call   1090 <lseek@plt>
mov    0x2b96(%rip),%rax
mov    (%rax),%edx
mov    %r14d,%edi
xor    %esi,%esi
call   1090 <lseek@plt>
mov    %eax,-0x4c(%rbp)
mov    0x2b58(%rip),%rax
mov    (%rax),%edx
mov    %r14d,%edi
xor    %esi,%esi
call   1090 <lseek@plt>
lea    -0x4c(%rbp),%rsi
mov    %ebx,%edi
mov    $0x4,%edx
xor    %eax,%eax
call   1060 <write@plt>
lea    -0x470(%rbp),%rsi
mov    %r14d,%edi
mov    $0x400,%edx
call   10d0 <read@plt>
test   %eax,%eax
je     1512 <main+0x2f2>
xor    %r15d,%r15d
mov    %r14d,-0x48(%rbp)
mov    %ebx,-0x38(%rbp)
test   %eax,%eax
js     154c <main+0x32c>
xor    %r13d,%r13d
nopw   0x0(%rax,%rax,1)
movslq %r13d,%rcx
lea    (%rcx,%rbp,1),%r12
add    $0xfffffffffffffb90,%r12
mov    -0x38(%rbp),%edi
mov    %r12,%rsi
mov    %eax,%ebx
mov    %eax,%edx
xor    %eax,%eax
call   1060 <write@plt>
test   %eax,%eax
js     1540 <main+0x320>
mov    %eax,%r14d
mov    %r15d,%edi
mov    %r12,%rsi
mov    %eax,%edx
call   1040 <crc32_le@plt>
mov    %eax,%r15d
mov    %eax,-0x3c(%rbp)
add    %r14d,%r13d
sub    %r14d,%ebx
mov    %ebx,%eax
jne    14b0 <main+0x290>
mov    -0x48(%rbp),%r14d
mov    %r14d,%edi
lea    -0x470(%rbp),%rsi
mov    $0x400,%edx
call   10d0 <read@plt>
test   %eax,%eax
mov    -0x38(%rbp),%ebx
jne    149c <main+0x27c>
xor    %r13d,%r13d
lea    -0x3c(%rbp),%rsi
mov    %ebx,%edi
mov    $0x4,%edx
xor    %eax,%eax
call   1060 <write@plt>
mov    %r14d,%edi
call   10c0 <close@plt>
jmp    12b3 <main+0x93>
lea    0xb50(%rip),%rdi
jmp    12a8 <main+0x88>
lea    0xb12(%rip),%rdi
jmp    12a8 <main+0x88>
lea    0xb1f(%rip),%rdi
jmp    12a8 <main+0x88>

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@INET_ADDRSTRLEN = global i32 0
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [24 x i8] c"Error creating socket.\0A\00"
@.str.1 = [22 x i8] c"Invalid port number.\0A\00"
@INADDR_ANY = global i32 0
@.str.2 = [45 x i8] c"Bound to port %d, waiting for connection...\0A\00"
@.str.3 = [24 x i8] c"Error binding to port.\0A\00"
@.str.4 = [34 x i8] c"Error listening for connections.\0A\00"
@.str.5 = [33 x i8] c"Error accepting new connection.\0A\00"
@.str.6 = [44 x i8] c"Received connection from %s, serving file.\0A\00"
@O_RDONLY = global i32 0
@.str.7 = [21 x i8] c"Error opening file.\0A\00"
@SEEK_END = global i32 0
@SEEK_CUR = global i32 0
@SEEK_SET = global i32 0
@.str.8 = [26 x i8] c"Error reading from file.\0A\00"
@.str.9 = [26 x i8] c"Error writing to socket.\0A\00"
define i32 @main(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 i32
%15 = alloca i8*
%16 = alloca i8*
%17 = alloca i64
%18 = alloca [1024 x i8]
%19 = alloca %struct.sockaddr_in
%20 = alloca %struct.sockaddr_in
%21 = alloca i32
%22 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %14
%23 = load i32, i32* @INET_ADDRSTRLEN
%24 = zext i32 %23 to i64
%25 = call i8* @llvm.stacksave()
store i8* %25, i8** %16
%26 = alloca i8, i64 %24
store i64 %24, i64* %17
store i32 12, i32* %21
%27 = load i32, i32* @AF_INET
%28 = load i32, i32* @SOCK_STREAM
%29 = call i32 @socket(i32 %27, i32 %28, i32 0)
store i32 %29, i32* %7
%30 = icmp slt i32 %29, 0
br i1 %30, label %31, label %33
31: ; preds = %2
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
store i32 1, i32* %22
br label %160
33: ; preds = %2
%34 = load i8**, i8*** %5
%35 = getelementptr inbounds i8*, i8** %34, i64 1
%36 = load i8*, i8** %35
%37 = call i32 @strtol(i8* %36, i8** %15, i32 0)
store i32 %37, i32* %8
%38 = load i8*, i8** %15
%39 = load i8, i8* %38
%40 = icmp ne i8 %39, 0
br i1 %40, label %41, label %43
41: ; preds = %33
%42 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %3
store i32 1, i32* %22
br label %160
43: ; preds = %33
%44 = call i32 @memset(%struct.sockaddr_in* %19, i32 0, i32 12)
%45 = load i32, i32* @AF_INET
%46 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %19, i32 0, i32 2
store i32 %45, i32* %46
%47 = load i32, i32* @INADDR_ANY
%48 = call i32 @htonl(i32 %47)
%49 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %19, i32 0, i32 0
%50 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %49, i32 0, i32 0
store i32 %48, i32* %50
%51 = load i32, i32* %8
%52 = call i32 @htons(i32 %51)
%53 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %19, i32 0, i32 1
store i32 %52, i32* %53
%54 = load i32, i32* %8
%55 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.2, i64 0, i64 0), i32 %54)
%56 = load i32, i32* %7
%57 = bitcast %struct.sockaddr_in* %19 to %struct.sockaddr*
%58 = call i64 @bind(i32 %56, %struct.sockaddr* %57, i32 12)
%59 = icmp slt i64 %58, 0
br i1 %59, label %60, label %62
60: ; preds = %43
%61 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %3
store i32 1, i32* %22
br label %160
62: ; preds = %43
%63 = load i32, i32* %7
%64 = call i64 @listen(i32 %63, i32 1)
%65 = icmp slt i64 %64, 0
br i1 %65, label %66, label %68
66: ; preds = %62
%67 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.4, i64 0, i64 0))
store i32 1, i32* %3
store i32 1, i32* %22
br label %160
68: ; preds = %62
br label %69
69: ; preds = %159, %68
br label %70
70: ; preds = %69
%71 = load i32, i32* %7
%72 = bitcast %struct.sockaddr_in* %20 to %struct.sockaddr*
%73 = call i32 @accept(i32 %71, %struct.sockaddr* %72, i32* %21)
store i32 %73, i32* %9
%74 = icmp slt i32 %73, 0
br i1 %74, label %75, label %77
75: ; preds = %70
%76 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.5, i64 0, i64 0))
store i32 1, i32* %3
store i32 1, i32* %22
br label %160
77: ; preds = %70
%78 = load i32, i32* @AF_INET
%79 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %20, i32 0, i32 0
%80 = load i32, i32* %21
%81 = call i32 @inet_ntop(i32 %78, %struct.TYPE_2__* %79, i8* %26, i32 %80)
%82 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.6, i64 0, i64 0), i8* %26)
%83 = call i64 (...) @fork()
%84 = icmp eq i64 %83, 0
br i1 %84, label %85, label %156
85: ; preds = %77
%86 = load i8**, i8*** %5
%87 = getelementptr inbounds i8*, i8** %86, i64 2
%88 = load i8*, i8** %87
%89 = load i32, i32* @O_RDONLY
%90 = call i32 @open(i8* %88, i32 %89, i32 0)
store i32 %90, i32* %6
%91 = icmp slt i32 %90, 0
br i1 %91, label %92, label %94
92: ; preds = %85
%93 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.7, i64 0, i64 0))
store i32 1, i32* %3
store i32 1, i32* %22
br label %160
94: ; preds = %85
%95 = load i32, i32* %6
%96 = load i32, i32* @SEEK_END
%97 = call i32 @lseek(i32 %95, i32 0, i32 %96)
%98 = load i32, i32* %6
%99 = load i32, i32* @SEEK_CUR
%100 = call i32 @lseek(i32 %98, i32 0, i32 %99)
store i32 %100, i32* %13
%101 = load i32, i32* %6
%102 = load i32, i32* @SEEK_SET
%103 = call i32 @lseek(i32 %101, i32 0, i32 %102)
%104 = load i32, i32* %9
%105 = call i32 (i32, ...) @write(i32 %104, i32* %13, i32 4)
br label %106
106: ; preds = %150, %94
br label %107
107: ; preds = %106
%108 = load i32, i32* %6
%109 = getelementptr inbounds [1024 x i8], [1024 x i8]* %18, i64 0, i64 0
%110 = call i32 @read(i32 %108, i8* %109, i32 1024)
store i32 %110, i32* %10
%111 = load i32, i32* %10
%112 = icmp eq i32 %111, 0
br i1 %112, label %113, label %114
113: ; preds = %107
br label %151
114: ; preds = %107
%115 = load i32, i32* %10
%116 = icmp slt i32 %115, 0
br i1 %116, label %117, label %119
117: ; preds = %114
%118 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.8, i64 0, i64 0))
store i32 1, i32* %3
store i32 1, i32* %22
br label %160
119: ; preds = %114
br label %120
120: ; preds = %119
store i32 0, i32* %12
br label %121
121: ; preds = %136, %120
%122 = load i32, i32* %10
%123 = icmp ne i32 %122, 0
br i1 %123, label %124, label %150
124: ; preds = %121
%125 = load i32, i32* %9
%126 = getelementptr inbounds [1024 x i8], [1024 x i8]* %18, i64 0, i64 0
%127 = load i32, i32* %12
%128 = sext i32 %127 to i64
%129 = getelementptr inbounds i8, i8* %126, i64 %128
%130 = load i32, i32* %10
%131 = call i32 (i32, ...) @write(i32 %125, i8* %129, i32 %130)
store i32 %131, i32* %11
%132 = load i32, i32* %11
%133 = icmp slt i32 %132, 0
br i1 %133, label %134, label %136
134: ; preds = %124
%135 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.9, i64 0, i64 0))
store i32 1, i32* %3
store i32 1, i32* %22
br label %160
136: ; preds = %124
%137 = load i32, i32* %14
%138 = getelementptr inbounds [1024 x i8], [1024 x i8]* %18, i64 0, i64 0
%139 = load i32, i32* %12
%140 = sext i32 %139 to i64
%141 = getelementptr inbounds i8, i8* %138, i64 %140
%142 = load i32, i32* %11
%143 = call i32 @crc32_le(i32 %137, i8* %141, i32 %142)
store i32 %143, i32* %14
%144 = load i32, i32* %11
%145 = load i32, i32* %10
%146 = sub nsw i32 %145, %144
store i32 %146, i32* %10
%147 = load i32, i32* %11
%148 = load i32, i32* %12
%149 = add nsw i32 %148, %147
store i32 %149, i32* %12
br label %121
150: ; preds = %121
br label %106
151: ; preds = %113
%152 = load i32, i32* %9
%153 = call i32 (i32, ...) @write(i32 %152, i32* %14, i32 4)
%154 = load i32, i32* %6
%155 = call i32 @close(i32 %154)
store i32 0, i32* %3
store i32 1, i32* %22
br label %160
156: ; preds = %77
%157 = load i32, i32* %9
%158 = call i32 @close(i32 %157)
br label %159
159: ; preds = %156
br label %69
160: ; preds = %151, %134, %117, %92, %75, %66, %60, %41, %31
%161 = load i8*, i8** %16
%162 = load i32, i32* %3
ret i32 %162
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@INET_ADDRSTRLEN = global i32 0
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@INADDR_ANY = global i32 0
@.str.2 = [45 x i8] c"Bound to port %d, waiting for connection...\0A\00"
@.str.6 = [44 x i8] c"Received connection from %s, serving file.\0A\00"
@O_RDONLY = global i32 0
@SEEK_END = global i32 0
@SEEK_CUR = global i32 0
@SEEK_SET = global i32 0
@str = [25 x i8] c"Error writing to socket.\00"
@str.10 = [25 x i8] c"Error reading from file.\00"
@str.11 = [20 x i8] c"Error opening file.\00"
@str.12 = [32 x i8] c"Error accepting new connection.\00"
@str.13 = [33 x i8] c"Error listening for connections.\00"
@str.14 = [23 x i8] c"Error binding to port.\00"
@str.15 = [21 x i8] c"Invalid port number.\00"
@str.16 = [23 x i8] c"Error creating socket.\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca [1024 x i8]
%7 = alloca %struct.sockaddr_in
%8 = alloca %struct.sockaddr_in
%9 = alloca i32
%10 = bitcast i32* %3 to i8*
%11 = bitcast i32* %4 to i8*
store i32 0, i32* %4
%12 = bitcast i8** %5 to i8*
%13 = load i32, i32* @INET_ADDRSTRLEN
%14 = zext i32 %13 to i64
%15 = tail call i8* @llvm.stacksave()
%16 = alloca i8, i64 %14
%17 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%18 = bitcast %struct.sockaddr_in* %7 to i8*
%19 = bitcast %struct.sockaddr_in* %8 to i8*
%20 = bitcast i32* %9 to i8*
store i32 12, i32* %9
%21 = load i32, i32* @AF_INET
%22 = load i32, i32* @SOCK_STREAM
%23 = tail call i32 @socket(i32 %21, i32 %22, i32 0)
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %2
%26 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str.16, i64 0, i64 0))
br label %121
27: ; preds = %2
%28 = getelementptr inbounds i8*, i8** %1, i64 1
%29 = load i8*, i8** %28
%30 = call i32 @strtol(i8* %29, i8** %5, i32 0)
%31 = load i8*, i8** %5
%32 = load i8, i8* %31
%33 = icmp eq i8 %32, 0
br i1 %33, label %36, label %34
34: ; preds = %27
%35 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str.15, i64 0, i64 0))
br label %121
36: ; preds = %27
%37 = call i32 @memset(%struct.sockaddr_in* %7, i32 0, i32 12)
%38 = load i32, i32* @AF_INET
%39 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i64 0, i32 2
store i32 %38, i32* %39
%40 = load i32, i32* @INADDR_ANY
%41 = call i32 @htonl(i32 %40)
%42 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i64 0, i32 0, i32 0
store i32 %41, i32* %42
%43 = call i32 @htons(i32 %30)
%44 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i64 0, i32 1
store i32 %43, i32* %44
%45 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([45 x i8], [45 x i8]* @.str.2, i64 0, i64 0), i32 %30)
%46 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%47 = call i64 @bind(i32 %23, %struct.sockaddr* %46, i32 12)
%48 = icmp slt i64 %47, 0
br i1 %48, label %49, label %51
49: ; preds = %36
%50 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str.14, i64 0, i64 0))
br label %121
51: ; preds = %36
%52 = call i64 @listen(i32 %23, i32 1)
%53 = icmp slt i64 %52, 0
br i1 %53, label %60, label %54
54: ; preds = %51
%55 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%56 = call i32 @accept(i32 %23, %struct.sockaddr* %55, i32* %9)
%57 = icmp slt i32 %56, 0
br i1 %57, label %62, label %58
58: ; preds = %54
%59 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i64 0, i32 0
br label %64
60: ; preds = %51
%61 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @str.13, i64 0, i64 0))
br label %121
62: ; preds = %117, %54
%63 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @str.12, i64 0, i64 0))
br label %121
64: ; preds = %58, %117
%65 = phi i32 [ %56, %58 ], [ %119, %117 ]
%66 = load i32, i32* @AF_INET
%67 = load i32, i32* %9
%68 = call i32 @inet_ntop(i32 %66, %struct.TYPE_2__* %59, i8* %16, i32 %67)
%69 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([44 x i8], [44 x i8]* @.str.6, i64 0, i64 0), i8* %16)
%70 = call i64 (...) @fork()
%71 = icmp eq i64 %70, 0
br i1 %71, label %72, label %117
72: ; preds = %64
%73 = getelementptr inbounds i8*, i8** %1, i64 2
%74 = load i8*, i8** %73
%75 = load i32, i32* @O_RDONLY
%76 = call i32 @open(i8* %74, i32 %75, i32 0)
%77 = icmp slt i32 %76, 0
br i1 %77, label %78, label %80
78: ; preds = %72
%79 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str.11, i64 0, i64 0))
br label %121
80: ; preds = %72
%81 = load i32, i32* @SEEK_END
%82 = call i32 @lseek(i32 %76, i32 0, i32 %81)
%83 = load i32, i32* @SEEK_CUR
%84 = call i32 @lseek(i32 %76, i32 0, i32 %83)
store i32 %84, i32* %3
%85 = load i32, i32* @SEEK_SET
%86 = call i32 @lseek(i32 %76, i32 0, i32 %85)
%87 = call i32 (i32, ...) @write(i32 %65, i32* %3, i32 4)
%88 = call i32 @read(i32 %76, i8* %17, i32 1024)
%89 = icmp eq i32 %88, 0
br i1 %89, label %114, label %93
90: ; preds = %109
%91 = call i32 @read(i32 %76, i8* %17, i32 1024)
%92 = icmp eq i32 %91, 0
br i1 %92, label %114, label %93
93: ; preds = %80, %90
%94 = phi i32 [ %110, %90 ], [ 0, %80 ]
%95 = phi i32 [ %91, %90 ], [ %88, %80 ]
%96 = icmp slt i32 %95, 0
br i1 %96, label %97, label %99
97: ; preds = %93
%98 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str.10, i64 0, i64 0))
br label %121
99: ; preds = %93, %109
%100 = phi i32 [ %110, %109 ], [ %94, %93 ]
%101 = phi i32 [ %111, %109 ], [ %95, %93 ]
%102 = phi i32 [ %112, %109 ], [ 0, %93 ]
%103 = sext i32 %102 to i64
%104 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 %103
%105 = call i32 (i32, ...) @write(i32 %65, i8* %104, i32 %101)
%106 = icmp slt i32 %105, 0
br i1 %106, label %107, label %109
107: ; preds = %99
%108 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str, i64 0, i64 0))
br label %121
109: ; preds = %99
%110 = call i32 @crc32_le(i32 %100, i8* %104, i32 %105)
store i32 %110, i32* %4
%111 = sub nsw i32 %101, %105
%112 = add nsw i32 %105, %102
%113 = icmp eq i32 %111, 0
br i1 %113, label %90, label %99
114: ; preds = %90, %80
%115 = call i32 (i32, ...) @write(i32 %65, i32* %4, i32 4)
%116 = call i32 @close(i32 %76)
br label %121
117: ; preds = %64
%118 = call i32 @close(i32 %65)
%119 = call i32 @accept(i32 %23, %struct.sockaddr* %55, i32* %9)
%120 = icmp slt i32 %119, 0
br i1 %120, label %62, label %64
121: ; preds = %114, %107, %97, %78, %62, %60, %49, %34, %25
%122 = phi i32 [ 1, %25 ], [ 1, %34 ], [ 1, %49 ], [ 1, %60 ], [ 1, %62 ], [ 1, %78 ], [ 0, %114 ], [ 1, %97 ], [ 1, %107 ]
ret i32 %122
}