#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 /*<<< orphan*/ my_addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int BUFSIZ;
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int accept(int, struct sockaddr *, int *);
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ htons(int);
char *inet_ntoa(TYPE_1__);
int /*<<< orphan*/ listen(int, int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, 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 server_sockfd;
  int client_sockfd;
  int len;
  struct sockaddr_in my_addr;
  struct sockaddr_in remote_addr;
  int sin_size;
  char buf[BUFSIZ];
  memset(&my_addr, 0, sizeof(my_addr));
  my_addr.sin_family = AF_INET;
  my_addr.sin_addr.s_addr = INADDR_ANY;
  my_addr.sin_port = htons(8000);
  if ((server_sockfd = socket(PF_INET, SOCK_STREAM, 0)) < 0) {
    perror("socket error");
    return 1;
  }
  if (bind(server_sockfd, (struct sockaddr *)&my_addr,
           sizeof(struct sockaddr)) < 0) {
    perror("bind error");
    return 1;
  }
  listen(server_sockfd, 5);
  sin_size = sizeof(struct sockaddr_in);
  if ((client_sockfd = accept(server_sockfd, (struct sockaddr *)&remote_addr,
                              &sin_size)) < 0) {
    perror("accept error");
    return 1;
  }
  printf("accept client %s\n", inet_ntoa(remote_addr.sin_addr));
  len = send(client_sockfd, "Welcome to my server\n", 21, 0);
  while ((len = recv(client_sockfd, buf, BUFSIZ, 0)) > 0) {
    buf[len] = '\0';
    printf("%s\n", buf);
    if (send(client_sockfd, buf, len, 0) < 0) {
      perror("write error ");
      return 1;
    }
  }
  close(client_sockfd);
  close(server_sockfd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2deb(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x48(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x60(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x50(%rbp)
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1080 <memset@plt>
mov    0x2dcf(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2dbb(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    $0x1f40,%edi
call   1050 <htons@plt>
mov    %eax,-0x24(%rbp)
mov    0x2d82(%rip),%rax
mov    (%rax),%edi
mov    0x2d89(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10e0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    127b <main+0xbb>
lea    0xd9d(%rip),%rdi
call   10c0 <perror@plt>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x54(%rbp)
jmp    13cb <main+0x20b>
mov    -0x14(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0x4,%edx
call   10b0 <bind@plt>
cmp    $0x0,%rax
jge    12b5 <main+0xf5>
lea    0xd70(%rip),%rdi
call   10c0 <perror@plt>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x54(%rbp)
jmp    13cb <main+0x20b>
mov    -0x14(%rbp),%edi
mov    $0x5,%esi
call   10a0 <listen@plt>
movl   $0xc,-0x3c(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x38(%rbp),%rsi
lea    -0x3c(%rbp),%rdx
call   10d0 <accept@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jge    1304 <main+0x144>
lea    0xd2c(%rip),%rdi
call   10c0 <perror@plt>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x54(%rbp)
jmp    13cb <main+0x20b>
mov    -0x38(%rbp),%edi
call   1040 <inet_ntoa@plt>
mov    %rax,%rsi
lea    0xd0f(%rip),%rdi
call   1070 <printf@plt>
mov    -0x18(%rbp),%edi
lea    0xd12(%rip),%rsi
mov    $0x15,%edx
xor    %ecx,%ecx
call   1060 <send@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x60(%rbp),%rsi
mov    -0x18(%rbp),%edi
mov    0x2c86(%rip),%rax
mov    (%rax),%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jle    13ad <main+0x1ed>
mov    -0x60(%rbp),%rsi
movslq -0x1c(%rbp),%rax
movb   $0x0,(%rsi,%rax,1)
lea    0xcc9(%rip),%rdi
call   1070 <printf@plt>
mov    -0x60(%rbp),%rsi
mov    -0x18(%rbp),%edi
mov    -0x1c(%rbp),%edx
xor    %ecx,%ecx
call   1060 <send@plt>
cmp    $0x0,%eax
jge    13a8 <main+0x1e8>
lea    0xcbd(%rip),%rdi
call   10c0 <perror@plt>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x54(%rbp)
jmp    13cb <main+0x20b>
jmp    1334 <main+0x174>
mov    -0x18(%rbp),%edi
call   1090 <close@plt>
mov    -0x14(%rbp),%edi
call   1090 <close@plt>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x54(%rbp)
mov    -0x48(%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,%r12
mov    0x2ded(%rip),%r14
mov    (%r14),%eax
mov    %rsp,%r13
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r13
mov    %r13,%rsp
lea    -0x40(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1080 <memset@plt>
mov    0x2de2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
mov    0x2dce(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x40(%rbp)
mov    $0x1f40,%edi
call   1050 <htons@plt>
mov    %eax,-0x3c(%rbp)
mov    0x2d95(%rip),%rax
mov    (%rax),%edi
mov    0x2d9c(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10e0 <socket@plt>
test   %eax,%eax
js     1347 <main+0x187>
mov    %eax,%ebx
lea    -0x40(%rbp),%rsi
mov    %eax,%edi
mov    $0x4,%edx
call   10b0 <bind@plt>
test   %rax,%rax
js     1350 <main+0x190>
mov    %ebx,%edi
mov    $0x5,%esi
call   10a0 <listen@plt>
movl   $0xc,-0x30(%rbp)
lea    -0x58(%rbp),%rsi
lea    -0x30(%rbp),%rdx
mov    %ebx,%edi
call   10d0 <accept@plt>
test   %eax,%eax
js     1359 <main+0x199>
mov    %eax,%r15d
mov    %ebx,-0x2c(%rbp)
mov    %r12,-0x48(%rbp)
mov    -0x58(%rbp),%edi
call   1040 <inet_ntoa@plt>
lea    0xd82(%rip),%rdi
mov    %rax,%rsi
call   1070 <printf@plt>
lea    0xd85(%rip),%rsi
mov    %r15d,%edi
mov    $0x15,%edx
xor    %ecx,%ecx
call   1060 <send@plt>
lea    0xd6b(%rip),%r12
nopl   0x0(%rax,%rax,1)
mov    (%r14),%edx
mov    %r15d,%edi
mov    %r13,%rsi
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %eax,%eax
jle    131f <main+0x15f>
mov    %eax,%ebx
mov    %eax,%eax
movb   $0x0,0x0(%r13,%rax,1)
mov    %r12,%rdi
mov    %r13,%rsi
call   1070 <printf@plt>
mov    %r15d,%edi
mov    %r13,%rsi
mov    %ebx,%edx
xor    %ecx,%ecx
call   1060 <send@plt>
test   %eax,%eax
jns    12d0 <main+0x110>
lea    0xd3a(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x1,%eax
jmp    1331 <main+0x171>
mov    %r15d,%edi
call   1090 <close@plt>
mov    -0x2c(%rbp),%edi
call   1090 <close@plt>
xor    %eax,%eax
mov    -0x48(%rbp),%r12
mov    %r12,%rsp
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xcb2(%rip),%rdi
jmp    1360 <main+0x1a0>
lea    0xcb6(%rip),%rdi
jmp    1360 <main+0x1a0>
lea    0xcb8(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x1,%eax
jmp    1335 <main+0x175>

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

LLVM IR (-O2):
%struct.sockaddr_in = type { %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
@PF_INET = 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"accept error\00"
@.str.3 = [18 x i8] c"accept client %s\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 = [13 x i8] c"write error \00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca %struct.sockaddr_in
%5 = alloca i32
%6 = bitcast %struct.sockaddr_in* %3 to i8*
%7 = bitcast %struct.sockaddr_in* %4 to i8*
%8 = bitcast i32* %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 = call i32 @memset(%struct.sockaddr_in* %3, i32 0, i32 12)
%14 = load i32, i32* @AF_INET
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %14, i32* %15
%16 = load i32, i32* @INADDR_ANY
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0, i32 0
store i32 %16, i32* %17
%18 = call i32 @htons(i32 8000)
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %18, i32* %19
%20 = load i32, i32* @PF_INET
%21 = load i32, i32* @SOCK_STREAM
%22 = call i32 @socket(i32 %20, i32 %21, i32 0)
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %2
%25 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
br label %60
26: ; preds = %2
%27 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%28 = call i64 @bind(i32 %22, %struct.sockaddr* %27, i32 4)
%29 = icmp slt i64 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %26
%31 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
br label %60
32: ; preds = %26
%33 = call i32 @listen(i32 %22, i32 5)
store i32 12, i32* %5
%34 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%35 = call i32 @accept(i32 %22, %struct.sockaddr* %34, i32* %5)
%36 = icmp slt i32 %35, 0
br i1 %36, label %37, label %39
37: ; preds = %32
%38 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
br label %60
39: ; preds = %32
%40 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0, i32 0
%41 = load i32, i32* %40
%42 = call i8* @inet_ntoa(i32 %41)
%43 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i8* %42)
%44 = call i32 @send(i32 %35, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0), i32 21, i32 0)
br label %45
45: ; preds = %49, %39
%46 = load i32, i32* @BUFSIZ
%47 = call i32 @recv(i32 %35, i8* %12, i32 %46, i32 0)
%48 = icmp sgt i32 %47, 0
br i1 %48, label %49, label %57
49: ; preds = %45
%50 = zext i32 %47 to i64
%51 = getelementptr inbounds i8, i8* %12, i64 %50
store i8 0, i8* %51
%52 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i8* %12)
%53 = call i32 @send(i32 %35, i8* %12, i32 %47, i32 0)
%54 = icmp slt i32 %53, 0
br i1 %54, label %55, label %45
55: ; preds = %49
%56 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.6, i64 0, i64 0))
br label %60
57: ; preds = %45
%58 = call i32 @close(i32 %35)
%59 = call i32 @close(i32 %22)
br label %60
60: ; preds = %57, %55, %37, %30, %24
%61 = phi i32 [ 1, %24 ], [ 1, %30 ], [ 1, %37 ], [ 1, %55 ], [ 0, %57 ]
ret i32 %61
}