#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_family;
};
struct sockaddr {
  int dummy;
};
typedef int socklen_t;
typedef int /*<<< orphan*/ data;

/* 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*/ close(int);
int /*<<< orphan*/ htons(int);
int listen(int, int);
int /*<<< orphan*/ printf(char *, char *);
int read(int, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main() {
  int socket_server = socket(AF_INET, SOCK_STREAM, 0);
  struct sockaddr_in server_st, client_st;
  server_st.sin_family = AF_INET;
  server_st.sin_port = htons(8080);
  int ret = bind(socket_server, (struct sockaddr *)(&server_st),
                 sizeof(struct sockaddr));
  ret = listen(socket_server, 10);
  socklen_t len = sizeof(struct sockaddr);
  int socket_client =
      accept(socket_server, (struct sockaddr *)(&client_st), &len);
  char data[16];
  int len_read = read(socket_client, data, sizeof(data));
  if (len_read > 0) {
    data[len_read] = '\0';
    printf("read:%s\n", data);
  }
  close(socket_client);
  close(socket_server);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e52(%rip),%rax
mov    (%rax),%edi
mov    0x2e39(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0x8(%rbp)
mov    0x2e36(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
mov    $0x1f90,%edi
call   1030 <htons@plt>
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x10(%rbp),%rsi
mov    $0x4,%edx
call   1080 <bind@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%edi
mov    $0xa,%esi
call   1070 <listen@plt>
mov    %eax,-0x1c(%rbp)
movl   $0x4,-0x20(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
lea    -0x20(%rbp),%rdx
call   1090 <accept@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%edi
lea    -0x40(%rbp),%rsi
mov    $0x10,%edx
call   1060 <read@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0x0,-0x44(%rbp)
jle    1239 <main+0xb9>
movslq -0x44(%rbp),%rax
movb   $0x0,-0x40(%rbp,%rax,1)
lea    -0x40(%rbp),%rsi
lea    0xdcc(%rip),%rdi
call   1040 <printf@plt>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
sub    $0x28,%rsp
mov    0x2e5b(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2e41(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,%ebx
mov    0x0(%rbp),%eax
mov    %eax,0xc(%rsp)
mov    $0x1f90,%edi
call   1030 <htons@plt>
mov    %eax,0x8(%rsp)
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0x4,%edx
call   1080 <bind@plt>
mov    %ebx,%edi
mov    $0xa,%esi
call   1070 <listen@plt>
movl   $0x4,0x4(%rsp)
lea    0x20(%rsp),%rsi
lea    0x4(%rsp),%rdx
mov    %ebx,%edi
call   1090 <accept@plt>
mov    %eax,%ebp
lea    0x10(%rsp),%rsi
mov    %eax,%edi
mov    $0x10,%edx
call   1060 <read@plt>
test   %eax,%eax
jle    121c <main+0x9c>
mov    %eax,%eax
movb   $0x0,0x10(%rsp,%rax,1)
lea    0xdee(%rip),%rdi
lea    0x10(%rsp),%rsi
call   1040 <printf@plt>
mov    %ebp,%edi
call   1050 <close@plt>
mov    %ebx,%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [9 x i8] c"read:%s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.sockaddr_in
%4 = alloca %struct.sockaddr_in
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca [16 x i8]
%9 = alloca i32
store i32 0, i32* %1
%10 = load i32, i32* @AF_INET
%11 = load i32, i32* @SOCK_STREAM
%12 = call i32 @socket(i32 %10, i32 %11, i32 0)
store i32 %12, i32* %2
%13 = load i32, i32* @AF_INET
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 1
store i32 %13, i32* %14
%15 = call i32 @htons(i32 8080)
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 0
store i32 %15, i32* %16
%17 = load i32, i32* %2
%18 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%19 = call i32 @bind(i32 %17, %struct.sockaddr* %18, i32 4)
store i32 %19, i32* %5
%20 = load i32, i32* %2
%21 = call i32 @listen(i32 %20, i32 10)
store i32 %21, i32* %5
store i32 4, i32* %6
%22 = load i32, i32* %2
%23 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%24 = call i32 @accept(i32 %22, %struct.sockaddr* %23, i32* %6)
store i32 %24, i32* %7
%25 = load i32, i32* %7
%26 = getelementptr inbounds [16 x i8], [16 x i8]* %8, i64 0, i64 0
%27 = call i32 @read(i32 %25, i8* %26, i32 16)
store i32 %27, i32* %9
%28 = load i32, i32* %9
%29 = icmp sgt i32 %28, 0
br i1 %29, label %30, label %36
30: ; preds = %0
%31 = load i32, i32* %9
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds [16 x i8], [16 x i8]* %8, i64 0, i64 %32
store i8 0, i8* %33
%34 = getelementptr inbounds [16 x i8], [16 x i8]* %8, i64 0, i64 0
%35 = call i32 @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* %34)
br label %36
36: ; preds = %30, %0
%37 = load i32, i32* %7
%38 = call i32 @close(i32 %37)
%39 = load i32, i32* %2
%40 = call i32 @close(i32 %39)
ret i32 0
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [9 x i8] c"read:%s\0A\00"
define i32 @main() {
%1 = alloca %struct.sockaddr_in
%2 = alloca %struct.sockaddr_in
%3 = alloca i32
%4 = alloca [16 x i8]
%5 = load i32, i32* @AF_INET
%6 = load i32, i32* @SOCK_STREAM
%7 = tail call i32 @socket(i32 %5, i32 %6, i32 0)
%8 = bitcast %struct.sockaddr_in* %1 to i8*
%9 = bitcast %struct.sockaddr_in* %2 to i8*
%10 = load i32, i32* @AF_INET
%11 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1
store i32 %10, i32* %11
%12 = tail call i32 @htons(i32 8080)
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0
store i32 %12, i32* %13
%14 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%15 = call i32 @bind(i32 %7, %struct.sockaddr* %14, i32 4)
%16 = call i32 @listen(i32 %7, i32 10)
%17 = bitcast i32* %3 to i8*
store i32 4, i32* %3
%18 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%19 = call i32 @accept(i32 %7, %struct.sockaddr* %18, i32* %3)
%20 = getelementptr inbounds [16 x i8], [16 x i8]* %4, i64 0, i64 0
%21 = call i32 @read(i32 %19, i8* %20, i32 16)
%22 = icmp sgt i32 %21, 0
br i1 %22, label %23, label %27
23: ; preds = %0
%24 = zext i32 %21 to i64
%25 = getelementptr inbounds [16 x i8], [16 x i8]* %4, i64 0, i64 %24
store i8 0, i8* %25
%26 = call i32 @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* %20)
br label %27
27: ; preds = %23, %0
%28 = call i32 @close(i32 %19)
%29 = call i32 @close(i32 %7)
ret i32 0
}