#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, n, rec_len;
  char recvline[4096], 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("Answer from server: %s ", buf);
  close(sockfd);
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x3040,%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    -0x3030(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1080 <memset@plt>
mov    0x2d62(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3028(%rbp)
mov    $0x1f40,%edi
call   1050 <htons@plt>
mov    %eax,-0x302c(%rbp)
mov    0x2d43(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    -0x3030(%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    -0x3030(%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    -0x2020(%rbp),%rdi
mov    0x2c95(%rip),%rax
mov    (%rax),%edx
mov    $0x1000,%esi
call   10a0 <fgets@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x303c(%rbp)
lea    -0x2020(%rbp),%rax
mov    %rax,-0x3038(%rbp)
lea    -0x2020(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x303c(%rbp),%edi
mov    -0x3038(%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    -0x3020(%rbp),%rsi
mov    $0x1000,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0xffffffff,%eax
jne    1401 <main+0x221>
lea    0xcba(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
movslq -0x1c(%rbp),%rax
movb   $0x0,-0x3020(%rbp,%rax,1)
lea    -0x3020(%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    0xe5f(%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    0xdaf(%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 = [24 x i8] c"Answer from server: %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 i32
%9 = alloca [4096 x i8]
%10 = alloca [4096 x i8]
%11 = alloca [4096 x i8]
%12 = alloca %struct.sockaddr_in
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 %18
15: ; preds = %2
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
%17 = call i32 @exit(i32 0)
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 %29
23: ; preds = %18
%24 = load i32, i32* @errno
%25 = call i8* @strerror(i32 %24)
%26 = load i32, i32* @errno
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0), i8* %25, i32 %26)
%28 = call i32 @exit(i32 0)
unreachable
29: ; preds = %18
%30 = call i32 @memset(%struct.sockaddr_in* %12, i32 0, i32 12)
%31 = load i32, i32* @AF_INET
%32 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %12, i32 0, i32 2
store i32 %31, i32* %32
%33 = call i32 @htons(i32 8000)
%34 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %12, i32 0, i32 1
store i32 %33, i32* %34
%35 = load i32, i32* @AF_INET
%36 = load i8**, i8*** %5
%37 = getelementptr inbounds i8*, i8** %36, i64 1
%38 = load i8*, i8** %37
%39 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %12, i32 0, i32 0
%40 = call i64 @inet_pton(i32 %35, i8* %38, i32* %39)
%41 = icmp sle i64 %40, 0
br i1 %41, label %42, label %48
42: ; preds = %29
%43 = load i8**, i8*** %5
%44 = getelementptr inbounds i8*, i8** %43, i64 1
%45 = load i8*, i8** %44
%46 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i8* %45)
%47 = call i32 @exit(i32 0)
unreachable
48: ; preds = %29
%49 = load i32, i32* %6
%50 = bitcast %struct.sockaddr_in* %12 to %struct.sockaddr*
%51 = call i64 @connect(i32 %49, %struct.sockaddr* %50, i32 12)
%52 = icmp slt i64 %51, 0
br i1 %52, label %53, label %59
53: ; preds = %48
%54 = load i32, i32* @errno
%55 = call i8* @strerror(i32 %54)
%56 = load i32, i32* @errno
%57 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.3, i64 0, i64 0), i8* %55, i32 %56)
%58 = call i32 @exit(i32 0)
unreachable
59: ; preds = %48
%60 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0))
%61 = getelementptr inbounds [4096 x i8], [4096 x i8]* %10, i64 0, i64 0
%62 = load i32, i32* @stdin
%63 = call i32 @fgets(i8* %61, i32 4096, i32 %62)
%64 = load i32, i32* %6
%65 = getelementptr inbounds [4096 x i8], [4096 x i8]* %10, i64 0, i64 0
%66 = getelementptr inbounds [4096 x i8], [4096 x i8]* %10, i64 0, i64 0
%67 = call i32 @strlen(i8* %66)
%68 = call i64 @send(i32 %64, i8* %65, i32 %67, i32 0)
%69 = icmp slt i64 %68, 0
br i1 %69, label %70, label %76
70: ; preds = %59
%71 = load i32, i32* @errno
%72 = call i8* @strerror(i32 %71)
%73 = load i32, i32* @errno
%74 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.5, i64 0, i64 0), i8* %72, i32 %73)
%75 = call i32 @exit(i32 0)
unreachable
76: ; preds = %59
%77 = load i32, i32* %6
%78 = getelementptr inbounds [4096 x i8], [4096 x i8]* %11, i64 0, i64 0
%79 = call i32 @recv(i32 %77, i8* %78, i32 4096, i32 0)
store i32 %79, i32* %8
%80 = icmp eq i32 %79, -1
br i1 %80, label %81, label %84
81: ; preds = %76
%82 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.6, i64 0, i64 0))
%83 = call i32 @exit(i32 1)
unreachable
84: ; preds = %76
%85 = load i32, i32* %8
%86 = sext i32 %85 to i64
%87 = getelementptr inbounds [4096 x i8], [4096 x i8]* %11, i64 0, i64 %86
store i8 0, i8* %87
%88 = getelementptr inbounds [4096 x i8], [4096 x i8]* %11, i64 0, i64 0
%89 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.7, i64 0, i64 0), i8* %88)
%90 = load i32, i32* %6
%91 = call i32 @close(i32 %90)
%92 = 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 = [24 x i8] c"Answer from server: %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 ([24 x i8], [24 x i8]* @.str.7, i64 0, i64 0), i8* %7)
%73 = call i32 @close(i32 %16)
%74 = call i32 @exit(i32 0)
unreachable
}