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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int accept(int, struct sockaddr *, int *);
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_addr(char *);
int listen(int, int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main(int argc, char **argv) {
  int ret = -1;
  int listen_fd = -1;
  int accept_fd = -1;
  struct sockaddr_in server_addr;
  struct sockaddr_in client_addr;
  socklen_t addr_len = 0;
  listen_fd = socket(AF_INET, SOCK_STREAM, 0);
  if (listen_fd < 0) {
    printf("error to listen_fd\n");
  }
  memset(&server_addr, 0, sizeof(server_addr));
  memset(&client_addr, 0, sizeof(client_addr));
  server_addr.sin_family = AF_INET;
  server_addr.sin_port = htons(8888);
  server_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
  ret = bind(listen_fd, (struct sockaddr *)&server_addr, sizeof(server_addr));
  if (ret < 0) {
    printf("error to bind\n");
  }
  ret = listen(listen_fd, 20);
  if (ret < 0) {
    printf("error to listen\n");
    return -1;
  }
  char buf[1024];
  int recv_len = 0;
  addr_len = sizeof(client_addr);
  accept_fd = accept(listen_fd, (struct sockaddr *)&client_addr, &addr_len);
  if (accept_fd < 0) {
    printf("error to accept\n");
  }
  while (1) {
    recv_len = read(accept_fd, buf, sizeof(buf) - 1);
    if (recv_len < 0) {
      printf("error to read\n");
      exit(0);
    }
    buf[recv_len] = '\0';
    printf("recv buf: %s\n", buf);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x450,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0xffffffff,-0x14(%rbp)
movl   $0xffffffff,-0x18(%rbp)
movl   $0xffffffff,-0x1c(%rbp)
movl   $0x0,-0x3c(%rbp)
mov    0x2e0c(%rip),%rax
mov    (%rax),%edi
mov    0x2df3(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    1209 <main+0x69>
lea    0xdfe(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
lea    -0x38(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    0x2db8(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    $0x22b8,%edi
call   1030 <htons@plt>
mov    %eax,-0x24(%rbp)
lea    0xdcb(%rip),%rdi
call   1070 <inet_addr@plt>
mov    %eax,-0x28(%rbp)
mov    -0x18(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0xc,%edx
call   1090 <bind@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    127d <main+0xdd>
lea    0xda8(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%edi
mov    $0x14,%esi
call   1080 <listen@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    12b3 <main+0x113>
lea    0xd8f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0xffffffff,%eax
add    $0x450,%rsp
pop    %rbp
ret
movl   $0x0,-0x444(%rbp)
movl   $0xc,-0x3c(%rbp)
mov    -0x18(%rbp),%edi
lea    -0x38(%rbp),%rsi
lea    -0x3c(%rbp),%rdx
call   10a0 <accept@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    12ef <main+0x14f>
lea    0xd56(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12f4 <main+0x154>
mov    -0x1c(%rbp),%edi
lea    -0x440(%rbp),%rsi
mov    $0x3ff,%edx
call   1060 <read@plt>
mov    %eax,-0x444(%rbp)
cmpl   $0x0,-0x444(%rbp)
jge    1330 <main+0x190>
lea    0xd2d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %edi,%edi
call   10b0 <exit@plt>
movslq -0x444(%rbp),%rax
movb   $0x0,-0x440(%rbp,%rax,1)
lea    -0x440(%rbp),%rsi
lea    0xd11(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12f4 <main+0x154>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x430,%rsp
movl   $0x0,0xc(%rsp)
mov    0x2e1e(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2e04(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    11ef <main+0x3f>
lea    0xe6a(%rip),%rdi
call   1030 <puts@plt>
lea    0x10(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
lea    0x20(%rsp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x0(%rbp),%eax
mov    %eax,0x18(%rsp)
mov    $0x22b8,%edi
call   1040 <htons@plt>
mov    %eax,0x14(%rsp)
lea    0xdd0(%rip),%rdi
call   1080 <inet_addr@plt>
mov    %eax,0x10(%rsp)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0xc,%edx
call   10a0 <bind@plt>
test   %eax,%eax
jns    1258 <main+0xa8>
lea    0xdf3(%rip),%rdi
call   1030 <puts@plt>
mov    %ebx,%edi
mov    $0x14,%esi
call   1090 <listen@plt>
test   %eax,%eax
jns    1285 <main+0xd5>
lea    0xdc7(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%eax
add    $0x430,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
movl   $0xc,0xc(%rsp)
lea    0x20(%rsp),%rsi
lea    0xc(%rsp),%rdx
mov    %ebx,%edi
call   10b0 <accept@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    12b0 <main+0x100>
lea    0xd7b(%rip),%rdi
call   1030 <puts@plt>
lea    0x30(%rsp),%rsi
mov    %ebp,%edi
mov    $0x3ff,%edx
call   1070 <read@plt>
test   %eax,%eax
js     1307 <main+0x157>
lea    0xd3e(%rip),%r14
lea    0x30(%rsp),%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %eax,%eax
movb   $0x0,0x30(%rsp,%rax,1)
mov    %r14,%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x3ff,%edx
call   1070 <read@plt>
test   %eax,%eax
jns    12e0 <main+0x130>
lea    0xd0a(%rip),%rdi
call   1030 <puts@plt>
xor    %edi,%edi
call   10c0 <exit@plt>

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 = [20 x i8] c"error to listen_fd\0A\00"
@.str.1 = [10 x i8] c"127.0.0.1\00"
@.str.2 = [15 x i8] c"error to bind\0A\00"
@.str.3 = [17 x i8] c"error to listen\0A\00"
@.str.4 = [17 x i8] c"error to accept\0A\00"
@.str.5 = [15 x i8] c"error to read\0A\00"
@.str.6 = [14 x i8] c"recv buf: %s\0A\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 %struct.sockaddr_in
%10 = alloca %struct.sockaddr_in
%11 = alloca i32
%12 = alloca [1024 x i8]
%13 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 -1, i32* %6
store i32 -1, i32* %7
store i32 -1, i32* %8
store i32 0, i32* %11
%14 = load i32, i32* @AF_INET
%15 = load i32, i32* @SOCK_STREAM
%16 = call i32 @socket(i32 %14, i32 %15, i32 0)
store i32 %16, i32* %7
%17 = load i32, i32* %7
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %2
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
br label %21
21: ; preds = %19, %2
%22 = call i32 @memset(%struct.sockaddr_in* %9, i32 0, i32 12)
%23 = call i32 @memset(%struct.sockaddr_in* %10, i32 0, i32 12)
%24 = load i32, i32* @AF_INET
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 2
store i32 %24, i32* %25
%26 = call i32 @htons(i32 8888)
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 1
store i32 %26, i32* %27
%28 = call i32 @inet_addr(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%29 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 0
%30 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %29, i32 0, i32 0
store i32 %28, i32* %30
%31 = load i32, i32* %7
%32 = bitcast %struct.sockaddr_in* %9 to %struct.sockaddr*
%33 = call i32 @bind(i32 %31, %struct.sockaddr* %32, i32 12)
store i32 %33, i32* %6
%34 = load i32, i32* %6
%35 = icmp slt i32 %34, 0
br i1 %35, label %36, label %38
36: ; preds = %21
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
br label %38
38: ; preds = %36, %21
%39 = load i32, i32* %7
%40 = call i32 @listen(i32 %39, i32 20)
store i32 %40, i32* %6
%41 = load i32, i32* %6
%42 = icmp slt i32 %41, 0
br i1 %42, label %43, label %45
43: ; preds = %38
%44 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0))
ret i32 -1
45: ; preds = %38
store i32 0, i32* %13
store i32 12, i32* %11
%46 = load i32, i32* %7
%47 = bitcast %struct.sockaddr_in* %10 to %struct.sockaddr*
%48 = call i32 @accept(i32 %46, %struct.sockaddr* %47, i32* %11)
store i32 %48, i32* %8
%49 = load i32, i32* %8
%50 = icmp slt i32 %49, 0
br i1 %50, label %51, label %53
51: ; preds = %45
%52 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0))
br label %53
53: ; preds = %51, %45
br label %54
54: ; preds = %53, %63
%55 = load i32, i32* %8
%56 = getelementptr inbounds [1024 x i8], [1024 x i8]* %12, i64 0, i64 0
%57 = call i32 @read(i32 %55, i8* %56, i32 1023)
store i32 %57, i32* %13
%58 = load i32, i32* %13
%59 = icmp slt i32 %58, 0
br i1 %59, label %60, label %63
60: ; preds = %54
%61 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0))
%62 = call i32 @exit(i32 0)
unreachable
63: ; preds = %54
%64 = load i32, i32* %13
%65 = sext i32 %64 to i64
%66 = getelementptr inbounds [1024 x i8], [1024 x i8]* %12, i64 0, i64 %65
store i8 0, i8* %66
%67 = getelementptr inbounds [1024 x i8], [1024 x i8]* %12, i64 0, i64 0
%68 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.6, i64 0, i64 0), i8* %67)
br label %54
}

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.1 = [10 x i8] c"127.0.0.1\00"
@.str.6 = [14 x i8] c"recv buf: %s\0A\00"
@str = [14 x i8] c"error to read\00"
@str.7 = [16 x i8] c"error to accept\00"
@str.8 = [16 x i8] c"error to listen\00"
@str.9 = [14 x i8] c"error to bind\00"
@str.10 = [19 x i8] c"error to listen_fd\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 = bitcast %struct.sockaddr_in* %4 to i8*
%9 = bitcast i32* %5 to i8*
store i32 0, i32* %5
%10 = load i32, i32* @AF_INET
%11 = load i32, i32* @SOCK_STREAM
%12 = tail call i32 @socket(i32 %10, i32 %11, i32 0)
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %2
%15 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str.10, i64 0, i64 0))
br label %16
16: ; preds = %14, %2
%17 = call i32 @memset(%struct.sockaddr_in* %3, i32 0, i32 12)
%18 = call i32 @memset(%struct.sockaddr_in* %4, i32 0, i32 12)
%19 = load i32, i32* @AF_INET
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %19, i32* %20
%21 = call i32 @htons(i32 8888)
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %21, i32* %22
%23 = call i32 @inet_addr(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0, i32 0
store i32 %23, i32* %24
%25 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%26 = call i32 @bind(i32 %12, %struct.sockaddr* %25, i32 12)
%27 = icmp slt i32 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %16
%29 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str.9, i64 0, i64 0))
br label %30
30: ; preds = %28, %16
%31 = call i32 @listen(i32 %12, i32 20)
%32 = icmp slt i32 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %30
%34 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str.8, i64 0, i64 0))
ret i32 -1
35: ; preds = %30
%36 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
store i32 12, i32* %5
%37 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%38 = call i32 @accept(i32 %12, %struct.sockaddr* %37, i32* %5)
%39 = icmp slt i32 %38, 0
br i1 %39, label %40, label %42
40: ; preds = %35
%41 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str.7, i64 0, i64 0))
br label %42
42: ; preds = %40, %35
%43 = call i32 @read(i32 %38, i8* %36, i32 1023)
%44 = icmp slt i32 %43, 0
br i1 %44, label %45, label %48
45: ; preds = %48, %42
%46 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str, i64 0, i64 0))
%47 = call i32 @exit(i32 0)
unreachable
48: ; preds = %42, %48
%49 = phi i32 [ %53, %48 ], [ %43, %42 ]
%50 = zext i32 %49 to i64
%51 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 %50
store i8 0, i8* %51
%52 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.6, i64 0, i64 0), i8* %36)
%53 = call i32 @read(i32 %38, i8* %36, i32 1023)
%54 = icmp slt i32 %53, 0
br i1 %54, label %45, label %48
}