#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 */

/* Type definitions */
struct addrinfo {
  int dummy;
};

/* Variables and functions */
int EXIT_FAILURE;
int EXIT_SUCCESS;
scalar_t__ checkaddr(char *, char *, struct addrinfo **);
scalar_t__ checkfile(int *, char *);
scalar_t__ checksock(int *, struct addrinfo *);
int /*<<< orphan*/ close(int);
scalar_t__ copyfile(int, int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
int /*<<< orphan*/ stdout;
int main(int argc, char *argv[]) {
  char *fname = NULL, *ip = NULL, *port = NULL;
  struct addrinfo *results = NULL;
  int fd = 0, sockfd = 0, ret = EXIT_SUCCESS;
  fprintf(stdout,
          "-- netcp v%d.%d by Wolfgang Richter "
          "<wolfgang.richter@gmail.com> --\n",
          1, 1);
  if (argc < 4) {
    fprintf(stdout, "%s <filename> <IP> <port>\n", argv[0]);
    return EXIT_FAILURE;
  }
  fname = argv[1];
  ip = argv[2];
  port = argv[3];
  fprintf(stdout, "copying '%s' to %s:%s\n", fname, ip, port);
  if (checkaddr(ip, port, &results)) {
    ret = EXIT_FAILURE;
    goto exit;
  }
  if (checksock(&sockfd, results)) {
    ret = EXIT_FAILURE;
    goto exit;
  }
  if (checkfile(&fd, fname)) {
    ret = EXIT_FAILURE;
    goto exit;
  }
  if (copyfile(fd, sockfd)) {
    ret = EXIT_FAILURE;
    goto exit;
  }
exit:
  if (results)
    freeaddrinfo(results);
  if (sockfd >= 0)
    close(sockfd);
  if (fd >= 0)
    close(fd);
  return ret;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
movl   $0x0,-0x34(%rbp)
movl   $0x0,-0x38(%rbp)
mov    0x2e15(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    0x2e11(%rip),%rax
mov    (%rax),%edi
lea    0xe30(%rip),%rsi
mov    $0x1,%ecx
mov    %ecx,%edx
mov    $0x0,%al
call   1060 <fprintf@plt>
cmpl   $0x4,-0x8(%rbp)
jge    1217 <main+0xa7>
mov    0x2de9(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe46(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    0x2dd3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1343 <main+0x1d3>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x28(%rbp)
mov    0x2d96(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rcx
mov    -0x28(%rbp),%r8
lea    0xe09(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rsi
lea    -0x30(%rbp),%rdx
call   1050 <checkaddr@plt>
cmp    $0x0,%rax
je     128a <main+0x11a>
mov    0x2d60(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
jmp    1305 <main+0x195>
mov    -0x30(%rbp),%rsi
lea    -0x38(%rbp),%rdi
call   1070 <checksock@plt>
cmp    $0x0,%rax
je     12b2 <main+0x142>
mov    0x2d38(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
jmp    1305 <main+0x195>
mov    -0x18(%rbp),%rsi
lea    -0x34(%rbp),%rdi
call   1040 <checkfile@plt>
cmp    $0x0,%rax
je     12da <main+0x16a>
mov    0x2d10(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
jmp    1305 <main+0x195>
mov    -0x34(%rbp),%edi
mov    -0x38(%rbp),%esi
call   1090 <copyfile@plt>
cmp    $0x0,%rax
je     1300 <main+0x190>
mov    0x2cea(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
jmp    1305 <main+0x195>
jmp    1305 <main+0x195>
cmpq   $0x0,-0x30(%rbp)
je     1319 <main+0x1a9>
mov    -0x30(%rbp),%rdi
call   1080 <freeaddrinfo@plt>
cmpl   $0x0,-0x38(%rbp)
jl     132b <main+0x1bb>
mov    -0x38(%rbp),%edi
call   1030 <close@plt>
cmpl   $0x0,-0x34(%rbp)
jl     133d <main+0x1cd>
mov    -0x34(%rbp),%edi
call   1030 <close@plt>
mov    -0x3c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbx
mov    %edi,%ebp
movq   $0x0,0x8(%rsp)
movl   $0x0,0x4(%rsp)
movl   $0x0,(%rsp)
mov    0x2e30(%rip),%rax
mov    (%rax),%r15d
mov    0x2e2e(%rip),%r12
mov    (%r12),%edi
lea    0xe4b(%rip),%rsi
mov    $0x1,%edx
mov    $0x1,%ecx
xor    %eax,%eax
call   1060 <fprintf@plt>
cmp    $0x3,%ebp
jg     11ef <main+0x7f>
mov    (%r12),%edi
mov    (%rbx),%rdx
lea    0xe6c(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    0x2df9(%rip),%rax
mov    (%rax),%r15d
jmp    1292 <main+0x122>
mov    0x8(%rbx),%r14
mov    0x10(%rbx),%rbp
mov    0x18(%rbx),%rbx
mov    (%r12),%edi
lea    0xe5a(%rip),%rsi
mov    %r14,%rdx
mov    %rbp,%rcx
mov    %rbx,%r8
xor    %eax,%eax
call   1060 <fprintf@plt>
lea    0x8(%rsp),%rdx
mov    %rbp,%rdi
mov    %rbx,%rsi
call   1050 <checkaddr@plt>
test   %rax,%rax
jne    1260 <main+0xf0>
mov    0x8(%rsp),%rsi
mov    %rsp,%rdi
call   1070 <checksock@plt>
test   %rax,%rax
jne    1260 <main+0xf0>
lea    0x4(%rsp),%rdi
mov    %r14,%rsi
call   1040 <checkfile@plt>
test   %rax,%rax
jne    1260 <main+0xf0>
mov    0x4(%rsp),%edi
mov    (%rsp),%esi
call   1090 <copyfile@plt>
test   %rax,%rax
je     126a <main+0xfa>
mov    0x2d79(%rip),%rax
mov    (%rax),%r15d
mov    0x8(%rsp),%rdi
test   %rdi,%rdi
je     1279 <main+0x109>
call   1080 <freeaddrinfo@plt>
mov    (%rsp),%edi
test   %edi,%edi
js     1285 <main+0x115>
call   1030 <close@plt>
mov    0x4(%rsp),%edi
test   %edi,%edi
js     1292 <main+0x122>
call   1030 <close@plt>
mov    %r15d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.addrinfo = type { i32 }
@EXIT_SUCCESS = global i32 0
@stdout = global i32 0
@.str = [69 x i8] c"-- netcp v%d.%d by Wolfgang Richter <wolfgang.richter@gmail.com> --\0A\00"
@.str.1 = [27 x i8] c"%s <filename> <IP> <port>\0A\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [23 x i8] c"copying '%s' to %s:%s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca %struct.addrinfo*
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i8* null, i8** %6
store i8* null, i8** %7
store i8* null, i8** %8
store %struct.addrinfo* null, %struct.addrinfo** %9
store i32 0, i32* %10
store i32 0, i32* %11
%13 = load i32, i32* @EXIT_SUCCESS
store i32 %13, i32* %12
%14 = load i32, i32* @stdout
%15 = call i32 (i32, i8*, ...) @fprintf(i32 %14, i8* getelementptr inbounds ([69 x i8], [69 x i8]* @.str, i64 0, i64 0), i32 1, i32 1)
%16 = load i32, i32* %4
%17 = icmp slt i32 %16, 4
br i1 %17, label %18, label %25
18: ; preds = %2
%19 = load i32, i32* @stdout
%20 = load i8**, i8*** %5
%21 = getelementptr inbounds i8*, i8** %20, i64 0
%22 = load i8*, i8** %21
%23 = call i32 (i32, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i8* %22)
%24 = load i32, i32* @EXIT_FAILURE
store i32 %24, i32* %3
br label %86
25: ; preds = %2
%26 = load i8**, i8*** %5
%27 = getelementptr inbounds i8*, i8** %26, i64 1
%28 = load i8*, i8** %27
store i8* %28, i8** %6
%29 = load i8**, i8*** %5
%30 = getelementptr inbounds i8*, i8** %29, i64 2
%31 = load i8*, i8** %30
store i8* %31, i8** %7
%32 = load i8**, i8*** %5
%33 = getelementptr inbounds i8*, i8** %32, i64 3
%34 = load i8*, i8** %33
store i8* %34, i8** %8
%35 = load i32, i32* @stdout
%36 = load i8*, i8** %6
%37 = load i8*, i8** %7
%38 = load i8*, i8** %8
%39 = call i32 (i32, i8*, ...) @fprintf(i32 %35, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i8* %36, i8* %37, i8* %38)
%40 = load i8*, i8** %7
%41 = load i8*, i8** %8
%42 = call i64 @checkaddr(i8* %40, i8* %41, %struct.addrinfo** %9)
%43 = icmp ne i64 %42, 0
br i1 %43, label %44, label %46
44: ; preds = %25
%45 = load i32, i32* @EXIT_FAILURE
store i32 %45, i32* %12
br label %66
46: ; preds = %25
%47 = load %struct.addrinfo*, %struct.addrinfo** %9
%48 = call i64 @checksock(i32* %11, %struct.addrinfo* %47)
%49 = icmp ne i64 %48, 0
br i1 %49, label %50, label %52
50: ; preds = %46
%51 = load i32, i32* @EXIT_FAILURE
store i32 %51, i32* %12
br label %66
52: ; preds = %46
%53 = load i8*, i8** %6
%54 = call i64 @checkfile(i32* %10, i8* %53)
%55 = icmp ne i64 %54, 0
br i1 %55, label %56, label %58
56: ; preds = %52
%57 = load i32, i32* @EXIT_FAILURE
store i32 %57, i32* %12
br label %66
58: ; preds = %52
%59 = load i32, i32* %10
%60 = load i32, i32* %11
%61 = call i64 @copyfile(i32 %59, i32 %60)
%62 = icmp ne i64 %61, 0
br i1 %62, label %63, label %65
63: ; preds = %58
%64 = load i32, i32* @EXIT_FAILURE
store i32 %64, i32* %12
br label %66
65: ; preds = %58
br label %66
66: ; preds = %65, %63, %56, %50, %44
%67 = load %struct.addrinfo*, %struct.addrinfo** %9
%68 = icmp ne %struct.addrinfo* %67, null
br i1 %68, label %69, label %72
69: ; preds = %66
%70 = load %struct.addrinfo*, %struct.addrinfo** %9
%71 = call i32 @freeaddrinfo(%struct.addrinfo* %70)
br label %72
72: ; preds = %69, %66
%73 = load i32, i32* %11
%74 = icmp sge i32 %73, 0
br i1 %74, label %75, label %78
75: ; preds = %72
%76 = load i32, i32* %11
%77 = call i32 @close(i32 %76)
br label %78
78: ; preds = %75, %72
%79 = load i32, i32* %10
%80 = icmp sge i32 %79, 0
br i1 %80, label %81, label %84
81: ; preds = %78
%82 = load i32, i32* %10
%83 = call i32 @close(i32 %82)
br label %84
84: ; preds = %81, %78
%85 = load i32, i32* %12
store i32 %85, i32* %3
br label %86
86: ; preds = %84, %18
%87 = load i32, i32* %3
ret i32 %87
}

LLVM IR (-O2):
%struct.addrinfo = type { i32 }
@EXIT_SUCCESS = global i32 0
@stdout = global i32 0
@.str = [69 x i8] c"-- netcp v%d.%d by Wolfgang Richter <wolfgang.richter@gmail.com> --\0A\00"
@.str.1 = [27 x i8] c"%s <filename> <IP> <port>\0A\00"
@EXIT_FAILURE = global i32 0
@.str.2 = [23 x i8] c"copying '%s' to %s:%s\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.addrinfo*
%4 = alloca i32
%5 = alloca i32
%6 = bitcast %struct.addrinfo** %3 to i8*
store %struct.addrinfo* null, %struct.addrinfo** %3
%7 = bitcast i32* %4 to i8*
store i32 0, i32* %4
%8 = bitcast i32* %5 to i8*
store i32 0, i32* %5
%9 = load i32, i32* @EXIT_SUCCESS
%10 = load i32, i32* @stdout
%11 = tail call i32 (i32, i8*, ...) @fprintf(i32 %10, i8* getelementptr inbounds ([69 x i8], [69 x i8]* @.str, i64 0, i64 0), i32 1, i32 1)
%12 = icmp slt i32 %0, 4
br i1 %12, label %13, label %18
13: ; preds = %2
%14 = load i32, i32* @stdout
%15 = load i8*, i8** %1
%16 = tail call i32 (i32, i8*, ...) @fprintf(i32 %14, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i8* %15)
%17 = load i32, i32* @EXIT_FAILURE
br label %65
18: ; preds = %2
%19 = getelementptr inbounds i8*, i8** %1, i64 1
%20 = load i8*, i8** %19
%21 = getelementptr inbounds i8*, i8** %1, i64 2
%22 = load i8*, i8** %21
%23 = getelementptr inbounds i8*, i8** %1, i64 3
%24 = load i8*, i8** %23
%25 = load i32, i32* @stdout
%26 = tail call i32 (i32, i8*, ...) @fprintf(i32 %25, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i8* %20, i8* %22, i8* %24)
%27 = call i64 @checkaddr(i8* %22, i8* %24, %struct.addrinfo** %3)
%28 = icmp eq i64 %27, 0
br i1 %28, label %31, label %29
29: ; preds = %18
%30 = load i32, i32* @EXIT_FAILURE
br label %49
31: ; preds = %18
%32 = load %struct.addrinfo*, %struct.addrinfo** %3
%33 = call i64 @checksock(i32* %5, %struct.addrinfo* %32)
%34 = icmp eq i64 %33, 0
br i1 %34, label %37, label %35
35: ; preds = %31
%36 = load i32, i32* @EXIT_FAILURE
br label %49
37: ; preds = %31
%38 = call i64 @checkfile(i32* %4, i8* %20)
%39 = icmp eq i64 %38, 0
br i1 %39, label %42, label %40
40: ; preds = %37
%41 = load i32, i32* @EXIT_FAILURE
br label %49
42: ; preds = %37
%43 = load i32, i32* %4
%44 = load i32, i32* %5
%45 = call i64 @copyfile(i32 %43, i32 %44)
%46 = icmp eq i64 %45, 0
%47 = load i32, i32* @EXIT_FAILURE
%48 = select i1 %46, i32 %9, i32 %47
br label %49
49: ; preds = %42, %40, %35, %29
%50 = phi i32 [ %30, %29 ], [ %36, %35 ], [ %41, %40 ], [ %48, %42 ]
%51 = load %struct.addrinfo*, %struct.addrinfo** %3
%52 = icmp eq %struct.addrinfo* %51, null
br i1 %52, label %55, label %53
53: ; preds = %49
%54 = call i32 @freeaddrinfo(%struct.addrinfo* %51)
br label %55
55: ; preds = %53, %49
%56 = load i32, i32* %5
%57 = icmp sgt i32 %56, -1
br i1 %57, label %58, label %60
58: ; preds = %55
%59 = call i32 @close(i32 %56)
br label %60
60: ; preds = %58, %55
%61 = load i32, i32* %4
%62 = icmp sgt i32 %61, -1
br i1 %62, label %63, label %65
63: ; preds = %60
%64 = call i32 @close(i32 %61)
br label %65
65: ; preds = %60, %63, %13
%66 = phi i32 [ %17, %13 ], [ %50, %63 ], [ %50, %60 ]
ret i32 %66
}