#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;
};
typedef int /*<<< orphan*/ servaddr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, struct sockaddr *, int);
int errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
scalar_t__ inet_pton(int /*<<< orphan*/, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int recv(int, char *, int, int /*<<< orphan*/);
scalar_t__ send(int, char *, int /*<<< orphan*/, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stdin;
char *strerror(int);
int /*<<< orphan*/ strlen(char *);
int main(int argc, char **argv) {
  int sockfd, rec_len;
  char sendline[4096];
  char buf[4096];
  struct sockaddr_in servaddr;
  if (argc != 2) {
    printf("usage: ./client <ipaddress>\n");
    exit(0);
  }
  if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
    printf("create socket error: %s(errno: %d)\n", strerror(errno), errno);
    exit(0);
  }
  memset(&servaddr, 0, sizeof(servaddr));
  servaddr.sin_family = AF_INET;
  servaddr.sin_port = htons(8000);
  if (inet_pton(AF_INET, argv[1], &servaddr.sin_addr) <= 0) {
    printf("inet_pton error for %s\n", argv[1]);
    exit(0);
  }
  if (connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0) {
    printf("connect error: %s(errno: %d)\n", strerror(errno), errno);
    exit(0);
  }
  printf("send msg to server: \n");
  fgets(sendline, 4096, stdin);
  if (send(sockfd, sendline, strlen(sendline), 0) < 0) {
    printf("send msg error: %s(errno: %d)\n", strerror(errno), errno);
    exit(0);
  }
  if ((rec_len = recv(sockfd, buf, 4096, 0)) == -1) {
    perror("recv error");
    exit(1);
  }
  buf[rec_len] = '\0';
  printf("Received : %s ", buf);
  close(sockfd);
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2040,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     1218 <main+0x38>
lea    0xdf6(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
xor    %edi,%edi
call   10d0 <exit@plt>
mov    0x2dc9(%rip),%rax
mov    (%rax),%edi
mov    0x2da8(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1100 <socket@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    126c <main+0x8c>
mov    0x2d84(%rip),%rax
mov    (%rax),%edi
call   10f0 <strerror@plt>
mov    %rax,%rsi
mov    0x2d73(%rip),%rax
mov    (%rax),%edx
lea    0xdbf(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
xor    %edi,%edi
call   10d0 <exit@plt>
lea    -0x2030(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1080 <memset@plt>
mov    0x2d62(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2028(%rbp)
mov    $0x1f40,%edi
call   1050 <htons@plt>
mov    %eax,-0x202c(%rbp)
mov    0x2d43(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    -0x2030(%rbp),%rdx
call   10b0 <inet_pton@plt>
cmp    $0x0,%rax
jg     12e2 <main+0x102>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xd6d(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
xor    %edi,%edi
call   10d0 <exit@plt>
mov    -0x14(%rbp),%edi
lea    -0x2030(%rbp),%rsi
mov    $0xc,%edx
call   10e0 <connect@plt>
cmp    $0x0,%rax
jge    132f <main+0x14f>
mov    0x2cc1(%rip),%rax
mov    (%rax),%edi
call   10f0 <strerror@plt>
mov    %rax,%rsi
mov    0x2cb0(%rip),%rax
mov    (%rax),%edx
lea    0xd38(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
xor    %edi,%edi
call   10d0 <exit@plt>
lea    0xd41(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
lea    -0x1020(%rbp),%rdi
mov    0x2c95(%rip),%rax
mov    (%rax),%edx
mov    $0x1000,%esi
call   10a0 <fgets@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x203c(%rbp)
lea    -0x1020(%rbp),%rax
mov    %rax,-0x2038(%rbp)
lea    -0x1020(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x203c(%rbp),%edi
mov    -0x2038(%rbp),%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1060 <send@plt>
cmp    $0x0,%rax
jge    13c9 <main+0x1e9>
mov    0x2c27(%rip),%rax
mov    (%rax),%edi
call   10f0 <strerror@plt>
mov    %rax,%rsi
mov    0x2c16(%rip),%rax
mov    (%rax),%edx
lea    0xcd2(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
xor    %edi,%edi
call   10d0 <exit@plt>
mov    -0x14(%rbp),%edi
lea    -0x2020(%rbp),%rsi
mov    $0x1000,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x18(%rbp)
cmp    $0xffffffff,%eax
jne    1401 <main+0x221>
lea    0xcba(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
movslq -0x18(%rbp),%rax
movb   $0x0,-0x2020(%rbp,%rax,1)
lea    -0x2020(%rbp),%rsi
lea    0xc9c(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x14(%rbp),%edi
call   1090 <close@plt>
xor    %edi,%edi
call   10d0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x2018,%rsp
cmp    $0x2,%edi
jne    124b <main+0x5b>
mov    %rsi,%rbx
mov    0x2ddc(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2dba(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1110 <socket@plt>
test   %eax,%eax
jns    125e <main+0x6e>
mov    0x2d9e(%rip),%rbx
mov    (%rbx),%edi
call   1100 <strerror@plt>
mov    (%rbx),%edx
lea    0xdc6(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1080 <printf@plt>
xor    %edi,%edi
call   10e0 <exit@plt>
lea    0xe56(%rip),%rdi
call   1040 <puts@plt>
xor    %edi,%edi
call   10e0 <exit@plt>
mov    %eax,%r15d
mov    %rsp,%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1090 <memset@plt>
mov    0x0(%rbp),%eax
mov    %eax,0x8(%rsp)
mov    $0x1f40,%edi
call   1060 <htons@plt>
mov    %eax,0x4(%rsp)
mov    0x0(%rbp),%edi
mov    0x8(%rbx),%rsi
mov    %r14,%rdx
call   10c0 <inet_pton@plt>
test   %rax,%rax
jg     12b5 <main+0xc5>
mov    0x8(%rbx),%rsi
lea    0xd7d(%rip),%rdi
xor    %eax,%eax
call   1080 <printf@plt>
xor    %edi,%edi
call   10e0 <exit@plt>
mov    %rsp,%rsi
mov    %r15d,%edi
mov    $0xc,%edx
call   10f0 <connect@plt>
test   %rax,%rax
jns    12e6 <main+0xf6>
mov    0x2cf7(%rip),%rbx
mov    (%rbx),%edi
call   1100 <strerror@plt>
mov    (%rbx),%edx
lea    0xd5b(%rip),%rdi
jmp    123a <main+0x4a>
lea    0xda6(%rip),%rdi
call   1040 <puts@plt>
mov    0x2ce7(%rip),%rax
mov    (%rax),%edx
lea    0x1010(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x1000,%esi
call   10b0 <fgets@plt>
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1070 <send@plt>
test   %rax,%rax
jns    1348 <main+0x158>
mov    0x2c95(%rip),%rbx
mov    (%rbx),%edi
call   1100 <strerror@plt>
mov    (%rbx),%edx
lea    0xd17(%rip),%rdi
jmp    123a <main+0x4a>
lea    0x10(%rsp),%rsi
mov    %r15d,%edi
mov    $0x1000,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
cmp    $0xffffffff,%eax
jne    1377 <main+0x187>
lea    0xd11(%rip),%rdi
call   10d0 <perror@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
cltq
movb   $0x0,0x10(%rsp,%rax,1)
lea    0xcff(%rip),%rdi
lea    0x10(%rsp),%rsi
xor    %eax,%eax
call   1080 <printf@plt>
mov    %r15d,%edi
call   10a0 <close@plt>
xor    %edi,%edi
call   10e0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@.str = [29 x i8] c"usage: ./client <ipaddress>\0A\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [36 x i8] c"create socket error: %s(errno: %d)\0A\00"
@errno = global i32 0
@.str.2 = [24 x i8] c"inet_pton error for %s\0A\00"
@.str.3 = [30 x i8] c"connect error: %s(errno: %d)\0A\00"
@.str.4 = [22 x i8] c"send msg to server: \0A\00"
@stdin = global i32 0
@.str.5 = [31 x i8] c"send msg error: %s(errno: %d)\0A\00"
@.str.6 = [11 x i8] c"recv error\00"
@.str.7 = [15 x i8] c"Received : %s \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 [4096 x i8]
%9 = alloca [4096 x i8]
%10 = alloca %struct.sockaddr_in
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 %16
13: ; preds = %2
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
%15 = call i32 @exit(i32 0)
unreachable
16: ; preds = %2
%17 = load i32, i32* @AF_INET
%18 = load i32, i32* @SOCK_STREAM
%19 = call i32 @socket(i32 %17, i32 %18, i32 0)
store i32 %19, i32* %6
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %27
21: ; preds = %16
%22 = load i32, i32* @errno
%23 = call i8* @strerror(i32 %22)
%24 = load i32, i32* @errno
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0), i8* %23, i32 %24)
%26 = call i32 @exit(i32 0)
unreachable
27: ; preds = %16
%28 = call i32 @memset(%struct.sockaddr_in* %10, i32 0, i32 12)
%29 = load i32, i32* @AF_INET
%30 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 2
store i32 %29, i32* %30
%31 = call i32 @htons(i32 8000)
%32 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 1
store i32 %31, i32* %32
%33 = load i32, i32* @AF_INET
%34 = load i8**, i8*** %5
%35 = getelementptr inbounds i8*, i8** %34, i64 1
%36 = load i8*, i8** %35
%37 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %10, i32 0, i32 0
%38 = call i64 @inet_pton(i32 %33, i8* %36, i32* %37)
%39 = icmp sle i64 %38, 0
br i1 %39, label %40, label %46
40: ; preds = %27
%41 = load i8**, i8*** %5
%42 = getelementptr inbounds i8*, i8** %41, i64 1
%43 = load i8*, i8** %42
%44 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i8* %43)
%45 = call i32 @exit(i32 0)
unreachable
46: ; preds = %27
%47 = load i32, i32* %6
%48 = bitcast %struct.sockaddr_in* %10 to %struct.sockaddr*
%49 = call i64 @connect(i32 %47, %struct.sockaddr* %48, i32 12)
%50 = icmp slt i64 %49, 0
br i1 %50, label %51, label %57
51: ; preds = %46
%52 = load i32, i32* @errno
%53 = call i8* @strerror(i32 %52)
%54 = load i32, i32* @errno
%55 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0), i8* %53, i32 %54)
%56 = call i32 @exit(i32 0)
unreachable
57: ; preds = %46
%58 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0))
%59 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 0
%60 = load i32, i32* @stdin
%61 = call i32 @fgets(i8* %59, i32 4096, i32 %60)
%62 = load i32, i32* %6
%63 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 0
%64 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 0
%65 = call i32 @strlen(i8* %64)
%66 = call i64 @send(i32 %62, i8* %63, i32 %65, i32 0)
%67 = icmp slt i64 %66, 0
br i1 %67, label %68, label %74
68: ; preds = %57
%69 = load i32, i32* @errno
%70 = call i8* @strerror(i32 %69)
%71 = load i32, i32* @errno
%72 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.5, i64 0, i64 0), i8* %70, i32 %71)
%73 = call i32 @exit(i32 0)
unreachable
74: ; preds = %57
%75 = load i32, i32* %6
%76 = getelementptr inbounds [4096 x i8], [4096 x i8]* %9, i64 0, i64 0
%77 = call i32 @recv(i32 %75, i8* %76, i32 4096, i32 0)
store i32 %77, i32* %7
%78 = icmp eq i32 %77, -1
br i1 %78, label %79, label %82
79: ; preds = %74
%80 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.6, i64 0, i64 0))
%81 = call i32 @exit(i32 1)
unreachable
82: ; preds = %74
%83 = load i32, i32* %7
%84 = sext i32 %83 to i64
%85 = getelementptr inbounds [4096 x i8], [4096 x i8]* %9, i64 0, i64 %84
store i8 0, i8* %85
%86 = getelementptr inbounds [4096 x i8], [4096 x i8]* %9, i64 0, i64 0
%87 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.7, i64 0, i64 0), i8* %86)
%88 = load i32, i32* %6
%89 = call i32 @close(i32 %88)
%90 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [36 x i8] c"create socket error: %s(errno: %d)\0A\00"
@errno = global i32 0
@.str.2 = [24 x i8] c"inet_pton error for %s\0A\00"
@.str.3 = [30 x i8] c"connect error: %s(errno: %d)\0A\00"
@stdin = global i32 0
@.str.5 = [31 x i8] c"send msg error: %s(errno: %d)\0A\00"
@.str.6 = [11 x i8] c"recv error\00"
@.str.7 = [15 x i8] c"Received : %s \00"
@str = [21 x i8] c"send msg to server: \00"
@str.8 = [28 x i8] c"usage: ./client <ipaddress>\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [4096 x i8]
%4 = alloca [4096 x i8]
%5 = alloca %struct.sockaddr_in
%6 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%7 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 0
%8 = bitcast %struct.sockaddr_in* %5 to i8*
%9 = icmp eq i32 %0, 2
br i1 %9, label %13, label %10
10: ; preds = %2
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @str.8, i64 0, i64 0))
%12 = tail call i32 @exit(i32 0)
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 %24
18: ; preds = %13
%19 = load i32, i32* @errno
%20 = tail call i8* @strerror(i32 %19)
%21 = load i32, i32* @errno
%22 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0), i8* %20, i32 %21)
%23 = tail call i32 @exit(i32 0)
unreachable
24: ; preds = %13
%25 = call i32 @memset(%struct.sockaddr_in* %5, i32 0, i32 12)
%26 = load i32, i32* @AF_INET
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 2
store i32 %26, i32* %27
%28 = call i32 @htons(i32 8000)
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 1
store i32 %28, i32* %29
%30 = load i32, i32* @AF_INET
%31 = getelementptr inbounds i8*, i8** %1, i64 1
%32 = load i8*, i8** %31
%33 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 0
%34 = call i64 @inet_pton(i32 %30, i8* %32, i32* %33)
%35 = icmp slt i64 %34, 1
br i1 %35, label %36, label %40
36: ; preds = %24
%37 = load i8*, i8** %31
%38 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i8* %37)
%39 = call i32 @exit(i32 0)
unreachable
40: ; preds = %24
%41 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%42 = call i64 @connect(i32 %16, %struct.sockaddr* %41, i32 12)
%43 = icmp slt i64 %42, 0
br i1 %43, label %44, label %50
44: ; preds = %40
%45 = load i32, i32* @errno
%46 = call i8* @strerror(i32 %45)
%47 = load i32, i32* @errno
%48 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0), i8* %46, i32 %47)
%49 = call i32 @exit(i32 0)
unreachable
50: ; preds = %40
%51 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str, i64 0, i64 0))
%52 = load i32, i32* @stdin
%53 = call i32 @fgets(i8* %6, i32 4096, i32 %52)
%54 = call i32 @strlen(i8* %6)
%55 = call i64 @send(i32 %16, i8* %6, i32 %54, i32 0)
%56 = icmp slt i64 %55, 0
br i1 %56, label %57, label %63
57: ; preds = %50
%58 = load i32, i32* @errno
%59 = call i8* @strerror(i32 %58)
%60 = load i32, i32* @errno
%61 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str.5, i64 0, i64 0), i8* %59, i32 %60)
%62 = call i32 @exit(i32 0)
unreachable
63: ; preds = %50
%64 = call i32 @recv(i32 %16, i8* %7, i32 4096, i32 0)
%65 = icmp eq i32 %64, -1
br i1 %65, label %66, label %69
66: ; preds = %63
%67 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.6, i64 0, i64 0))
%68 = call i32 @exit(i32 1)
unreachable
69: ; preds = %63
%70 = sext i32 %64 to i64
%71 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 %70
store i8 0, i8* %71
%72 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.7, i64 0, i64 0), i8* %7)
%73 = call i32 @close(i32 %16)
%74 = call i32 @exit(i32 0)
unreachable
}