#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 /*<<< 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 *, ...);
int recv(int, char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ scanf(char *, char *);
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[1024];
  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");
    return 1;
  }
  if (bind(server_sockfd, (struct sockaddr *)&my_addr,
           sizeof(struct sockaddr)) < 0) {
    perror("bind");
    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");
    return 1;
  }
  printf("accept client %s\n", inet_ntoa(remote_addr.sin_addr));
  len = send(client_sockfd, "Welcome to my server\n", 21, 0);
  char str[100] = {0};
  while ((len = recv(client_sockfd, buf, 1024, 0)) > 0) {
    buf[len] = '\0';
    printf("client say: %s\n", buf);
    printf("Enter string to send:");
    scanf("%s", str);
    if (send(client_sockfd, str, len, 0) < 0) {
      perror("write");
      return 1;
    }
  }
  close(client_sockfd);
  close(server_sockfd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x4b0,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1080 <memset@plt>
mov    0x2de8(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2dd4(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    $0x1f40,%edi
call   1050 <htons@plt>
mov    %eax,-0x24(%rbp)
mov    0x2da3(%rip),%rax
mov    (%rax),%edi
mov    0x2da2(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    125b <main+0x8b>
lea    0xdb6(%rip),%rdi
call   10d0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    13d0 <main+0x200>
mov    -0x14(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0x4,%edx
call   10c0 <bind@plt>
cmp    $0x0,%rax
jge    128e <main+0xbe>
lea    0xd8a(%rip),%rdi
call   10d0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    13d0 <main+0x200>
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   10e0 <accept@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jge    12d6 <main+0x106>
lea    0xd47(%rip),%rdi
call   10d0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    13d0 <main+0x200>
mov    -0x38(%rbp),%edi
call   1040 <inet_ntoa@plt>
mov    %rax,%rsi
lea    0xd2b(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x18(%rbp),%edi
lea    0xd2c(%rip),%rsi
mov    $0x15,%edx
xor    %ecx,%ecx
call   1060 <send@plt>
mov    %eax,-0x1c(%rbp)
lea    -0x4b0(%rbp),%rdi
xor    %esi,%esi
mov    $0x64,%edx
call   1080 <memset@plt>
mov    -0x18(%rbp),%edi
lea    -0x440(%rbp),%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jle    13b9 <main+0x1e9>
movslq -0x1c(%rbp),%rax
movb   $0x0,-0x440(%rbp,%rax,1)
lea    -0x440(%rbp),%rsi
lea    0xce4(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
lea    0xce6(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
lea    -0x4b0(%rbp),%rsi
lea    0xce7(%rip),%rdi
call   10b0 <scanf@plt>
mov    -0x18(%rbp),%edi
lea    -0x4b0(%rbp),%rsi
mov    -0x1c(%rbp),%edx
xor    %ecx,%ecx
call   1060 <send@plt>
cmp    $0x0,%eax
jge    13b4 <main+0x1e4>
lea    0xcc1(%rip),%rdi
call   10d0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    13d0 <main+0x200>
jmp    131b <main+0x14b>
mov    -0x18(%rbp),%edi
call   1090 <close@plt>
mov    -0x14(%rbp),%edi
call   1090 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x4b0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x4a8,%rsp
lea    0x10(%rsp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1080 <memset@plt>
mov    0x2def(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x18(%rsp)
mov    0x2dda(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
mov    $0x1f40,%edi
call   1050 <htons@plt>
mov    %eax,0x14(%rsp)
mov    0x2da7(%rip),%rax
mov    (%rax),%edi
mov    0x2da6(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
test   %eax,%eax
js     1388 <main+0x1b8>
mov    %eax,%ebx
lea    0x10(%rsp),%rsi
mov    %eax,%edi
mov    $0x4,%edx
call   10c0 <bind@plt>
test   %rax,%rax
js     1391 <main+0x1c1>
mov    %ebx,%edi
mov    $0x5,%esi
call   10a0 <listen@plt>
movl   $0xc,0xc(%rsp)
lea    0x20(%rsp),%rsi
lea    0xc(%rsp),%rdx
mov    %ebx,%edi
call   10e0 <accept@plt>
test   %eax,%eax
js     139a <main+0x1ca>
mov    %eax,%ebp
mov    %ebx,0x8(%rsp)
mov    0x20(%rsp),%edi
call   1040 <inet_ntoa@plt>
lea    0xd79(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
lea    0xd7a(%rip),%rsi
mov    %ebp,%edi
mov    $0x15,%edx
xor    %ecx,%ecx
call   1060 <send@plt>
xorps  %xmm0,%xmm0
movaps %xmm0,0x80(%rsp)
movaps %xmm0,0x70(%rsp)
movaps %xmm0,0x60(%rsp)
movaps %xmm0,0x50(%rsp)
movaps %xmm0,0x40(%rsp)
movaps %xmm0,0x30(%rsp)
movl   $0x0,0x90(%rsp)
lea    0xa0(%rsp),%rbx
lea    0xd54(%rip),%r12
lea    0xd63(%rip),%r13
lea    0x30(%rsp),%r14
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %eax,%eax
jle    1374 <main+0x1a4>
mov    %eax,%r15d
mov    %eax,%eax
movb   $0x0,0xa0(%rsp,%rax,1)
lea    0xd02(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
mov    %r12,%rdi
xor    %eax,%eax
call   1070 <printf@plt>
mov    %r13,%rdi
mov    %r14,%rsi
call   10b0 <scanf@plt>
mov    %ebp,%edi
mov    %r14,%rsi
mov    %r15d,%edx
xor    %ecx,%ecx
call   1060 <send@plt>
test   %eax,%eax
jns    1310 <main+0x140>
lea    0xcf2(%rip),%rdi
jmp    13a1 <main+0x1d1>
mov    %ebp,%edi
call   1090 <close@plt>
mov    0x8(%rsp),%edi
call   1090 <close@plt>
xor    %eax,%eax
jmp    13ab <main+0x1db>
lea    0xc71(%rip),%rdi
jmp    13a1 <main+0x1d1>
lea    0xc6f(%rip),%rdi
jmp    13a1 <main+0x1d1>
lea    0xc6b(%rip),%rdi
call   10d0 <perror@plt>
mov    $0x1,%eax
add    $0x4a8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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