#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_port;
  int /*<<< orphan*/ sin_addr;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
struct hostent {
  int /*<<< orphan*/ h_length;
  scalar_t__ h_addr;
};
typedef int /*<<< orphan*/ msg;
typedef int /*<<< orphan*/ client;
typedef int /*<<< orphan*/ buff;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ bzero(struct sockaddr_in *, int);
int close(int);
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ exit(int);
struct hostent *gethostbyname(char *);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ memcpy(unsigned char *, unsigned char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
int read(int, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int strcmp(char *, char *);
int write(int, char *, int);
main(int argc, char *argv[]) {
  int client_socket, n;
  struct sockaddr_in client;
  struct hostent *hp;
  char buff[512];
  char msg[] = "hello";
  if (argc < 3) {
    printf("no server host specified at command line: <client1 localhost "
           "port#>\n");
    exit(1);
  }
  if ((client_socket = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
    printf("client cannot create socket.\n");
    exit(1);
  }
  bzero(&client, sizeof(client));
  client.sin_family = AF_INET;
  hp = gethostbyname(argv[1]);
  memcpy((unsigned char *)&client.sin_addr, (unsigned char *)hp->h_addr,
         hp->h_length);
  client.sin_port = htons((int)argv[2]);
  if (connect(client_socket, (struct sockaddr *)&client, sizeof(client)) < 0) {
    printf("Client failed to connect.\n");
    exit(1);
  }
  if ((n = write(client_socket, msg, sizeof(msg))) <= 0) {
    printf("CLIENT: problem with write.\n");
    exit(1);
  } else {
    printf("CLIENT: write to server succeeded.\n");
  }
  if ((n = read(client_socket, buff, sizeof(buff))) <= 0) {
    printf("CLIENT: problem with read.\n");
    exit(1);
  }
  n = strcmp(msg, buff);
  if (n != 0) {
    printf("CLIENT: received string from server, but no match.\n");
    exit(1);
  } else {
    printf("CLIENT: received string from server, and match.\n");
  }
  printf("CLIENT: closing connected socket and exiting program.\n");
  if ((n = close(client_socket)) != 0) {
    printf("CLIENT: problem with close.\n");
    exit(1);
  }
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x240,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0xe21(%rip),%eax
mov    %eax,-0x236(%rbp)
mov    0xe18(%rip),%ax
mov    %ax,-0x232(%rbp)
cmpl   $0x3,-0x8(%rbp)
jge    1213 <main+0x53>
lea    0xe02(%rip),%rdi
call   1050 <printf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    0x2dce(%rip),%rax
mov    (%rax),%edi
mov    0x2db5(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10e0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    124e <main+0x8e>
lea    0xe0c(%rip),%rdi
call   1050 <printf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
lea    -0x28(%rbp),%rdi
mov    $0xc,%esi
call   10b0 <bzero@plt>
mov    0x2d85(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1090 <gethostbyname@plt>
mov    %rax,-0x30(%rbp)
lea    -0x28(%rbp),%rdi
add    $0x4,%rdi
mov    -0x30(%rbp),%rax
mov    0x8(%rax),%rsi
mov    -0x30(%rbp),%rax
mov    (%rax),%edx
call   10a0 <memcpy@plt>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
mov    %eax,%edi
call   1040 <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    12d7 <main+0x117>
lea    0xda1(%rip),%rdi
call   1050 <printf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x14(%rbp),%edi
lea    -0x236(%rbp),%rsi
mov    $0x6,%edx
call   1030 <write@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jg     130d <main+0x14d>
lea    0xd86(%rip),%rdi
call   1050 <printf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
lea    0xd8d(%rip),%rdi
call   1050 <printf@plt>
mov    -0x14(%rbp),%edi
lea    -0x230(%rbp),%rsi
mov    $0x200,%edx
call   1070 <read@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jg     134f <main+0x18f>
lea    0xd85(%rip),%rdi
call   1050 <printf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
lea    -0x236(%rbp),%rdi
lea    -0x230(%rbp),%rsi
call   1080 <strcmp@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
je     1385 <main+0x1c5>
lea    0xd6b(%rip),%rdi
call   1050 <printf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
lea    0xd89(%rip),%rdi
call   1050 <printf@plt>
lea    0xdae(%rip),%rdi
call   1050 <printf@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
je     13c7 <main+0x207>
lea    0xdc5(%rip),%rdi
call   1050 <printf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
xor    %edi,%edi
call   10c0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x228,%rsp
movw   $0x6f,0xc(%rsp)
movl   $0x6c6c6568,0x8(%rsp)
6c
cmp    $0x2,%edi
jg     11ed <main+0x2d>
lea    0xf64(%rip),%rdi
jmp    1314 <main+0x154>
mov    %rsi,%rbx
mov    0x2df1(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2dd7(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10e0 <socket@plt>
test   %eax,%eax
jns    121a <main+0x5a>
lea    0xf1a(%rip),%rdi
jmp    1314 <main+0x154>
mov    %eax,%r15d
lea    0x10(%rsp),%r14
mov    %r14,%rdi
mov    $0xc,%esi
call   10b0 <bzero@plt>
mov    0x0(%rbp),%eax
mov    %eax,0x18(%rsp)
mov    0x8(%rbx),%rdi
call   1090 <gethostbyname@plt>
lea    0x14(%rsp),%rdi
mov    0x8(%rax),%rsi
mov    (%rax),%edx
call   10a0 <memcpy@plt>
mov    0x10(%rbx),%edi
call   1050 <htons@plt>
mov    %eax,0x10(%rsp)
mov    %r15d,%edi
mov    %r14,%rsi
mov    $0xc,%edx
call   10d0 <connect@plt>
test   %rax,%rax
jns    127c <main+0xbc>
lea    0xe9e(%rip),%rdi
jmp    1314 <main+0x154>
lea    0x8(%rsp),%rsi
mov    %r15d,%edi
mov    $0x6,%edx
call   1040 <write@plt>
test   %eax,%eax
jg     129b <main+0xdb>
lea    0xe60(%rip),%rdi
jmp    1314 <main+0x154>
lea    0xd64(%rip),%rdi
call   1030 <puts@plt>
lea    0x20(%rsp),%rsi
mov    %r15d,%edi
mov    $0x200,%edx
call   1070 <read@plt>
test   %eax,%eax
jg     12c6 <main+0x106>
lea    0xe1a(%rip),%rdi
jmp    1314 <main+0x154>
lea    0x8(%rsp),%rdi
lea    0x20(%rsp),%rsi
call   1080 <strcmp@plt>
test   %eax,%eax
jne    1304 <main+0x144>
lea    0xd49(%rip),%rdi
call   1030 <puts@plt>
lea    0xd6d(%rip),%rdi
call   1030 <puts@plt>
mov    %r15d,%edi
call   1060 <close@plt>
test   %eax,%eax
jne    130d <main+0x14d>
xor    %edi,%edi
call   10c0 <exit@plt>
lea    0xda0(%rip),%rdi
jmp    1314 <main+0x154>
lea    0xd7b(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.hostent = type { i32, i64 }
%struct.sockaddr = type { i32 }
@__const.main.msg = [6 x i8] c"hello\00"
@.str = [69 x i8] c"no server host specified at command line: <client1 localhost port#>\0A\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [30 x i8] c"client cannot create socket.\0A\00"
@.str.2 = [27 x i8] c"Client failed to connect.\0A\00"
@.str.3 = [29 x i8] c"CLIENT: problem with write.\0A\00"
@.str.4 = [36 x i8] c"CLIENT: write to server succeeded.\0A\00"
@.str.5 = [28 x i8] c"CLIENT: problem with read.\0A\00"
@.str.6 = [52 x i8] c"CLIENT: received string from server, but no match.\0A\00"
@.str.7 = [49 x i8] c"CLIENT: received string from server, and match.\0A\00"
@.str.8 = [55 x i8] c"CLIENT: closing connected socket and exiting program.\0A\00"
@.str.9 = [29 x i8] c"CLIENT: problem with close.\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 %struct.sockaddr_in
%9 = alloca %struct.hostent*
%10 = alloca [512 x i8]
%11 = alloca [6 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = bitcast [6 x i8]* %11 to i8*
%13 = load i32, i32* %4
%14 = icmp slt i32 %13, 3
br i1 %14, label %15, label %18
15: ; preds = %2
%16 = call i32 @printf(i8* getelementptr inbounds ([69 x i8], [69 x i8]* @.str, i64 0, i64 0))
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %2
%19 = load i32, i32* @AF_INET
%20 = load i32, i32* @SOCK_STREAM
%21 = call i32 @socket(i32 %19, i32 %20, i32 0)
store i32 %21, i32* %6
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %18
%24 = call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0))
%25 = call i32 @exit(i32 1)
unreachable
26: ; preds = %18
%27 = call i32 @bzero(%struct.sockaddr_in* %8, i32 12)
%28 = load i32, i32* @AF_INET
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 2
store i32 %28, i32* %29
%30 = load i8**, i8*** %5
%31 = getelementptr inbounds i8*, i8** %30, i64 1
%32 = load i8*, i8** %31
%33 = call %struct.hostent* @gethostbyname(i8* %32)
store %struct.hostent* %33, %struct.hostent** %9
%34 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 1
%35 = bitcast i32* %34 to i8*
%36 = load %struct.hostent*, %struct.hostent** %9
%37 = getelementptr inbounds %struct.hostent, %struct.hostent* %36, i32 0, i32 1
%38 = load i64, i64* %37
%39 = inttoptr i64 %38 to i8*
%40 = load %struct.hostent*, %struct.hostent** %9
%41 = getelementptr inbounds %struct.hostent, %struct.hostent* %40, i32 0, i32 0
%42 = load i32, i32* %41
%43 = call i32 @memcpy(i8* %35, i8* %39, i32 %42)
%44 = load i8**, i8*** %5
%45 = getelementptr inbounds i8*, i8** %44, i64 2
%46 = load i8*, i8** %45
%47 = ptrtoint i8* %46 to i32
%48 = call i32 @htons(i32 %47)
%49 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 0
store i32 %48, i32* %49
%50 = load i32, i32* %6
%51 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%52 = call i64 @connect(i32 %50, %struct.sockaddr* %51, i32 12)
%53 = icmp slt i64 %52, 0
br i1 %53, label %54, label %57
54: ; preds = %26
%55 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0))
%56 = call i32 @exit(i32 1)
unreachable
57: ; preds = %26
%58 = load i32, i32* %6
%59 = getelementptr inbounds [6 x i8], [6 x i8]* %11, i64 0, i64 0
%60 = call i32 @write(i32 %58, i8* %59, i32 6)
store i32 %60, i32* %7
%61 = icmp sle i32 %60, 0
br i1 %61, label %62, label %65
62: ; preds = %57
%63 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0))
%64 = call i32 @exit(i32 1)
unreachable
65: ; preds = %57
%66 = call i32 @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.4, i64 0, i64 0))
br label %67
67: ; preds = %65
%68 = load i32, i32* %6
%69 = getelementptr inbounds [512 x i8], [512 x i8]* %10, i64 0, i64 0
%70 = call i32 @read(i32 %68, i8* %69, i32 512)
store i32 %70, i32* %7
%71 = icmp sle i32 %70, 0
br i1 %71, label %72, label %75
72: ; preds = %67
%73 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.5, i64 0, i64 0))
%74 = call i32 @exit(i32 1)
unreachable
75: ; preds = %67
%76 = getelementptr inbounds [6 x i8], [6 x i8]* %11, i64 0, i64 0
%77 = getelementptr inbounds [512 x i8], [512 x i8]* %10, i64 0, i64 0
%78 = call i32 @strcmp(i8* %76, i8* %77)
store i32 %78, i32* %7
%79 = load i32, i32* %7
%80 = icmp ne i32 %79, 0
br i1 %80, label %81, label %84
81: ; preds = %75
%82 = call i32 @printf(i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.6, i64 0, i64 0))
%83 = call i32 @exit(i32 1)
unreachable
84: ; preds = %75
%85 = call i32 @printf(i8* getelementptr inbounds ([49 x i8], [49 x i8]* @.str.7, i64 0, i64 0))
br label %86
86: ; preds = %84
%87 = call i32 @printf(i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.8, i64 0, i64 0))
%88 = load i32, i32* %6
%89 = call i32 @close(i32 %88)
store i32 %89, i32* %7
%90 = icmp ne i32 %89, 0
br i1 %90, label %91, label %94
91: ; preds = %86
%92 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.9, i64 0, i64 0))
%93 = call i32 @exit(i32 1)
unreachable
94: ; preds = %86
%95 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.hostent = type { i32, i64 }
%struct.sockaddr = type { i32 }
@__const.main.msg = [6 x i8] c"hello\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@str = [35 x i8] c"CLIENT: write to server succeeded.\00"
@str.10 = [48 x i8] c"CLIENT: received string from server, and match.\00"
@str.11 = [54 x i8] c"CLIENT: closing connected socket and exiting program.\00"
@str.12 = [28 x i8] c"CLIENT: problem with close.\00"
@str.13 = [51 x i8] c"CLIENT: received string from server, but no match.\00"
@str.14 = [27 x i8] c"CLIENT: problem with read.\00"
@str.15 = [28 x i8] c"CLIENT: problem with write.\00"
@str.16 = [26 x i8] c"Client failed to connect.\00"
@str.17 = [29 x i8] c"client cannot create socket.\00"
@str.18 = [68 x i8] c"no server host specified at command line: <client1 localhost port#>\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca [512 x i8]
%5 = alloca [6 x i8]
%6 = bitcast %struct.sockaddr_in* %3 to i8*
%7 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%8 = getelementptr inbounds [6 x i8], [6 x i8]* %5, i64 0, i64 0
%9 = icmp slt i32 %0, 3
br i1 %9, label %10, label %13
10: ; preds = %2
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([68 x i8], [68 x i8]* @str.18, i64 0, i64 0))
%12 = tail call i32 @exit(i32 1)
unreachable
13: ; preds = %2
%14 = load i32, i32* @AF_INET
%15 = load i32, i32* @SOCK_STREAM
%16 = tail call i32 @socket(i32 %14, i32 %15, i32 0)
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %13
%19 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @str.17, i64 0, i64 0))
%20 = tail call i32 @exit(i32 1)
unreachable
21: ; preds = %13
%22 = call i32 @bzero(%struct.sockaddr_in* %3, i32 12)
%23 = load i32, i32* @AF_INET
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %23, i32* %24
%25 = getelementptr inbounds i8*, i8** %1, i64 1
%26 = load i8*, i8** %25
%27 = tail call %struct.hostent* @gethostbyname(i8* %26)
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
%29 = bitcast i32* %28 to i8*
%30 = getelementptr inbounds %struct.hostent, %struct.hostent* %27, i64 0, i32 1
%31 = load i64, i64* %30
%32 = inttoptr i64 %31 to i8*
%33 = getelementptr inbounds %struct.hostent, %struct.hostent* %27, i64 0, i32 0
%34 = load i32, i32* %33
%35 = call i32 @memcpy(i8* %29, i8* %32, i32 %34)
%36 = getelementptr inbounds i8*, i8** %1, i64 2
%37 = load i8*, i8** %36
%38 = ptrtoint i8* %37 to i64
%39 = trunc i64 %38 to i32
%40 = call i32 @htons(i32 %39)
%41 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %40, i32* %41
%42 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%43 = call i64 @connect(i32 %16, %struct.sockaddr* %42, i32 12)
%44 = icmp slt i64 %43, 0
br i1 %44, label %45, label %48
45: ; preds = %21
%46 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str.16, i64 0, i64 0))
%47 = call i32 @exit(i32 1)
unreachable
48: ; preds = %21
%49 = call i32 @write(i32 %16, i8* %8, i32 6)
%50 = icmp slt i32 %49, 1
br i1 %50, label %51, label %54
51: ; preds = %48
%52 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @str.15, i64 0, i64 0))
%53 = call i32 @exit(i32 1)
unreachable
54: ; preds = %48
%55 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([35 x i8], [35 x i8]* @str, i64 0, i64 0))
%56 = call i32 @read(i32 %16, i8* %7, i32 512)
%57 = icmp slt i32 %56, 1
br i1 %57, label %58, label %61
58: ; preds = %54
%59 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str.14, i64 0, i64 0))
%60 = call i32 @exit(i32 1)
unreachable
61: ; preds = %54
%62 = call i32 @strcmp(i8* %8, i8* %7)
%63 = icmp eq i32 %62, 0
br i1 %63, label %67, label %64
64: ; preds = %61
%65 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([51 x i8], [51 x i8]* @str.13, i64 0, i64 0))
%66 = call i32 @exit(i32 1)
unreachable
67: ; preds = %61
%68 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([48 x i8], [48 x i8]* @str.10, i64 0, i64 0))
%69 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([54 x i8], [54 x i8]* @str.11, i64 0, i64 0))
%70 = call i32 @close(i32 %16)
%71 = icmp eq i32 %70, 0
br i1 %71, label %75, label %72
72: ; preds = %67
%73 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @str.12, i64 0, i64 0))
%74 = call i32 @exit(i32 1)
unreachable
75: ; preds = %67
%76 = call i32 @exit(i32 0)
unreachable
}