#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_storage {
  int dummy;
};
struct sockaddr {
  int dummy;
};
typedef size_t ssize_t;
typedef int socklen_t;
typedef int /*<<< orphan*/ clntAddr;

/* Variables and functions */
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ memset(struct sockaddr_storage *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printSocketAddress(struct sockaddr *);
size_t recvfrom(int const, char *, int const, int /*<<< orphan*/,
                struct sockaddr *, int *);
size_t sendto(int const, char *, size_t, int /*<<< orphan*/, struct sockaddr *,
              int);
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ sysError(char *);
void acceptClient(const int servSock) {
  for (;;) {
    struct sockaddr_storage clntAddr;
    socklen_t clntLen = sizeof(clntAddr);
    memset(&clntAddr, 0, clntLen);
    const int BUFF = 1000;
    char buff[BUFF];
    ssize_t bytesRcvd = 0;
    bytesRcvd = recvfrom(servSock, buff, BUFF, 0, (struct sockaddr *)&clntAddr,
                         &clntLen);
    if (bytesRcvd < 0)
      sysError("failed to get data");
    else if (bytesRcvd == 0)
      sysError("connection died prematurely");
    buff[bytesRcvd] = '\0';
    fprintf(stdout, "\nNew connection established\n");
    printSocketAddress((struct sockaddr *)&clntAddr);
    fprintf(stdout, "Received %d bytes: %s\n", bytesRcvd, buff);
    ssize_t bytesSent = 0;
    bytesSent = sendto(servSock, buff, bytesRcvd, 0,
                       (struct sockaddr *)&clntAddr, clntLen);
    if (bytesSent < 0)
      sysError("failed to send data");
    else if (bytesSent != bytesRcvd)
      sysError("sent unexpected amount of data");
    fprintf(stdout, "Sent back %d bytes\n\n", bytesSent);
  }
}


x86 assembly (clang, -O0):
<acceptClient>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x4,-0xc(%rbp)
mov    -0xc(%rbp),%edx
lea    -0x8(%rbp),%rdi
xor    %esi,%esi
call   1040 <memset@plt>
movl   $0x3e8,-0x10(%rbp)
mov    %rsp,%rax
mov    %rax,-0x18(%rbp)
mov    %rsp,%rsi
add    $0xfffffffffffffc10,%rsi
mov    %rsi,-0x30(%rbp)
mov    %rsi,%rsp
movq   $0x0,-0x20(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x8(%rbp),%r8
mov    $0x3e8,%edx
xor    %ecx,%ecx
lea    -0xc(%rbp),%r9
call   1070 <recvfrom@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jae    11de <acceptClient+0x7e>
lea    0xe2c(%rip),%rdi
call   1030 <sysError@plt>
jmp    11fa <acceptClient+0x9a>
cmpq   $0x0,-0x20(%rbp)
jne    11f5 <acceptClient+0x95>
lea    0xe23(%rip),%rdi
call   1030 <sysError@plt>
jmp    11fa <acceptClient+0x9a>
mov    -0x30(%rbp),%rax
mov    -0x20(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    0x2dd3(%rip),%rax
mov    (%rax),%edi
lea    0xe19(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
lea    -0x8(%rbp),%rdi
call   1080 <printSocketAddress@plt>
mov    -0x30(%rbp),%rcx
mov    0x2daf(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rdx
lea    0xe0e(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x30(%rbp),%rsi
movq   $0x0,-0x28(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x20(%rbp),%rdx
lea    -0x8(%rbp),%r8
mov    -0xc(%rbp),%r9d
xor    %ecx,%ecx
call   1050 <sendto@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jae    1287 <acceptClient+0x127>
lea    0xde6(%rip),%rdi
call   1030 <sysError@plt>
jmp    12a6 <acceptClient+0x146>
mov    -0x28(%rbp),%rax
cmp    -0x20(%rbp),%rax
je     12a1 <acceptClient+0x141>
lea    0xddb(%rip),%rdi
call   1030 <sysError@plt>
jmp    12a6 <acceptClient+0x146>
mov    0x2d33(%rip),%rax
mov    (%rax),%edi
mov    -0x28(%rbp),%rdx
lea    0xddc(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x18(%rbp),%rax
mov    %rax,%rsp
jmp    116b <acceptClient+0xb>

x86 assembly (clang, -O2):
<acceptClient>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edi,%r15d
lea    -0x40(%rbp),%rbx
jmp    11a4 <acceptClient+0x44>
nopw   0x0(%rax,%rax,1)
mov    0x2e59(%rip),%rax
mov    (%rax),%edi
lea    0xedf(%rip),%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    -0x38(%rbp),%rsp
mov    %r14d,%r15d
mov    %r13,%rbx
movl   $0x4,-0x2c(%rbp)
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1040 <memset@plt>
mov    %rsp,-0x38(%rbp)
mov    %rsp,%r13
add    $0xfffffffffffffc10,%r13
mov    %r13,%rsp
mov    %r15d,%edi
mov    %r13,%rsi
mov    $0x3e8,%edx
xor    %ecx,%ecx
mov    %rbx,%r8
lea    -0x2c(%rbp),%r9
call   1070 <recvfrom@plt>
mov    %rax,%r12
test   %rax,%rax
jne    11f8 <acceptClient+0x98>
lea    0xe0d(%rip),%rdi
call   1030 <sysError@plt>
movb   $0x0,0x0(%r13,%r12,1)
mov    0x2ddb(%rip),%r14
mov    (%r14),%edi
lea    0xe0d(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    %rbx,%rdi
call   1080 <printSocketAddress@plt>
mov    (%r14),%edi
lea    0xe11(%rip),%rsi
mov    %r12,%rdx
mov    %r13,%rcx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    -0x2c(%rbp),%r9d
mov    %r15d,%r14d
mov    %r15d,%edi
mov    %r13,%rsi
mov    %r12,%rdx
xor    %ecx,%ecx
mov    %rbx,%r13
mov    %rbx,%r8
call   1050 <sendto@plt>
mov    %rax,%rbx
cmp    %r12,%rax
je     1180 <acceptClient+0x20>
lea    0xdeb(%rip),%rdi
call   1030 <sysError@plt>
jmp    1180 <acceptClient+0x20>

LLVM IR (-O0):
%struct.sockaddr_storage = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [19 x i8] c"failed to get data\00"
@.str.1 = [28 x i8] c"connection died prematurely\00"
@stdout = global i32 0
@.str.2 = [29 x i8] c"\0ANew connection established\0A\00"
@.str.3 = [23 x i8] c"Received %d bytes: %s\0A\00"
@.str.4 = [20 x i8] c"failed to send data\00"
@.str.5 = [31 x i8] c"sent unexpected amount of data\00"
@.str.6 = [21 x i8] c"Sent back %d bytes\0A\0A\00"
define void @acceptClient(i32 %0) {
%2 = alloca i32
%3 = alloca %struct.sockaddr_storage
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i64
store i32 %0, i32* %2
br label %9
9: ; preds = %53, %1
store i32 4, i32* %4
%10 = load i32, i32* %4
%11 = call i32 @memset(%struct.sockaddr_storage* %3, i32 0, i32 %10)
store i32 1000, i32* %5
%12 = call i8* @llvm.stacksave()
store i8* %12, i8** %6
%13 = alloca i8, i64 1000
store i64 0, i64* %7
%14 = load i32, i32* %2
%15 = bitcast %struct.sockaddr_storage* %3 to %struct.sockaddr*
%16 = call i64 @recvfrom(i32 %14, i8* %13, i32 1000, i32 0, %struct.sockaddr* %15, i32* %4)
store i64 %16, i64* %7
%17 = load i64, i64* %7
%18 = icmp ult i64 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %9
%20 = call i32 @sysError(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
br label %27
21: ; preds = %9
%22 = load i64, i64* %7
%23 = icmp eq i64 %22, 0
br i1 %23, label %24, label %26
24: ; preds = %21
%25 = call i32 @sysError(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
br label %26
26: ; preds = %24, %21
br label %27
27: ; preds = %26, %19
%28 = load i64, i64* %7
%29 = getelementptr inbounds i8, i8* %13, i64 %28
store i8 0, i8* %29
%30 = load i32, i32* @stdout
%31 = call i32 (i32, i8*, ...) @fprintf(i32 %30, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0))
%32 = bitcast %struct.sockaddr_storage* %3 to %struct.sockaddr*
%33 = call i32 @printSocketAddress(%struct.sockaddr* %32)
%34 = load i32, i32* @stdout
%35 = load i64, i64* %7
%36 = call i32 (i32, i8*, ...) @fprintf(i32 %34, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i64 %35, i8* %13)
store i64 0, i64* %8
%37 = load i32, i32* %2
%38 = load i64, i64* %7
%39 = bitcast %struct.sockaddr_storage* %3 to %struct.sockaddr*
%40 = load i32, i32* %4
%41 = call i64 @sendto(i32 %37, i8* %13, i64 %38, i32 0, %struct.sockaddr* %39, i32 %40)
store i64 %41, i64* %8
%42 = load i64, i64* %8
%43 = icmp ult i64 %42, 0
br i1 %43, label %44, label %46
44: ; preds = %27
%45 = call i32 @sysError(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.4, i64 0, i64 0))
br label %53
46: ; preds = %27
%47 = load i64, i64* %8
%48 = load i64, i64* %7
%49 = icmp ne i64 %47, %48
br i1 %49, label %50, label %52
50: ; preds = %46
%51 = call i32 @sysError(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.5, i64 0, i64 0))
br label %52
52: ; preds = %50, %46
br label %53
53: ; preds = %52, %44
%54 = load i32, i32* @stdout
%55 = load i64, i64* %8
%56 = call i32 (i32, i8*, ...) @fprintf(i32 %54, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.6, i64 0, i64 0), i64 %55)
%57 = load i8*, i8** %6
br label %9
}

LLVM IR (-O2):
%struct.sockaddr_storage = type { i32 }
%struct.sockaddr = type { i32 }
@.str.1 = [28 x i8] c"connection died prematurely\00"
@stdout = global i32 0
@.str.2 = [29 x i8] c"\0ANew connection established\0A\00"
@.str.3 = [23 x i8] c"Received %d bytes: %s\0A\00"
@.str.5 = [31 x i8] c"sent unexpected amount of data\00"
@.str.6 = [21 x i8] c"Sent back %d bytes\0A\0A\00"
define void @acceptClient(i32 %0) {
%2 = alloca %struct.sockaddr_storage
%3 = alloca i32
%4 = bitcast %struct.sockaddr_storage* %2 to i8*
%5 = bitcast i32* %3 to i8*
%6 = bitcast %struct.sockaddr_storage* %2 to %struct.sockaddr*
br label %7
7: ; preds = %28, %1
store i32 4, i32* %3
%8 = call i32 @memset(%struct.sockaddr_storage* %2, i32 0, i32 4)
%9 = call i8* @llvm.stacksave()
%10 = alloca [1000 x i8]
%11 = getelementptr inbounds [1000 x i8], [1000 x i8]* %10, i64 0, i64 0
%12 = call i64 @recvfrom(i32 %0, i8* %11, i32 1000, i32 0, %struct.sockaddr* %6, i32* %3)
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %7
%15 = call i32 @sysError(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
br label %16
16: ; preds = %7, %14
%17 = getelementptr inbounds [1000 x i8], [1000 x i8]* %10, i64 0, i64 %12
store i8 0, i8* %17
%18 = load i32, i32* @stdout
%19 = call i32 (i32, i8*, ...) @fprintf(i32 %18, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0))
%20 = call i32 @printSocketAddress(%struct.sockaddr* %6)
%21 = load i32, i32* @stdout
%22 = call i32 (i32, i8*, ...) @fprintf(i32 %21, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i64 %12, i8* %11)
%23 = load i32, i32* %3
%24 = call i64 @sendto(i32 %0, i8* %11, i64 %12, i32 0, %struct.sockaddr* %6, i32 %23)
%25 = icmp eq i64 %24, %12
br i1 %25, label %28, label %26
26: ; preds = %16
%27 = call i32 @sysError(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.5, i64 0, i64 0))
br label %28
28: ; preds = %16, %26
%29 = load i32, i32* @stdout
%30 = call i32 (i32, i8*, ...) @fprintf(i32 %29, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.6, i64 0, i64 0), i64 %24)
br label %7
}