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

/* Variables and functions */
int EOF;
int accept(int, struct sockaddr *, int /*<<< orphan*/ *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ perror(char *);
int recv(int, char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ writeSocket(int, char *);
void waitLoop(int sockfd) {
  int clientfd, nbytes;
  struct sockaddr addr;
  socklen_t len;
  char buf[512];
  while (1) {
    clientfd = accept(sockfd, &addr, &len);
    if (clientfd < 0) {
      perror("accept");
      exit(1);
    }
    while ((nbytes = recv(clientfd, buf, 512, 0)) != EOF) {
      buf[nbytes] = '\0';
      strcat(buf, "\r\n");
      writeSocket(clientfd, buf);
    }
    close(clientfd);
  }
}


x86 assembly (clang, -O0):
<waitLoop>:
push   %rbp
mov    %rsp,%rbp
sub    $0x220,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x10(%rbp),%rsi
lea    -0x14(%rbp),%rdx
call   1070 <accept@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    11b1 <waitLoop+0x41>
lea    0xe5e(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
jmp    11b6 <waitLoop+0x46>
mov    -0x8(%rbp),%edi
lea    -0x220(%rbp),%rsi
mov    $0x200,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0xc(%rbp)
mov    0x2e12(%rip),%rcx
cmp    (%rcx),%eax
je     1211 <waitLoop+0xa1>
movslq -0xc(%rbp),%rax
movb   $0x0,-0x220(%rbp,%rax,1)
lea    -0x220(%rbp),%rdi
lea    0xe0f(%rip),%rsi
call   1080 <strcat@plt>
mov    -0x8(%rbp),%edi
lea    -0x220(%rbp),%rsi
call   1050 <writeSocket@plt>
jmp    11b6 <waitLoop+0x46>
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
jmp    117e <waitLoop+0xe>

x86 assembly (clang, -O2):
<waitLoop>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x218,%rsp
mov    %edi,%r14d
lea    0x8(%rsp),%rsi
lea    0xc(%rsp),%rdx
call   1070 <accept@plt>
test   %eax,%eax
js     120f <waitLoop+0x9f>
mov    %eax,%ebp
lea    0x10(%rsp),%rbx
mov    0x2e43(%rip),%r15
lea    0xc(%rsp),%r12
lea    0xe56(%rip),%r13
jmp    11dc <waitLoop+0x6c>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
cltq
movb   $0x0,0x10(%rsp,%rax,1)
mov    %rbx,%rdi
mov    %r13,%rsi
call   1080 <strcat@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
call   1050 <writeSocket@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x200,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
cmp    (%r15),%eax
jne    11c0 <waitLoop+0x50>
mov    %ebp,%edi
call   1040 <close@plt>
mov    %r14d,%edi
lea    0x8(%rsp),%rsi
mov    %r12,%rdx
call   1070 <accept@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    11dc <waitLoop+0x6c>
lea    0xdea(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr = type { i32 }
@.str = [7 x i8] c"accept\00"
@EOF = global i32 0
@.str.1 = [3 x i8] c"\0D\0A\00"
define void @waitLoop(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.sockaddr
%6 = alloca i32
%7 = alloca [512 x i8]
store i32 %0, i32* %2
br label %8
8: ; preds = %1, %32
%9 = load i32, i32* %2
%10 = call i32 @accept(i32 %9, %struct.sockaddr* %5, i32* %6)
store i32 %10, i32* %3
%11 = load i32, i32* %3
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %8
%14 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%15 = call i32 @exit(i32 1)
unreachable
16: ; preds = %8
br label %17
17: ; preds = %23, %16
%18 = load i32, i32* %3
%19 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 0
%20 = call i32 @recv(i32 %18, i8* %19, i32 512, i32 0)
store i32 %20, i32* %4
%21 = load i32, i32* @EOF
%22 = icmp ne i32 %20, %21
br i1 %22, label %23, label %32
23: ; preds = %17
%24 = load i32, i32* %4
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 %25
store i8 0, i8* %26
%27 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 0
%28 = call i32 @strcat(i8* %27, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%29 = load i32, i32* %3
%30 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 0
%31 = call i32 @writeSocket(i32 %29, i8* %30)
br label %17
32: ; preds = %17
%33 = load i32, i32* %3
%34 = call i32 @close(i32 %33)
br label %8
}

LLVM IR (-O2):
%struct.sockaddr = type { i32 }
@.str = [7 x i8] c"accept\00"
@EOF = global i32 0
@.str.1 = [3 x i8] c"\0D\0A\00"
define void @waitLoop(i32 %0) {
%2 = alloca %struct.sockaddr
%3 = alloca i32
%4 = alloca [512 x i8]
%5 = bitcast %struct.sockaddr* %2 to i8*
%6 = bitcast i32* %3 to i8*
%7 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%8 = call i32 @accept(i32 %0, %struct.sockaddr* %2, i32* %3)
%9 = icmp slt i32 %8, 0
br i1 %9, label %15, label %10
10: ; preds = %1, %27
%11 = phi i32 [ %29, %27 ], [ %8, %1 ]
%12 = call i32 @recv(i32 %11, i8* %7, i32 512, i32 0)
%13 = load i32, i32* @EOF
%14 = icmp eq i32 %12, %13
br i1 %14, label %27, label %18
15: ; preds = %27, %1
%16 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %10, %18
%19 = phi i32 [ %24, %18 ], [ %12, %10 ]
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 %20
store i8 0, i8* %21
%22 = call i32 @strcat(i8* %7, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%23 = call i32 @writeSocket(i32 %11, i8* %7)
%24 = call i32 @recv(i32 %11, i8* %7, i32 512, i32 0)
%25 = load i32, i32* @EOF
%26 = icmp eq i32 %24, %25
br i1 %26, label %27, label %18
27: ; preds = %18, %10
%28 = call i32 @close(i32 %11)
%29 = call i32 @accept(i32 %0, %struct.sockaddr* %2, i32* %3)
%30 = icmp slt i32 %29, 0
br i1 %30, label %15, label %10
}