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

/* 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*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(int);
int listen(int, int);
int /*<<< orphan*/ printf(char *, int);
int read(int, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
int write(int, char *, int);
int main(void) {
  int sockfd;
  int clifd;
  struct sockaddr_in saddr;
  int ret;
  char buf[1024], buf2[1024];
  sockfd = socket(AF_INET, SOCK_STREAM, 0);
  if (sockfd < 0) {
    printf("create sockfd failure: %d\n", sockfd);
    return -1;
  }
  saddr.sin_family = AF_INET;
  saddr.sin_port = htons(8999);
  saddr.sin_addr.s_addr = htonl(INADDR_ANY);
  ret = bind(sockfd, (struct sockaddr *)(&saddr), sizeof(saddr));
  if (ret < 0) {
    printf("bind failure! ret = %d\n", ret);
    return -1;
  }
  ret = listen(sockfd, 5);
  if (ret < 0) {
    printf("listen failure! ret = %d\n", ret);
    return -1;
  }
  clifd = accept(sockfd, (struct sockaddr *)(&saddr), sizeof(saddr));
  if (ret < 0) {
    printf("accept failure! clifd = %d\n", clifd);
    return -1;
  }
  strcpy(buf, "hello , this is server.");
  ret = write(clifd, buf, 1024);
  if (ret < 0) {
    printf("write banner failure! ret = %d\n", ret);
    return -1;
  }
  while (1) {
    ret = read(clifd, buf, 1024);
    if (ret < 0) {
      printf("read failure! ret = %d\n", ret);
      continue;
    }
    strcpy(buf2, "You said:");
    strcat(buf2, buf);
    ret = write(clifd, buf2, 1024);
    if (ret < 0) {
      printf("write failure! ret = %d\n", ret);
      continue;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x820,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e1f(%rip),%rax
mov    (%rax),%edi
mov    0x2dfe(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    1203 <main+0x53>
mov    -0x8(%rbp),%esi
lea    0xe0e(%rip),%rdi
call   1060 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13bd <main+0x20d>
mov    0x2dde(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    $0x2327,%edi
call   1050 <htons@plt>
mov    %eax,-0x14(%rbp)
mov    0x2dbd(%rip),%rax
mov    (%rax),%edi
call   1070 <htonl@plt>
mov    %eax,-0x18(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   10a0 <bind@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1266 <main+0xb6>
mov    -0x1c(%rbp),%esi
lea    0xdc6(%rip),%rdi
call   1060 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13bd <main+0x20d>
mov    -0x8(%rbp),%edi
mov    $0x5,%esi
call   1090 <listen@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    129b <main+0xeb>
mov    -0x1c(%rbp),%esi
lea    0xda9(%rip),%rdi
call   1060 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13bd <main+0x20d>
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   10b0 <accept@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    12d4 <main+0x124>
mov    -0xc(%rbp),%esi
lea    0xd8a(%rip),%rdi
call   1060 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13bd <main+0x20d>
lea    -0x420(%rbp),%rdi
lea    0xd87(%rip),%rsi
call   1030 <strcpy@plt>
mov    -0xc(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    $0x400,%edx
call   1040 <write@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1323 <main+0x173>
mov    -0x1c(%rbp),%esi
lea    0xd6f(%rip),%rdi
call   1060 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13bd <main+0x20d>
jmp    1328 <main+0x178>
mov    -0xc(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    $0x400,%edx
call   1080 <read@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    135d <main+0x1ad>
mov    -0x1c(%rbp),%esi
lea    0xd4e(%rip),%rdi
call   1060 <printf@plt>
jmp    1328 <main+0x178>
lea    -0x820(%rbp),%rdi
lea    0xd4e(%rip),%rsi
call   1030 <strcpy@plt>
lea    -0x820(%rbp),%rdi
lea    -0x420(%rbp),%rsi
call   10c0 <strcat@plt>
mov    -0xc(%rbp),%edi
lea    -0x820(%rbp),%rsi
mov    $0x400,%edx
call   1040 <write@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    13b8 <main+0x208>
mov    -0x1c(%rbp),%esi
lea    0xd15(%rip),%rdi
call   1060 <printf@plt>
jmp    1328 <main+0x178>
jmp    1328 <main+0x178>
mov    -0x4(%rbp),%eax
add    $0x820,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x818,%rsp
mov    0x2e20(%rip),%rbp
mov    0x0(%rbp),%edi
mov    0x2dfe(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
mov    %eax,%ebx
test   %eax,%eax
js     12eb <main+0x13b>
mov    0x0(%rbp),%eax
mov    %eax,0x8(%rsp)
mov    $0x2327,%edi
call   1050 <htons@plt>
mov    %eax,0x4(%rsp)
mov    0x2ddf(%rip),%rax
mov    (%rax),%edi
call   1070 <htonl@plt>
mov    %eax,(%rsp)
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   10a0 <bind@plt>
test   %eax,%eax
js     12f6 <main+0x146>
mov    %ebx,%edi
mov    $0x5,%esi
call   1090 <listen@plt>
test   %eax,%eax
js     12ff <main+0x14f>
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   10b0 <accept@plt>
mov    %eax,%ebx
lea    0xdff(%rip),%rsi
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
call   1030 <strcpy@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0x400,%edx
call   1040 <write@plt>
test   %eax,%eax
js     1308 <main+0x158>
lea    0x10(%rsp),%r14
lea    0xe07(%rip),%r15
lea    0xe18(%rip),%r12
lea    0x410(%rsp),%r13
lea    0xe13(%rip),%rbp
jmp    12aa <main+0xfa>
cs nopw 0x0(%rax,%rax,1)
mov    %r15,%rdi
mov    %eax,%esi
call   1060 <printf@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x400,%edx
call   1080 <read@plt>
test   %eax,%eax
js     12a0 <main+0xf0>
mov    %r13,%rdi
mov    %r12,%rsi
call   1030 <strcpy@plt>
mov    %r13,%rdi
mov    %r14,%rsi
call   10c0 <strcat@plt>
mov    %ebx,%edi
mov    %r13,%rsi
mov    $0x400,%edx
call   1040 <write@plt>
test   %eax,%eax
jns    12aa <main+0xfa>
mov    %rbp,%rdi
jmp    12a3 <main+0xf3>
lea    0xd0e(%rip),%rdi
mov    %ebx,%esi
jmp    1311 <main+0x161>
lea    0xd1e(%rip),%rdi
jmp    130f <main+0x15f>
lea    0xd2d(%rip),%rdi
jmp    130f <main+0x15f>
lea    0xd56(%rip),%rdi
mov    %eax,%esi
call   1060 <printf@plt>
mov    $0xffffffff,%eax
add    $0x818,%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
@SOCK_STREAM = global i32 0
@.str = [27 x i8] c"create sockfd failure: %d\0A\00"
@INADDR_ANY = global i32 0
@.str.1 = [24 x i8] c"bind failure! ret = %d\0A\00"
@.str.2 = [26 x i8] c"listen failure! ret = %d\0A\00"
@.str.3 = [28 x i8] c"accept failure! clifd = %d\0A\00"
@.str.4 = [24 x i8] c"hello , this is server.\00"
@.str.5 = [32 x i8] c"write banner failure! ret = %d\0A\00"
@.str.6 = [24 x i8] c"read failure! ret = %d\0A\00"
@.str.7 = [10 x i8] c"You said:\00"
@.str.8 = [25 x i8] c"write failure! ret = %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.sockaddr_in
%5 = alloca i32
%6 = alloca [1024 x i8]
%7 = alloca [1024 x i8]
store i32 0, i32* %1
%8 = load i32, i32* @AF_INET
%9 = load i32, i32* @SOCK_STREAM
%10 = call i32 @socket(i32 %8, i32 %9, i32 0)
store i32 %10, i32* %2
%11 = load i32, i32* %2
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %0
%14 = load i32, i32* %2
%15 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i32 %14)
store i32 -1, i32* %1
br label %86
16: ; preds = %0
%17 = load i32, i32* @AF_INET
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 2
store i32 %17, i32* %18
%19 = call i32 @htons(i32 8999)
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 1
store i32 %19, i32* %20
%21 = load i32, i32* @INADDR_ANY
%22 = call i32 @htonl(i32 %21)
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 0
%24 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %23, i32 0, i32 0
store i32 %22, i32* %24
%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)
store i32 %27, i32* %5
%28 = load i32, i32* %5
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %33
30: ; preds = %16
%31 = load i32, i32* %5
%32 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i32 %31)
store i32 -1, i32* %1
br label %86
33: ; preds = %16
%34 = load i32, i32* %2
%35 = call i32 @listen(i32 %34, i32 5)
store i32 %35, i32* %5
%36 = load i32, i32* %5
%37 = icmp slt i32 %36, 0
br i1 %37, label %38, label %41
38: ; preds = %33
%39 = load i32, i32* %5
%40 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i32 %39)
store i32 -1, i32* %1
br label %86
41: ; preds = %33
%42 = load i32, i32* %2
%43 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%44 = call i32 @accept(i32 %42, %struct.sockaddr* %43, i32 12)
store i32 %44, i32* %3
%45 = load i32, i32* %5
%46 = icmp slt i32 %45, 0
br i1 %46, label %47, label %50
47: ; preds = %41
%48 = load i32, i32* %3
%49 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i32 %48)
store i32 -1, i32* %1
br label %86
50: ; preds = %41
%51 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%52 = call i32 @strcpy(i8* %51, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0))
%53 = load i32, i32* %3
%54 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%55 = call i32 @write(i32 %53, i8* %54, i32 1024)
store i32 %55, i32* %5
%56 = load i32, i32* %5
%57 = icmp slt i32 %56, 0
br i1 %57, label %58, label %61
58: ; preds = %50
%59 = load i32, i32* %5
%60 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.5, i64 0, i64 0), i32 %59)
store i32 -1, i32* %1
br label %86
61: ; preds = %50
br label %62
62: ; preds = %61, %68, %82, %85
%63 = load i32, i32* %3
%64 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%65 = call i32 @read(i32 %63, i8* %64, i32 1024)
store i32 %65, i32* %5
%66 = load i32, i32* %5
%67 = icmp slt i32 %66, 0
br i1 %67, label %68, label %71
68: ; preds = %62
%69 = load i32, i32* %5
%70 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.6, i64 0, i64 0), i32 %69)
br label %62
71: ; preds = %62
%72 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%73 = call i32 @strcpy(i8* %72, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.7, i64 0, i64 0))
%74 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%75 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%76 = call i32 @strcat(i8* %74, i8* %75)
%77 = load i32, i32* %3
%78 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%79 = call i32 @write(i32 %77, i8* %78, i32 1024)
store i32 %79, i32* %5
%80 = load i32, i32* %5
%81 = icmp slt i32 %80, 0
br i1 %81, label %82, label %85
82: ; preds = %71
%83 = load i32, i32* %5
%84 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.8, i64 0, i64 0), i32 %83)
br label %62
85: ; preds = %71
br label %62
86: ; preds = %58, %47, %38, %30, %13
%87 = load i32, i32* %1
ret i32 %87
}

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
@SOCK_STREAM = global i32 0
@.str = [27 x i8] c"create sockfd failure: %d\0A\00"
@INADDR_ANY = global i32 0
@.str.1 = [24 x i8] c"bind failure! ret = %d\0A\00"
@.str.2 = [26 x i8] c"listen failure! ret = %d\0A\00"
@.str.4 = [24 x i8] c"hello , this is server.\00"
@.str.5 = [32 x i8] c"write banner failure! ret = %d\0A\00"
@.str.6 = [24 x i8] c"read failure! ret = %d\0A\00"
@.str.7 = [10 x i8] c"You said:\00"
@.str.8 = [25 x i8] c"write failure! ret = %d\0A\00"
define i32 @main() {
%1 = alloca %struct.sockaddr_in
%2 = alloca [1024 x i8]
%3 = alloca [1024 x i8]
%4 = bitcast %struct.sockaddr_in* %1 to i8*
%5 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%6 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%7 = load i32, i32* @AF_INET
%8 = load i32, i32* @SOCK_STREAM
%9 = tail call i32 @socket(i32 %7, i32 %8, i32 0)
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %0
%12 = tail call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i32 %9)
br label %51
13: ; preds = %0
%14 = load i32, i32* @AF_INET
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 2
store i32 %14, i32* %15
%16 = tail call i32 @htons(i32 8999)
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1
store i32 %16, i32* %17
%18 = load i32, i32* @INADDR_ANY
%19 = tail call i32 @htonl(i32 %18)
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0, i32 0
store i32 %19, i32* %20
%21 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%22 = call i32 @bind(i32 %9, %struct.sockaddr* %21, i32 12)
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %13
%25 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i32 %22)
br label %51
26: ; preds = %13
%27 = call i32 @listen(i32 %9, i32 5)
%28 = icmp slt i32 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %26
%30 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i32 %27)
br label %51
31: ; preds = %26
%32 = call i32 @accept(i32 %9, %struct.sockaddr* %21, i32 12)
%33 = call i32 @strcpy(i8* %5, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0))
%34 = call i32 @write(i32 %32, i8* %5, i32 1024)
%35 = icmp slt i32 %34, 0
br i1 %35, label %36, label %38
36: ; preds = %31
%37 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.5, i64 0, i64 0), i32 %34)
br label %51
38: ; preds = %31, %43
%39 = call i32 @read(i32 %32, i8* %5, i32 1024)
%40 = icmp slt i32 %39, 0
br i1 %40, label %41, label %44
41: ; preds = %38
%42 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.6, i64 0, i64 0), i32 %39)
br label %43
43: ; preds = %41, %49, %44
br label %38
44: ; preds = %38
%45 = call i32 @strcpy(i8* %6, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.7, i64 0, i64 0))
%46 = call i32 @strcat(i8* %6, i8* %5)
%47 = call i32 @write(i32 %32, i8* %6, i32 1024)
%48 = icmp slt i32 %47, 0
br i1 %48, label %49, label %43
49: ; preds = %44
%50 = call i32 @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.8, i64 0, i64 0), i32 %47)
br label %43
51: ; preds = %36, %29, %24, %11
ret i32 -1
}