#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;
};
struct hostent {
  int /*<<< orphan*/ h_length;
  int /*<<< orphan*/ h_addr;
};
typedef int /*<<< orphan*/ servaddr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
scalar_t__ EOF;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ bzero(struct sockaddr_in *, int);
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fputs(char *, int /*<<< orphan*/);
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ memcpy(char *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int read(int, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stdout;
int main(int argc, char *argv[]) {
  int sockfd;
  int nbytes;
  char recvline[4096 + 1];
  struct sockaddr_in servaddr;
  struct hostent *servinfo;
  if (argc != 2) {
    argv[1] = "localhost";
    printf("No IP supplied, using localhost\n");
  }
  servinfo = gethostbyname(argv[1]);
  if (!servinfo) {
    perror("gethostbyname error");
    return 1;
  }
  if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
    perror("socket error");
    return 1;
  }
  bzero(&servaddr, sizeof(servaddr));
  servaddr.sin_family = AF_INET;
  servaddr.sin_port = htons(60000);
  memcpy((char *)&servaddr.sin_addr, servinfo->h_addr, servinfo->h_length);
  if (connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0) {
    perror("connect error");
    return 1;
  }
  while ((nbytes = read(sockfd, recvline, 4096)) > 0) {
    recvline[nbytes] = '\0';
    if (fputs(recvline, stdout) == EOF) {
      perror("fputs error");
      return 1;
    }
  }
  if (nbytes < 0) {
    perror("read error");
    return 1;
  }
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1040,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11ee <main+0x3e>
mov    -0x10(%rbp),%rax
lea    0xe22(%rip),%rcx
mov    %rcx,0x8(%rax)
lea    0xe21(%rip),%rdi
call   1040 <printf@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1070 <gethostbyname@plt>
mov    %rax,-0x1038(%rbp)
cmpq   $0x0,-0x1038(%rbp)
jne    1228 <main+0x78>
lea    0xe14(%rip),%rdi
call   1090 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1386 <main+0x1d6>
mov    0x2db9(%rip),%rax
mov    (%rax),%edi
mov    0x2d98(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    1265 <main+0xb5>
lea    0xdeb(%rip),%rdi
call   1090 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1386 <main+0x1d6>
lea    -0x1030(%rbp),%rdi
mov    $0xc,%esi
call   10a0 <bzero@plt>
mov    0x2d6b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1028(%rbp)
mov    $0xea60,%edi
call   1030 <htons@plt>
mov    %eax,-0x102c(%rbp)
lea    -0x1030(%rbp),%rdi
mov    -0x1038(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x1038(%rbp),%rax
mov    (%rax),%edx
call   1080 <memcpy@plt>
mov    -0x14(%rbp),%edi
lea    -0x1030(%rbp),%rsi
mov    $0xc,%edx
call   10c0 <connect@plt>
cmp    $0x0,%rax
jge    12ea <main+0x13a>
lea    0xd73(%rip),%rdi
call   1090 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1386 <main+0x1d6>
jmp    12ef <main+0x13f>
mov    -0x14(%rbp),%edi
lea    -0x1020(%rbp),%rsi
mov    $0x1000,%edx
call   1060 <read@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jle    135d <main+0x1ad>
movslq -0x18(%rbp),%rax
movb   $0x0,-0x1020(%rbp,%rax,1)
lea    -0x1020(%rbp),%rdi
mov    0x2c9f(%rip),%rax
mov    (%rax),%esi
call   1050 <fputs@plt>
mov    0x2ca9(%rip),%rcx
cmp    (%rcx),%rax
jne    1358 <main+0x1a8>
lea    0xd13(%rip),%rdi
call   1090 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1386 <main+0x1d6>
jmp    12ef <main+0x13f>
cmpl   $0x0,-0x18(%rbp)
jge    137f <main+0x1cf>
lea    0xcf8(%rip),%rdi
call   1090 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1386 <main+0x1d6>
xor    %edi,%edi
call   10b0 <exit@plt>
mov    -0x4(%rbp),%eax
add    $0x1040,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x1018,%rsp
mov    %rsi,%rbx
cmp    $0x2,%edi
je     11dc <main+0x2c>
lea    0xe90(%rip),%rax
mov    %rax,0x8(%rbx)
lea    0xe6f(%rip),%rdi
call   1030 <puts@plt>
mov    0x8(%rbx),%rdi
call   1070 <gethostbyname@plt>
test   %rax,%rax
jne    11f6 <main+0x46>
lea    0xe0f(%rip),%rdi
jmp    12cc <main+0x11c>
mov    %rax,%r14
mov    0x2de8(%rip),%rbx
mov    (%rbx),%edi
mov    0x2dc7(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
test   %eax,%eax
jns    1222 <main+0x72>
lea    0xdf7(%rip),%rdi
jmp    12cc <main+0x11c>
mov    %eax,%ebp
mov    %rsp,%r15
mov    %r15,%rdi
mov    $0xc,%esi
call   10a0 <bzero@plt>
mov    (%rbx),%eax
mov    %eax,0x8(%rsp)
mov    $0xea60,%edi
call   1040 <htons@plt>
mov    %eax,0x4(%rsp)
mov    (%r14),%edx
mov    0x4(%r14),%esi
mov    %r15,%rdi
call   1080 <memcpy@plt>
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0xc,%edx
call   10c0 <connect@plt>
test   %rax,%rax
jns    1274 <main+0xc4>
lea    0xdaf(%rip),%rdi
jmp    12cc <main+0x11c>
lea    0x10(%rsp),%rbx
mov    0x2d48(%rip),%r14
mov    0x2d59(%rip),%r15
nopw   0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x1000,%edx
call   1060 <read@plt>
test   %eax,%eax
jle    12c3 <main+0x113>
mov    %eax,%eax
movb   $0x0,0x10(%rsp,%rax,1)
mov    (%r14),%esi
mov    %rbx,%rdi
call   1050 <fputs@plt>
cmp    (%r15),%rax
jne    1290 <main+0xe0>
lea    0xd6e(%rip),%rdi
jmp    12cc <main+0x11c>
jns    12e4 <main+0x134>
lea    0xd6f(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%eax
add    $0x1018,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %edi,%edi
call   10b0 <exit@plt>

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

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.hostent = type { i32, i32 }
%struct.sockaddr = type { i32 }
@.str = [10 x i8] c"localhost\00"
@.str.2 = [20 x i8] c"gethostbyname error\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.3 = [13 x i8] c"socket error\00"
@.str.4 = [14 x i8] c"connect error\00"
@stdout = global i32 0
@EOF = global i64 0
@.str.5 = [12 x i8] c"fputs error\00"
@.str.6 = [11 x i8] c"read error\00"
@str = [32 x i8] c"No IP supplied, using localhost\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [4097 x i8]
%4 = alloca %struct.sockaddr_in
%5 = getelementptr inbounds [4097 x i8], [4097 x i8]* %3, i64 0, i64 0
%6 = bitcast %struct.sockaddr_in* %4 to i8*
%7 = icmp eq i32 %0, 2
br i1 %7, label %11, label %8
8: ; preds = %2
%9 = getelementptr inbounds i8*, i8** %1, i64 1
store i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8** %9
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @str, i64 0, i64 0))
br label %11
11: ; preds = %8, %2
%12 = getelementptr inbounds i8*, i8** %1, i64 1
%13 = load i8*, i8** %12
%14 = tail call %struct.hostent* @gethostbyname(i8* %13)
%15 = icmp eq %struct.hostent* %14, null
br i1 %15, label %16, label %18
16: ; preds = %11
%17 = tail call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
br label %59
18: ; preds = %11
%19 = load i32, i32* @AF_INET
%20 = load i32, i32* @SOCK_STREAM
%21 = tail call i32 @socket(i32 %19, i32 %20, i32 0)
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %18
%24 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
br label %59
25: ; preds = %18
%26 = call i32 @bzero(%struct.sockaddr_in* %4, i32 12)
%27 = load i32, i32* @AF_INET
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 2
store i32 %27, i32* %28
%29 = tail call i32 @htons(i32 60000)
%30 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1
store i32 %29, i32* %30
%31 = getelementptr inbounds %struct.hostent, %struct.hostent* %14, i64 0, i32 1
%32 = load i32, i32* %31
%33 = getelementptr inbounds %struct.hostent, %struct.hostent* %14, i64 0, i32 0
%34 = load i32, i32* %33
%35 = call i32 @memcpy(i8* %6, i32 %32, i32 %34)
%36 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%37 = call i64 @connect(i32 %21, %struct.sockaddr* %36, i32 12)
%38 = icmp slt i64 %37, 0
br i1 %38, label %39, label %41
39: ; preds = %25
%40 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0))
br label %59
41: ; preds = %25, %44
%42 = call i32 @read(i32 %21, i8* %5, i32 4096)
%43 = icmp sgt i32 %42, 0
br i1 %43, label %44, label %53
44: ; preds = %41
%45 = zext i32 %42 to i64
%46 = getelementptr inbounds [4097 x i8], [4097 x i8]* %3, i64 0, i64 %45
store i8 0, i8* %46
%47 = load i32, i32* @stdout
%48 = call i64 @fputs(i8* %5, i32 %47)
%49 = load i64, i64* @EOF
%50 = icmp eq i64 %48, %49
br i1 %50, label %51, label %41
51: ; preds = %44
%52 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0))
br label %59
53: ; preds = %41
%54 = icmp slt i32 %42, 0
br i1 %54, label %55, label %57
55: ; preds = %53
%56 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.6, i64 0, i64 0))
br label %59
57: ; preds = %53
%58 = call i32 @exit(i32 0)
unreachable
59: ; preds = %55, %51, %39, %23, %16
ret i32 1
}