#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 {
  int /*<<< orphan*/ sin_port;
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ myaddr;
typedef int /*<<< orphan*/ content;
typedef int /*<<< orphan*/ clientaddr;

/* 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 /*<<< orphan*/ close(int);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
int listen(int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ send(int, char *, int, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main(void) {
  int server_fd, client_fd;
  struct sockaddr_in myaddr;
  struct sockaddr_in clientaddr;
  int client_len = sizeof(clientaddr);
  char content[] = "<html><h1>hello sundy<h1></html>";
  myaddr.sin_family = AF_INET;
  myaddr.sin_addr.s_addr = htonl(INADDR_ANY);
  myaddr.sin_port = htons(80);
  server_fd = socket(AF_INET, SOCK_STREAM, 0);
  if (-1 == server_fd) {
    perror("socket error");
  }
  if (bind(server_fd, (struct sockaddr *)&myaddr, sizeof(myaddr)) == -1) {
    perror("bind error");
  }
  if (listen(server_fd, 20) == -1) {
    perror("listen error");
  }
  while (1) {
    client_fd = accept(server_fd, (struct sockaddr *)&clientaddr, &client_len);
    if (-1 == client_fd) {
      perror("accept error");
    }
    printf("accept\n");
    send(client_fd, content, sizeof(content), 0);
    close(client_fd);
  }
  close(server_fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0xc,-0x2c(%rbp)
lea    -0x50(%rbp),%rdi
lea    0xe2f(%rip),%rsi
mov    $0x21,%edx
call   1080 <memcpy@plt>
mov    0x2e06(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    0x2df2(%rip),%rax
mov    (%rax),%edi
call   1060 <htonl@plt>
mov    %eax,-0x14(%rbp)
mov    $0x50,%edi
call   1030 <htons@plt>
mov    %eax,-0x18(%rbp)
mov    0x2ddc(%rip),%rax
mov    (%rax),%edi
mov    0x2dbb(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
mov    %eax,-0x8(%rbp)
mov    $0xffffffff,%eax
cmp    -0x8(%rbp),%eax
jne    123b <main+0x8b>
lea    0xdeb(%rip),%rdi
call   10b0 <perror@plt>
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   10a0 <bind@plt>
cmp    $0xffffffff,%eax
jne    1261 <main+0xb1>
lea    0xdd2(%rip),%rdi
call   10b0 <perror@plt>
mov    -0x8(%rbp),%edi
mov    $0x14,%esi
call   1090 <listen@plt>
cmp    $0xffffffff,%eax
jne    1283 <main+0xd3>
lea    0xdbb(%rip),%rdi
call   10b0 <perror@plt>
jmp    1288 <main+0xd8>
mov    -0x8(%rbp),%edi
lea    -0x28(%rbp),%rsi
lea    -0x2c(%rbp),%rdx
call   10c0 <accept@plt>
mov    %eax,-0xc(%rbp)
mov    $0xffffffff,%eax
cmp    -0xc(%rbp),%eax
jne    12b5 <main+0x105>
lea    0xd96(%rip),%rdi
call   10b0 <perror@plt>
lea    0xd97(%rip),%rdi
call   1050 <printf@plt>
mov    -0xc(%rbp),%edi
lea    -0x50(%rbp),%rsi
mov    $0x21,%edx
xor    %ecx,%ecx
call   1040 <send@plt>
mov    -0xc(%rbp),%edi
call   1070 <close@plt>
jmp    1288 <main+0xd8>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x58,%rsp
movl   $0xc,0xc(%rsp)
movaps 0xe53(%rip),%xmm0
movaps %xmm0,0x30(%rsp)
movaps 0xe37(%rip),%xmm0
movaps %xmm0,0x20(%rsp)
movb   $0x0,0x40(%rsp)
mov    0x2e0e(%rip),%rbx
mov    (%rbx),%eax
mov    %eax,0x18(%rsp)
mov    0x2df9(%rip),%rax
mov    (%rax),%edi
call   1060 <htonl@plt>
mov    %eax,0x14(%rsp)
mov    $0x50,%edi
call   1040 <htons@plt>
mov    %eax,0x10(%rsp)
mov    (%rbx),%edi
mov    0x2dc7(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     12b5 <main+0x115>
lea    0x10(%rsp),%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   1090 <bind@plt>
cmp    $0xffffffff,%eax
je     12c6 <main+0x126>
mov    %ebx,%edi
mov    $0x14,%esi
call   1080 <listen@plt>
cmp    $0xffffffff,%eax
je     12d7 <main+0x137>
lea    0x48(%rsp),%r14
lea    0xc(%rsp),%r15
lea    0xdf6(%rip),%r13
lea    0x20(%rsp),%rbp
jmp    1292 <main+0xf2>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %r13,%rdi
call   1030 <puts@plt>
mov    %r12d,%edi
mov    %rbp,%rsi
mov    $0x21,%edx
xor    %ecx,%ecx
call   1050 <send@plt>
mov    %r12d,%edi
call   1070 <close@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    %r15,%rdx
call   10b0 <accept@plt>
mov    %eax,%r12d
cmp    $0xffffffff,%eax
jne    1270 <main+0xd0>
lea    0xd98(%rip),%rdi
call   10a0 <perror@plt>
jmp    1270 <main+0xd0>
lea    0xd65(%rip),%rdi
call   10a0 <perror@plt>
jmp    121d <main+0x7d>
lea    0xd61(%rip),%rdi
call   10a0 <perror@plt>
jmp    1237 <main+0x97>
lea    0xd5b(%rip),%rdi
call   10a0 <perror@plt>
jmp    124c <main+0xac>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@__const.main.content = [33 x i8] c"<html><h1>hello sundy<h1></html>\00"
@AF_INET = global i32 0
@INADDR_ANY = global i32 0
@SOCK_STREAM = global i32 0
@.str = [13 x i8] c"socket error\00"
@.str.1 = [11 x i8] c"bind error\00"
@.str.2 = [13 x i8] c"listen error\00"
@.str.3 = [13 x i8] c"accept error\00"
@.str.4 = [8 x i8] c"accept\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.sockaddr_in
%5 = alloca %struct.sockaddr_in
%6 = alloca i32
%7 = alloca [33 x i8]
store i32 0, i32* %1
store i32 12, i32* %6
%8 = bitcast [33 x i8]* %7 to i8*
%9 = load i32, i32* @AF_INET
%10 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 2
store i32 %9, i32* %10
%11 = load i32, i32* @INADDR_ANY
%12 = call i32 @htonl(i32 %11)
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 1
%14 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %13, i32 0, i32 0
store i32 %12, i32* %14
%15 = call i32 @htons(i32 80)
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 0
store i32 %15, i32* %16
%17 = load i32, i32* @AF_INET
%18 = load i32, i32* @SOCK_STREAM
%19 = call i32 @socket(i32 %17, i32 %18, i32 0)
store i32 %19, i32* %2
%20 = load i32, i32* %2
%21 = icmp eq i32 -1, %20
br i1 %21, label %22, label %24
22: ; preds = %0
%23 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
br label %24
24: ; preds = %22, %0
%25 = load i32, i32* %2
%26 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%27 = call i32 @bind(i32 %25, %struct.sockaddr* %26, i32 12)
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %31
29: ; preds = %24
%30 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
br label %31
31: ; preds = %29, %24
%32 = load i32, i32* %2
%33 = call i32 @listen(i32 %32, i32 20)
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %37
35: ; preds = %31
%36 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
br label %37
37: ; preds = %35, %31
br label %38
38: ; preds = %37, %46
%39 = load i32, i32* %2
%40 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%41 = call i32 @accept(i32 %39, %struct.sockaddr* %40, i32* %6)
store i32 %41, i32* %3
%42 = load i32, i32* %3
%43 = icmp eq i32 -1, %42
br i1 %43, label %44, label %46
44: ; preds = %38
%45 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
br label %46
46: ; preds = %44, %38
%47 = call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0))
%48 = load i32, i32* %3
%49 = getelementptr inbounds [33 x i8], [33 x i8]* %7, i64 0, i64 0
%50 = call i32 @send(i32 %48, i8* %49, i32 33, i32 0)
%51 = load i32, i32* %3
%52 = call i32 @close(i32 %51)
br label %38
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@__const.main.content = [33 x i8] c"<html><h1>hello sundy<h1></html>\00"
@AF_INET = global i32 0
@INADDR_ANY = global i32 0
@SOCK_STREAM = global i32 0
@.str = [13 x i8] c"socket error\00"
@.str.1 = [11 x i8] c"bind error\00"
@.str.2 = [13 x i8] c"listen error\00"
@.str.3 = [13 x i8] c"accept error\00"
@str = [7 x i8] c"accept\00"
define i32 @main() {
%1 = alloca %struct.sockaddr_in
%2 = alloca %struct.sockaddr_in
%3 = alloca i32
%4 = alloca [33 x i8]
%5 = bitcast %struct.sockaddr_in* %1 to i8*
%6 = bitcast %struct.sockaddr_in* %2 to i8*
%7 = bitcast i32* %3 to i8*
store i32 12, i32* %3
%8 = getelementptr inbounds [33 x i8], [33 x i8]* %4, i64 0, i64 0
%9 = load i32, i32* @AF_INET
%10 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 2
store i32 %9, i32* %10
%11 = load i32, i32* @INADDR_ANY
%12 = tail call i32 @htonl(i32 %11)
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1, i32 0
store i32 %12, i32* %13
%14 = tail call i32 @htons(i32 80)
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0
store i32 %14, i32* %15
%16 = load i32, i32* @AF_INET
%17 = load i32, i32* @SOCK_STREAM
%18 = tail call i32 @socket(i32 %16, i32 %17, i32 0)
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %22
20: ; preds = %0
%21 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
br label %22
22: ; preds = %20, %0
%23 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%24 = call i32 @bind(i32 %18, %struct.sockaddr* %23, i32 12)
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %28
26: ; preds = %22
%27 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
br label %28
28: ; preds = %26, %22
%29 = call i32 @listen(i32 %18, i32 20)
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %33
31: ; preds = %28
%32 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
br label %33
33: ; preds = %31, %28
%34 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
br label %35
35: ; preds = %40, %33
%36 = call i32 @accept(i32 %18, %struct.sockaddr* %34, i32* %3)
%37 = icmp eq i32 %36, -1
br i1 %37, label %38, label %40
38: ; preds = %35
%39 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
br label %40
40: ; preds = %38, %35
%41 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @str, i64 0, i64 0))
%42 = call i32 @send(i32 %36, i8* %8, i32 33, i32 0)
%43 = call i32 @close(i32 %36)
br label %35
}