#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
struct TYPE_2__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int socklen_t;
typedef int /*<<< orphan*/ cli_addr;
typedef int /*<<< orphan*/ buff;
typedef int /*<<< orphan*/ addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_STREAM;
int accept(int, struct sockaddr *, int *);
int bind(int, struct sockaddr *, int);
int errno;
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
int listen(int, int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, ...);
int recv(int, char *, int, int /*<<< orphan*/);
int send(int, char *, int /*<<< orphan*/, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ sprintf(char *, char *);
int /*<<< orphan*/ strlen(char *);
int main(int argc, char *const *argv) {
  int fd;
  struct sockaddr_in addr;
  fd = socket(AF_INET, SOCK_STREAM, 0);
  if (fd == -1) {
    printf("create socket fail, errno: %d", errno);
    return -1;
  }
  addr.sin_family = AF_INET;
  addr.sin_port = htons(8060);
  addr.sin_addr.s_addr = htonl(INADDR_ANY);
  if (bind(fd, (struct sockaddr *)&addr, sizeof(addr)) == -1) {
    printf("bind fail, errno: %d", errno);
    return -1;
  }
  if (listen(fd, 20) == -1) {
    printf("listen fail, errno: %d", errno);
    return -1;
  }
  struct sockaddr_in cli_addr;
  socklen_t addr_len = sizeof(cli_addr);
  int conn = accept(fd, (struct sockaddr *)&cli_addr, &addr_len);
  if (conn < 0) {
    printf("accept fail, errno: %d", errno);
    return -1;
  }
  printf("client connect\n");
  char buff[1024];
  memset(buff, 0, sizeof(buff));
  sprintf(buff, "aaaaa");
  int len = send(conn, buff, strlen(buff), 0);
  while (1) {
    memset(buff, 0, sizeof(buff));
    len = recv(conn, buff, sizeof(buff), 0);
    if (len <= 0) {
      printf("client disconnect, errno: %d", errno);
      break;
    }
    printf("got resp: %s", buff);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x460,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e08(%rip),%rax
mov    (%rax),%edi
mov    0x2de7(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10e0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1222 <main+0x62>
mov    0x2dc2(%rip),%rax
mov    (%rax),%esi
lea    0xdf1(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13f5 <main+0x235>
mov    0x2dbf(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    $0x1f7c,%edi
call   1050 <htons@plt>
mov    %eax,-0x1c(%rbp)
mov    0x2d9e(%rip),%rax
mov    (%rax),%edi
call   1080 <htonl@plt>
mov    %eax,-0x20(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   10b0 <bind@plt>
cmp    $0xffffffff,%eax
jne    1289 <main+0xc9>
mov    0x2d5b(%rip),%rax
mov    (%rax),%esi
lea    0xda8(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13f5 <main+0x235>
mov    -0x14(%rbp),%edi
mov    $0x14,%esi
call   10a0 <listen@plt>
cmp    $0xffffffff,%eax
jne    12c2 <main+0x102>
mov    0x2d22(%rip),%rax
mov    (%rax),%esi
lea    0xd84(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13f5 <main+0x235>
movl   $0xc,-0x34(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x30(%rbp),%rsi
lea    -0x34(%rbp),%rdx
call   10c0 <accept@plt>
mov    %eax,-0x38(%rbp)
cmpl   $0x0,-0x38(%rbp)
jge    1309 <main+0x149>
mov    0x2cdb(%rip),%rax
mov    (%rax),%esi
lea    0xd54(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13f5 <main+0x235>
lea    0xd51(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
lea    -0x440(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1090 <memset@plt>
lea    -0x440(%rbp),%rdi
lea    0xd39(%rip),%rsi
call   10d0 <sprintf@plt>
mov    -0x38(%rbp),%eax
mov    %eax,-0x454(%rbp)
lea    -0x440(%rbp),%rax
mov    %rax,-0x450(%rbp)
lea    -0x440(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x454(%rbp),%edi
mov    -0x450(%rbp),%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1060 <send@plt>
mov    %eax,-0x444(%rbp)
lea    -0x440(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1090 <memset@plt>
mov    -0x38(%rbp),%edi
lea    -0x440(%rbp),%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x444(%rbp)
cmpl   $0x0,-0x444(%rbp)
jg     13d4 <main+0x214>
mov    0x2c09(%rip),%rax
mov    (%rax),%esi
lea    0xcaf(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
jmp    13ee <main+0x22e>
lea    -0x440(%rbp),%rsi
lea    0xcb2(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
jmp    137c <main+0x1bc>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x460,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x438,%rsp
mov    0x2e14(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2df2(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10e0 <socket@plt>
cmp    $0xffffffff,%eax
je     133a <main+0x17a>
mov    %eax,%ebx
mov    0x0(%rbp),%eax
mov    %eax,0x18(%rsp)
mov    $0x1f7c,%edi
call   1060 <htons@plt>
mov    %eax,0x14(%rsp)
mov    0x2dd2(%rip),%rax
mov    (%rax),%edi
call   1090 <htonl@plt>
mov    %eax,0x10(%rsp)
lea    0x10(%rsp),%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   10c0 <bind@plt>
cmp    $0xffffffff,%eax
je     134c <main+0x18c>
mov    %ebx,%edi
mov    $0x14,%esi
call   10b0 <listen@plt>
cmp    $0xffffffff,%eax
je     135e <main+0x19e>
movl   $0xc,0xc(%rsp)
lea    0x20(%rsp),%rsi
lea    0xc(%rsp),%rdx
mov    %ebx,%edi
call   10d0 <accept@plt>
test   %eax,%eax
js     1370 <main+0x1b0>
mov    %eax,%ebp
lea    0xe1f(%rip),%rdi
call   1040 <puts@plt>
lea    0x30(%rsp),%rbx
xor    %r14d,%r14d
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   10a0 <memset@plt>
movl   $0x61616161,0x30(%rsp)
61
movw   $0x61,0x34(%rsp)
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1070 <send@plt>
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   10a0 <memset@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %eax,%eax
jle    1321 <main+0x161>
lea    0xda6(%rip),%r15
lea    0x30(%rsp),%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %r15,%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1080 <printf@plt>
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   10a0 <memset@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %eax,%eax
jg     12f0 <main+0x130>
mov    0x2ca0(%rip),%rax
mov    (%rax),%esi
lea    0xd36(%rip),%rdi
xor    %eax,%eax
call   1080 <printf@plt>
jmp    138d <main+0x1cd>
mov    0x2c87(%rip),%rax
mov    (%rax),%esi
lea    0xcb6(%rip),%rdi
jmp    1380 <main+0x1c0>
mov    0x2c75(%rip),%rax
mov    (%rax),%esi
lea    0xcc2(%rip),%rdi
jmp    1380 <main+0x1c0>
mov    0x2c63(%rip),%rax
mov    (%rax),%esi
lea    0xcc5(%rip),%rdi
jmp    1380 <main+0x1c0>
mov    0x2c51(%rip),%rax
mov    (%rax),%esi
lea    0xcca(%rip),%rdi
xor    %eax,%eax
call   1080 <printf@plt>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
add    $0x438,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [30 x i8] c"create socket fail, errno: %d\00"
@errno = global i32 0
@INADDR_ANY = global i32 0
@.str.1 = [21 x i8] c"bind fail, errno: %d\00"
@.str.2 = [23 x i8] c"listen fail, errno: %d\00"
@.str.3 = [23 x i8] c"accept fail, errno: %d\00"
@.str.4 = [16 x i8] c"client connect\0A\00"
@.str.5 = [6 x i8] c"aaaaa\00"
@.str.6 = [29 x i8] c"client disconnect, errno: %d\00"
@.str.7 = [13 x i8] c"got resp: %s\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca %struct.sockaddr_in
%8 = alloca %struct.sockaddr_in
%9 = alloca i32
%10 = alloca i32
%11 = alloca [1024 x i8]
%12 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%13 = load i32, i32* @AF_INET
%14 = load i32, i32* @SOCK_STREAM
%15 = call i32 @socket(i32 %13, i32 %14, i32 0)
store i32 %15, i32* %6
%16 = load i32, i32* %6
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %21
18: ; preds = %2
%19 = load i32, i32* @errno
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i32 %19)
store i32 -1, i32* %3
br label %79
21: ; preds = %2
%22 = load i32, i32* @AF_INET
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 2
store i32 %22, i32* %23
%24 = call i32 @htons(i32 8060)
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 1
store i32 %24, i32* %25
%26 = load i32, i32* @INADDR_ANY
%27 = call i32 @htonl(i32 %26)
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
%29 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %28, i32 0, i32 0
store i32 %27, i32* %29
%30 = load i32, i32* %6
%31 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%32 = call i32 @bind(i32 %30, %struct.sockaddr* %31, i32 12)
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %37
34: ; preds = %21
%35 = load i32, i32* @errno
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i32 %35)
store i32 -1, i32* %3
br label %79
37: ; preds = %21
%38 = load i32, i32* %6
%39 = call i32 @listen(i32 %38, i32 20)
%40 = icmp eq i32 %39, -1
br i1 %40, label %41, label %44
41: ; preds = %37
%42 = load i32, i32* @errno
%43 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i32 %42)
store i32 -1, i32* %3
br label %79
44: ; preds = %37
store i32 12, i32* %9
%45 = load i32, i32* %6
%46 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%47 = call i32 @accept(i32 %45, %struct.sockaddr* %46, i32* %9)
store i32 %47, i32* %10
%48 = load i32, i32* %10
%49 = icmp slt i32 %48, 0
br i1 %49, label %50, label %53
50: ; preds = %44
%51 = load i32, i32* @errno
%52 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i32 %51)
store i32 -1, i32* %3
br label %79
53: ; preds = %44
%54 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0))
%55 = getelementptr inbounds [1024 x i8], [1024 x i8]* %11, i64 0, i64 0
%56 = call i32 @memset(i8* %55, i32 0, i32 1024)
%57 = getelementptr inbounds [1024 x i8], [1024 x i8]* %11, i64 0, i64 0
%58 = call i32 @sprintf(i8* %57, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
%59 = load i32, i32* %10
%60 = getelementptr inbounds [1024 x i8], [1024 x i8]* %11, i64 0, i64 0
%61 = getelementptr inbounds [1024 x i8], [1024 x i8]* %11, i64 0, i64 0
%62 = call i32 @strlen(i8* %61)
%63 = call i32 @send(i32 %59, i8* %60, i32 %62, i32 0)
store i32 %63, i32* %12
br label %64
64: ; preds = %53, %75
%65 = getelementptr inbounds [1024 x i8], [1024 x i8]* %11, i64 0, i64 0
%66 = call i32 @memset(i8* %65, i32 0, i32 1024)
%67 = load i32, i32* %10
%68 = getelementptr inbounds [1024 x i8], [1024 x i8]* %11, i64 0, i64 0
%69 = call i32 @recv(i32 %67, i8* %68, i32 1024, i32 0)
store i32 %69, i32* %12
%70 = load i32, i32* %12
%71 = icmp sle i32 %70, 0
br i1 %71, label %72, label %75
72: ; preds = %64
%73 = load i32, i32* @errno
%74 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.6, i64 0, i64 0), i32 %73)
br label %78
75: ; preds = %64
%76 = getelementptr inbounds [1024 x i8], [1024 x i8]* %11, i64 0, i64 0
%77 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.7, i64 0, i64 0), i8* %76)
br label %64
78: ; preds = %72
store i32 0, i32* %3
br label %79
79: ; preds = %78, %50, %41, %34, %18
%80 = load i32, i32* %3
ret i32 %80
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [30 x i8] c"create socket fail, errno: %d\00"
@errno = global i32 0
@INADDR_ANY = global i32 0
@.str.1 = [21 x i8] c"bind fail, errno: %d\00"
@.str.2 = [23 x i8] c"listen fail, errno: %d\00"
@.str.3 = [23 x i8] c"accept fail, errno: %d\00"
@.str.5 = [6 x i8] c"aaaaa\00"
@.str.6 = [29 x i8] c"client disconnect, errno: %d\00"
@.str.7 = [13 x i8] c"got resp: %s\00"
@str = [15 x i8] c"client connect\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca %struct.sockaddr_in
%5 = alloca i32
%6 = alloca [1024 x i8]
%7 = bitcast %struct.sockaddr_in* %3 to i8*
%8 = load i32, i32* @AF_INET
%9 = load i32, i32* @SOCK_STREAM
%10 = tail call i32 @socket(i32 %8, i32 %9, i32 0)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %15
12: ; preds = %2
%13 = load i32, i32* @errno
%14 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i32 %13)
br label %63
15: ; preds = %2
%16 = load i32, i32* @AF_INET
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %16, i32* %17
%18 = tail call i32 @htons(i32 8060)
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %18, i32* %19
%20 = load i32, i32* @INADDR_ANY
%21 = tail call i32 @htonl(i32 %20)
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0, i32 0
store i32 %21, i32* %22
%23 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%24 = call i32 @bind(i32 %10, %struct.sockaddr* %23, i32 12)
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %29
26: ; preds = %15
%27 = load i32, i32* @errno
%28 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i32 %27)
br label %63
29: ; preds = %15
%30 = call i32 @listen(i32 %10, i32 20)
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %35
32: ; preds = %29
%33 = load i32, i32* @errno
%34 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i32 %33)
br label %63
35: ; preds = %29
%36 = bitcast %struct.sockaddr_in* %4 to i8*
%37 = bitcast i32* %5 to i8*
store i32 12, i32* %5
%38 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%39 = call i32 @accept(i32 %10, %struct.sockaddr* %38, i32* %5)
%40 = icmp slt i32 %39, 0
br i1 %40, label %41, label %44
41: ; preds = %35
%42 = load i32, i32* @errno
%43 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i32 %42)
br label %61
44: ; preds = %35
%45 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str, i64 0, i64 0))
%46 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%47 = call i32 @memset(i8* %46, i32 0, i32 1024)
%48 = call i32 @strlen(i8* %46)
%49 = call i32 @send(i32 %39, i8* %46, i32 %48, i32 0)
%50 = call i32 @memset(i8* %46, i32 0, i32 1024)
%51 = call i32 @recv(i32 %39, i8* %46, i32 1024, i32 0)
%52 = icmp slt i32 %51, 1
br i1 %52, label %53, label %56
53: ; preds = %56, %44
%54 = load i32, i32* @errno
%55 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str.6, i64 0, i64 0), i32 %54)
br label %61
56: ; preds = %44, %56
%57 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.7, i64 0, i64 0), i8* %46)
%58 = call i32 @memset(i8* %46, i32 0, i32 1024)
%59 = call i32 @recv(i32 %39, i8* %46, i32 1024, i32 0)
%60 = icmp slt i32 %59, 1
br i1 %60, label %53, label %56
61: ; preds = %53, %41
%62 = phi i32 [ -1, %41 ], [ 0, %53 ]
br label %63
63: ; preds = %61, %32, %26, %12
%64 = phi i32 [ -1, %12 ], [ -1, %26 ], [ -1, %32 ], [ %62, %61 ]
ret i32 %64
}