#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 sockaddr_in {
  int /*<<< orphan*/ sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ servaddr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int BUFSIZ;
scalar_t__ EOF;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ err_quit(char *, char *);
int /*<<< orphan*/ err_sys(char *, ...);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fputs(char *, int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
scalar_t__ inet_pton(int /*<<< orphan*/, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, int);
int read(int, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stdout;
int main(int argc, char *argv[]) {
  int sockfd, n;
  char recvline[BUFSIZ];
  struct sockaddr_in servaddr;
  int counter;
  if (argc != 2) {
    err_quit("Usage: %s IP-address", argv[0]);
  }
  if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
    err_sys("socket AF_INET SOCK_STREAM error");
  memset(&servaddr, 0, sizeof(servaddr));
  servaddr.sin_family = AF_INET;
  servaddr.sin_port = htons(13);
  if (inet_pton(AF_INET, argv[1], &servaddr.sin_addr) <= 0) {
    err_sys("inet_pton error for: %s\n", argv[1]);
  }
  if (connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0)
    err_sys("connect error");
  counter = 0;
  while ((n = read(sockfd, recvline, BUFSIZ)) > 0) {
    ++counter;
    recvline[n] = '\0';
    if (fputs(recvline, stdout) == EOF)
      err_sys("fputs error");
  }
  if (n < 0)
    err_sys("read error");
  printf("counter = %d\n", counter);
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2dfb(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x48(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x28(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     120f <main+0x5f>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xdf6(%rip),%rdi
call   1080 <err_quit@plt>
mov    0x2dd2(%rip),%rax
mov    (%rax),%edi
mov    0x2db1(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    1242 <main+0x92>
lea    0xdda(%rip),%rdi
mov    $0x0,%al
call   1030 <err_sys@plt>
lea    -0x38(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1070 <memset@plt>
mov    0x2d8f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    $0xd,%edi
call   1040 <htons@plt>
mov    %eax,-0x34(%rbp)
mov    0x2d76(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    -0x38(%rbp),%rdx
call   10a0 <inet_pton@plt>
cmp    $0x0,%rax
jg     12a5 <main+0xf5>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xd98(%rip),%rdi
mov    $0x0,%al
call   1030 <err_sys@plt>
mov    -0x14(%rbp),%edi
lea    -0x38(%rbp),%rsi
mov    $0xc,%edx
call   10c0 <connect@plt>
cmp    $0x0,%rax
jge    12ce <main+0x11e>
lea    0xd88(%rip),%rdi
mov    $0x0,%al
call   1030 <err_sys@plt>
movl   $0x0,-0x3c(%rbp)
mov    -0x48(%rbp),%rsi
mov    -0x14(%rbp),%edi
mov    0x2ce5(%rip),%rax
mov    (%rax),%edx
call   1090 <read@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jle    133c <main+0x18c>
mov    -0x48(%rbp),%rdi
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
movslq -0x18(%rbp),%rax
movb   $0x0,(%rdi,%rax,1)
mov    0x2cae(%rip),%rax
mov    (%rax),%esi
call   1060 <fputs@plt>
mov    0x2cc0(%rip),%rcx
cmp    (%rcx),%rax
jne    1337 <main+0x187>
lea    0xd2d(%rip),%rdi
mov    $0x0,%al
call   1030 <err_sys@plt>
jmp    12d5 <main+0x125>
cmpl   $0x0,-0x18(%rbp)
jge    1354 <main+0x1a4>
lea    0xd1c(%rip),%rdi
mov    $0x0,%al
call   1030 <err_sys@plt>
mov    -0x3c(%rbp),%esi
lea    0xd16(%rip),%rdi
call   1050 <printf@plt>
xor    %edi,%edi
call   10b0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r15
mov    0x2dfd(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%r12
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r12
mov    %r12,%rsp
cmp    $0x2,%edi
je     11f2 <main+0x42>
mov    (%r15),%rsi
lea    0xe13(%rip),%rdi
call   1080 <err_quit@plt>
mov    0x2def(%rip),%r13
mov    0x0(%r13),%edi
mov    0x2dcc(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
mov    %eax,%r14d
test   %eax,%eax
jns    1222 <main+0x72>
lea    0xdfa(%rip),%rdi
xor    %eax,%eax
call   1030 <err_sys@plt>
lea    -0x38(%rbp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1070 <memset@plt>
mov    0x0(%r13),%eax
mov    %eax,-0x30(%rbp)
mov    $0xd,%edi
call   1040 <htons@plt>
mov    %eax,-0x34(%rbp)
mov    0x0(%r13),%edi
mov    0x8(%r15),%rsi
mov    %rbx,%rdx
call   10a0 <inet_pton@plt>
test   %rax,%rax
jg     1270 <main+0xc0>
mov    0x8(%r15),%rsi
lea    0xdcd(%rip),%rdi
xor    %eax,%eax
call   1030 <err_sys@plt>
lea    -0x38(%rbp),%rsi
mov    %r14d,%edi
mov    $0xc,%edx
call   10c0 <connect@plt>
test   %rax,%rax
jns    1294 <main+0xe4>
lea    0xdc2(%rip),%rdi
xor    %eax,%eax
call   1030 <err_sys@plt>
mov    0x2d2d(%rip),%rax
mov    (%rax),%edx
mov    %r14d,%edi
mov    %r12,%rsi
call   1090 <read@plt>
xor    %ebx,%ebx
test   %eax,%eax
jle    1303 <main+0x153>
mov    0x2d0b(%rip),%r13
mov    0x2d24(%rip),%r15
xor    %ebx,%ebx
jmp    12db <main+0x12b>
mov    0x2d01(%rip),%rax
mov    (%rax),%edx
mov    %r14d,%edi
mov    %r12,%rsi
call   1090 <read@plt>
add    $0x1,%ebx
test   %eax,%eax
jle    1303 <main+0x153>
mov    %eax,%eax
movb   $0x0,(%r12,%rax,1)
mov    0x0(%r13),%esi
mov    %r12,%rdi
call   1060 <fputs@plt>
cmp    (%r15),%rax
jne    12c0 <main+0x110>
lea    0xd63(%rip),%rdi
xor    %eax,%eax
call   1030 <err_sys@plt>
jmp    12c0 <main+0x110>
test   %eax,%eax
jns    1315 <main+0x165>
lea    0xd5b(%rip),%rdi
xor    %eax,%eax
call   1030 <err_sys@plt>
lea    0xd58(%rip),%rdi
mov    %ebx,%esi
call   1050 <printf@plt>
xor    %edi,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@BUFSIZ = global i32 0
@.str = [21 x i8] c"Usage: %s IP-address\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [33 x i8] c"socket AF_INET SOCK_STREAM error\00"
@.str.2 = [25 x i8] c"inet_pton error for: %s\0A\00"
@.str.3 = [14 x i8] c"connect error\00"
@stdout = global i32 0
@EOF = global i64 0
@.str.4 = [12 x i8] c"fputs error\00"
@.str.5 = [11 x i8] c"read error\00"
@.str.6 = [14 x i8] c"counter = %d\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 i8*
%9 = alloca i64
%10 = alloca %struct.sockaddr_in
%11 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = load i32, i32* @BUFSIZ
%13 = zext i32 %12 to i64
%14 = call i8* @llvm.stacksave()
store i8* %14, i8** %8
%15 = alloca i8, i64 %13
store i64 %13, i64* %9
%16 = load i32, i32* %4
%17 = icmp ne i32 %16, 2
br i1 %17, label %18, label %23
18: ; preds = %2
%19 = load i8**, i8*** %5
%20 = getelementptr inbounds i8*, i8** %19, i64 0
%21 = load i8*, i8** %20
%22 = call i32 @err_quit(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %21)
br label %23
23: ; preds = %18, %2
%24 = load i32, i32* @AF_INET
%25 = load i32, i32* @SOCK_STREAM
%26 = call i32 @socket(i32 %24, i32 %25, i32 0)
store i32 %26, i32* %6
%27 = icmp slt i32 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %23
%29 = call i32 (i8*, ...) @err_sys(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0))
br label %30
30: ; preds = %28, %23
%31 = call i32 @memset(%struct.sockaddr_in* %10, i32 0, i32 12)
%32 = load i32, i32* @AF_INET
%33 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 2
store i32 %32, i32* %33
%34 = call i32 @htons(i32 13)
%35 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 1
store i32 %34, i32* %35
%36 = load i32, i32* @AF_INET
%37 = load i8**, i8*** %5
%38 = getelementptr inbounds i8*, i8** %37, i64 1
%39 = load i8*, i8** %38
%40 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 0
%41 = call i64 @inet_pton(i32 %36, i8* %39, i32* %40)
%42 = icmp sle i64 %41, 0
br i1 %42, label %43, label %48
43: ; preds = %30
%44 = load i8**, i8*** %5
%45 = getelementptr inbounds i8*, i8** %44, i64 1
%46 = load i8*, i8** %45
%47 = call i32 (i8*, ...) @err_sys(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i8* %46)
br label %48
48: ; preds = %43, %30
%49 = load i32, i32* %6
%50 = bitcast %struct.sockaddr_in* %10 to %struct.sockaddr*
%51 = call i64 @connect(i32 %49, %struct.sockaddr* %50, i32 12)
%52 = icmp slt i64 %51, 0
br i1 %52, label %53, label %55
53: ; preds = %48
%54 = call i32 (i8*, ...) @err_sys(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
br label %55
55: ; preds = %53, %48
store i32 0, i32* %11
br label %56
56: ; preds = %73, %55
%57 = load i32, i32* %6
%58 = load i32, i32* @BUFSIZ
%59 = call i32 @read(i32 %57, i8* %15, i32 %58)
store i32 %59, i32* %7
%60 = icmp sgt i32 %59, 0
br i1 %60, label %61, label %74
61: ; preds = %56
%62 = load i32, i32* %11
%63 = add nsw i32 %62, 1
store i32 %63, i32* %11
%64 = load i32, i32* %7
%65 = sext i32 %64 to i64
%66 = getelementptr inbounds i8, i8* %15, i64 %65
store i8 0, i8* %66
%67 = load i32, i32* @stdout
%68 = call i64 @fputs(i8* %15, i32 %67)
%69 = load i64, i64* @EOF
%70 = icmp eq i64 %68, %69
br i1 %70, label %71, label %73
71: ; preds = %61
%72 = call i32 (i8*, ...) @err_sys(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0))
br label %73
73: ; preds = %71, %61
br label %56
74: ; preds = %56
%75 = load i32, i32* %7
%76 = icmp slt i32 %75, 0
br i1 %76, label %77, label %79
77: ; preds = %74
%78 = call i32 (i8*, ...) @err_sys(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.5, i64 0, i64 0))
br label %79
79: ; preds = %77, %74
%80 = load i32, i32* %11
%81 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.6, i64 0, i64 0), i32 %80)
%82 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@BUFSIZ = global i32 0
@.str = [21 x i8] c"Usage: %s IP-address\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [33 x i8] c"socket AF_INET SOCK_STREAM error\00"
@.str.2 = [25 x i8] c"inet_pton error for: %s\0A\00"
@.str.3 = [14 x i8] c"connect error\00"
@stdout = global i32 0
@EOF = global i64 0
@.str.4 = [12 x i8] c"fputs error\00"
@.str.5 = [11 x i8] c"read error\00"
@.str.6 = [14 x i8] c"counter = %d\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.sockaddr_in
%4 = load i32, i32* @BUFSIZ
%5 = zext i32 %4 to i64
%6 = alloca i8, i64 %5
%7 = bitcast %struct.sockaddr_in* %3 to i8*
%8 = icmp eq i32 %0, 2
br i1 %8, label %12, label %9
9: ; preds = %2
%10 = load i8*, i8** %1
%11 = tail call i32 @err_quit(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %10)
br label %12
12: ; preds = %9, %2
%13 = load i32, i32* @AF_INET
%14 = load i32, i32* @SOCK_STREAM
%15 = tail call i32 @socket(i32 %13, i32 %14, i32 0)
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %12
%18 = tail call i32 (i8*, ...) @err_sys(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0))
br label %19
19: ; preds = %17, %12
%20 = call i32 @memset(%struct.sockaddr_in* %3, i32 0, i32 12)
%21 = load i32, i32* @AF_INET
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %21, i32* %22
%23 = call i32 @htons(i32 13)
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %23, i32* %24
%25 = load i32, i32* @AF_INET
%26 = getelementptr inbounds i8*, i8** %1, i64 1
%27 = load i8*, i8** %26
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
%29 = call i64 @inet_pton(i32 %25, i8* %27, i32* %28)
%30 = icmp slt i64 %29, 1
br i1 %30, label %31, label %34
31: ; preds = %19
%32 = load i8*, i8** %26
%33 = call i32 (i8*, ...) @err_sys(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i8* %32)
br label %34
34: ; preds = %31, %19
%35 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%36 = call i64 @connect(i32 %15, %struct.sockaddr* %35, i32 12)
%37 = icmp slt i64 %36, 0
br i1 %37, label %38, label %40
38: ; preds = %34
%39 = call i32 (i8*, ...) @err_sys(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
br label %40
40: ; preds = %38, %34
%41 = load i32, i32* @BUFSIZ
%42 = call i32 @read(i32 %15, i8* %6, i32 %41)
%43 = icmp sgt i32 %42, 0
br i1 %43, label %44, label %60
44: ; preds = %40, %56
%45 = phi i32 [ %58, %56 ], [ %42, %40 ]
%46 = phi i32 [ %47, %56 ], [ 0, %40 ]
%47 = add nuw nsw i32 %46, 1
%48 = zext i32 %45 to i64
%49 = getelementptr inbounds i8, i8* %6, i64 %48
store i8 0, i8* %49
%50 = load i32, i32* @stdout
%51 = call i64 @fputs(i8* %6, i32 %50)
%52 = load i64, i64* @EOF
%53 = icmp eq i64 %51, %52
br i1 %53, label %54, label %56
54: ; preds = %44
%55 = call i32 (i8*, ...) @err_sys(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0))
br label %56
56: ; preds = %54, %44
%57 = load i32, i32* @BUFSIZ
%58 = call i32 @read(i32 %15, i8* %6, i32 %57)
%59 = icmp sgt i32 %58, 0
br i1 %59, label %44, label %60
60: ; preds = %56, %40
%61 = phi i32 [ 0, %40 ], [ %47, %56 ]
%62 = phi i32 [ %42, %40 ], [ %58, %56 ]
%63 = icmp slt i32 %62, 0
br i1 %63, label %64, label %66
64: ; preds = %60
%65 = call i32 (i8*, ...) @err_sys(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.5, i64 0, i64 0))
br label %66
66: ; preds = %64, %60
%67 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.6, i64 0, i64 0), i32 %61)
%68 = call i32 @exit(i32 0)
unreachable
}