#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*/ buf;
typedef int /*<<< orphan*/ addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
int connect(int, struct sockaddr *, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_aton(char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main() {
  int cs = socket(AF_INET, SOCK_STREAM, 0);
  if (cs == -1) {
    printf("socket create : %m\n");
    exit(-1);
  }
  printf("create socket succecs......\n");
  struct sockaddr_in addr;
  addr.sin_family = AF_INET;
  addr.sin_port = htons(8686);
  inet_aton("192.168.137.3", &addr.sin_addr);
  int r = connect(cs, (struct sockaddr *)&addr, sizeof(addr));
  if (r == -1) {
    printf("socket connet : %m\n");
    close(cs);
    return -1;
  }
  printf("connet socket succecs......\n");
  char buf[100];
  while (1) {
    r = read(cs, buf, sizeof(buf));
    if (r == -1) {
      printf("socket read: %m\n");
      break;
    }
    if (r == 0) {
      printf("socket closed: %m\n");
      break;
    }
    if (r > 0) {
      buf[r] = '\0';
      printf("::%s\n", buf);
    }
  }
  close(cs);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e4f(%rip),%rax
mov    (%rax),%edi
mov    0x2e36(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    11d0 <main+0x50>
lea    0xe41(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
lea    0xe3d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2e03(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    $0x21ee,%edi
call   1030 <htons@plt>
mov    %eax,-0x14(%rbp)
lea    -0x18(%rbp),%rsi
lea    0xe2f(%rip),%rdi
call   1070 <inet_aton@plt>
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   1090 <connect@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    1247 <main+0xc7>
lea    0xe13(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12dc <main+0x15c>
lea    0xe05(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%edi
lea    -0x80(%rbp),%rsi
mov    $0x64,%edx
call   1060 <read@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    1286 <main+0x106>
lea    0xdf6(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12cd <main+0x14d>
cmpl   $0x0,-0x1c(%rbp)
jne    12a3 <main+0x123>
lea    0xdea(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12cd <main+0x14d>
cmpl   $0x0,-0x1c(%rbp)
jle    12c8 <main+0x148>
movslq -0x1c(%rbp),%rax
movb   $0x0,-0x80(%rbp,%rax,1)
lea    -0x80(%rbp),%rsi
lea    0xdd3(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1255 <main+0xd5>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x80,%rsp
mov    0x2e46(%rip),%rbx
mov    (%rbx),%edi
mov    0x2e2d(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
cmp    $0xffffffff,%eax
je     129a <main+0x10a>
mov    %eax,%ebp
lea    0xe9a(%rip),%rdi
call   1030 <puts@plt>
mov    (%rbx),%eax
mov    %eax,0x8(%rsp)
mov    $0x21ee,%edi
call   1040 <htons@plt>
mov    %eax,0x4(%rsp)
lea    0xe2e(%rip),%rdi
mov    %rsp,%rbx
mov    %rbx,%rsi
call   1080 <inet_aton@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0xc,%edx
call   10a0 <connect@plt>
cmp    $0xffffffff,%eax
je     1250 <main+0xc0>
lea    0xe70(%rip),%rdi
call   1030 <puts@plt>
lea    0x10(%rsp),%rbx
lea    0xe3d(%rip),%r14
nopl   (%rax)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x64,%edx
call   1070 <read@plt>
cmp    $0xffffffff,%eax
je     126c <main+0xdc>
test   %eax,%eax
je     1275 <main+0xe5>
jle    1220 <main+0x90>
mov    %eax,%eax
movb   $0x0,0x10(%rsp,%rax,1)
mov    %r14,%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    1220 <main+0x90>
lea    0xdcb(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %ebp,%edi
call   1060 <close@plt>
mov    $0xffffffff,%ebx
jmp    128c <main+0xfc>
lea    0xdc3(%rip),%rdi
jmp    127c <main+0xec>
lea    0xdcb(%rip),%rdi
xor    %ebx,%ebx
xor    %eax,%eax
call   1050 <printf@plt>
mov    %ebp,%edi
call   1060 <close@plt>
mov    %ebx,%eax
add    $0x80,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xd5f(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [20 x i8] c"socket create : %m\0A\00"
@.str.1 = [29 x i8] c"create socket succecs......\0A\00"
@.str.2 = [14 x i8] c"192.168.137.3\00"
@.str.3 = [20 x i8] c"socket connet : %m\0A\00"
@.str.4 = [29 x i8] c"connet socket succecs......\0A\00"
@.str.5 = [17 x i8] c"socket read: %m\0A\00"
@.str.6 = [19 x i8] c"socket closed: %m\0A\00"
@.str.7 = [6 x i8] c"::%s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.sockaddr_in
%4 = alloca i32
%5 = alloca [100 x i8]
store i32 0, i32* %1
%6 = load i32, i32* @AF_INET
%7 = load i32, i32* @SOCK_STREAM
%8 = call i32 @socket(i32 %6, i32 %7, i32 0)
store i32 %8, i32* %2
%9 = load i32, i32* %2
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %14
11: ; preds = %0
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
%13 = call i32 @exit(i32 -1)
unreachable
14: ; preds = %0
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
%16 = load i32, i32* @AF_INET
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 2
store i32 %16, i32* %17
%18 = call i32 @htons(i32 8686)
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 1
store i32 %18, i32* %19
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 0
%21 = call i32 @inet_aton(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32* %20)
%22 = load i32, i32* %2
%23 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%24 = call i32 @connect(i32 %22, %struct.sockaddr* %23, i32 12)
store i32 %24, i32* %4
%25 = load i32, i32* %4
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %31
27: ; preds = %14
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0))
%29 = load i32, i32* %2
%30 = call i32 @close(i32 %29)
store i32 -1, i32* %1
br label %59
31: ; preds = %14
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0))
br label %33
33: ; preds = %31, %55
%34 = load i32, i32* %2
%35 = getelementptr inbounds [100 x i8], [100 x i8]* %5, i64 0, i64 0
%36 = call i32 @read(i32 %34, i8* %35, i32 100)
store i32 %36, i32* %4
%37 = load i32, i32* %4
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %41
39: ; preds = %33
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.5, i64 0, i64 0))
br label %56
41: ; preds = %33
%42 = load i32, i32* %4
%43 = icmp eq i32 %42, 0
br i1 %43, label %44, label %46
44: ; preds = %41
%45 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.6, i64 0, i64 0))
br label %56
46: ; preds = %41
%47 = load i32, i32* %4
%48 = icmp sgt i32 %47, 0
br i1 %48, label %49, label %55
49: ; preds = %46
%50 = load i32, i32* %4
%51 = sext i32 %50 to i64
%52 = getelementptr inbounds [100 x i8], [100 x i8]* %5, i64 0, i64 %51
store i8 0, i8* %52
%53 = getelementptr inbounds [100 x i8], [100 x i8]* %5, i64 0, i64 0
%54 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.7, i64 0, i64 0), i8* %53)
br label %55
55: ; preds = %49, %46
br label %33
56: ; preds = %44, %39
%57 = load i32, i32* %2
%58 = call i32 @close(i32 %57)
store i32 0, i32* %1
br label %59
59: ; preds = %56, %27
%60 = load i32, i32* %1
ret i32 %60
}

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 = [20 x i8] c"socket create : %m\0A\00"
@.str.2 = [14 x i8] c"192.168.137.3\00"
@.str.3 = [20 x i8] c"socket connet : %m\0A\00"
@.str.5 = [17 x i8] c"socket read: %m\0A\00"
@.str.6 = [19 x i8] c"socket closed: %m\0A\00"
@.str.7 = [6 x i8] c"::%s\0A\00"
@str = [28 x i8] c"create socket succecs......\00"
@str.8 = [28 x i8] c"connet socket succecs......\00"
define i32 @main() {
%1 = alloca %struct.sockaddr_in
%2 = alloca [100 x i8]
%3 = load i32, i32* @AF_INET
%4 = load i32, i32* @SOCK_STREAM
%5 = tail call i32 @socket(i32 %3, i32 %4, i32 0)
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %10
7: ; preds = %0
%8 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
%9 = tail call i32 @exit(i32 -1)
unreachable
10: ; preds = %0
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @str, i64 0, i64 0))
%12 = bitcast %struct.sockaddr_in* %1 to i8*
%13 = load i32, i32* @AF_INET
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 2
store i32 %13, i32* %14
%15 = tail call i32 @htons(i32 8686)
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1
store i32 %15, i32* %16
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0
%18 = call i32 @inet_aton(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32* %17)
%19 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%20 = call i32 @connect(i32 %5, %struct.sockaddr* %19, i32 12)
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %25
22: ; preds = %10
%23 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0))
%24 = call i32 @close(i32 %5)
br label %42
25: ; preds = %10
%26 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @str.8, i64 0, i64 0))
%27 = getelementptr inbounds [100 x i8], [100 x i8]* %2, i64 0, i64 0
br label %28
28: ; preds = %36, %25
%29 = call i32 @read(i32 %5, i8* %27, i32 100)
switch i32 %29, label %30 [
i32 -1, label %38
i32 0, label %37
]
30: ; preds = %28
%31 = icmp sgt i32 %29, 0
br i1 %31, label %32, label %36
32: ; preds = %30
%33 = zext i32 %29 to i64
%34 = getelementptr inbounds [100 x i8], [100 x i8]* %2, i64 0, i64 %33
store i8 0, i8* %34
%35 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.7, i64 0, i64 0), i8* %27)
br label %36
36: ; preds = %32, %30
br label %28
37: ; preds = %28
br label %38
38: ; preds = %28, %37
%39 = phi i8* [ getelementptr inbounds ([19 x i8], [19 x i8]* @.str.6, i64 0, i64 0), %37 ], [ getelementptr inbounds ([17 x i8], [17 x i8]* @.str.5, i64 0, i64 0), %28 ]
%40 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) %39)
%41 = call i32 @close(i32 %5)
br label %42
42: ; preds = %38, %22
%43 = phi i32 [ -1, %22 ], [ 0, %38 ]
ret i32 %43
}