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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ IPPROTO_TCP;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ bcopy(int /*<<< orphan*/, int /*<<< orphan*/ *,
                         int /*<<< orphan*/);
int /*<<< orphan*/ bzero(struct sockaddr_in *, int);
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/);
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ printf(char *, ...);
int recv(int, char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ send(int, char *, int /*<<< orphan*/, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ strlen(char *);
int main(int argc, char *argv[]) {
  if (argc != 2) {
    printf("Usage: %s <IP Address>\n", argv[0]);
    exit(0);
  }
  char *address = argv[1];
  int serverSocket;
  struct sockaddr_in serverData;
  struct hostent *host;
  char buffer[1024];
  bool isConnected;
  int bytesReceived;
  bzero(&serverData, sizeof(serverData));
  serverData.sin_family = AF_INET;
  host = gethostbyname(address);
  bcopy(host->h_addr, &(serverData.sin_addr.s_addr), host->h_length);
  serverData.sin_port = htons(1234);
  serverSocket = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
  if (connect(serverSocket, (struct sockaddr *)&serverData,
              sizeof(serverData)) < 0) {
    printf("Error connecting\n");
    close(serverSocket);
  } else {
    isConnected = true;
    while (isConnected) {
      printf("Send> ");
      fgets(buffer, 1024, stdin);
      send(serverSocket, buffer, strlen(buffer), 0);
      bytesReceived = recv(serverSocket, buffer, 1024, 0);
      buffer[bytesReceived] = '\0';
      printf("Received: %s\n", buffer);
    }
    close(serverSocket);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x450,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     120f <main+0x3f>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xdff(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
xor    %edi,%edi
call   10d0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x18(%rbp)
lea    -0x28(%rbp),%rdi
mov    $0xc,%esi
call   10c0 <bzero@plt>
mov    0x2db8(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
call   10a0 <gethostbyname@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    0x4(%rax),%edi
lea    -0x28(%rbp),%rsi
add    $0x4,%rsi
mov    -0x30(%rbp),%rax
mov    (%rax),%edx
call   10b0 <bcopy@plt>
mov    $0x4d2,%edi
call   1050 <htons@plt>
mov    %eax,-0x28(%rbp)
mov    0x2d50(%rip),%rax
mov    (%rax),%edi
mov    0x2d4f(%rip),%rax
mov    (%rax),%esi
mov    0x2d56(%rip),%rax
mov    (%rax),%edx
call   10f0 <socket@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0xc,%edx
call   10e0 <connect@plt>
cmp    $0x0,%rax
jge    12c2 <main+0xf2>
lea    0xd6a(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x1c(%rbp),%edi
call   1080 <close@plt>
jmp    1387 <main+0x1b7>
movl   $0x1,-0x434(%rbp)
cmpl   $0x0,-0x434(%rbp)
je     137f <main+0x1af>
lea    0xd4a(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
lea    -0x430(%rbp),%rdi
mov    0x2ceb(%rip),%rax
mov    (%rax),%edx
mov    $0x400,%esi
call   1090 <fgets@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x444(%rbp)
lea    -0x430(%rbp),%rax
mov    %rax,-0x440(%rbp)
lea    -0x430(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x444(%rbp),%edi
mov    -0x440(%rbp),%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1060 <send@plt>
mov    -0x1c(%rbp),%edi
lea    -0x430(%rbp),%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x438(%rbp)
movslq -0x438(%rbp),%rax
movb   $0x0,-0x430(%rbp,%rax,1)
lea    -0x430(%rbp),%rsi
lea    0xcbe(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
jmp    12cc <main+0xfc>
mov    -0x1c(%rbp),%edi
call   1080 <close@plt>
xor    %eax,%eax
add    $0x450,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x410,%rsp
cmp    $0x2,%edi
jne    130d <main+0x12d>
mov    0x8(%rsi),%rbp
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0xc,%esi
call   10d0 <bzero@plt>
mov    0x2dd5(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    %rbp,%rdi
call   10b0 <gethostbyname@plt>
mov    (%rax),%edx
mov    0x4(%rax),%edi
lea    0x4(%rsp),%rsi
call   10c0 <bcopy@plt>
mov    $0x4d2,%edi
call   1060 <htons@plt>
mov    %eax,(%rsp)
mov    0x2d7c(%rip),%rax
mov    (%rax),%edi
mov    0x2d7b(%rip),%rax
mov    (%rax),%esi
mov    0x2d82(%rip),%rax
mov    (%rax),%edx
call   1100 <socket@plt>
mov    %eax,%ebp
mov    %eax,%edi
mov    %rbx,%rsi
mov    $0xc,%edx
call   10f0 <connect@plt>
test   %rax,%rax
js     12e8 <main+0x108>
lea    0xd9e(%rip),%r14
mov    0x2d5f(%rip),%r12
lea    0x10(%rsp),%rbx
lea    0xd92(%rip),%r15
nopl   (%rax)
mov    %r14,%rdi
xor    %eax,%eax
call   1080 <printf@plt>
mov    (%r12),%edx
mov    %rbx,%rdi
mov    $0x400,%esi
call   10a0 <fgets@plt>
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1070 <send@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
cltq
movb   $0x0,0x10(%rsp,%rax,1)
mov    %r15,%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1080 <printf@plt>
jmp    1290 <main+0xb0>
lea    0xd3e(%rip),%rdi
call   1040 <puts@plt>
mov    %ebp,%edi
call   1090 <close@plt>
xor    %eax,%eax
add    $0x410,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    (%rsi),%rsi
lea    0xce9(%rip),%rdi
xor    %eax,%eax
call   1080 <printf@plt>
xor    %edi,%edi
call   10e0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.hostent = type { i32, i32 }
%struct.sockaddr = type { i32 }
@.str = [24 x i8] c"Usage: %s <IP Address>\0A\00"
@AF_INET = global i32 0
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@.str.1 = [18 x i8] c"Error connecting\0A\00"
@.str.2 = [7 x i8] c"Send> \00"
@stdin = global i32 0
@.str.3 = [14 x i8] c"Received: %s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca i32
%8 = alloca %struct.sockaddr_in
%9 = alloca %struct.hostent*
%10 = alloca [1024 x i8]
%11 = alloca i32
%12 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%13 = load i32, i32* %4
%14 = icmp ne i32 %13, 2
br i1 %14, label %15, label %21
15: ; preds = %2
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 0
%18 = load i8*, i8** %17
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %18)
%20 = call i32 @exit(i32 0)
unreachable
21: ; preds = %2
%22 = load i8**, i8*** %5
%23 = getelementptr inbounds i8*, i8** %22, i64 1
%24 = load i8*, i8** %23
store i8* %24, i8** %6
%25 = call i32 @bzero(%struct.sockaddr_in* %8, i32 12)
%26 = load i32, i32* @AF_INET
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 2
store i32 %26, i32* %27
%28 = load i8*, i8** %6
%29 = call %struct.hostent* @gethostbyname(i8* %28)
store %struct.hostent* %29, %struct.hostent** %9
%30 = load %struct.hostent*, %struct.hostent** %9
%31 = getelementptr inbounds %struct.hostent, %struct.hostent* %30, i32 0, i32 1
%32 = load i32, i32* %31
%33 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 1
%34 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %33, i32 0, i32 0
%35 = load %struct.hostent*, %struct.hostent** %9
%36 = getelementptr inbounds %struct.hostent, %struct.hostent* %35, i32 0, i32 0
%37 = load i32, i32* %36
%38 = call i32 @bcopy(i32 %32, i32* %34, i32 %37)
%39 = call i32 @htons(i32 1234)
%40 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 0
store i32 %39, i32* %40
%41 = load i32, i32* @PF_INET
%42 = load i32, i32* @SOCK_STREAM
%43 = load i32, i32* @IPPROTO_TCP
%44 = call i32 @socket(i32 %41, i32 %42, i32 %43)
store i32 %44, i32* %7
%45 = load i32, i32* %7
%46 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%47 = call i64 @connect(i32 %45, %struct.sockaddr* %46, i32 12)
%48 = icmp slt i64 %47, 0
br i1 %48, label %49, label %53
49: ; preds = %21
%50 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
%51 = load i32, i32* %7
%52 = call i32 @close(i32 %51)
br label %78
53: ; preds = %21
store i32 1, i32* %11
br label %54
54: ; preds = %57, %53
%55 = load i32, i32* %11
%56 = icmp ne i32 %55, 0
br i1 %56, label %57, label %75
57: ; preds = %54
%58 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%59 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%60 = load i32, i32* @stdin
%61 = call i32 @fgets(i8* %59, i32 1024, i32 %60)
%62 = load i32, i32* %7
%63 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%64 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%65 = call i32 @strlen(i8* %64)
%66 = call i32 @send(i32 %62, i8* %63, i32 %65, i32 0)
%67 = load i32, i32* %7
%68 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%69 = call i32 @recv(i32 %67, i8* %68, i32 1024, i32 0)
store i32 %69, i32* %12
%70 = load i32, i32* %12
%71 = sext i32 %70 to i64
%72 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 %71
store i8 0, i8* %72
%73 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%74 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), i8* %73)
br label %54
75: ; preds = %54
%76 = load i32, i32* %7
%77 = call i32 @close(i32 %76)
br label %78
78: ; preds = %75, %49
ret i32 0
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.hostent = type { i32, i32 }
%struct.sockaddr = type { i32 }
@.str = [24 x i8] c"Usage: %s <IP Address>\0A\00"
@AF_INET = global i32 0
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@.str.2 = [7 x i8] c"Send> \00"
@stdin = global i32 0
@.str.3 = [14 x i8] c"Received: %s\0A\00"
@str = [17 x i8] c"Error connecting\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca [1024 x i8]
%5 = icmp eq i32 %0, 2
br i1 %5, label %10, label %6
6: ; preds = %2
%7 = load i8*, i8** %1
%8 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %7)
%9 = tail call i32 @exit(i32 0)
unreachable
10: ; preds = %2
%11 = getelementptr inbounds i8*, i8** %1, i64 1
%12 = load i8*, i8** %11
%13 = bitcast %struct.sockaddr_in* %3 to i8*
%14 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%15 = call i32 @bzero(%struct.sockaddr_in* %3, i32 12)
%16 = load i32, i32* @AF_INET
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %16, i32* %17
%18 = tail call %struct.hostent* @gethostbyname(i8* %12)
%19 = getelementptr inbounds %struct.hostent, %struct.hostent* %18, i64 0, i32 1
%20 = load i32, i32* %19
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1, i32 0
%22 = getelementptr inbounds %struct.hostent, %struct.hostent* %18, i64 0, i32 0
%23 = load i32, i32* %22
%24 = call i32 @bcopy(i32 %20, i32* %21, i32 %23)
%25 = call i32 @htons(i32 1234)
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %25, i32* %26
%27 = load i32, i32* @PF_INET
%28 = load i32, i32* @SOCK_STREAM
%29 = load i32, i32* @IPPROTO_TCP
%30 = call i32 @socket(i32 %27, i32 %28, i32 %29)
%31 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%32 = call i64 @connect(i32 %30, %struct.sockaddr* %31, i32 12)
%33 = icmp slt i64 %32, 0
br i1 %33, label %34, label %37
34: ; preds = %10
%35 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str, i64 0, i64 0))
%36 = call i32 @close(i32 %30)
ret i32 0
37: ; preds = %10, %37
%38 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%39 = load i32, i32* @stdin
%40 = call i32 @fgets(i8* %14, i32 1024, i32 %39)
%41 = call i32 @strlen(i8* %14)
%42 = call i32 @send(i32 %30, i8* %14, i32 %41, i32 0)
%43 = call i32 @recv(i32 %30, i8* %14, i32 1024, i32 0)
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 %44
store i8 0, i8* %45
%46 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), i8* %14)
br label %37
}