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

/* Variables and functions */
int AF_INET;
int BUFSIZ;
scalar_t__ EOF;
int SOCK_STREAM;
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ err_quit(char *, char *);
int /*<<< orphan*/ err_ret(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, 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, int, int);
int /*<<< orphan*/ stdout;
int main(int argc, char *argv[]) {
  int sockfd, n;
  char recvline[BUFSIZ];
  struct sockaddr_in servaddr;
  if (argc != 2) {
    err_quit("Usage: %s IP-address", argv[0]);
  }
  printf("AF_INET: %d\n", AF_INET);
  if ((sockfd = socket(9999, SOCK_STREAM, 0)) < 0)
    err_ret("socket 9999 error");
  if ((sockfd = socket(AF_INET, SOCK_STREAM, 9999)) < 0)
    err_ret("socket third-arg 999 error");
  if ((sockfd = socket(AF_INET, 9999, 0)) < 0)
    err_sys("socket second-arg 999 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");
  while ((n = read(sockfd, recvline, BUFSIZ)) > 0) {
    recvline[n] = '\0';
    if (fputs(recvline, stdout) == EOF)
      err_sys("fputs error");
  }
  if (n < 0)
    err_sys("read error");
  exit(0);
}


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)
mov    0x2deb(%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,-0x40(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x28(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     121f <main+0x5f>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xde6(%rip),%rdi
call   1080 <err_quit@plt>
mov    0x2dc2(%rip),%rax
mov    (%rax),%esi
lea    0xde6(%rip),%rdi
call   1050 <printf@plt>
mov    0x2d95(%rip),%rax
mov    (%rax),%esi
mov    $0x270f,%edi
xor    %edx,%edx
call   10e0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    1261 <main+0xa1>
lea    0xdc6(%rip),%rdi
call   10b0 <err_ret@plt>
mov    0x2d80(%rip),%rax
mov    (%rax),%edi
mov    0x2d5f(%rip),%rax
mov    (%rax),%esi
mov    $0x270f,%edx
call   10e0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    1295 <main+0xd5>
lea    0xda4(%rip),%rdi
call   10b0 <err_ret@plt>
mov    0x2d4c(%rip),%rax
mov    (%rax),%edi
mov    $0x270f,%esi
xor    %edx,%edx
call   10e0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    12c4 <main+0x104>
lea    0xd92(%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    0x2d0d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
mov    $0xd,%edi
call   1040 <htons@plt>
mov    %eax,-0x30(%rbp)
mov    0x2cf4(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    -0x38(%rbp),%rdx
add    $0x4,%rdx
call   10a0 <inet_pton@plt>
cmp    $0x0,%rax
jg     132b <main+0x16b>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xd47(%rip),%rdi
mov    $0x0,%al
call   1030 <err_sys@plt>
mov    -0x14(%rbp),%edi
lea    -0x38(%rbp),%rsi
mov    $0xc,%edx
call   10d0 <connect@plt>
cmp    $0x0,%rax
jge    1354 <main+0x194>
lea    0xd37(%rip),%rdi
mov    $0x0,%al
call   1030 <err_sys@plt>
jmp    1359 <main+0x199>
mov    -0x40(%rbp),%rsi
mov    -0x14(%rbp),%edi
mov    0x2c61(%rip),%rax
mov    (%rax),%edx
call   1090 <read@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jle    13b7 <main+0x1f7>
mov    -0x40(%rbp),%rdi
movslq -0x18(%rbp),%rax
movb   $0x0,(%rdi,%rax,1)
mov    0x2c33(%rip),%rax
mov    (%rax),%esi
call   1060 <fputs@plt>
mov    0x2c45(%rip),%rcx
cmp    (%rcx),%rax
jne    13b2 <main+0x1f2>
lea    0xce7(%rip),%rdi
mov    $0x0,%al
call   1030 <err_sys@plt>
jmp    1359 <main+0x199>
cmpl   $0x0,-0x18(%rbp)
jge    13cf <main+0x20f>
lea    0xcd6(%rip),%rdi
mov    $0x0,%al
call   1030 <err_sys@plt>
xor    %edi,%edi
call   10c0 <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    0x2ded(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
cmp    $0x2,%edi
je     1202 <main+0x42>
mov    (%r15),%rsi
lea    0xe03(%rip),%rdi
call   1080 <err_quit@plt>
mov    0x2ddf(%rip),%r13
mov    0x0(%r13),%esi
lea    0xe01(%rip),%rdi
call   1050 <printf@plt>
mov    0x2db0(%rip),%r14
mov    (%r14),%esi
mov    $0x270f,%edi
xor    %edx,%edx
call   10e0 <socket@plt>
test   %eax,%eax
jns    123f <main+0x7f>
lea    0xde8(%rip),%rdi
call   10b0 <err_ret@plt>
mov    0x0(%r13),%edi
mov    (%r14),%esi
mov    $0x270f,%edx
call   10e0 <socket@plt>
test   %eax,%eax
jns    1260 <main+0xa0>
lea    0xdd9(%rip),%rdi
call   10b0 <err_ret@plt>
mov    0x0(%r13),%edi
mov    $0x270f,%esi
xor    %edx,%edx
call   10e0 <socket@plt>
mov    %eax,%r14d
test   %eax,%eax
jns    1285 <main+0xc5>
lea    0xdd1(%rip),%rdi
xor    %eax,%eax
call   1030 <err_sys@plt>
lea    -0x38(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1070 <memset@plt>
mov    0x0(%r13),%eax
mov    %eax,-0x38(%rbp)
mov    $0xd,%edi
call   1040 <htons@plt>
mov    %eax,-0x30(%rbp)
mov    0x0(%r13),%edi
mov    0x8(%r15),%rsi
lea    -0x34(%rbp),%rdx
call   10a0 <inet_pton@plt>
test   %rax,%rax
jg     12d1 <main+0x111>
mov    0x8(%r15),%rsi
lea    0xda1(%rip),%rdi
xor    %eax,%eax
call   1030 <err_sys@plt>
lea    -0x38(%rbp),%rsi
mov    %r14d,%edi
mov    $0xc,%edx
call   10d0 <connect@plt>
test   %rax,%rax
jns    12f5 <main+0x135>
lea    0xd96(%rip),%rdi
xor    %eax,%eax
call   1030 <err_sys@plt>
mov    0x2ccc(%rip),%rax
mov    (%rax),%edx
mov    %r14d,%edi
mov    %rbx,%rsi
call   1090 <read@plt>
test   %eax,%eax
jle    136c <main+0x1ac>
mov    0x2cac(%rip),%r13
mov    0x2cc5(%rip),%r12
lea    0xd70(%rip),%r15
jmp    1348 <main+0x188>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    0x2c91(%rip),%rax
mov    (%rax),%edx
mov    %r14d,%edi
mov    %rbx,%rsi
call   1090 <read@plt>
test   %eax,%eax
jle    136c <main+0x1ac>
mov    %eax,%eax
movb   $0x0,(%rbx,%rax,1)
mov    0x0(%r13),%esi
mov    %rbx,%rdi
call   1060 <fputs@plt>
cmp    (%r12),%rax
jne    1330 <main+0x170>
mov    %r15,%rdi
xor    %eax,%eax
call   1030 <err_sys@plt>
jmp    1330 <main+0x170>
test   %eax,%eax
jns    137e <main+0x1be>
lea    0xd27(%rip),%rdi
xor    %eax,%eax
call   1030 <err_sys@plt>
xor    %edi,%edi
call   10c0 <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"
@.str.1 = [13 x i8] c"AF_INET: %d\0A\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.2 = [18 x i8] c"socket 9999 error\00"
@.str.3 = [27 x i8] c"socket third-arg 999 error\00"
@.str.4 = [28 x i8] c"socket second-arg 999 error\00"
@.str.5 = [25 x i8] c"inet_pton error for: %s\0A\00"
@.str.6 = [14 x i8] c"connect error\00"
@stdout = global i32 0
@EOF = global i64 0
@.str.7 = [12 x i8] c"fputs error\00"
@.str.8 = [11 x i8] c"read error\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
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* @BUFSIZ
%12 = zext i32 %11 to i64
%13 = call i8* @llvm.stacksave()
store i8* %13, i8** %8
%14 = alloca i8, i64 %12
store i64 %12, i64* %9
%15 = load i32, i32* %4
%16 = icmp ne i32 %15, 2
br i1 %16, label %17, label %22
17: ; preds = %2
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 0
%20 = load i8*, i8** %19
%21 = call i32 @err_quit(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i8* %20)
br label %22
22: ; preds = %17, %2
%23 = load i32, i32* @AF_INET
%24 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i32 %23)
%25 = load i32, i32* @SOCK_STREAM
%26 = call i32 @socket(i32 9999, i32 %25, i32 0)
store i32 %26, i32* %6
%27 = icmp slt i32 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %22
%29 = call i32 @err_ret(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
br label %30
30: ; preds = %28, %22
%31 = load i32, i32* @AF_INET
%32 = load i32, i32* @SOCK_STREAM
%33 = call i32 @socket(i32 %31, i32 %32, i32 9999)
store i32 %33, i32* %6
%34 = icmp slt i32 %33, 0
br i1 %34, label %35, label %37
35: ; preds = %30
%36 = call i32 @err_ret(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0))
br label %37
37: ; preds = %35, %30
%38 = load i32, i32* @AF_INET
%39 = call i32 @socket(i32 %38, i32 9999, i32 0)
store i32 %39, i32* %6
%40 = icmp slt i32 %39, 0
br i1 %40, label %41, label %43
41: ; preds = %37
%42 = call i32 (i8*, ...) @err_sys(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0))
br label %43
43: ; preds = %41, %37
%44 = call i32 @memset(%struct.sockaddr_in* %10, i32 0, i32 12)
%45 = load i32, i32* @AF_INET
%46 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 0
store i32 %45, i32* %46
%47 = call i32 @htons(i32 13)
%48 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 2
store i32 %47, i32* %48
%49 = load i32, i32* @AF_INET
%50 = load i8**, i8*** %5
%51 = getelementptr inbounds i8*, i8** %50, i64 1
%52 = load i8*, i8** %51
%53 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 1
%54 = call i64 @inet_pton(i32 %49, i8* %52, i32* %53)
%55 = icmp sle i64 %54, 0
br i1 %55, label %56, label %61
56: ; preds = %43
%57 = load i8**, i8*** %5
%58 = getelementptr inbounds i8*, i8** %57, i64 1
%59 = load i8*, i8** %58
%60 = call i32 (i8*, ...) @err_sys(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0), i8* %59)
br label %61
61: ; preds = %56, %43
%62 = load i32, i32* %6
%63 = bitcast %struct.sockaddr_in* %10 to %struct.sockaddr*
%64 = call i64 @connect(i32 %62, %struct.sockaddr* %63, i32 12)
%65 = icmp slt i64 %64, 0
br i1 %65, label %66, label %68
66: ; preds = %61
%67 = call i32 (i8*, ...) @err_sys(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.6, i64 0, i64 0))
br label %68
68: ; preds = %66, %61
br label %69
69: ; preds = %84, %68
%70 = load i32, i32* %6
%71 = load i32, i32* @BUFSIZ
%72 = call i32 @read(i32 %70, i8* %14, i32 %71)
store i32 %72, i32* %7
%73 = icmp sgt i32 %72, 0
br i1 %73, label %74, label %85
74: ; preds = %69
%75 = load i32, i32* %7
%76 = sext i32 %75 to i64
%77 = getelementptr inbounds i8, i8* %14, i64 %76
store i8 0, i8* %77
%78 = load i32, i32* @stdout
%79 = call i64 @fputs(i8* %14, i32 %78)
%80 = load i64, i64* @EOF
%81 = icmp eq i64 %79, %80
br i1 %81, label %82, label %84
82: ; preds = %74
%83 = call i32 (i8*, ...) @err_sys(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.7, i64 0, i64 0))
br label %84
84: ; preds = %82, %74
br label %69
85: ; preds = %69
%86 = load i32, i32* %7
%87 = icmp slt i32 %86, 0
br i1 %87, label %88, label %90
88: ; preds = %85
%89 = call i32 (i8*, ...) @err_sys(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.8, i64 0, i64 0))
br label %90
90: ; preds = %88, %85
%91 = 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"
@.str.1 = [13 x i8] c"AF_INET: %d\0A\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.2 = [18 x i8] c"socket 9999 error\00"
@.str.3 = [27 x i8] c"socket third-arg 999 error\00"
@.str.4 = [28 x i8] c"socket second-arg 999 error\00"
@.str.5 = [25 x i8] c"inet_pton error for: %s\0A\00"
@.str.6 = [14 x i8] c"connect error\00"
@stdout = global i32 0
@EOF = global i64 0
@.str.7 = [12 x i8] c"fputs error\00"
@.str.8 = [11 x i8] c"read error\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 = tail call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i32 %13)
%15 = load i32, i32* @SOCK_STREAM
%16 = tail call i32 @socket(i32 9999, i32 %15, i32 0)
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %12
%19 = tail call i32 @err_ret(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
br label %20
20: ; preds = %18, %12
%21 = load i32, i32* @AF_INET
%22 = load i32, i32* @SOCK_STREAM
%23 = tail call i32 @socket(i32 %21, i32 %22, i32 9999)
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %20
%26 = tail call i32 @err_ret(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.3, i64 0, i64 0))
br label %27
27: ; preds = %25, %20
%28 = load i32, i32* @AF_INET
%29 = tail call i32 @socket(i32 %28, i32 9999, i32 0)
%30 = icmp slt i32 %29, 0
br i1 %30, label %31, label %33
31: ; preds = %27
%32 = tail call i32 (i8*, ...) @err_sys(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0))
br label %33
33: ; preds = %31, %27
%34 = call i32 @memset(%struct.sockaddr_in* %3, i32 0, i32 12)
%35 = load i32, i32* @AF_INET
%36 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %35, i32* %36
%37 = call i32 @htons(i32 13)
%38 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %37, i32* %38
%39 = load i32, i32* @AF_INET
%40 = getelementptr inbounds i8*, i8** %1, i64 1
%41 = load i8*, i8** %40
%42 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
%43 = call i64 @inet_pton(i32 %39, i8* %41, i32* %42)
%44 = icmp slt i64 %43, 1
br i1 %44, label %45, label %48
45: ; preds = %33
%46 = load i8*, i8** %40
%47 = call i32 (i8*, ...) @err_sys(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0), i8* %46)
br label %48
48: ; preds = %45, %33
%49 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%50 = call i64 @connect(i32 %29, %struct.sockaddr* %49, i32 12)
%51 = icmp slt i64 %50, 0
br i1 %51, label %52, label %54
52: ; preds = %48
%53 = call i32 (i8*, ...) @err_sys(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.6, i64 0, i64 0))
br label %54
54: ; preds = %52, %48
%55 = load i32, i32* @BUFSIZ
%56 = call i32 @read(i32 %29, i8* %6, i32 %55)
%57 = icmp sgt i32 %56, 0
br i1 %57, label %58, label %72
58: ; preds = %54, %68
%59 = phi i32 [ %70, %68 ], [ %56, %54 ]
%60 = zext i32 %59 to i64
%61 = getelementptr inbounds i8, i8* %6, i64 %60
store i8 0, i8* %61
%62 = load i32, i32* @stdout
%63 = call i64 @fputs(i8* %6, i32 %62)
%64 = load i64, i64* @EOF
%65 = icmp eq i64 %63, %64
br i1 %65, label %66, label %68
66: ; preds = %58
%67 = call i32 (i8*, ...) @err_sys(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.7, i64 0, i64 0))
br label %68
68: ; preds = %66, %58
%69 = load i32, i32* @BUFSIZ
%70 = call i32 @read(i32 %29, i8* %6, i32 %69)
%71 = icmp sgt i32 %70, 0
br i1 %71, label %58, label %72
72: ; preds = %68, %54
%73 = phi i32 [ %56, %54 ], [ %70, %68 ]
%74 = icmp slt i32 %73, 0
br i1 %74, label %75, label %77
75: ; preds = %72
%76 = call i32 (i8*, ...) @err_sys(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.8, i64 0, i64 0))
br label %77
77: ; preds = %75, %72
%78 = call i32 @exit(i32 0)
unreachable
}