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

/* Type definitions */
typedef size_t ssize_t;
typedef int /*<<< orphan*/ line;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ bzero(char **, int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
size_t read(int, char *, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
char *strerror(int /*<<< orphan*/);
scalar_t__ strlen(char *);
size_t write(int, char *, scalar_t__);
void server_communicate(int sockfd) {
  ssize_t ret;
  char line[512];
  fprintf(stdout, "ready to read\n");
  while (ret = read(sockfd, line, 512)) {
    line[ret] = 0;
    if (ret < 0) {
      fprintf(stderr, "read from client error: %s\n", strerror(errno));
      exit(EXIT_FAILURE);
    } else if (ret == 0)
      break;
    else {
      fprintf(stdout, "string from client: %s\n", line);
    }
    bzero(&line, sizeof(line));
    ret = write(sockfd, "success", strlen("success") + 1);
    if (ret < 0) {
      fprintf(stderr, "send message to client error: %s\n", strerror(errno));
      exit(EXIT_FAILURE);
    }
  }
  fprintf(stdout, "client connect end\n");
}


x86 assembly (clang, -O0):
<server_communicate>:
push   %rbp
mov    %rsp,%rbp
sub    $0x220,%rsp
mov    %edi,-0x4(%rbp)
mov    0x2e43(%rip),%rax
mov    (%rax),%edi
lea    0xe72(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x4(%rbp),%edi
lea    -0x210(%rbp),%rsi
mov    $0x200,%edx
call   1050 <read@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
je     12d8 <server_communicate+0x168>
mov    -0x10(%rbp),%rax
movb   $0x0,-0x210(%rbp,%rax,1)
cmpq   $0x0,-0x10(%rbp)
jae    1210 <server_communicate+0xa0>
mov    0x2e0b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x214(%rbp)
mov    0x2df4(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x214(%rbp),%edi
mov    %rax,%rdx
lea    0xe14(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    0x2dc7(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
cmpq   $0x0,-0x10(%rbp)
jne    1220 <server_communicate+0xb0>
jmp    12d8 <server_communicate+0x168>
mov    0x2da1(%rip),%rax
mov    (%rax),%edi
lea    -0x210(%rbp),%rdx
lea    0xdf4(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
jmp    1243 <server_communicate+0xd3>
lea    -0x210(%rbp),%rdi
mov    $0x200,%esi
call   1070 <bzero@plt>
mov    -0x4(%rbp),%eax
mov    %eax,-0x218(%rbp)
lea    0xddf(%rip),%rdi
call   1040 <strlen@plt>
mov    -0x218(%rbp),%edi
mov    %rax,%rdx
add    $0x1,%rdx
lea    0xdc6(%rip),%rsi
call   1030 <write@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jae    12d3 <server_communicate+0x163>
mov    0x2d48(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x21c(%rbp)
mov    0x2d31(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x21c(%rbp),%edi
mov    %rax,%rdx
lea    0xd8d(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    0x2d04(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
jmp    1195 <server_communicate+0x25>
mov    0x2ce9(%rip),%rax
mov    (%rax),%edi
lea    0xd85(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
add    $0x220,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<server_communicate>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x200,%rsp
mov    %edi,%ebp
mov    0x2e88(%rip),%r12
mov    (%r12),%edi
lea    0xe9d(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    %rsp,%rsi
mov    %ebp,%edi
mov    $0x200,%edx
call   1040 <read@plt>
test   %rax,%rax
je     11d5 <server_communicate+0x95>
lea    0xe8a(%rip),%r14
mov    %rsp,%rbx
lea    0xe98(%rip),%r15
nop
movb   $0x0,(%rsp,%rax,1)
mov    (%r12),%edi
mov    %r14,%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    %rbx,%rdi
mov    $0x200,%esi
call   1060 <bzero@plt>
mov    $0x8,%edx
mov    %ebp,%edi
mov    %r15,%rsi
call   1030 <write@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x200,%edx
call   1040 <read@plt>
test   %rax,%rax
jne    1190 <server_communicate+0x50>
mov    (%r12),%edi
lea    0xe4f(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
add    $0x200,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stdout = global i32 0
@.str = [15 x i8] c"ready to read\0A\00"
@stderr = global i32 0
@.str.1 = [28 x i8] c"read from client error: %s\0A\00"
@errno = global i32 0
@EXIT_FAILURE = global i32 0
@.str.2 = [24 x i8] c"string from client: %s\0A\00"
@.str.3 = [8 x i8] c"success\00"
@.str.4 = [34 x i8] c"send message to client error: %s\0A\00"
@.str.5 = [20 x i8] c"client connect end\0A\00"
define void @server_communicate(i32 %0) {
%2 = alloca i32
%3 = alloca i64
%4 = alloca [512 x i8]
store i32 %0, i32* %2
%5 = load i32, i32* @stdout
%6 = call i32 (i32, i8*, ...) @fprintf(i32 %5, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
br label %7
7: ; preds = %49, %1
%8 = load i32, i32* %2
%9 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%10 = call i64 @read(i32 %8, i8* %9, i32 512)
store i64 %10, i64* %3
%11 = icmp ne i64 %10, 0
br i1 %11, label %12, label %50
12: ; preds = %7
%13 = load i64, i64* %3
%14 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 %13
store i8 0, i8* %14
%15 = load i64, i64* %3
%16 = icmp ult i64 %15, 0
br i1 %16, label %17, label %24
17: ; preds = %12
%18 = load i32, i32* @stderr
%19 = load i32, i32* @errno
%20 = call i8* @strerror(i32 %19)
%21 = call i32 (i32, i8*, ...) @fprintf(i32 %18, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i8* %20)
%22 = load i32, i32* @EXIT_FAILURE
%23 = call i32 @exit(i32 %22)
unreachable
24: ; preds = %12
%25 = load i64, i64* %3
%26 = icmp eq i64 %25, 0
br i1 %26, label %27, label %28
27: ; preds = %24
br label %50
28: ; preds = %24
%29 = load i32, i32* @stdout
%30 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%31 = call i32 (i32, i8*, ...) @fprintf(i32 %29, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i8* %30)
br label %32
32: ; preds = %28
br label %33
33: ; preds = %32
%34 = bitcast [512 x i8]* %4 to i8**
%35 = call i32 @bzero(i8** %34, i32 512)
%36 = load i32, i32* %2
%37 = call i64 @strlen(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
%38 = add nsw i64 %37, 1
%39 = call i64 @write(i32 %36, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i64 %38)
store i64 %39, i64* %3
%40 = load i64, i64* %3
%41 = icmp ult i64 %40, 0
br i1 %41, label %42, label %49
42: ; preds = %33
%43 = load i32, i32* @stderr
%44 = load i32, i32* @errno
%45 = call i8* @strerror(i32 %44)
%46 = call i32 (i32, i8*, ...) @fprintf(i32 %43, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.4, i64 0, i64 0), i8* %45)
%47 = load i32, i32* @EXIT_FAILURE
%48 = call i32 @exit(i32 %47)
unreachable
49: ; preds = %33
br label %7
50: ; preds = %27, %7
%51 = load i32, i32* @stdout
%52 = call i32 (i32, i8*, ...) @fprintf(i32 %51, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0))
ret void
}

LLVM IR (-O2):
@stdout = global i32 0
@.str = [15 x i8] c"ready to read\0A\00"
@stderr = global i32 0
@errno = global i32 0
@EXIT_FAILURE = global i32 0
@.str.2 = [24 x i8] c"string from client: %s\0A\00"
@.str.3 = [8 x i8] c"success\00"
@.str.5 = [20 x i8] c"client connect end\0A\00"
define void @server_communicate(i32 %0) {
%2 = alloca [512 x i8]
%3 = getelementptr inbounds [512 x i8], [512 x i8]* %2, i64 0, i64 0
%4 = load i32, i32* @stdout
%5 = tail call i32 (i32, i8*, ...) @fprintf(i32 %4, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%6 = call i64 @read(i32 %0, i8* %3, i32 512)
%7 = icmp eq i64 %6, 0
br i1 %7, label %19, label %8
8: ; preds = %1
%9 = bitcast [512 x i8]* %2 to i8**
br label %10
10: ; preds = %8, %10
%11 = phi i64 [ %6, %8 ], [ %17, %10 ]
%12 = getelementptr inbounds [512 x i8], [512 x i8]* %2, i64 0, i64 %11
store i8 0, i8* %12
%13 = load i32, i32* @stdout
%14 = call i32 (i32, i8*, ...) @fprintf(i32 %13, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i8* %3)
%15 = call i32 @bzero(i8** %9, i32 512)
%16 = call i64 @write(i32 %0, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), i64 8)
%17 = call i64 @read(i32 %0, i8* %3, i32 512)
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %10
19: ; preds = %10, %1
%20 = load i32, i32* @stdout
%21 = call i32 (i32, i8*, ...) @fprintf(i32 %20, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0))
ret void
}