#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
struct TYPE_2__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_family;
};
struct hostent {
  int /*<<< orphan*/ h_length;
  scalar_t__ h_addr;
};
typedef int /*<<< orphan*/ serv_addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int atoi(char *);
int /*<<< orphan*/ bcopy(char *, char *, int /*<<< orphan*/);
int /*<<< orphan*/ bzero(char *, int);
scalar_t__ connect(int, struct sockaddr_in *, int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int read(int, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int main(int argc, char *argv[]) {
  while (1) {
    int sockdesc, port, n;
    struct sockaddr_in serv_addr;
    struct hostent *server;
    int count_chr;
    char buffer[140];
    if (argc < 3) {
      fprintf(stderr, "usage %s hostname port\n", argv[0]);
      exit(0);
    }
    port = atoi(argv[2]);
    sockdesc = socket(AF_INET, SOCK_STREAM, 0);
    if (sockdesc < 0)
      perror("socket error");
    server = gethostbyname(argv[1]);
    if (server == NULL) {
      fprintf(stderr, "wrong host\n");
      exit(0);
    }
    bzero((char *)&serv_addr, sizeof(serv_addr));
    serv_addr.sin_family = AF_INET;
    bcopy((char *)server->h_addr, (char *)&serv_addr.sin_addr.s_addr,
          server->h_length);
    serv_addr.sin_port = htons(port);
    if (connect(sockdesc, &serv_addr, sizeof(serv_addr)) < 0)
      perror("connection error");
    bzero(buffer, 141);
    n = read(sockdesc, buffer, 140);
    if (n < 0)
      perror("socket read error");
    if (n > 0)
      printf("%s\n", buffer);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xc0,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
jge    1208 <main+0x48>
mov    0x2dee(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe06(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
xor    %edi,%edi
call   10c0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   10b0 <atoi@plt>
mov    %eax,-0x18(%rbp)
mov    0x2dc9(%rip),%rax
mov    (%rax),%edi
mov    0x2da8(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10e0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    124a <main+0x8a>
lea    0xdd3(%rip),%rdi
call   1090 <perror@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1060 <gethostbyname@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1284 <main+0xc4>
mov    0x2d6b(%rip),%rax
mov    (%rax),%edi
lea    0xdaf(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
xor    %edi,%edi
call   10c0 <exit@plt>
lea    -0x28(%rbp),%rdi
mov    $0xc,%esi
call   10a0 <bzero@plt>
mov    0x2d4f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x30(%rbp),%rax
mov    0x8(%rax),%rdi
lea    -0x28(%rbp),%rsi
add    $0x4,%rsi
mov    -0x30(%rbp),%rax
mov    (%rax),%edx
call   1080 <bcopy@plt>
mov    -0x18(%rbp),%edi
call   1030 <htons@plt>
mov    %eax,-0x28(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0xc,%edx
call   10d0 <connect@plt>
cmp    $0x0,%rax
jge    12eb <main+0x12b>
lea    0xd4b(%rip),%rdi
call   1090 <perror@plt>
lea    -0xc0(%rbp),%rdi
mov    $0x8d,%esi
call   10a0 <bzero@plt>
mov    -0x14(%rbp),%edi
lea    -0xc0(%rbp),%rsi
mov    $0x8c,%edx
call   1050 <read@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1329 <main+0x169>
lea    0xd1e(%rip),%rdi
call   1090 <perror@plt>
cmpl   $0x0,-0x1c(%rbp)
jle    1346 <main+0x186>
lea    -0xc0(%rbp),%rsi
lea    0xd13(%rip),%rdi
call   1040 <printf@plt>
jmp    11d9 <main+0x19>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0xa8,%rsp
mov    %rsi,%rbx
cmp    $0x3,%edi
jge    11fa <main+0x3a>
mov    0x2df8(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%rdx
lea    0xe14(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
xor    %edi,%edi
call   10c0 <exit@plt>
mov    %rsp,%r12
lea    0x10(%rsp),%rbp
jmp    1210 <main+0x50>
lea    0xe37(%rip),%rdi
call   1090 <perror@plt>
mov    0x10(%rbx),%rdi
call   10b0 <atoi@plt>
mov    %eax,%r13d
mov    0x2dc5(%rip),%rax
mov    (%rax),%edi
mov    0x2da4(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10e0 <socket@plt>
mov    %eax,%r15d
test   %eax,%eax
js     12df <main+0x11f>
mov    0x8(%rbx),%rdi
call   1060 <gethostbyname@plt>
test   %rax,%rax
je     12fe <main+0x13e>
mov    %rax,%r14
mov    %r12,%rdi
mov    $0xc,%esi
call   10a0 <bzero@plt>
mov    0x2d7f(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    0x8(%r14),%rsi
mov    (%r14),%edx
lea    0x4(%rsp),%rdi
call   1080 <memmove@plt>
mov    %r13d,%edi
call   1030 <htons@plt>
mov    %eax,(%rsp)
mov    %r15d,%edi
mov    %r12,%rsi
mov    $0xc,%edx
call   10d0 <connect@plt>
test   %rax,%rax
js     12f0 <main+0x130>
mov    %rbp,%rdi
mov    $0x8d,%esi
call   10a0 <bzero@plt>
mov    %r15d,%edi
mov    %rbp,%rsi
mov    $0x8c,%edx
call   1050 <read@plt>
test   %eax,%eax
js     1204 <main+0x44>
je     1210 <main+0x50>
lea    0xd82(%rip),%rdi
mov    %rbp,%rsi
call   1040 <printf@plt>
jmp    1210 <main+0x50>
lea    0xd32(%rip),%rdi
call   1090 <perror@plt>
jmp    1240 <main+0x80>
lea    0xd3a(%rip),%rdi
call   1090 <perror@plt>
jmp    12a0 <main+0xe0>
mov    0x2cd3(%rip),%rax
mov    (%rax),%edi
lea    0xd17(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
xor    %edi,%edi
call   10c0 <exit@plt>

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

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.hostent = type { i32, i64 }
@stderr = global i32 0
@.str = [24 x i8] c"usage %s hostname port\0A\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [13 x i8] c"socket error\00"
@.str.2 = [12 x i8] c"wrong host\0A\00"
@.str.3 = [17 x i8] c"connection error\00"
@.str.4 = [18 x i8] c"socket read error\00"
@.str.5 = [4 x i8] c"%s\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca [140 x i8]
%5 = bitcast %struct.sockaddr_in* %3 to i8*
%6 = getelementptr inbounds [140 x i8], [140 x i8]* %4, i64 0, i64 0
%7 = icmp slt i32 %0, 3
br i1 %7, label %15, label %8
8: ; preds = %2
%9 = getelementptr inbounds i8*, i8** %1, i64 2
%10 = getelementptr inbounds i8*, i8** %1, i64 1
%11 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1, i32 0
%13 = bitcast i32* %12 to i8*
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
br label %20
15: ; preds = %2
%16 = load i32, i32* @stderr
%17 = load i8*, i8** %1
%18 = call i32 (i32, i8*, ...) @fprintf(i32 %16, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %17)
%19 = call i32 @exit(i32 0)
unreachable
20: ; preds = %60, %8
%21 = load i8*, i8** %9
%22 = call i32 @atoi(i8* %21)
%23 = load i32, i32* @AF_INET
%24 = load i32, i32* @SOCK_STREAM
%25 = call i32 @socket(i32 %23, i32 %24, i32 0)
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %20
%28 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
br label %29
29: ; preds = %27, %20
%30 = load i8*, i8** %10
%31 = call %struct.hostent* @gethostbyname(i8* %30)
%32 = icmp eq %struct.hostent* %31, null
br i1 %32, label %33, label %37
33: ; preds = %29
%34 = load i32, i32* @stderr
%35 = call i32 (i32, i8*, ...) @fprintf(i32 %34, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0))
%36 = call i32 @exit(i32 0)
unreachable
37: ; preds = %29
%38 = call i32 @bzero(i8* %5, i32 12)
%39 = load i32, i32* @AF_INET
store i32 %39, i32* %11
%40 = getelementptr inbounds %struct.hostent, %struct.hostent* %31, i64 0, i32 1
%41 = load i64, i64* %40
%42 = inttoptr i64 %41 to i8*
%43 = getelementptr inbounds %struct.hostent, %struct.hostent* %31, i64 0, i32 0
%44 = load i32, i32* %43
%45 = call i32 @htons(i32 %22)
store i32 %45, i32* %14
%46 = call i64 @connect(i32 %25, %struct.sockaddr_in* %3, i32 12)
%47 = icmp slt i64 %46, 0
br i1 %47, label %48, label %50
48: ; preds = %37
%49 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0))
br label %50
50: ; preds = %48, %37
%51 = call i32 @bzero(i8* %6, i32 141)
%52 = call i32 @read(i32 %25, i8* %6, i32 140)
%53 = icmp slt i32 %52, 0
br i1 %53, label %54, label %56
54: ; preds = %50
%55 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0))
br label %60
56: ; preds = %50
%57 = icmp eq i32 %52, 0
br i1 %57, label %60, label %58
58: ; preds = %56
%59 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i8* %6)
br label %60
60: ; preds = %54, %58, %56
br label %20
}