#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 scalar_t__ time_t;
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 /*<<< orphan*/ dest;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int EAGAIN;
int EWOULDBLOCK;
int EXIT_FAILURE;
int EXIT_SUCCESS;
int /*<<< orphan*/ MSG_DONTWAIT;
int /*<<< orphan*/ SOCK_STREAM;
int atoi(char *);
int /*<<< orphan*/ close(int);
int connect(int, struct sockaddr *, int);
int errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int fwrite(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_addr(char *);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int recv(int, char *, int, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
scalar_t__ strlen(char *);
scalar_t__ time(int /*<<< orphan*/ *);
int write(int, char *, scalar_t__);
int main(int argc, char *argv[]) {
  int socketfd, port;
  struct sockaddr_in dest;
  char buffer[1024];
  FILE *save_file;
  if (argc != 4) {
    fprintf(stderr, "Invalid, should be: <server-ip> <port> <filename>");
    exit(EXIT_FAILURE);
  }
  char *host = argv[1];
  port = atoi(argv[2]);
  char *file_name = argv[3];
  socketfd = socket(AF_INET, SOCK_STREAM, 0);
  dest.sin_family = AF_INET;
  dest.sin_port = htons(port);
  dest.sin_addr.s_addr = inet_addr(host);
  if (connect(socketfd, (struct sockaddr *)&dest, sizeof(dest)) == -1) {
    fprintf(stderr, "Failed to connect to host %s on port %u.\n", host, port);
    exit(errno);
  }
  if (write(socketfd, file_name, strlen(file_name) + 1) == -1) {
    fprintf(stderr, "Error sending file request to client.\n");
    exit(EXIT_FAILURE);
  }
  save_file = fopen(file_name, "wb");
  if (save_file == NULL) {
    fprintf(stderr, "Error creating save file,\n");
    exit(EXIT_FAILURE);
  }
  time_t last_chunk = time(NULL);
  for (int res;;) {
    memset(&buffer[0], 0, 1024);
    res = recv(socketfd, buffer, 1024, MSG_DONTWAIT);
    if (res == -1) {
      if (errno == EAGAIN || errno == EWOULDBLOCK) {
        if (time(NULL) > last_chunk + 5) {
          fprintf(stderr, "File server timed out.\n");
          break;
        }
        continue;
      } else {
        fprintf(stderr, "Connection refused. Errno: %d\n", errno);
        break;
      }
    }
    if (res == 0) {
      break;
    } else if (res > 0) {
      if (fwrite(buffer, sizeof(char), res, save_file) < res) {
        fprintf(stderr, "Error streaming save buffer to file. Errno: %d\n",
                errno);
        break;
      }
      last_chunk = time(NULL);
    }
  }
  close(socketfd);
  fclose(save_file);
  exit(EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x470,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x4,-0x8(%rbp)
je     1248 <main+0x48>
mov    0x2dae(%rip),%rax
mov    (%rax),%edi
lea    0xdcd(%rip),%rsi
mov    $0x0,%al
call   10b0 <fprintf@plt>
mov    0x2d6f(%rip),%rax
mov    (%rax),%edi
call   10f0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x440(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   10e0 <atoi@plt>
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x448(%rbp)
mov    0x2d6b(%rip),%rax
mov    (%rax),%edi
mov    0x2d4a(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1120 <socket@plt>
mov    %eax,-0x14(%rbp)
mov    0x2d4f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%edi
call   1070 <htons@plt>
mov    %eax,-0x24(%rbp)
mov    -0x440(%rbp),%rdi
call   10a0 <inet_addr@plt>
mov    %eax,-0x28(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0xc,%edx
call   1100 <connect@plt>
cmp    $0xffffffff,%eax
jne    1301 <main+0x101>
mov    0x2cff(%rip),%rax
mov    (%rax),%edi
mov    -0x440(%rbp),%rdx
mov    -0x18(%rbp),%ecx
lea    0xd47(%rip),%rsi
mov    $0x0,%al
call   10b0 <fprintf@plt>
mov    0x2cce(%rip),%rax
mov    (%rax),%edi
call   10f0 <exit@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x464(%rbp)
mov    -0x448(%rbp),%rax
mov    %rax,-0x460(%rbp)
mov    -0x448(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0x464(%rbp),%edi
mov    -0x460(%rbp),%rsi
mov    %rax,%rdx
add    $0x1,%rdx
call   1040 <write@plt>
cmp    $0xffffffff,%eax
jne    136b <main+0x16b>
mov    0x2c8b(%rip),%rax
mov    (%rax),%edi
lea    0xd07(%rip),%rsi
mov    $0x0,%al
call   10b0 <fprintf@plt>
mov    0x2c4c(%rip),%rax
mov    (%rax),%edi
call   10f0 <exit@plt>
mov    -0x448(%rbp),%rdi
lea    0xd0b(%rip),%rsi
call   10d0 <fopen@plt>
mov    %rax,-0x438(%rbp)
cmpq   $0x0,-0x438(%rbp)
jne    13b8 <main+0x1b8>
mov    0x2c3e(%rip),%rax
mov    (%rax),%edi
lea    0xce4(%rip),%rsi
mov    $0x0,%al
call   10b0 <fprintf@plt>
mov    0x2bff(%rip),%rax
mov    (%rax),%edi
call   10f0 <exit@plt>
xor    %eax,%eax
mov    %eax,%edi
call   10c0 <time@plt>
mov    %rax,-0x450(%rbp)
lea    -0x430(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1080 <memset@plt>
mov    -0x14(%rbp),%edi
lea    -0x430(%rbp),%rsi
mov    0x2bcc(%rip),%rax
mov    (%rax),%ecx
mov    $0x400,%edx
call   1030 <recv@plt>
mov    %eax,-0x454(%rbp)
cmpl   $0xffffffff,-0x454(%rbp)
jne    149e <main+0x29e>
mov    0x2bb6(%rip),%rax
mov    (%rax),%eax
mov    0x2b7d(%rip),%rcx
cmp    (%rcx),%eax
je     143b <main+0x23b>
mov    0x2b9e(%rip),%rax
mov    (%rax),%eax
mov    0x2b8d(%rip),%rcx
cmp    (%rcx),%eax
jne    1479 <main+0x279>
xor    %eax,%eax
mov    %eax,%edi
call   10c0 <time@plt>
mov    -0x450(%rbp),%rcx
add    $0x5,%rcx
cmp    %rcx,%rax
jle    1474 <main+0x274>
mov    0x2b79(%rip),%rax
mov    (%rax),%edi
lea    0xc3a(%rip),%rsi
mov    $0x0,%al
call   10b0 <fprintf@plt>
jmp    1526 <main+0x326>
jmp    13c8 <main+0x1c8>
mov    0x2b58(%rip),%rax
mov    (%rax),%edi
mov    0x2b3f(%rip),%rax
mov    (%rax),%edx
lea    0xc28(%rip),%rsi
mov    $0x0,%al
call   10b0 <fprintf@plt>
jmp    1526 <main+0x326>
cmpl   $0x0,-0x454(%rbp)
jne    14b0 <main+0x2b0>
jmp    1526 <main+0x326>
cmpl   $0x0,-0x454(%rbp)
jle    151c <main+0x31c>
lea    -0x430(%rbp),%rdi
mov    -0x454(%rbp),%edx
mov    -0x438(%rbp),%rcx
mov    $0x1,%esi
call   1110 <fwrite@plt>
cmp    -0x454(%rbp),%eax
jge    150c <main+0x30c>
mov    0x2aea(%rip),%rax
mov    (%rax),%edi
mov    0x2ad1(%rip),%rax
mov    (%rax),%edx
lea    0xbd9(%rip),%rsi
mov    $0x0,%al
call   10b0 <fprintf@plt>
jmp    1526 <main+0x326>
xor    %eax,%eax
mov    %eax,%edi
call   10c0 <time@plt>
mov    %rax,-0x450(%rbp)
jmp    1521 <main+0x321>
jmp    13c8 <main+0x1c8>
mov    -0x14(%rbp),%edi
call   1090 <close@plt>
mov    -0x438(%rbp),%rdi
call   1050 <fclose@plt>
mov    0x2a67(%rip),%rax
mov    (%rax),%edi
call   10f0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x428,%rsp
cmp    $0x4,%edi
jne    12ac <main+0xac>
mov    %rsi,%rbp
mov    0x8(%rsi),%r15
mov    0x10(%rsi),%rdi
call   10e0 <atoi@plt>
mov    %eax,%ebx
mov    0x18(%rbp),%r14
mov    0x2db1(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2d8f(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1120 <socket@plt>
mov    %eax,%r12d
mov    0x0(%rbp),%eax
mov    %eax,0x18(%rsp)
mov    %ebx,%edi
call   1070 <htons@plt>
mov    %eax,0x14(%rsp)
mov    %r15,%rdi
call   10a0 <inet_addr@plt>
mov    %eax,0x10(%rsp)
lea    0x10(%rsp),%rsi
mov    %r12d,%edi
mov    $0xc,%edx
call   1100 <connect@plt>
cmp    $0xffffffff,%eax
jne    12be <main+0xbe>
mov    0x2d4f(%rip),%rax
mov    (%rax),%edi
lea    0xda1(%rip),%rsi
mov    %r15,%rdx
mov    %ebx,%ecx
xor    %eax,%eax
call   10b0 <fprintf@plt>
mov    0x2d23(%rip),%rax
mov    (%rax),%edi
call   10f0 <exit@plt>
mov    0x2d25(%rip),%rax
mov    (%rax),%edi
lea    0xd44(%rip),%rsi
jmp    1315 <main+0x115>
mov    %r14,%rdi
call   1060 <strlen@plt>
lea    0x1(%rax),%rdx
mov    %r12d,%edi
mov    %r14,%rsi
call   1040 <write@plt>
cmp    $0xffffffff,%eax
jne    12ec <main+0xec>
mov    0x2cf7(%rip),%rax
mov    (%rax),%edi
lea    0xd73(%rip),%rsi
jmp    1315 <main+0x115>
lea    0xd91(%rip),%rsi
mov    %r14,%rdi
call   10d0 <fopen@plt>
mov    %rax,0x8(%rsp)
test   %rax,%rax
jne    132a <main+0x12a>
mov    0x2ccc(%rip),%rax
mov    (%rax),%edi
lea    0xd72(%rip),%rsi
xor    %eax,%eax
call   10b0 <fprintf@plt>
mov    0x2c8d(%rip),%rax
mov    (%rax),%edi
call   10f0 <exit@plt>
lea    0x20(%rsp),%r15
mov    0x2c82(%rip),%r14
mov    0x2c5b(%rip),%r13
nopl   (%rax)
xor    %edi,%edi
call   10c0 <time@plt>
mov    %rax,%rbp
add    $0x5,%rbp
jmp    135c <main+0x15c>
xor    %edi,%edi
call   10c0 <time@plt>
cmp    %rbp,%rax
jg     13da <main+0x1da>
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1080 <memset@plt>
mov    (%r14),%ecx
mov    %r12d,%edi
mov    %r15,%rsi
mov    $0x400,%edx
call   1030 <recv@plt>
cmp    $0xffffffff,%eax
jne    139f <main+0x19f>
mov    0x2c3e(%rip),%rax
mov    (%rax),%edx
cmp    0x0(%r13),%edx
je     1350 <main+0x150>
mov    0x2c27(%rip),%rax
cmp    (%rax),%edx
je     1350 <main+0x150>
jmp    13f3 <main+0x1f3>
mov    %eax,%ebx
test   %eax,%eax
je     140a <main+0x20a>
jle    135c <main+0x15c>
mov    %r15,%rdi
mov    $0x1,%esi
mov    %ebx,%edx
mov    0x8(%rsp),%rcx
call   1110 <fwrite@plt>
cmp    %ebx,%eax
jge    1340 <main+0x140>
mov    0x2c12(%rip),%rax
mov    (%rax),%edi
mov    0x2bf9(%rip),%rax
mov    (%rax),%edx
lea    0xd01(%rip),%rsi
jmp    1403 <main+0x203>
mov    0x2bf7(%rip),%rax
mov    (%rax),%edi
lea    0xcb8(%rip),%rsi
xor    %eax,%eax
call   10b0 <fprintf@plt>
jmp    140a <main+0x20a>
mov    0x2bde(%rip),%rax
mov    (%rax),%edi
lea    0xcb7(%rip),%rsi
xor    %eax,%eax
call   10b0 <fprintf@plt>
mov    %r12d,%edi
call   1090 <close@plt>
mov    0x8(%rsp),%rdi
call   1050 <fclose@plt>
mov    0x2b85(%rip),%rax
mov    (%rax),%edi
call   10f0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@stderr = global i32 0
@.str = [51 x i8] c"Invalid, should be: <server-ip>\C2\A0<port> <filename>\00"
@EXIT_FAILURE = global i32 0
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [42 x i8] c"Failed to connect to host %s on port %u.\0A\00"
@errno = global i32 0
@.str.2 = [39 x i8] c"Error sending file request to client.\0A\00"
@.str.3 = [3 x i8] c"wb\00"
@.str.4 = [27 x i8] c"Error creating save file,\0A\00"
@MSG_DONTWAIT = global i32 0
@EAGAIN = global i32 0
@EWOULDBLOCK = global i32 0
@.str.5 = [24 x i8] c"File server timed out.\0A\00"
@.str.6 = [31 x i8] c"Connection refused. Errno: %d\0A\00"
@.str.7 = [48 x i8] c"Error streaming save buffer to file. Errno: %d\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.sockaddr_in
%9 = alloca [1024 x i8]
%10 = alloca i32*
%11 = alloca i8*
%12 = alloca i8*
%13 = alloca i64
%14 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%15 = load i32, i32* %4
%16 = icmp ne i32 %15, 4
br i1 %16, label %17, label %22
17: ; preds = %2
%18 = load i32, i32* @stderr
%19 = call i32 (i32, i8*, ...) @fprintf(i32 %18, i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str, i64 0, i64 0))
%20 = load i32, i32* @EXIT_FAILURE
%21 = call i32 @exit(i32 %20)
unreachable
22: ; preds = %2
%23 = load i8**, i8*** %5
%24 = getelementptr inbounds i8*, i8** %23, i64 1
%25 = load i8*, i8** %24
store i8* %25, i8** %11
%26 = load i8**, i8*** %5
%27 = getelementptr inbounds i8*, i8** %26, i64 2
%28 = load i8*, i8** %27
%29 = call i32 @atoi(i8* %28)
store i32 %29, i32* %7
%30 = load i8**, i8*** %5
%31 = getelementptr inbounds i8*, i8** %30, i64 3
%32 = load i8*, i8** %31
store i8* %32, i8** %12
%33 = load i32, i32* @AF_INET
%34 = load i32, i32* @SOCK_STREAM
%35 = call i32 @socket(i32 %33, i32 %34, i32 0)
store i32 %35, i32* %6
%36 = load i32, i32* @AF_INET
%37 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 2
store i32 %36, i32* %37
%38 = load i32, i32* %7
%39 = call i32 @htons(i32 %38)
%40 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 1
store i32 %39, i32* %40
%41 = load i8*, i8** %11
%42 = call i32 @inet_addr(i8* %41)
%43 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 0
%44 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %43, i32 0, i32 0
store i32 %42, i32* %44
%45 = load i32, i32* %6
%46 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%47 = call i32 @connect(i32 %45, %struct.sockaddr* %46, i32 12)
%48 = icmp eq i32 %47, -1
br i1 %48, label %49, label %56
49: ; preds = %22
%50 = load i32, i32* @stderr
%51 = load i8*, i8** %11
%52 = load i32, i32* %7
%53 = call i32 (i32, i8*, ...) @fprintf(i32 %50, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.1, i64 0, i64 0), i8* %51, i32 %52)
%54 = load i32, i32* @errno
%55 = call i32 @exit(i32 %54)
unreachable
56: ; preds = %22
%57 = load i32, i32* %6
%58 = load i8*, i8** %12
%59 = load i8*, i8** %12
%60 = call i64 @strlen(i8* %59)
%61 = add nsw i64 %60, 1
%62 = call i32 @write(i32 %57, i8* %58, i64 %61)
%63 = icmp eq i32 %62, -1
br i1 %63, label %64, label %69
64: ; preds = %56
%65 = load i32, i32* @stderr
%66 = call i32 (i32, i8*, ...) @fprintf(i32 %65, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0))
%67 = load i32, i32* @EXIT_FAILURE
%68 = call i32 @exit(i32 %67)
unreachable
69: ; preds = %56
%70 = load i8*, i8** %12
%71 = call i32* @fopen(i8* %70, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
store i32* %71, i32** %10
%72 = load i32*, i32** %10
%73 = icmp eq i32* %72, null
br i1 %73, label %74, label %79
74: ; preds = %69
%75 = load i32, i32* @stderr
%76 = call i32 (i32, i8*, ...) @fprintf(i32 %75, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.4, i64 0, i64 0))
%77 = load i32, i32* @EXIT_FAILURE
%78 = call i32 @exit(i32 %77)
unreachable
79: ; preds = %69
%80 = call i64 @time(i32* null)
store i64 %80, i64* %13
br label %81
81: ; preds = %132, %106, %79
%82 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%83 = call i32 @memset(i8* %82, i32 0, i32 1024)
%84 = load i32, i32* %6
%85 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%86 = load i32, i32* @MSG_DONTWAIT
%87 = call i32 @recv(i32 %84, i8* %85, i32 1024, i32 %86)
store i32 %87, i32* %14
%88 = load i32, i32* %14
%89 = icmp eq i32 %88, -1
br i1 %89, label %90, label %111
90: ; preds = %81
%91 = load i32, i32* @errno
%92 = load i32, i32* @EAGAIN
%93 = icmp eq i32 %91, %92
br i1 %93, label %98, label %94
94: ; preds = %90
%95 = load i32, i32* @errno
%96 = load i32, i32* @EWOULDBLOCK
%97 = icmp eq i32 %95, %96
br i1 %97, label %98, label %107
98: ; preds = %94, %90
%99 = call i64 @time(i32* null)
%100 = load i64, i64* %13
%101 = add nsw i64 %100, 5
%102 = icmp sgt i64 %99, %101
br i1 %102, label %103, label %106
103: ; preds = %98
%104 = load i32, i32* @stderr
%105 = call i32 (i32, i8*, ...) @fprintf(i32 %104, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i64 0, i64 0))
br label %133
106: ; preds = %98
br label %81
107: ; preds = %94
%108 = load i32, i32* @stderr
%109 = load i32, i32* @errno
%110 = call i32 (i32, i8*, ...) @fprintf(i32 %108, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.6, i64 0, i64 0), i32 %109)
br label %133
111: ; preds = %81
%112 = load i32, i32* %14
%113 = icmp eq i32 %112, 0
br i1 %113, label %114, label %115
114: ; preds = %111
br label %133
115: ; preds = %111
%116 = load i32, i32* %14
%117 = icmp sgt i32 %116, 0
br i1 %117, label %118, label %131
118: ; preds = %115
%119 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%120 = load i32, i32* %14
%121 = load i32*, i32** %10
%122 = call i32 @fwrite(i8* %119, i32 1, i32 %120, i32* %121)
%123 = load i32, i32* %14
%124 = icmp slt i32 %122, %123
br i1 %124, label %125, label %129
125: ; preds = %118
%126 = load i32, i32* @stderr
%127 = load i32, i32* @errno
%128 = call i32 (i32, i8*, ...) @fprintf(i32 %126, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.7, i64 0, i64 0), i32 %127)
br label %133
129: ; preds = %118
%130 = call i64 @time(i32* null)
store i64 %130, i64* %13
br label %131
131: ; preds = %129, %115
br label %132
132: ; preds = %131
br label %81
133: ; preds = %125, %114, %107, %103
%134 = load i32, i32* %6
%135 = call i32 @close(i32 %134)
%136 = load i32*, i32** %10
%137 = call i32 @fclose(i32* %136)
%138 = load i32, i32* @EXIT_SUCCESS
%139 = call i32 @exit(i32 %138)
unreachable
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@stderr = global i32 0
@.str = [51 x i8] c"Invalid, should be: <server-ip>\C2\A0<port> <filename>\00"
@EXIT_FAILURE = global i32 0
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [42 x i8] c"Failed to connect to host %s on port %u.\0A\00"
@errno = global i32 0
@.str.2 = [39 x i8] c"Error sending file request to client.\0A\00"
@.str.3 = [3 x i8] c"wb\00"
@.str.4 = [27 x i8] c"Error creating save file,\0A\00"
@MSG_DONTWAIT = global i32 0
@EAGAIN = global i32 0
@EWOULDBLOCK = global i32 0
@.str.5 = [24 x i8] c"File server timed out.\0A\00"
@.str.6 = [31 x i8] c"Connection refused. Errno: %d\0A\00"
@.str.7 = [48 x i8] c"Error streaming save buffer to file. Errno: %d\0A\00"
@EXIT_SUCCESS = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca [1024 x i8]
%5 = bitcast %struct.sockaddr_in* %3 to i8*
%6 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%7 = icmp eq i32 %0, 4
br i1 %7, label %13, label %8
8: ; preds = %2
%9 = load i32, i32* @stderr
%10 = tail call i32 (i32, i8*, ...) @fprintf(i32 %9, i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str, i64 0, i64 0))
%11 = load i32, i32* @EXIT_FAILURE
%12 = tail call i32 @exit(i32 %11)
unreachable
13: ; preds = %2
%14 = getelementptr inbounds i8*, i8** %1, i64 1
%15 = load i8*, i8** %14
%16 = getelementptr inbounds i8*, i8** %1, i64 2
%17 = load i8*, i8** %16
%18 = tail call i32 @atoi(i8* %17)
%19 = getelementptr inbounds i8*, i8** %1, i64 3
%20 = load i8*, i8** %19
%21 = load i32, i32* @AF_INET
%22 = load i32, i32* @SOCK_STREAM
%23 = tail call i32 @socket(i32 %21, i32 %22, i32 0)
%24 = load i32, i32* @AF_INET
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %24, i32* %25
%26 = tail call i32 @htons(i32 %18)
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %26, i32* %27
%28 = tail call i32 @inet_addr(i8* %15)
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0, i32 0
store i32 %28, i32* %29
%30 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%31 = call i32 @connect(i32 %23, %struct.sockaddr* %30, i32 12)
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %38
33: ; preds = %13
%34 = load i32, i32* @stderr
%35 = call i32 (i32, i8*, ...) @fprintf(i32 %34, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.1, i64 0, i64 0), i8* %15, i32 %18)
%36 = load i32, i32* @errno
%37 = call i32 @exit(i32 %36)
unreachable
38: ; preds = %13
%39 = call i64 @strlen(i8* dereferenceable(1) %20)
%40 = add nsw i64 %39, 1
%41 = call i32 @write(i32 %23, i8* %20, i64 %40)
%42 = icmp eq i32 %41, -1
br i1 %42, label %43, label %48
43: ; preds = %38
%44 = load i32, i32* @stderr
%45 = call i32 (i32, i8*, ...) @fprintf(i32 %44, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0))
%46 = load i32, i32* @EXIT_FAILURE
%47 = call i32 @exit(i32 %46)
unreachable
48: ; preds = %38
%49 = call i32* @fopen(i8* %20, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%50 = icmp eq i32* %49, null
br i1 %50, label %51, label %56
51: ; preds = %48
%52 = load i32, i32* @stderr
%53 = call i32 (i32, i8*, ...) @fprintf(i32 %52, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.4, i64 0, i64 0))
%54 = load i32, i32* @EXIT_FAILURE
%55 = call i32 @exit(i32 %54)
unreachable
56: ; preds = %48, %82
%57 = call i64 @time(i32* null)
%58 = add nsw i64 %57, 5
br label %59
59: ; preds = %56, %80
br label %60
60: ; preds = %59, %71
%61 = call i32 @memset(i8* %6, i32 0, i32 1024)
%62 = load i32, i32* @MSG_DONTWAIT
%63 = call i32 @recv(i32 %23, i8* %6, i32 1024, i32 %62)
switch i32 %63, label %80 [
i32 -1, label %64
i32 0, label %89
]
64: ; preds = %60
%65 = load i32, i32* @errno
%66 = load i32, i32* @EAGAIN
%67 = icmp eq i32 %65, %66
%68 = load i32, i32* @EWOULDBLOCK
%69 = icmp eq i32 %65, %68
%70 = select i1 %67, i1 true, i1 %69
br i1 %70, label %71, label %77
71: ; preds = %64
%72 = call i64 @time(i32* null)
%73 = icmp sgt i64 %72, %58
br i1 %73, label %74, label %60
74: ; preds = %71
%75 = load i32, i32* @stderr
%76 = call i32 (i32, i8*, ...) @fprintf(i32 %75, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i64 0, i64 0))
br label %89
77: ; preds = %64
%78 = load i32, i32* @stderr
%79 = call i32 (i32, i8*, ...) @fprintf(i32 %78, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.6, i64 0, i64 0), i32 %65)
br label %89
80: ; preds = %60
%81 = icmp sgt i32 %63, 0
br i1 %81, label %82, label %59
82: ; preds = %80
%83 = call i32 @fwrite(i8* %6, i32 1, i32 %63, i32* %49)
%84 = icmp slt i32 %83, %63
br i1 %84, label %85, label %56
85: ; preds = %82
%86 = load i32, i32* @stderr
%87 = load i32, i32* @errno
%88 = call i32 (i32, i8*, ...) @fprintf(i32 %86, i8* getelementptr inbounds ([48 x i8], [48 x i8]* @.str.7, i64 0, i64 0), i32 %87)
br label %89
89: ; preds = %60, %85, %77, %74
%90 = call i32 @close(i32 %23)
%91 = call i32 @fclose(i32* %49)
%92 = load i32, i32* @EXIT_SUCCESS
%93 = call i32 @exit(i32 %92)
unreachable
}