#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*/ socklen_t;
typedef int /*<<< orphan*/ serverAddr;
typedef int /*<<< orphan*/ clientAddr;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int BUFSIZ;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_STREAM;
int accept(int, struct sockaddr *, int /*<<< orphan*/ *);
int atoi(char *);
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ bzero(char *, int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ listen(int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ *stderr;
int write(int, char *, int);
int main(int argc, char *argv[]) {
  int sockfd, newsockfd, serverPort, clientLength, nBytes;
  ;
  char buffer[BUFSIZ];
  char option = ' ';
  int sent = 0;
  struct sockaddr_in serverAddr, clientAddr;
  if (argc != 2) {
    fprintf(stderr, "Usage: tcpserver [server_port]\n");
    return -1;
  }
  printf("Starting TCP server...\n");
  serverPort = atoi(argv[1]);
  if (serverPort <= 0 || serverPort > 65535) {
    fprintf(stderr, "The port number given is wrong.\n");
    return -2;
  }
  sockfd = socket(AF_INET, SOCK_STREAM, 0);
  if (sockfd < 0) {
    perror("Error opening TCP socket");
    return -3;
  }
  bzero((char *)&serverAddr, sizeof(serverAddr));
  serverAddr.sin_family = AF_INET;
  serverAddr.sin_addr.s_addr = INADDR_ANY;
  serverAddr.sin_port = htons(serverPort);
  if (bind(sockfd, (struct sockaddr *)&serverAddr, sizeof(serverAddr)) < 0) {
    perror("Error on binding TCP socket");
    return -4;
  }
  listen(sockfd, 5);
  clientLength = sizeof(clientAddr);
  newsockfd = accept(sockfd, (struct sockaddr *)&clientAddr,
                     (socklen_t *)&clientLength);
  if (newsockfd < 0) {
    perror("Error on accepting actual TCP client");
    return -5;
  }
  bzero(buffer, BUFSIZ);
  nBytes = read(newsockfd, buffer, BUFSIZ);
  if (nBytes < 0) {
    perror("Error reading from TCP socket");
    return -6;
  }
  printf("This is the file to transffer: %s\n", buffer);
  FILE *file;
  char sample[10];
  if (fopen(buffer, "r") != NULL) {
    nBytes = write(newsockfd, "1", 1);
    if (nBytes < 0) {
      perror("Error writing to TCP socket");
      return -7;
    }
  } else {
    nBytes = write(newsockfd, "0", 1);
    if (nBytes < 0) {
      perror("Error writing to TCP socket");
      return -7;
    }
  }
  nBytes = read(newsockfd, sample, 1);
  if (nBytes < 0) {
    perror("Error reading from TCP socket");
    return -6;
  }
  option = sample[0];
  char readBuffer[512];
  switch (option) {
  case '1':
    if ((file = fopen(buffer, "w")) == NULL) {
      fprintf(stderr, "Cannot write on file.");
      return -8;
    }
    break;
  case '0':
    if ((file = fopen(buffer, "a+")) == NULL) {
      fprintf(stderr, "Cannot append to file.");
      return -9;
    }
  }
  while ((nBytes = read(newsockfd, readBuffer, 512)) > 0) {
    if (nBytes < 0) {
      fprintf(stderr, "Error while receiving the file.\n");
      return -10;
    }
    fprintf(file, readBuffer);
    fprintf(stderr, readBuffer);
    if (readBuffer == "")
      break;
  }
  fclose(file);
  sent = 1;
  if (sent) {
    nBytes = write(newsockfd, "Transfer completed and successful", 33);
  } else {
    printf("Error writting the file in TCP server");
  }
  if (nBytes < 0) {
    perror("Error writing to TCP socket");
    return -7;
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x290,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2dc0(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x30(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x288(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x38(%rbp)
movb   $0x20,-0x39(%rbp)
movl   $0x0,-0x40(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     1266 <main+0x86>
mov    0x2d8c(%rip),%rax
mov    (%rax),%rdi
lea    0xdb2(%rip),%rsi
call   1080 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x64(%rbp)
jmp    1700 <main+0x520>
lea    0xdb3(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10f0 <atoi@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jle    129b <main+0xbb>
cmpl   $0xffff,-0x1c(%rbp)
jle    12c4 <main+0xe4>
mov    0x2d2e(%rip),%rax
mov    (%rax),%rdi
lea    0xd8c(%rip),%rsi
call   1080 <fprintf@plt>
movl   $0xfffffffe,-0x4(%rbp)
movl   $0x1,-0x64(%rbp)
jmp    1700 <main+0x520>
mov    0x2d1d(%rip),%rax
mov    (%rax),%edi
mov    0x2cf4(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1100 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1309 <main+0x129>
lea    0xd68(%rip),%rdi
call   10c0 <perror@plt>
movl   $0xfffffffd,-0x4(%rbp)
movl   $0x1,-0x64(%rbp)
jmp    1700 <main+0x520>
lea    -0x50(%rbp),%rdi
mov    $0xc,%esi
call   10d0 <bzero@plt>
mov    0x2cca(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x48(%rbp)
mov    0x2cb6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x1c(%rbp),%edi
call   1050 <htons@plt>
mov    %eax,-0x50(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x50(%rbp),%rsi
mov    $0xc,%edx
call   10a0 <bind@plt>
cmp    $0x0,%rax
jge    1374 <main+0x194>
lea    0xd16(%rip),%rdi
call   10c0 <perror@plt>
movl   $0xfffffffc,-0x4(%rbp)
movl   $0x1,-0x64(%rbp)
jmp    1700 <main+0x520>
mov    -0x14(%rbp),%edi
mov    $0x5,%esi
call   1090 <listen@plt>
movl   $0xc,-0x20(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x60(%rbp),%rsi
lea    -0x20(%rbp),%rdx
call   10e0 <accept@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    13c4 <main+0x1e4>
lea    0xce2(%rip),%rdi
call   10c0 <perror@plt>
movl   $0xfffffffb,-0x4(%rbp)
movl   $0x1,-0x64(%rbp)
jmp    1700 <main+0x520>
mov    -0x288(%rbp),%rdi
mov    0x2bee(%rip),%rax
mov    (%rax),%esi
call   10d0 <bzero@plt>
mov    -0x288(%rbp),%rsi
mov    -0x18(%rbp),%edi
mov    0x2bd6(%rip),%rax
mov    (%rax),%edx
call   1070 <read@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    141d <main+0x23d>
lea    0xcae(%rip),%rdi
call   10c0 <perror@plt>
movl   $0xfffffffa,-0x4(%rbp)
movl   $0x1,-0x64(%rbp)
jmp    1700 <main+0x520>
mov    -0x288(%rbp),%rsi
lea    0xca6(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x288(%rbp),%rdi
lea    0xd6d(%rip),%rsi
call   10b0 <fopen@plt>
cmp    $0x0,%rax
je     1494 <main+0x2b4>
mov    -0x18(%rbp),%edi
lea    0xc9b(%rip),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    148f <main+0x2af>
lea    0xc7f(%rip),%rdi
call   10c0 <perror@plt>
movl   $0xfffffff9,-0x4(%rbp)
movl   $0x1,-0x64(%rbp)
jmp    1700 <main+0x520>
jmp    14d9 <main+0x2f9>
mov    -0x18(%rbp),%edi
lea    0xc74(%rip),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    14d4 <main+0x2f4>
lea    0xc3a(%rip),%rdi
call   10c0 <perror@plt>
movl   $0xfffffff9,-0x4(%rbp)
movl   $0x1,-0x64(%rbp)
jmp    1700 <main+0x520>
jmp    14d9 <main+0x2f9>
mov    -0x18(%rbp),%edi
lea    -0x7a(%rbp),%rsi
mov    $0x1,%edx
call   1070 <read@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    1516 <main+0x336>
lea    0xbb5(%rip),%rdi
call   10c0 <perror@plt>
movl   $0xfffffffa,-0x4(%rbp)
movl   $0x1,-0x64(%rbp)
jmp    1700 <main+0x520>
mov    -0x7a(%rbp),%al
mov    %al,-0x39(%rbp)
movsbl -0x39(%rbp),%eax
mov    %eax,-0x28c(%rbp)
sub    $0x30,%eax
je     1597 <main+0x3b7>
jmp    1534 <main+0x354>
mov    -0x28c(%rbp),%eax
sub    $0x31,%eax
jne    15e6 <main+0x406>
jmp    1548 <main+0x368>
mov    -0x288(%rbp),%rdi
lea    0xbbe(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x70(%rbp)
cmp    $0x0,%rax
jne    1592 <main+0x3b2>
mov    0x2a60(%rip),%rax
mov    (%rax),%rdi
lea    0xb9c(%rip),%rsi
call   1080 <fprintf@plt>
movl   $0xfffffff8,-0x4(%rbp)
movl   $0x1,-0x64(%rbp)
jmp    1700 <main+0x520>
jmp    15e6 <main+0x406>
mov    -0x288(%rbp),%rdi
lea    0xb87(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x70(%rbp)
cmp    $0x0,%rax
jne    15e1 <main+0x401>
mov    0x2a11(%rip),%rax
mov    (%rax),%rdi
lea    0xb66(%rip),%rsi
call   1080 <fprintf@plt>
movl   $0xfffffff7,-0x4(%rbp)
movl   $0x1,-0x64(%rbp)
jmp    1700 <main+0x520>
jmp    15e6 <main+0x406>
jmp    15eb <main+0x40b>
mov    -0x18(%rbp),%edi
lea    -0x280(%rbp),%rsi
mov    $0x200,%edx
call   1070 <read@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jle    1685 <main+0x4a5>
cmpl   $0x0,-0x24(%rbp)
jge    163e <main+0x45e>
mov    0x29b4(%rip),%rax
mov    (%rax),%rdi
lea    0xb20(%rip),%rsi
call   1080 <fprintf@plt>
movl   $0xfffffff6,-0x4(%rbp)
movl   $0x1,-0x64(%rbp)
jmp    1700 <main+0x520>
mov    -0x70(%rbp),%rdi
lea    -0x280(%rbp),%rsi
call   1080 <fprintf@plt>
mov    0x297b(%rip),%rax
mov    (%rax),%rdi
lea    -0x280(%rbp),%rsi
call   1080 <fprintf@plt>
lea    -0x280(%rbp),%rax
lea    0x9c5(%rip),%rcx
cmp    %rcx,%rax
jne    1680 <main+0x4a0>
jmp    1685 <main+0x4a5>
jmp    15eb <main+0x40b>
mov    -0x70(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x1,-0x40(%rbp)
cmpl   $0x0,-0x40(%rbp)
je     16bb <main+0x4db>
mov    -0x18(%rbp),%edi
lea    0xabe(%rip),%rsi
mov    $0x21,%edx
call   1030 <write@plt>
mov    %eax,-0x24(%rbp)
jmp    16c9 <main+0x4e9>
lea    0xac7(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
cmpl   $0x0,-0x24(%rbp)
jge    16f2 <main+0x512>
lea    0xa1c(%rip),%rdi
call   10c0 <perror@plt>
movl   $0xfffffff9,-0x4(%rbp)
movl   $0x1,-0x64(%rbp)
jmp    1700 <main+0x520>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x64(%rbp)
mov    -0x30(%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    $0x238,%rsp
mov    %rsp,%r14
mov    0x2da2(%rip),%r13
mov    0x0(%r13),%eax
mov    %rsp,%r15
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r15
mov    %r15,%rsp
cmp    $0x2,%edi
jne    1286 <main+0x86>
mov    %rsi,%rbx
lea    0xf10(%rip),%rdi
call   1030 <puts@plt>
mov    0x8(%rbx),%rdi
call   1100 <atoi@plt>
mov    %eax,%ebx
lea    -0x10000(%rbx),%eax
cmp    $0xffff0000,%eax
ja     12bd <main+0xbd>
mov    0x2d6a(%rip),%rax
mov    (%rax),%rcx
lea    0xdb0(%rip),%rdi
mov    $0x20,%esi
mov    $0x1,%edx
call   1110 <fwrite@plt>
mov    $0xfffffffe,%eax
jmp    12ab <main+0xab>
mov    0x2d43(%rip),%rax
mov    (%rax),%rcx
lea    0xd69(%rip),%rdi
mov    $0x1f,%esi
mov    $0x1,%edx
call   1110 <fwrite@plt>
mov    $0xffffffff,%eax
mov    %r14,%rsp
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %r14,-0x30(%rbp)
mov    0x2d20(%rip),%rax
mov    (%rax),%edi
mov    0x2cf7(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1120 <socket@plt>
test   %eax,%eax
js     1534 <main+0x334>
mov    %eax,%r14d
lea    -0x40(%rbp),%r12
mov    %r12,%rdi
mov    $0xc,%esi
call   10e0 <bzero@plt>
mov    0x2ceb(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
mov    0x2cd7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    %ebx,%edi
call   1060 <htons@plt>
mov    %eax,-0x40(%rbp)
mov    %r14d,%edi
mov    %r12,%rsi
mov    $0xc,%edx
call   10b0 <bind@plt>
test   %rax,%rax
js     154e <main+0x34e>
mov    %r14d,%edi
mov    $0x5,%esi
call   10a0 <listen@plt>
movl   $0xc,-0x34(%rbp)
lea    -0x58(%rbp),%rsi
lea    -0x34(%rbp),%rdx
mov    %r14d,%edi
call   10f0 <accept@plt>
test   %eax,%eax
js     1568 <main+0x368>
mov    %eax,%r14d
mov    0x0(%r13),%ebx
mov    %r15,%rdi
mov    %ebx,%esi
call   10e0 <bzero@plt>
mov    %r14d,%edi
mov    %r15,%rsi
mov    %ebx,%edx
call   1080 <read@plt>
test   %eax,%eax
js     14ec <main+0x2ec>
lea    0xd2f(%rip),%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
lea    0xd41(%rip),%rsi
mov    %r15,%rdi
call   10c0 <fopen@plt>
test   %rax,%rax
je     13b1 <main+0x1b1>
lea    0xd2f(%rip),%rsi
jmp    13b8 <main+0x1b8>
lea    0xd44(%rip),%rsi
mov    %r14d,%edi
mov    $0x1,%edx
call   1040 <write@plt>
test   %eax,%eax
js     1582 <main+0x382>
lea    -0x4a(%rbp),%rsi
mov    %r14d,%edi
mov    $0x1,%edx
call   1080 <read@plt>
test   %eax,%eax
js     14ec <main+0x2ec>
movsbl -0x4a(%rbp),%eax
cmp    $0x30,%eax
je     1439 <main+0x239>
cmp    $0x31,%eax
jne    1454 <main+0x254>
lea    0xd03(%rip),%rsi
mov    %r15,%rdi
call   10c0 <fopen@plt>
mov    %rax,%r12
test   %rax,%rax
jne    1454 <main+0x254>
mov    0x2bbe(%rip),%rax
mov    (%rax),%rcx
lea    0xce4(%rip),%rdi
mov    $0x15,%esi
mov    $0x1,%edx
call   1110 <fwrite@plt>
mov    $0xfffffff8,%eax
mov    -0x30(%rbp),%r14
jmp    12ab <main+0xab>
lea    0xcd6(%rip),%rsi
mov    %r15,%rdi
call   10c0 <fopen@plt>
mov    %rax,%r12
test   %rax,%rax
je     1506 <main+0x306>
lea    -0x260(%rbp),%rsi
mov    %r14d,%edi
mov    $0x200,%edx
call   1080 <read@plt>
test   %eax,%eax
jle    14aa <main+0x2aa>
lea    -0x260(%rbp),%rbx
mov    0x2b56(%rip),%r15
nopw   0x0(%rax,%rax,1)
mov    %r12,%rdi
mov    %rbx,%rsi
call   1090 <fprintf@plt>
mov    (%r15),%rdi
mov    %rbx,%rsi
call   1090 <fprintf@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0x200,%edx
call   1080 <read@plt>
test   %eax,%eax
jg     1480 <main+0x280>
mov    %r12,%rdi
call   1050 <fclose@plt>
lea    0xc77(%rip),%rsi
mov    %r14d,%edi
mov    $0x21,%edx
call   1040 <write@plt>
mov    %eax,%ecx
xor    %eax,%eax
test   %ecx,%ecx
mov    -0x30(%rbp),%r14
jns    12ab <main+0xab>
lea    0xc03(%rip),%rdi
call   10d0 <perror@plt>
mov    $0xfffffff9,%eax
jmp    12ab <main+0xab>
lea    0xba8(%rip),%rdi
call   10d0 <perror@plt>
mov    $0xfffffffa,%eax
mov    -0x30(%rbp),%r14
jmp    12ab <main+0xab>
mov    0x2ac3(%rip),%rax
mov    (%rax),%rcx
lea    0xc02(%rip),%rdi
mov    $0x16,%esi
mov    $0x1,%edx
call   1110 <fwrite@plt>
mov    $0xfffffff7,%eax
mov    -0x30(%rbp),%r14
jmp    12ab <main+0xab>
lea    0xb06(%rip),%rdi
call   10d0 <perror@plt>
mov    $0xfffffffd,%eax
mov    -0x30(%rbp),%r14
jmp    12ab <main+0xab>
lea    0xb05(%rip),%rdi
call   10d0 <perror@plt>
mov    $0xfffffffc,%eax
mov    -0x30(%rbp),%r14
jmp    12ab <main+0xab>
lea    0xb07(%rip),%rdi
call   10d0 <perror@plt>
mov    $0xfffffffb,%eax
mov    -0x30(%rbp),%r14
jmp    12ab <main+0xab>
lea    0xb57(%rip),%rdi
call   10d0 <perror@plt>
mov    $0xfffffff9,%eax
mov    -0x30(%rbp),%r14
jmp    12ab <main+0xab>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@BUFSIZ = global i32 0
@stderr = global i32* null
@.str = [32 x i8] c"Usage: tcpserver [server_port]\0A\00"
@.str.1 = [24 x i8] c"Starting TCP server...\0A\00"
@.str.2 = [33 x i8] c"The port number given is wrong.\0A\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.3 = [25 x i8] c"Error opening TCP socket\00"
@INADDR_ANY = global i32 0
@.str.4 = [28 x i8] c"Error on binding TCP socket\00"
@.str.5 = [37 x i8] c"Error on accepting actual TCP client\00"
@.str.6 = [30 x i8] c"Error reading from TCP socket\00"
@.str.7 = [35 x i8] c"This is the file to transffer: %s\0A\00"
@.str.8 = [2 x i8] c"r\00"
@.str.9 = [2 x i8] c"1\00"
@.str.10 = [28 x i8] c"Error writing to TCP socket\00"
@.str.11 = [2 x i8] c"0\00"
@.str.12 = [2 x i8] c"w\00"
@.str.13 = [22 x i8] c"Cannot write on file.\00"
@.str.14 = [3 x i8] c"a+\00"
@.str.15 = [23 x i8] c"Cannot append to file.\00"
@.str.16 = [33 x i8] c"Error while receiving the file.\0A\00"
@.str.17 = [1 x i8] zeroinitializer
@.str.18 = [34 x i8] c"Transfer completed and successful\00"
@.str.19 = [38 x i8] c"Error writting the file in TCP server\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 i8*
%12 = alloca i64
%13 = alloca i8
%14 = alloca i32
%15 = alloca %struct.sockaddr_in
%16 = alloca %struct.sockaddr_in
%17 = alloca i32
%18 = alloca i32*
%19 = alloca [10 x i8]
%20 = alloca [512 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%21 = load i32, i32* @BUFSIZ
%22 = zext i32 %21 to i64
%23 = call i8* @llvm.stacksave()
store i8* %23, i8** %11
%24 = alloca i8, i64 %22
store i64 %22, i64* %12
store i8 32, i8* %13
store i32 0, i32* %14
%25 = load i32, i32* %4
%26 = icmp ne i32 %25, 2
br i1 %26, label %27, label %30
27: ; preds = %2
%28 = load i32*, i32** @stderr
%29 = call i32 @fprintf(i32* %28, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
store i32 1, i32* %17
br label %175
30: ; preds = %2
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
%32 = load i8**, i8*** %5
%33 = getelementptr inbounds i8*, i8** %32, i64 1
%34 = load i8*, i8** %33
%35 = call i32 @atoi(i8* %34)
store i32 %35, i32* %8
%36 = load i32, i32* %8
%37 = icmp sle i32 %36, 0
br i1 %37, label %41, label %38
38: ; preds = %30
%39 = load i32, i32* %8
%40 = icmp sgt i32 %39, 65535
br i1 %40, label %41, label %44
41: ; preds = %38, %30
%42 = load i32*, i32** @stderr
%43 = call i32 @fprintf(i32* %42, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0))
store i32 -2, i32* %3
store i32 1, i32* %17
br label %175
44: ; preds = %38
%45 = load i32, i32* @AF_INET
%46 = load i32, i32* @SOCK_STREAM
%47 = call i32 @socket(i32 %45, i32 %46, i32 0)
store i32 %47, i32* %6
%48 = load i32, i32* %6
%49 = icmp slt i32 %48, 0
br i1 %49, label %50, label %52
50: ; preds = %44
%51 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0))
store i32 -3, i32* %3
store i32 1, i32* %17
br label %175
52: ; preds = %44
%53 = bitcast %struct.sockaddr_in* %15 to i8*
%54 = call i32 @bzero(i8* %53, i32 12)
%55 = load i32, i32* @AF_INET
%56 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %15, i32 0, i32 2
store i32 %55, i32* %56
%57 = load i32, i32* @INADDR_ANY
%58 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %15, i32 0, i32 1
%59 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %58, i32 0, i32 0
store i32 %57, i32* %59
%60 = load i32, i32* %8
%61 = call i32 @htons(i32 %60)
%62 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %15, i32 0, i32 0
store i32 %61, i32* %62
%63 = load i32, i32* %6
%64 = bitcast %struct.sockaddr_in* %15 to %struct.sockaddr*
%65 = call i64 @bind(i32 %63, %struct.sockaddr* %64, i32 12)
%66 = icmp slt i64 %65, 0
br i1 %66, label %67, label %69
67: ; preds = %52
%68 = call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0))
store i32 -4, i32* %3
store i32 1, i32* %17
br label %175
69: ; preds = %52
%70 = load i32, i32* %6
%71 = call i32 @listen(i32 %70, i32 5)
store i32 12, i32* %9
%72 = load i32, i32* %6
%73 = bitcast %struct.sockaddr_in* %16 to %struct.sockaddr*
%74 = call i32 @accept(i32 %72, %struct.sockaddr* %73, i32* %9)
store i32 %74, i32* %7
%75 = load i32, i32* %7
%76 = icmp slt i32 %75, 0
br i1 %76, label %77, label %79
77: ; preds = %69
%78 = call i32 @perror(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.5, i64 0, i64 0))
store i32 -5, i32* %3
store i32 1, i32* %17
br label %175
79: ; preds = %69
%80 = load i32, i32* @BUFSIZ
%81 = call i32 @bzero(i8* %24, i32 %80)
%82 = load i32, i32* %7
%83 = load i32, i32* @BUFSIZ
%84 = call i32 @read(i32 %82, i8* %24, i32 %83)
store i32 %84, i32* %10
%85 = load i32, i32* %10
%86 = icmp slt i32 %85, 0
br i1 %86, label %87, label %89
87: ; preds = %79
%88 = call i32 @perror(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.6, i64 0, i64 0))
store i32 -6, i32* %3
store i32 1, i32* %17
br label %175
89: ; preds = %79
%90 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.7, i64 0, i64 0), i8* %24)
%91 = call i32* @fopen(i8* %24, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.8, i64 0, i64 0))
%92 = icmp ne i32* %91, null
br i1 %92, label %93, label %101
93: ; preds = %89
%94 = load i32, i32* %7
%95 = call i32 @write(i32 %94, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.9, i64 0, i64 0), i32 1)
store i32 %95, i32* %10
%96 = load i32, i32* %10
%97 = icmp slt i32 %96, 0
br i1 %97, label %98, label %100
98: ; preds = %93
%99 = call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.10, i64 0, i64 0))
store i32 -7, i32* %3
store i32 1, i32* %17
br label %175
100: ; preds = %93
br label %109
101: ; preds = %89
%102 = load i32, i32* %7
%103 = call i32 @write(i32 %102, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.11, i64 0, i64 0), i32 1)
store i32 %103, i32* %10
%104 = load i32, i32* %10
%105 = icmp slt i32 %104, 0
br i1 %105, label %106, label %108
106: ; preds = %101
%107 = call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.10, i64 0, i64 0))
store i32 -7, i32* %3
store i32 1, i32* %17
br label %175
108: ; preds = %101
br label %109
109: ; preds = %108, %100
%110 = load i32, i32* %7
%111 = getelementptr inbounds [10 x i8], [10 x i8]* %19, i64 0, i64 0
%112 = call i32 @read(i32 %110, i8* %111, i32 1)
store i32 %112, i32* %10
%113 = load i32, i32* %10
%114 = icmp slt i32 %113, 0
br i1 %114, label %115, label %117
115: ; preds = %109
%116 = call i32 @perror(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.6, i64 0, i64 0))
store i32 -6, i32* %3
store i32 1, i32* %17
br label %175
117: ; preds = %109
%118 = getelementptr inbounds [10 x i8], [10 x i8]* %19, i64 0, i64 0
%119 = load i8, i8* %118
store i8 %119, i8* %13
%120 = load i8, i8* %13
%121 = sext i8 %120 to i32
switch i32 %121, label %136 [
i32 49, label %122
i32 48, label %129
]
122: ; preds = %117
%123 = call i32* @fopen(i8* %24, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.12, i64 0, i64 0))
store i32* %123, i32** %18
%124 = icmp eq i32* %123, null
br i1 %124, label %125, label %128
125: ; preds = %122
%126 = load i32*, i32** @stderr
%127 = call i32 @fprintf(i32* %126, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.13, i64 0, i64 0))
store i32 -8, i32* %3
store i32 1, i32* %17
br label %175
128: ; preds = %122
br label %136
129: ; preds = %117
%130 = call i32* @fopen(i8* %24, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.14, i64 0, i64 0))
store i32* %130, i32** %18
%131 = icmp eq i32* %130, null
br i1 %131, label %132, label %135
132: ; preds = %129
%133 = load i32*, i32** @stderr
%134 = call i32 @fprintf(i32* %133, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.15, i64 0, i64 0))
store i32 -9, i32* %3
store i32 1, i32* %17
br label %175
135: ; preds = %129
br label %136
136: ; preds = %135, %117, %128
br label %137
137: ; preds = %158, %136
%138 = load i32, i32* %7
%139 = getelementptr inbounds [512 x i8], [512 x i8]* %20, i64 0, i64 0
%140 = call i32 @read(i32 %138, i8* %139, i32 512)
store i32 %140, i32* %10
%141 = icmp sgt i32 %140, 0
br i1 %141, label %142, label %159
142: ; preds = %137
%143 = load i32, i32* %10
%144 = icmp slt i32 %143, 0
br i1 %144, label %145, label %148
145: ; preds = %142
%146 = load i32*, i32** @stderr
%147 = call i32 @fprintf(i32* %146, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.16, i64 0, i64 0))
store i32 -10, i32* %3
store i32 1, i32* %17
br label %175
148: ; preds = %142
%149 = load i32*, i32** %18
%150 = getelementptr inbounds [512 x i8], [512 x i8]* %20, i64 0, i64 0
%151 = call i32 @fprintf(i32* %149, i8* %150)
%152 = load i32*, i32** @stderr
%153 = getelementptr inbounds [512 x i8], [512 x i8]* %20, i64 0, i64 0
%154 = call i32 @fprintf(i32* %152, i8* %153)
%155 = getelementptr inbounds [512 x i8], [512 x i8]* %20, i64 0, i64 0
%156 = icmp eq i8* %155, getelementptr inbounds ([1 x i8], [1 x i8]* @.str.17, i64 0, i64 0)
br i1 %156, label %157, label %158
157: ; preds = %148
br label %159
158: ; preds = %148
br label %137
159: ; preds = %157, %137
%160 = load i32*, i32** %18
%161 = call i32 @fclose(i32* %160)
store i32 1, i32* %14
%162 = load i32, i32* %14
%163 = icmp ne i32 %162, 0
br i1 %163, label %164, label %167
164: ; preds = %159
%165 = load i32, i32* %7
%166 = call i32 @write(i32 %165, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.18, i64 0, i64 0), i32 33)
store i32 %166, i32* %10
br label %169
167: ; preds = %159
%168 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.19, i64 0, i64 0))
br label %169
169: ; preds = %167, %164
%170 = load i32, i32* %10
%171 = icmp slt i32 %170, 0
br i1 %171, label %172, label %174
172: ; preds = %169
%173 = call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.10, i64 0, i64 0))
store i32 -7, i32* %3
store i32 1, i32* %17
br label %175
174: ; preds = %169
store i32 0, i32* %3
store i32 1, i32* %17
br label %175
175: ; preds = %174, %172, %145, %132, %125, %115, %106, %98, %87, %77, %67, %50, %41, %27
%176 = load i8*, i8** %11
%177 = load i32, i32* %3
ret i32 %177
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@BUFSIZ = global i32 0
@stderr = global i32* null
@.str = [32 x i8] c"Usage: tcpserver [server_port]\0A\00"
@.str.2 = [33 x i8] c"The port number given is wrong.\0A\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.3 = [25 x i8] c"Error opening TCP socket\00"
@INADDR_ANY = global i32 0
@.str.4 = [28 x i8] c"Error on binding TCP socket\00"
@.str.5 = [37 x i8] c"Error on accepting actual TCP client\00"
@.str.6 = [30 x i8] c"Error reading from TCP socket\00"
@.str.7 = [35 x i8] c"This is the file to transffer: %s\0A\00"
@.str.8 = [2 x i8] c"r\00"
@.str.9 = [2 x i8] c"1\00"
@.str.10 = [28 x i8] c"Error writing to TCP socket\00"
@.str.11 = [2 x i8] c"0\00"
@.str.12 = [2 x i8] c"w\00"
@.str.13 = [22 x i8] c"Cannot write on file.\00"
@.str.14 = [3 x i8] c"a+\00"
@.str.15 = [23 x i8] c"Cannot append to file.\00"
@.str.18 = [34 x i8] c"Transfer completed and successful\00"
@str = [23 x i8] c"Starting TCP server...\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = alloca %struct.sockaddr_in
%5 = alloca %struct.sockaddr_in
%6 = alloca [10 x i8]
%7 = alloca [512 x i8]
%8 = bitcast i32* %3 to i8*
%9 = load i32, i32* @BUFSIZ
%10 = zext i32 %9 to i64
%11 = tail call i8* @llvm.stacksave()
%12 = alloca i8, i64 %10
%13 = bitcast %struct.sockaddr_in* %4 to i8*
%14 = bitcast %struct.sockaddr_in* %5 to i8*
%15 = icmp eq i32 %0, 2
br i1 %15, label %19, label %16
16: ; preds = %2
%17 = load i32*, i32** @stderr
%18 = tail call i64 @fwrite(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i64 31, i64 1, i32* %17)
br label %119
19: ; preds = %2
%20 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str, i64 0, i64 0))
%21 = getelementptr inbounds i8*, i8** %1, i64 1
%22 = load i8*, i8** %21
%23 = tail call i32 @atoi(i8* %22)
%24 = add i32 %23, -65536
%25 = icmp ult i32 %24, -65535
br i1 %25, label %26, label %29
26: ; preds = %19
%27 = load i32*, i32** @stderr
%28 = tail call i64 @fwrite(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i64 32, i64 1, i32* %27)
br label %119
29: ; preds = %19
%30 = load i32, i32* @AF_INET
%31 = load i32, i32* @SOCK_STREAM
%32 = tail call i32 @socket(i32 %30, i32 %31, i32 0)
%33 = icmp slt i32 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %29
%35 = tail call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0))
br label %119
36: ; preds = %29
%37 = call i32 @bzero(i8* %13, i32 12)
%38 = load i32, i32* @AF_INET
%39 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 2
store i32 %38, i32* %39
%40 = load i32, i32* @INADDR_ANY
%41 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1, i32 0
store i32 %40, i32* %41
%42 = tail call i32 @htons(i32 %23)
%43 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0
store i32 %42, i32* %43
%44 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%45 = call i64 @bind(i32 %32, %struct.sockaddr* %44, i32 12)
%46 = icmp slt i64 %45, 0
br i1 %46, label %47, label %49
47: ; preds = %36
%48 = call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0))
br label %119
49: ; preds = %36
%50 = call i32 @listen(i32 %32, i32 5)
store i32 12, i32* %3
%51 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%52 = call i32 @accept(i32 %32, %struct.sockaddr* %51, i32* %3)
%53 = icmp slt i32 %52, 0
br i1 %53, label %54, label %56
54: ; preds = %49
%55 = call i32 @perror(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.5, i64 0, i64 0))
br label %119
56: ; preds = %49
%57 = load i32, i32* @BUFSIZ
%58 = call i32 @bzero(i8* %12, i32 %57)
%59 = call i32 @read(i32 %52, i8* %12, i32 %57)
%60 = icmp slt i32 %59, 0
br i1 %60, label %61, label %63
61: ; preds = %56
%62 = call i32 @perror(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.6, i64 0, i64 0))
br label %119
63: ; preds = %56
%64 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @.str.7, i64 0, i64 0), i8* %12)
%65 = getelementptr inbounds [10 x i8], [10 x i8]* %6, i64 0, i64 0
%66 = call i32* @fopen(i8* %12, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.8, i64 0, i64 0))
%67 = icmp eq i32* %66, null
br i1 %67, label %73, label %68
68: ; preds = %63
%69 = call i32 @write(i32 %52, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.9, i64 0, i64 0), i32 1)
%70 = icmp slt i32 %69, 0
br i1 %70, label %71, label %78
71: ; preds = %68
%72 = call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.10, i64 0, i64 0))
br label %117
73: ; preds = %63
%74 = call i32 @write(i32 %52, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.11, i64 0, i64 0), i32 1)
%75 = icmp slt i32 %74, 0
br i1 %75, label %76, label %78
76: ; preds = %73
%77 = call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.10, i64 0, i64 0))
br label %117
78: ; preds = %73, %68
%79 = call i32 @read(i32 %52, i8* %65, i32 1)
%80 = icmp slt i32 %79, 0
br i1 %80, label %81, label %83
81: ; preds = %78
%82 = call i32 @perror(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.6, i64 0, i64 0))
br label %117
83: ; preds = %78
%84 = load i8, i8* %65
%85 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 0
%86 = sext i8 %84 to i32
switch i32 %86, label %99 [
i32 49, label %87
i32 48, label %93
]
87: ; preds = %83
%88 = call i32* @fopen(i8* %12, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.12, i64 0, i64 0))
%89 = icmp eq i32* %88, null
br i1 %89, label %90, label %99
90: ; preds = %87
%91 = load i32*, i32** @stderr
%92 = call i64 @fwrite(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.13, i64 0, i64 0), i64 21, i64 1, i32* %91)
br label %115
93: ; preds = %83
%94 = call i32* @fopen(i8* %12, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.14, i64 0, i64 0))
%95 = icmp eq i32* %94, null
br i1 %95, label %96, label %99
96: ; preds = %93
%97 = load i32*, i32** @stderr
%98 = call i64 @fwrite(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.15, i64 0, i64 0), i64 22, i64 1, i32* %97)
br label %115
99: ; preds = %93, %87, %83
%100 = phi i32* [ undef, %83 ], [ %94, %93 ], [ %88, %87 ]
%101 = call i32 @read(i32 %52, i8* %85, i32 512)
%102 = icmp sgt i32 %101, 0
br i1 %102, label %103, label %109
103: ; preds = %99, %103
%104 = call i32 @fprintf(i32* %100, i8* %85)
%105 = load i32*, i32** @stderr
%106 = call i32 @fprintf(i32* %105, i8* %85)
%107 = call i32 @read(i32 %52, i8* %85, i32 512)
%108 = icmp sgt i32 %107, 0
br i1 %108, label %103, label %109
109: ; preds = %103, %99
%110 = call i32 @fclose(i32* %100)
%111 = call i32 @write(i32 %52, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.18, i64 0, i64 0), i32 33)
%112 = icmp slt i32 %111, 0
br i1 %112, label %113, label %115
113: ; preds = %109
%114 = call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.10, i64 0, i64 0))
br label %115
115: ; preds = %109, %113, %96, %90
%116 = phi i32 [ -7, %113 ], [ -9, %96 ], [ -8, %90 ], [ 0, %109 ]
br label %117
117: ; preds = %115, %81, %76, %71
%118 = phi i32 [ -7, %71 ], [ -6, %81 ], [ %116, %115 ], [ -7, %76 ]
br label %119
119: ; preds = %117, %61, %54, %47, %34, %26, %16
%120 = phi i32 [ -1, %16 ], [ -2, %26 ], [ -3, %34 ], [ -4, %47 ], [ -5, %54 ], [ -6, %61 ], [ %118, %117 ]
ret i32 %120
}