#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 */
struct sockaddr_in {
  int dummy;
};
struct sockaddr {
  int dummy;
};
typedef int socklen_t;
typedef int /*<<< orphan*/ cliaddr;

/* Variables and functions */
scalar_t__ EINTR;
scalar_t__ errno;
int /*<<< orphan*/ printf(char *, ...);
int recvfrom(int, char *, int, int /*<<< orphan*/, struct sockaddr *, int *);
int /*<<< orphan*/ sendto(int, char *, int, int /*<<< orphan*/,
                          struct sockaddr *, int);
char *strerror(scalar_t__);
void dg_echo(int sockfd) {
  char buff[4096] = {0};
  struct sockaddr_in cliaddr;
  socklen_t clilen = sizeof(cliaddr);
  while (1) {
    int ret =
        recvfrom(sockfd, buff, 4096, 0, (struct sockaddr *)&cliaddr, &clilen);
    if (ret >= 0) {
      printf("rcv cline msg\n");
      sendto(sockfd, buff, ret, 0, (struct sockaddr *)&cliaddr, clilen);
    } else {
      if (EINTR == errno) {
        continue;
      } else {
        printf("accept error[%s]", strerror(errno));
        break;
      }
    }
  }
}


x86 assembly (clang, -O0):
<dg_echo>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1020,%rsp
mov    %edi,-0x4(%rbp)
lea    -0x1010(%rbp),%rdi
xor    %esi,%esi
mov    $0x1000,%edx
call   1040 <memset@plt>
movl   $0x4,-0x101c(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x1010(%rbp),%rsi
lea    -0x1018(%rbp),%r8
mov    $0x1000,%edx
xor    %ecx,%ecx
lea    -0x101c(%rbp),%r9
call   1060 <recvfrom@plt>
mov    %eax,-0x1020(%rbp)
cmpl   $0x0,-0x1020(%rbp)
jl     11ea <dg_echo+0x9a>
lea    0xe47(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x4(%rbp),%edi
lea    -0x1010(%rbp),%rsi
mov    -0x1020(%rbp),%edx
lea    -0x1018(%rbp),%r8
mov    -0x101c(%rbp),%r9d
xor    %ecx,%ecx
call   1050 <sendto@plt>
jmp    122e <dg_echo+0xde>
mov    0x2de7(%rip),%rax
mov    (%rax),%rax
mov    0x2de5(%rip),%rcx
cmp    (%rcx),%rax
jne    1209 <dg_echo+0xb9>
jmp    117b <dg_echo+0x2b>
mov    0x2dd0(%rip),%rax
mov    (%rax),%rdi
call   1070 <strerror@plt>
mov    %rax,%rsi
lea    0xded(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1233 <dg_echo+0xe3>
jmp    117b <dg_echo+0x2b>
add    $0x1020,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dg_echo>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1018,%rsp
mov    %edi,%ebx
lea    0x10(%rsp),%r14
mov    $0x1000,%edx
mov    %r14,%rdi
xor    %esi,%esi
call   1050 <memset@plt>
movl   $0x4,0x4(%rsp)
lea    0x8(%rsp),%r15
lea    0x4(%rsp),%r12
lea    0xe71(%rip),%r13
jmp    11d0 <dg_echo+0x70>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %eax,%ebp
mov    %r13,%rdi
call   1030 <puts@plt>
mov    0x4(%rsp),%r9d
mov    %ebx,%edi
mov    %r14,%rsi
mov    %ebp,%edx
xor    %ecx,%ecx
mov    %r15,%r8
call   1060 <sendto@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x1000,%edx
xor    %ecx,%ecx
mov    %r15,%r8
mov    %r12,%r9
call   1070 <recvfrom@plt>
test   %eax,%eax
jns    11b0 <dg_echo+0x50>
mov    0x2dee(%rip),%rax
mov    (%rax),%rdi
mov    0x2ddc(%rip),%rax
cmp    %rdi,(%rax)
je     11d0 <dg_echo+0x70>
call   1080 <strerror@plt>
lea    0xdf3(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
add    $0x1018,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [15 x i8] c"rcv cline msg\0A\00"
@EINTR = global i64 0
@errno = global i64 0
@.str.1 = [17 x i8] c"accept error[%s]\00"
define void @dg_echo(i32 %0) {
%2 = alloca i32
%3 = alloca [4096 x i8]
%4 = alloca %struct.sockaddr_in
%5 = alloca i32
%6 = alloca i32
store i32 %0, i32* %2
%7 = bitcast [4096 x i8]* %3 to i8*
store i32 4, i32* %5
br label %8
8: ; preds = %1, %27, %32
%9 = load i32, i32* %2
%10 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%11 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%12 = call i32 @recvfrom(i32 %9, i8* %10, i32 4096, i32 0, %struct.sockaddr* %11, i32* %5)
store i32 %12, i32* %6
%13 = load i32, i32* %6
%14 = icmp sge i32 %13, 0
br i1 %14, label %15, label %23
15: ; preds = %8
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%17 = load i32, i32* %2
%18 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%19 = load i32, i32* %6
%20 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%21 = load i32, i32* %5
%22 = call i32 @sendto(i32 %17, i8* %18, i32 %19, i32 0, %struct.sockaddr* %20, i32 %21)
br label %32
23: ; preds = %8
%24 = load i64, i64* @EINTR
%25 = load i64, i64* @errno
%26 = icmp eq i64 %24, %25
br i1 %26, label %27, label %28
27: ; preds = %23
br label %8
28: ; preds = %23
%29 = load i64, i64* @errno
%30 = call i8* @strerror(i64 %29)
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* %30)
br label %33
32: ; preds = %15
br label %8
33: ; preds = %28
ret void
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@EINTR = global i64 0
@errno = global i64 0
@.str.1 = [17 x i8] c"accept error[%s]\00"
@str = [14 x i8] c"rcv cline msg\00"
define void @dg_echo(i32 %0) {
%2 = alloca [4096 x i8]
%3 = alloca %struct.sockaddr_in
%4 = alloca i32
%5 = getelementptr inbounds [4096 x i8], [4096 x i8]* %2, i64 0, i64 0
%6 = bitcast %struct.sockaddr_in* %3 to i8*
%7 = bitcast i32* %4 to i8*
store i32 4, i32* %4
%8 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
br label %9
9: ; preds = %20, %1
%10 = call i32 @recvfrom(i32 %0, i8* %5, i32 4096, i32 0, %struct.sockaddr* %8, i32* %4)
%11 = icmp sgt i32 %10, -1
br i1 %11, label %12, label %16
12: ; preds = %9
%13 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str, i64 0, i64 0))
%14 = load i32, i32* %4
%15 = call i32 @sendto(i32 %0, i8* %5, i32 %10, i32 0, %struct.sockaddr* %8, i32 %14)
br label %20
16: ; preds = %9
%17 = load i64, i64* @EINTR
%18 = load i64, i64* @errno
%19 = icmp eq i64 %17, %18
br i1 %19, label %20, label %21
20: ; preds = %16, %12
br label %9
21: ; preds = %16
%22 = call i8* @strerror(i64 %18)
%23 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* %22)
ret void
}