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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int BUFSIZ;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ SOCK_STREAM;
int accept(int, struct sockaddr *, int *);
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ bzero(int /*<<< orphan*/ *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ htons(int);
int listen(int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int recv(int, char *, int, int /*<<< orphan*/);
int send(int, char *, int, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  int fd, new_fd, struct_len, numbytes, i;
  struct sockaddr_in server_addr;
  struct sockaddr_in client_addr;
  char buff[BUFSIZ];
  server_addr.sin_family = AF_INET;
  server_addr.sin_port = htons(8000);
  server_addr.sin_addr.s_addr = INADDR_ANY;
  bzero(&(server_addr.sin_zero), 8);
  struct_len = sizeof(struct sockaddr_in);
  fd = socket(AF_INET, SOCK_STREAM, 0);
  while (bind(fd, (struct sockaddr *)&server_addr, struct_len) == -1)
    ;
  printf("Bind Success!\n");
  while (listen(fd, 10) == -1)
    ;
  printf("Listening....\n");
  printf("Ready for Accept,Waitting...\n");
  new_fd = accept(fd, (struct sockaddr *)&client_addr, &struct_len);
  printf("Get the Client.\n");
  numbytes = send(new_fd, "Welcome to my server\n", 21, 0);
  while ((numbytes = recv(new_fd, buff, BUFSIZ, 0)) > 0) {
    buff[numbytes] = '\0';
    printf("%s\n", buff);
    if (send(new_fd, buff, numbytes, 0) < 0) {
      perror("write");
      return 1;
    }
  }
  close(new_fd);
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2dfb(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x50(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x68(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x58(%rbp)
mov    0x2def(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    $0x1f40,%edi
call   1040 <htons@plt>
mov    %eax,-0x30(%rbp)
mov    0x2dce(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
lea    -0x38(%rbp),%rdi
mov    $0x8,%esi
call   10b0 <bzero@plt>
movl   $0x10,-0x1c(%rbp)
mov    0x2db5(%rip),%rax
mov    (%rax),%edi
mov    0x2d94(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x38(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1090 <bind@plt>
cmp    $0xffffffff,%eax
jne    1265 <main+0xb5>
jmp    1248 <main+0x98>
lea    0xd94(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x14(%rbp),%edi
mov    $0xa,%esi
call   1080 <listen@plt>
cmp    $0xffffffff,%eax
jne    128e <main+0xde>
jmp    1273 <main+0xc3>
lea    0xd7a(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    0xd7b(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x14(%rbp),%edi
lea    -0x48(%rbp),%rsi
lea    -0x1c(%rbp),%rdx
call   10c0 <accept@plt>
mov    %eax,-0x18(%rbp)
lea    0xd78(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x18(%rbp),%edi
lea    0xd78(%rip),%rsi
mov    $0x15,%edx
xor    %ecx,%ecx
call   1050 <send@plt>
mov    %eax,-0x20(%rbp)
mov    -0x68(%rbp),%rsi
mov    -0x18(%rbp),%edi
mov    0x2cd6(%rip),%rax
mov    (%rax),%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x20(%rbp)
cmp    $0x0,%eax
jle    135f <main+0x1af>
mov    -0x68(%rbp),%rsi
movslq -0x20(%rbp),%rax
movb   $0x0,(%rsi,%rax,1)
lea    0xd49(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x68(%rbp),%rsi
mov    -0x18(%rbp),%edi
mov    -0x20(%rbp),%edx
xor    %ecx,%ecx
call   1050 <send@plt>
cmp    $0x0,%eax
jge    135a <main+0x1aa>
lea    0xd25(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x5c(%rbp)
jmp    137d <main+0x1cd>
jmp    12e4 <main+0x134>
mov    -0x18(%rbp),%edi
call   1070 <close@plt>
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x5c(%rbp)
mov    -0x50(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %rsp,-0x38(%rbp)
mov    0x2dfc(%rip),%r13
mov    0x0(%r13),%eax
mov    %rsp,%r15
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r15
mov    %r15,%rsp
mov    0x2e00(%rip),%r14
mov    (%r14),%eax
mov    %eax,-0x3c(%rbp)
mov    $0x1f40,%edi
call   1050 <htons@plt>
mov    %eax,-0x40(%rbp)
mov    0x2dde(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
lea    -0x48(%rbp),%rbx
mov    %rbx,%rdi
mov    $0x8,%esi
call   10b0 <bzero@plt>
movl   $0x10,-0x2c(%rbp)
mov    (%r14),%edi
mov    0x2da7(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
mov    %eax,%r14d
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0x10,%edx
call   1090 <bind@plt>
cmp    $0xffffffff,%eax
je     1240 <main+0x90>
lea    0xdc0(%rip),%rdi
call   1040 <puts@plt>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r14d,%edi
mov    $0xa,%esi
call   1080 <listen@plt>
cmp    $0xffffffff,%eax
je     1270 <main+0xc0>
lea    0xda1(%rip),%rdi
call   1040 <puts@plt>
lea    0xda3(%rip),%rdi
call   1040 <puts@plt>
lea    -0x58(%rbp),%rsi
lea    -0x2c(%rbp),%rdx
mov    %r14d,%edi
call   10c0 <accept@plt>
mov    %eax,%r12d
lea    0xda1(%rip),%rdi
call   1040 <puts@plt>
lea    0xd40(%rip),%rsi
mov    %r12d,%edi
mov    $0x15,%edx
xor    %ecx,%ecx
call   1060 <send@plt>
nop
mov    0x0(%r13),%edx
mov    %r12d,%edi
mov    %r15,%rsi
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %eax,%eax
jle    131c <main+0x16c>
mov    %eax,%ebx
mov    %eax,%eax
movb   $0x0,(%r15,%rax,1)
mov    %r15,%rdi
call   1040 <puts@plt>
mov    %r12d,%edi
mov    %r15,%rsi
mov    %ebx,%edx
xor    %ecx,%ecx
call   1060 <send@plt>
test   %eax,%eax
jns    12d0 <main+0x120>
lea    0xd06(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%eax
jmp    132e <main+0x17e>
mov    %r12d,%edi
call   1070 <close@plt>
mov    %r14d,%edi
call   1070 <close@plt>
xor    %eax,%eax
mov    -0x38(%rbp),%rsp
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@BUFSIZ = global i32 0
@AF_INET = global i32 0
@INADDR_ANY = global i32 0
@SOCK_STREAM = global i32 0
@.str = [15 x i8] c"Bind Success!\0A\00"
@.str.1 = [15 x i8] c"Listening....\0A\00"
@.str.2 = [30 x i8] c"Ready for Accept,Waitting...\0A\00"
@.str.3 = [17 x i8] c"Get the Client.\0A\00"
@.str.4 = [22 x i8] c"Welcome to my server\0A\00"
@.str.5 = [4 x i8] c"%s\0A\00"
@.str.6 = [6 x i8] c"write\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 i32
%10 = alloca i32
%11 = alloca %struct.sockaddr_in
%12 = alloca %struct.sockaddr_in
%13 = alloca i8*
%14 = alloca i64
%15 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%16 = load i32, i32* @BUFSIZ
%17 = zext i32 %16 to i64
%18 = call i8* @llvm.stacksave()
store i8* %18, i8** %13
%19 = alloca i8, i64 %17
store i64 %17, i64* %14
%20 = load i32, i32* @AF_INET
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 3
store i32 %20, i32* %21
%22 = call i32 @htons(i32 8000)
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 2
store i32 %22, i32* %23
%24 = load i32, i32* @INADDR_ANY
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 1
%26 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %25, i32 0, i32 0
store i32 %24, i32* %26
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 0
%28 = call i32 @bzero(i32* %27, i32 8)
store i32 16, i32* %8
%29 = load i32, i32* @AF_INET
%30 = load i32, i32* @SOCK_STREAM
%31 = call i32 @socket(i32 %29, i32 %30, i32 0)
store i32 %31, i32* %6
br label %32
32: ; preds = %38, %2
%33 = load i32, i32* %6
%34 = bitcast %struct.sockaddr_in* %11 to %struct.sockaddr*
%35 = load i32, i32* %8
%36 = call i32 @bind(i32 %33, %struct.sockaddr* %34, i32 %35)
%37 = icmp eq i32 %36, -1
br i1 %37, label %38, label %39
38: ; preds = %32
br label %32
39: ; preds = %32
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
br label %41
41: ; preds = %45, %39
%42 = load i32, i32* %6
%43 = call i32 @listen(i32 %42, i32 10)
%44 = icmp eq i32 %43, -1
br i1 %44, label %45, label %46
45: ; preds = %41
br label %41
46: ; preds = %41
%47 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%48 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0))
%49 = load i32, i32* %6
%50 = bitcast %struct.sockaddr_in* %12 to %struct.sockaddr*
%51 = call i32 @accept(i32 %49, %struct.sockaddr* %50, i32* %8)
store i32 %51, i32* %7
%52 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0))
%53 = load i32, i32* %7
%54 = call i32 @send(i32 %53, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0), i32 21, i32 0)
store i32 %54, i32* %9
br label %55
55: ; preds = %71, %46
%56 = load i32, i32* %7
%57 = load i32, i32* @BUFSIZ
%58 = call i32 @recv(i32 %56, i8* %19, i32 %57, i32 0)
store i32 %58, i32* %9
%59 = icmp sgt i32 %58, 0
br i1 %59, label %60, label %72
60: ; preds = %55
%61 = load i32, i32* %9
%62 = sext i32 %61 to i64
%63 = getelementptr inbounds i8, i8* %19, i64 %62
store i8 0, i8* %63
%64 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i8* %19)
%65 = load i32, i32* %7
%66 = load i32, i32* %9
%67 = call i32 @send(i32 %65, i8* %19, i32 %66, i32 0)
%68 = icmp slt i32 %67, 0
br i1 %68, label %69, label %71
69: ; preds = %60
%70 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i64 0, i64 0))
store i32 1, i32* %3
store i32 1, i32* %15
br label %77
71: ; preds = %60
br label %55
72: ; preds = %55
%73 = load i32, i32* %7
%74 = call i32 @close(i32 %73)
%75 = load i32, i32* %6
%76 = call i32 @close(i32 %75)
store i32 0, i32* %3
store i32 1, i32* %15
br label %77
77: ; preds = %72, %69
%78 = load i8*, i8** %13
%79 = load i32, i32* %3
ret i32 %79
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@BUFSIZ = global i32 0
@AF_INET = global i32 0
@INADDR_ANY = global i32 0
@SOCK_STREAM = global i32 0
@.str.4 = [22 x i8] c"Welcome to my server\0A\00"
@.str.6 = [6 x i8] c"write\00"
@str = [14 x i8] c"Bind Success!\00"
@str.7 = [14 x i8] c"Listening....\00"
@str.8 = [29 x i8] c"Ready for Accept,Waitting...\00"
@str.9 = [16 x i8] c"Get the Client.\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i32
%4 = alloca %struct.sockaddr_in
%5 = alloca %struct.sockaddr_in
%6 = bitcast i32* %3 to i8*
%7 = bitcast %struct.sockaddr_in* %4 to i8*
%8 = bitcast %struct.sockaddr_in* %5 to i8*
%9 = load i32, i32* @BUFSIZ
%10 = zext i32 %9 to i64
%11 = tail call i8* @llvm.stacksave()
%12 = alloca i8, i64 %10
%13 = load i32, i32* @AF_INET
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 3
store i32 %13, i32* %14
%15 = tail call i32 @htons(i32 8000)
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 2
store i32 %15, i32* %16
%17 = load i32, i32* @INADDR_ANY
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1, i32 0
store i32 %17, i32* %18
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0
%20 = call i32 @bzero(i32* %19, i32 8)
store i32 16, i32* %3
%21 = load i32, i32* @AF_INET
%22 = load i32, i32* @SOCK_STREAM
%23 = tail call i32 @socket(i32 %21, i32 %22, i32 0)
%24 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
br label %25
25: ; preds = %25, %2
%26 = call i32 @bind(i32 %23, %struct.sockaddr* %24, i32 16)
%27 = icmp eq i32 %26, -1
br i1 %27, label %25, label %28
28: ; preds = %25
%29 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str, i64 0, i64 0))
br label %30
30: ; preds = %30, %28
%31 = call i32 @listen(i32 %23, i32 10)
%32 = icmp eq i32 %31, -1
br i1 %32, label %30, label %33
33: ; preds = %30
%34 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str.7, i64 0, i64 0))
%35 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @str.8, i64 0, i64 0))
%36 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%37 = call i32 @accept(i32 %23, %struct.sockaddr* %36, i32* %3)
%38 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str.9, i64 0, i64 0))
%39 = call i32 @send(i32 %37, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0), i32 21, i32 0)
br label %40
40: ; preds = %44, %33
%41 = load i32, i32* @BUFSIZ
%42 = call i32 @recv(i32 %37, i8* %12, i32 %41, i32 0)
%43 = icmp sgt i32 %42, 0
br i1 %43, label %44, label %52
44: ; preds = %40
%45 = zext i32 %42 to i64
%46 = getelementptr inbounds i8, i8* %12, i64 %45
store i8 0, i8* %46
%47 = call i32 @puts(i8* %12)
%48 = call i32 @send(i32 %37, i8* %12, i32 %42, i32 0)
%49 = icmp slt i32 %48, 0
br i1 %49, label %50, label %40
50: ; preds = %44
%51 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i64 0, i64 0))
br label %55
52: ; preds = %40
%53 = call i32 @close(i32 %37)
%54 = call i32 @close(i32 %23)
br label %55
55: ; preds = %52, %50
%56 = phi i32 [ 1, %50 ], [ 0, %52 ]
ret i32 %56
}