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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ bzero(struct sockaddr_in *, int);
int close(int);
int connect(int, struct sockaddr *, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_pton(int /*<<< orphan*/, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int recv(int, char *, int, int /*<<< orphan*/);
int send(int, char *, scalar_t__, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ strlen(char *);
int main(int argc, char *argv[]) {
  struct sockaddr_in sin;
  char buf[100];
  int s_fd;
  int port = 8000;
  char *str = "test sting";
  int n;
  if (argc > 1) {
    str = argv[1];
  }
  bzero(&sin, sizeof(sin));
  sin.sin_family = AF_INET;
  inet_pton(AF_INET, "127.0.0.1", &sin.sin_addr);
  sin.sin_port = htons(port);
  if ((s_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
    perror("fail to creat socket");
    exit(1);
  }
  if (connect(s_fd, (struct sockaddr *)&sin, sizeof(sin)) == -1) {
    perror("fail to connect");
    exit(1);
  }
  n = send(s_fd, str, strlen(str) + 1, 0);
  if (n == -1) {
    perror("fail to send");
    exit(1);
  }
  n = recv(s_fd, buf, 100, 0);
  if (n == -1) {
    perror("fail to recv");
    exit(1);
  }
  printf("the length of str : %s\n", buf);
  if (close(s_fd) == -1) {
    perror("fail to close");
    exit(1);
  }
  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)
movl   $0x1f40,-0x98(%rbp)
lea    0xe16(%rip),%rax
mov    %rax,-0xa0(%rbp)
cmpl   $0x1,-0x8(%rbp)
jle    120a <main+0x4a>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0xa0(%rbp)
lea    -0x20(%rbp),%rdi
mov    $0xc,%esi
call   10b0 <bzero@plt>
mov    0x2dc9(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    0x2dbd(%rip),%rax
mov    (%rax),%edi
lea    -0x20(%rbp),%rdx
add    $0x4,%rdx
lea    0xdcf(%rip),%rsi
call   1090 <inet_pton@plt>
mov    -0x98(%rbp),%edi
call   1050 <htons@plt>
mov    %eax,-0x20(%rbp)
mov    0x2d92(%rip),%rax
mov    (%rax),%edi
mov    0x2d79(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10e0 <socket@plt>
mov    %eax,-0x94(%rbp)
cmp    $0xffffffff,%eax
jne    128d <main+0xcd>
lea    0xd97(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x94(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   10d0 <connect@plt>
cmp    $0xffffffff,%eax
jne    12c0 <main+0x100>
lea    0xd79(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x94(%rbp),%eax
mov    %eax,-0xb4(%rbp)
mov    -0xa0(%rbp),%rax
mov    %rax,-0xb0(%rbp)
mov    -0xa0(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0xb4(%rbp),%edi
mov    -0xb0(%rbp),%rsi
mov    %rax,%rdx
add    $0x1,%rdx
xor    %ecx,%ecx
call   1060 <send@plt>
mov    %eax,-0xa4(%rbp)
cmpl   $0xffffffff,-0xa4(%rbp)
jne    132a <main+0x16a>
lea    0xd1f(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x94(%rbp),%edi
lea    -0x90(%rbp),%rsi
mov    $0x64,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0xa4(%rbp)
cmpl   $0xffffffff,-0xa4(%rbp)
jne    136c <main+0x1ac>
lea    0xcea(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
lea    -0x90(%rbp),%rsi
lea    0xcda(%rip),%rdi
call   1070 <printf@plt>
mov    -0x94(%rbp),%edi
call   1080 <close@plt>
cmp    $0xffffffff,%eax
jne    13a9 <main+0x1e9>
lea    0xcd2(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
xor    %eax,%eax
add    $0xc0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
sub    $0x78,%rsp
cmp    $0x2,%edi
jl     11d1 <main+0x11>
mov    0x8(%rsi),%rbx
jmp    11d8 <main+0x18>
lea    0xe28(%rip),%rbx
mov    %rsp,%rdi
mov    $0xc,%esi
call   10b0 <bzero@plt>
mov    0x2dfc(%rip),%rbp
mov    0x0(%rbp),%edi
mov    %edi,0x8(%rsp)
lea    0x4(%rsp),%rdx
lea    0xe0c(%rip),%rsi
call   1090 <inet_pton@plt>
mov    $0x1f40,%edi
call   1050 <htons@plt>
mov    %eax,(%rsp)
mov    0x0(%rbp),%edi
mov    0x2dbd(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10e0 <socket@plt>
cmp    $0xffffffff,%eax
je     129a <main+0xda>
mov    %eax,%ebp
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0xc,%edx
call   10d0 <connect@plt>
cmp    $0xffffffff,%eax
je     12a3 <main+0xe3>
mov    %rbx,%rdi
call   1040 <strlen@plt>
lea    0x1(%rax),%rdx
mov    %ebp,%edi
mov    %rbx,%rsi
xor    %ecx,%ecx
call   1060 <send@plt>
cmp    $0xffffffff,%eax
je     12ac <main+0xec>
lea    0x10(%rsp),%rsi
mov    %ebp,%edi
mov    $0x64,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
cmp    $0xffffffff,%eax
je     12b5 <main+0xf5>
lea    0xdd9(%rip),%rdi
lea    0x10(%rsp),%rsi
call   1070 <printf@plt>
mov    %ebp,%edi
call   1080 <close@plt>
cmp    $0xffffffff,%eax
je     12be <main+0xfe>
xor    %eax,%eax
add    $0x78,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xd74(%rip),%rdi
jmp    12c5 <main+0x105>
lea    0xd80(%rip),%rdi
jmp    12c5 <main+0x105>
lea    0xd87(%rip),%rdi
jmp    12c5 <main+0x105>
lea    0xd8b(%rip),%rdi
jmp    12c5 <main+0x105>
lea    0xda7(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@.str = [11 x i8] c"test sting\00"
@AF_INET = global i32 0
@.str.1 = [10 x i8] c"127.0.0.1\00"
@SOCK_STREAM = global i32 0
@.str.2 = [21 x i8] c"fail to creat socket\00"
@.str.3 = [16 x i8] c"fail to connect\00"
@.str.4 = [13 x i8] c"fail to send\00"
@.str.5 = [13 x i8] c"fail to recv\00"
@.str.6 = [24 x i8] c"the length of str : %s\0A\00"
@.str.7 = [14 x i8] c"fail to close\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.sockaddr_in
%7 = alloca [100 x i8]
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 8000, i32* %9
store i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8** %10
%12 = load i32, i32* %4
%13 = icmp sgt i32 %12, 1
br i1 %13, label %14, label %18
14: ; preds = %2
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 1
%17 = load i8*, i8** %16
store i8* %17, i8** %10
br label %18
18: ; preds = %14, %2
%19 = call i32 @bzero(%struct.sockaddr_in* %6, i32 12)
%20 = load i32, i32* @AF_INET
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 2
store i32 %20, i32* %21
%22 = load i32, i32* @AF_INET
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
%24 = call i32 @inet_pton(i32 %22, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32* %23)
%25 = load i32, i32* %9
%26 = call i32 @htons(i32 %25)
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
store i32 %26, i32* %27
%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* %8
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %35
32: ; preds = %18
%33 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
%34 = call i32 @exit(i32 1)
unreachable
35: ; preds = %18
%36 = load i32, i32* %8
%37 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%38 = call i32 @connect(i32 %36, %struct.sockaddr* %37, i32 12)
%39 = icmp eq i32 %38, -1
br i1 %39, label %40, label %43
40: ; preds = %35
%41 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
%42 = call i32 @exit(i32 1)
unreachable
43: ; preds = %35
%44 = load i32, i32* %8
%45 = load i8*, i8** %10
%46 = load i8*, i8** %10
%47 = call i64 @strlen(i8* %46)
%48 = add nsw i64 %47, 1
%49 = call i32 @send(i32 %44, i8* %45, i64 %48, i32 0)
store i32 %49, i32* %11
%50 = load i32, i32* %11
%51 = icmp eq i32 %50, -1
br i1 %51, label %52, label %55
52: ; preds = %43
%53 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
%54 = call i32 @exit(i32 1)
unreachable
55: ; preds = %43
%56 = load i32, i32* %8
%57 = getelementptr inbounds [100 x i8], [100 x i8]* %7, i64 0, i64 0
%58 = call i32 @recv(i32 %56, i8* %57, i32 100, i32 0)
store i32 %58, i32* %11
%59 = load i32, i32* %11
%60 = icmp eq i32 %59, -1
br i1 %60, label %61, label %64
61: ; preds = %55
%62 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0))
%63 = call i32 @exit(i32 1)
unreachable
64: ; preds = %55
%65 = getelementptr inbounds [100 x i8], [100 x i8]* %7, i64 0, i64 0
%66 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.6, i64 0, i64 0), i8* %65)
%67 = load i32, i32* %8
%68 = call i32 @close(i32 %67)
%69 = icmp eq i32 %68, -1
br i1 %69, label %70, label %73
70: ; preds = %64
%71 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.7, i64 0, i64 0))
%72 = call i32 @exit(i32 1)
unreachable
73: ; preds = %64
ret i32 0
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@.str = [11 x i8] c"test sting\00"
@AF_INET = global i32 0
@.str.1 = [10 x i8] c"127.0.0.1\00"
@SOCK_STREAM = global i32 0
@.str.2 = [21 x i8] c"fail to creat socket\00"
@.str.3 = [16 x i8] c"fail to connect\00"
@.str.4 = [13 x i8] c"fail to send\00"
@.str.5 = [13 x i8] c"fail to recv\00"
@.str.6 = [24 x i8] c"the length of str : %s\0A\00"
@.str.7 = [14 x i8] c"fail to close\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca [100 x i8]
%5 = bitcast %struct.sockaddr_in* %3 to i8*
%6 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%7 = icmp sgt i32 %0, 1
br i1 %7, label %8, label %11
8: ; preds = %2
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
br label %11
11: ; preds = %8, %2
%12 = phi i8* [ %10, %8 ], [ getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), %2 ]
%13 = call i32 @bzero(%struct.sockaddr_in* %3, i32 12)
%14 = load i32, i32* @AF_INET
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %14, i32* %15
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
%17 = call i32 @inet_pton(i32 %14, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32* %16)
%18 = call i32 @htons(i32 8000)
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %18, i32* %19
%20 = load i32, i32* @AF_INET
%21 = load i32, i32* @SOCK_STREAM
%22 = call i32 @socket(i32 %20, i32 %21, i32 0)
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %27
24: ; preds = %11
%25 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
%26 = call i32 @exit(i32 1)
unreachable
27: ; preds = %11
%28 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%29 = call i32 @connect(i32 %22, %struct.sockaddr* %28, i32 12)
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %34
31: ; preds = %27
%32 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
%33 = call i32 @exit(i32 1)
unreachable
34: ; preds = %27
%35 = call i64 @strlen(i8* dereferenceable(1) %12)
%36 = add nsw i64 %35, 1
%37 = call i32 @send(i32 %22, i8* %12, i64 %36, i32 0)
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %42
39: ; preds = %34
%40 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
%41 = call i32 @exit(i32 1)
unreachable
42: ; preds = %34
%43 = call i32 @recv(i32 %22, i8* %6, i32 100, i32 0)
%44 = icmp eq i32 %43, -1
br i1 %44, label %45, label %48
45: ; preds = %42
%46 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0))
%47 = call i32 @exit(i32 1)
unreachable
48: ; preds = %42
%49 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.6, i64 0, i64 0), i8* %6)
%50 = call i32 @close(i32 %22)
%51 = icmp eq i32 %50, -1
br i1 %51, label %52, label %55
52: ; preds = %48
%53 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.7, i64 0, i64 0))
%54 = call i32 @exit(i32 1)
unreachable
55: ; preds = %48
ret i32 0
}