#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 */
typedef int /*<<< orphan*/ uint16_t;
struct sockaddr_in {
  int /*<<< orphan*/ sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
struct in_addr {
  int dummy;
};
struct hostent {
  int /*<<< orphan*/ *h_addr_list;
};
typedef scalar_t__ ssize_t;
typedef int /*<<< orphan*/ sin;
typedef int /*<<< orphan*/ inaddr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ IPPROTO_TCP;
int /*<<< orphan*/ SOCK_STREAM;
int atoi(char *);
int /*<<< orphan*/ close(int);
int connect(int, struct sockaddr *, int);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/);
struct hostent *gethostbyname(char *);
scalar_t__ getline(char **, size_t *, int /*<<< orphan*/);
int /*<<< orphan*/ htons(int /*<<< orphan*/);
scalar_t__ inet_aton(char *, int /*<<< orphan*/ *);
char *inet_ntoa(struct in_addr);
int /*<<< orphan*/ memcpy(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ write(int, char *, size_t);
int main(int argc, char **argv) {
  char *ip = 0, *line = 0;
  int port, sock;
  struct hostent *he;
  struct in_addr inaddr;
  struct sockaddr_in sin;
  size_t line_buf_len = 0;
  ssize_t curr_line_len;
  if (argc != 3) {
    printf("Usage: ncc <host> <port>\n", stderr);
    return 1;
  }
  port = atoi(*(argv + 2));
  if (port < 1 || port > 65535) {
    fputs("Port is out of range.\n", stderr);
    return 1;
  }
  he = gethostbyname(*(argv + 1));
  if (!he) {
    fputs("nc: cannot resolve host\n", stderr);
    return 1;
  }
  memcpy((char *)&inaddr, *he->h_addr_list, sizeof(inaddr));
  ip = inet_ntoa(inaddr);
  sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  if (sock == -1) {
    perror("ncc");
    return 1;
  }
  memset(&sin, 0, sizeof(sin));
  sin.sin_family = AF_INET;
  sin.sin_port = htons((uint16_t)port);
  if (inet_aton(ip, &sin.sin_addr) == 0) {
    perror("ncc");
    close(sock);
    return 1;
  }
  if (connect(sock, (struct sockaddr *)&sin, sizeof(sin)) == -1) {
    perror("ncc");
    close(sock);
    return 1;
  }
  while ((curr_line_len = getline(&line, &line_buf_len, stdin)) > 0)
    write(sock, line, (size_t)curr_line_len);
  close(sock);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x50(%rbp)
cmpl   $0x3,-0x8(%rbp)
je     1249 <main+0x59>
mov    0x2d99(%rip),%rax
mov    (%rax),%esi
lea    0xdc8(%rip),%rdi
call   1060 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    141b <main+0x22b>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   10e0 <atoi@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x1,-0x24(%rbp)
jl     1270 <main+0x80>
cmpl   $0xffff,-0x24(%rbp)
jle    1291 <main+0xa1>
mov    0x2d51(%rip),%rax
mov    (%rax),%esi
lea    0xd9a(%rip),%rdi
call   1070 <fputs@plt>
movl   $0x1,-0x4(%rbp)
jmp    141b <main+0x22b>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10a0 <gethostbyname@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    12ce <main+0xde>
mov    0x2d14(%rip),%rax
mov    (%rax),%esi
lea    0xd74(%rip),%rdi
call   1070 <fputs@plt>
movl   $0x1,-0x4(%rbp)
jmp    141b <main+0x22b>
lea    -0x38(%rbp),%rdi
mov    -0x30(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%esi
mov    $0x4,%edx
call   10b0 <memcpy@plt>
mov    -0x38(%rbp),%edi
call   1040 <inet_ntoa@plt>
mov    %rax,-0x18(%rbp)
mov    0x2cf0(%rip),%rax
mov    (%rax),%edi
mov    0x2cbf(%rip),%rax
mov    (%rax),%esi
mov    0x2cce(%rip),%rax
mov    (%rax),%edx
call   1110 <socket@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0xffffffff,-0x28(%rbp)
jne    1336 <main+0x146>
lea    0xd25(%rip),%rdi
call   10d0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    141b <main+0x22b>
lea    -0x48(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1080 <memset@plt>
mov    0x2c9b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x40(%rbp)
mov    -0x24(%rbp),%edi
call   1050 <htons@plt>
mov    %eax,-0x44(%rbp)
mov    -0x18(%rbp),%rdi
lea    -0x48(%rbp),%rsi
call   10c0 <inet_aton@plt>
cmp    $0x0,%rax
jne    1394 <main+0x1a4>
lea    0xccf(%rip),%rdi
call   10d0 <perror@plt>
mov    -0x28(%rbp),%edi
call   1090 <close@plt>
movl   $0x1,-0x4(%rbp)
jmp    141b <main+0x22b>
mov    -0x28(%rbp),%edi
lea    -0x48(%rbp),%rsi
mov    $0xc,%edx
call   1100 <connect@plt>
cmp    $0xffffffff,%eax
jne    13ce <main+0x1de>
lea    0xc95(%rip),%rdi
call   10d0 <perror@plt>
mov    -0x28(%rbp),%edi
call   1090 <close@plt>
movl   $0x1,-0x4(%rbp)
jmp    141b <main+0x22b>
jmp    13d3 <main+0x1e3>
mov    0x2c06(%rip),%rax
mov    (%rax),%edx
lea    -0x20(%rbp),%rdi
lea    -0x50(%rbp),%rsi
call   10f0 <getline@plt>
mov    %rax,-0x58(%rbp)
cmp    $0x0,%rax
jle    140c <main+0x21c>
mov    -0x28(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x58(%rbp),%rdx
call   1030 <write@plt>
jmp    13d3 <main+0x1e3>
mov    -0x28(%rbp),%edi
call   1090 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x30,%rsp
movq   $0x0,0x10(%rsp)
movq   $0x0,0x18(%rsp)
cmp    $0x3,%edi
jne    1245 <main+0x55>
mov    %rsi,%rbx
mov    0x10(%rsi),%rdi
call   10e0 <atoi@plt>
mov    %eax,%r14d
lea    -0x10000(%r14),%eax
cmp    $0xffff0000,%eax
ja     125f <main+0x6f>
mov    0x2d91(%rip),%rax
mov    (%rax),%esi
lea    0xdda(%rip),%rdi
jmp    137f <main+0x18f>
mov    0x2d7c(%rip),%rax
mov    (%rax),%esi
lea    0xdab(%rip),%rdi
call   1060 <printf@plt>
jmp    1384 <main+0x194>
mov    0x8(%rbx),%rdi
call   10a0 <gethostbyname@plt>
test   %rax,%rax
je     136f <main+0x17f>
mov    (%rax),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdi
mov    $0x4,%edx
call   10b0 <memcpy@plt>
mov    0x8(%rsp),%edi
call   1040 <inet_ntoa@plt>
mov    %rax,%r15
mov    0x2d50(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2d1e(%rip),%rax
mov    (%rax),%esi
mov    0x2d2d(%rip),%rax
mov    (%rax),%edx
call   1110 <socket@plt>
cmp    $0xffffffff,%eax
je     13ac <main+0x1bc>
mov    %eax,%r12d
lea    0x20(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1080 <memset@plt>
mov    0x0(%rbp),%eax
mov    %eax,0x28(%rsp)
mov    %r14d,%edi
call   1050 <htons@plt>
mov    %eax,0x24(%rsp)
mov    %r15,%rdi
mov    %rbx,%rsi
call   10c0 <inet_aton@plt>
test   %rax,%rax
je     1396 <main+0x1a6>
lea    0x20(%rsp),%rsi
mov    %r12d,%edi
mov    $0xc,%edx
call   1100 <connect@plt>
cmp    $0xffffffff,%eax
je     1396 <main+0x1a6>
mov    0x2cc5(%rip),%r14
mov    (%r14),%edx
lea    0x10(%rsp),%rdi
lea    0x18(%rsp),%rsi
call   10f0 <getline@plt>
test   %rax,%rax
jle    1363 <main+0x173>
lea    0x10(%rsp),%rbx
lea    0x18(%rsp),%rbp
nopl   0x0(%rax)
mov    0x10(%rsp),%rsi
mov    %r12d,%edi
mov    %rax,%rdx
call   1030 <write@plt>
mov    (%r14),%edx
mov    %rbx,%rdi
mov    %rbp,%rsi
call   10f0 <getline@plt>
test   %rax,%rax
jg     1340 <main+0x150>
mov    %r12d,%edi
call   1090 <close@plt>
xor    %eax,%eax
jmp    1389 <main+0x199>
mov    0x2c52(%rip),%rax
mov    (%rax),%esi
lea    0xcb2(%rip),%rdi
call   1070 <fputs@plt>
mov    $0x1,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xcad(%rip),%rdi
call   10d0 <perror@plt>
mov    %r12d,%edi
call   1090 <close@plt>
jmp    1384 <main+0x194>
lea    0xc97(%rip),%rdi
call   10d0 <perror@plt>
jmp    1384 <main+0x194>

LLVM IR (-O0):
%struct.hostent = type { i32* }
%struct.in_addr = type { i32 }
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@.str = [26 x i8] c"Usage: ncc <host> <port>\0A\00"
@stderr = global i32 0
@.str.1 = [23 x i8] c"Port is out of range.\0A\00"
@.str.2 = [25 x i8] c"nc: cannot resolve host\0A\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@.str.3 = [4 x i8] c"ncc\00"
@stdin = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca %struct.hostent*
%11 = alloca %struct.in_addr
%12 = alloca %struct.sockaddr_in
%13 = alloca i64
%14 = alloca i64
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 i64 0, i64* %13
%15 = load i32, i32* %4
%16 = icmp ne i32 %15, 3
br i1 %16, label %17, label %20
17: ; preds = %2
%18 = load i32, i32* @stderr
%19 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i32 %18)
store i32 1, i32* %3
br label %98
20: ; preds = %2
%21 = load i8**, i8*** %5
%22 = getelementptr inbounds i8*, i8** %21, i64 2
%23 = load i8*, i8** %22
%24 = call i32 @atoi(i8* %23)
store i32 %24, i32* %8
%25 = load i32, i32* %8
%26 = icmp slt i32 %25, 1
br i1 %26, label %30, label %27
27: ; preds = %20
%28 = load i32, i32* %8
%29 = icmp sgt i32 %28, 65535
br i1 %29, label %30, label %33
30: ; preds = %27, %20
%31 = load i32, i32* @stderr
%32 = call i32 @fputs(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i32 %31)
store i32 1, i32* %3
br label %98
33: ; preds = %27
%34 = load i8**, i8*** %5
%35 = getelementptr inbounds i8*, i8** %34, i64 1
%36 = load i8*, i8** %35
%37 = call %struct.hostent* @gethostbyname(i8* %36)
store %struct.hostent* %37, %struct.hostent** %10
%38 = load %struct.hostent*, %struct.hostent** %10
%39 = icmp ne %struct.hostent* %38, null
br i1 %39, label %43, label %40
40: ; preds = %33
%41 = load i32, i32* @stderr
%42 = call i32 @fputs(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i32 %41)
store i32 1, i32* %3
br label %98
43: ; preds = %33
%44 = bitcast %struct.in_addr* %11 to i8*
%45 = load %struct.hostent*, %struct.hostent** %10
%46 = getelementptr inbounds %struct.hostent, %struct.hostent* %45, i32 0, i32 0
%47 = load i32*, i32** %46
%48 = load i32, i32* %47
%49 = call i32 @memcpy(i8* %44, i32 %48, i32 4)
%50 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %11, i32 0, i32 0
%51 = load i32, i32* %50
%52 = call i8* @inet_ntoa(i32 %51)
store i8* %52, i8** %6
%53 = load i32, i32* @AF_INET
%54 = load i32, i32* @SOCK_STREAM
%55 = load i32, i32* @IPPROTO_TCP
%56 = call i32 @socket(i32 %53, i32 %54, i32 %55)
store i32 %56, i32* %9
%57 = load i32, i32* %9
%58 = icmp eq i32 %57, -1
br i1 %58, label %59, label %61
59: ; preds = %43
%60 = call i32 @perror(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %3
br label %98
61: ; preds = %43
%62 = call i32 @memset(%struct.sockaddr_in* %12, i32 0, i32 12)
%63 = load i32, i32* @AF_INET
%64 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %12, i32 0, i32 2
store i32 %63, i32* %64
%65 = load i32, i32* %8
%66 = call i32 @htons(i32 %65)
%67 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %12, i32 0, i32 1
store i32 %66, i32* %67
%68 = load i8*, i8** %6
%69 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %12, i32 0, i32 0
%70 = call i64 @inet_aton(i8* %68, i32* %69)
%71 = icmp eq i64 %70, 0
br i1 %71, label %72, label %76
72: ; preds = %61
%73 = call i32 @perror(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0))
%74 = load i32, i32* %9
%75 = call i32 @close(i32 %74)
store i32 1, i32* %3
br label %98
76: ; preds = %61
%77 = load i32, i32* %9
%78 = bitcast %struct.sockaddr_in* %12 to %struct.sockaddr*
%79 = call i32 @connect(i32 %77, %struct.sockaddr* %78, i32 12)
%80 = icmp eq i32 %79, -1
br i1 %80, label %81, label %85
81: ; preds = %76
%82 = call i32 @perror(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0))
%83 = load i32, i32* %9
%84 = call i32 @close(i32 %83)
store i32 1, i32* %3
br label %98
85: ; preds = %76
br label %86
86: ; preds = %90, %85
%87 = load i32, i32* @stdin
%88 = call i64 @getline(i8** %7, i64* %13, i32 %87)
store i64 %88, i64* %14
%89 = icmp sgt i64 %88, 0
br i1 %89, label %90, label %95
90: ; preds = %86
%91 = load i32, i32* %9
%92 = load i8*, i8** %7
%93 = load i64, i64* %14
%94 = call i32 @write(i32 %91, i8* %92, i64 %93)
br label %86
95: ; preds = %86
%96 = load i32, i32* %9
%97 = call i32 @close(i32 %96)
store i32 0, i32* %3
br label %98
98: ; preds = %95, %81, %72, %59, %40, %30, %17
%99 = load i32, i32* %3
ret i32 %99
}

LLVM IR (-O2):
%struct.in_addr = type { i32 }
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.hostent = type { i32* }
%struct.sockaddr = type { i32 }
@.str = [26 x i8] c"Usage: ncc <host> <port>\0A\00"
@stderr = global i32 0
@.str.1 = [23 x i8] c"Port is out of range.\0A\00"
@.str.2 = [25 x i8] c"nc: cannot resolve host\0A\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@.str.3 = [4 x i8] c"ncc\00"
@stdin = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i8*
%4 = alloca %struct.in_addr
%5 = alloca %struct.sockaddr_in
%6 = alloca i64
%7 = bitcast i8** %3 to i8*
store i8* null, i8** %3
%8 = bitcast %struct.in_addr* %4 to i8*
%9 = bitcast %struct.sockaddr_in* %5 to i8*
%10 = bitcast i64* %6 to i8*
store i64 0, i64* %6
%11 = icmp eq i32 %0, 3
br i1 %11, label %15, label %12
12: ; preds = %2
%13 = load i32, i32* @stderr
%14 = tail call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0), i32 %13)
br label %79
15: ; preds = %2
%16 = getelementptr inbounds i8*, i8** %1, i64 2
%17 = load i8*, i8** %16
%18 = tail call i32 @atoi(i8* %17)
%19 = add i32 %18, -65536
%20 = icmp ult i32 %19, -65535
br i1 %20, label %21, label %24
21: ; preds = %15
%22 = load i32, i32* @stderr
%23 = tail call i32 @fputs(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i32 %22)
br label %79
24: ; preds = %15
%25 = getelementptr inbounds i8*, i8** %1, i64 1
%26 = load i8*, i8** %25
%27 = tail call %struct.hostent* @gethostbyname(i8* %26)
%28 = icmp eq %struct.hostent* %27, null
br i1 %28, label %29, label %32
29: ; preds = %24
%30 = load i32, i32* @stderr
%31 = tail call i32 @fputs(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i32 %30)
br label %79
32: ; preds = %24
%33 = getelementptr inbounds %struct.hostent, %struct.hostent* %27, i64 0, i32 0
%34 = load i32*, i32** %33
%35 = load i32, i32* %34
%36 = call i32 @memcpy(i8* %8, i32 %35, i32 4)
%37 = getelementptr inbounds %struct.in_addr, %struct.in_addr* %4, i64 0, i32 0
%38 = load i32, i32* %37
%39 = call i8* @inet_ntoa(i32 %38)
%40 = load i32, i32* @AF_INET
%41 = load i32, i32* @SOCK_STREAM
%42 = load i32, i32* @IPPROTO_TCP
%43 = call i32 @socket(i32 %40, i32 %41, i32 %42)
%44 = icmp eq i32 %43, -1
br i1 %44, label %45, label %47
45: ; preds = %32
%46 = call i32 @perror(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0))
br label %79
47: ; preds = %32
%48 = call i32 @memset(%struct.sockaddr_in* %5, i32 0, i32 12)
%49 = load i32, i32* @AF_INET
%50 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 2
store i32 %49, i32* %50
%51 = call i32 @htons(i32 %18)
%52 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 1
store i32 %51, i32* %52
%53 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 0
%54 = call i64 @inet_aton(i8* %39, i32* %53)
%55 = icmp eq i64 %54, 0
br i1 %55, label %56, label %59
56: ; preds = %47
%57 = call i32 @perror(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0))
%58 = call i32 @close(i32 %43)
br label %79
59: ; preds = %47
%60 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%61 = call i32 @connect(i32 %43, %struct.sockaddr* %60, i32 12)
%62 = icmp eq i32 %61, -1
br i1 %62, label %67, label %63
63: ; preds = %59
%64 = load i32, i32* @stdin
%65 = call i64 @getline(i8** %3, i64* %6, i32 %64)
%66 = icmp sgt i64 %65, 0
br i1 %66, label %70, label %77
67: ; preds = %59
%68 = call i32 @perror(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0))
%69 = call i32 @close(i32 %43)
br label %79
70: ; preds = %63, %70
%71 = phi i64 [ %75, %70 ], [ %65, %63 ]
%72 = load i8*, i8** %3
%73 = call i32 @write(i32 %43, i8* %72, i64 %71)
%74 = load i32, i32* @stdin
%75 = call i64 @getline(i8** %3, i64* %6, i32 %74)
%76 = icmp sgt i64 %75, 0
br i1 %76, label %70, label %77
77: ; preds = %70, %63
%78 = call i32 @close(i32 %43)
br label %79
79: ; preds = %77, %67, %56, %45, %29, %21, %12
%80 = phi i32 [ 1, %12 ], [ 1, %21 ], [ 1, %45 ], [ 1, %56 ], [ 1, %67 ], [ 0, %77 ], [ 1, %29 ]
ret i32 %80
}