#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 timeval {
  int tv_sec;
  scalar_t__ tv_usec;
};
typedef int /*<<< orphan*/ fd_set;

/* Variables and functions */
int /*<<< orphan*/ bzero(char *, int);
int /*<<< orphan*/ error(char *);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ fwrite(char *, int, int, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
int recv(int, char *, int, int /*<<< orphan*/);
int removeCharReturns(char *, int, int *);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
int waitForMessage(int sockfd, char *buffer, const int bufferSize,
                   fd_set *rfds) {
  int retval, n;
  int maxfd = sockfd + 1;
  struct timeval tv;
  tv.tv_sec = 10;
  tv.tv_usec = 0;
  bzero(buffer, sizeof(char) * bufferSize);
  retval = select(maxfd, rfds, NULL, NULL, &tv);
  if (retval == -1)
    error("select()");
  else if (retval) {
    n = recv(sockfd, buffer, 255, 0);
    if (n > 2) {
      buffer[n - 2] = '\0';
    }
    if (n < 0)
      error("ERROR reading from socket");
    else if (n != 0) {
      int overlap = 0;
      n = removeCharReturns(buffer, n, &overlap);
      fwrite(buffer, 1, n, stdout);
      fflush(stdout);
      return 0;
    } else {
      fprintf(stderr, "Why did n = %d?\n", n);
      return -1;
    }
  } else {
    printf("Timeout\n");
    return -1;
  }
  return -1;
}


x86 assembly (clang, -O0):
<waitForMessage>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
movl   $0xa,-0x40(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
movslq -0x14(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%esi
call   10a0 <bzero@plt>
mov    -0x2c(%rbp),%edi
mov    -0x20(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x40(%rbp),%r8
mov    %rcx,%rdx
call   1070 <select@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    1206 <waitForMessage+0x76>
lea    0xe04(%rip),%rdi
call   1090 <error@plt>
jmp    12fa <waitForMessage+0x16a>
cmpl   $0x0,-0x24(%rbp)
je     12dd <waitForMessage+0x14d>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0xff,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x2,-0x28(%rbp)
jle    1241 <waitForMessage+0xb1>
mov    -0x10(%rbp),%rax
mov    -0x28(%rbp),%ecx
sub    $0x2,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
cmpl   $0x0,-0x28(%rbp)
jge    125c <waitForMessage+0xcc>
lea    0xdb7(%rip),%rdi
call   1090 <error@plt>
jmp    12d8 <waitForMessage+0x148>
cmpl   $0x0,-0x28(%rbp)
je     12b4 <waitForMessage+0x124>
movl   $0x0,-0x44(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%esi
lea    -0x44(%rbp),%rdx
call   1050 <removeCharReturns@plt>
mov    %eax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%edx
mov    0x2d4a(%rip),%rax
mov    (%rax),%ecx
mov    $0x1,%esi
call   10b0 <fwrite@plt>
mov    0x2d37(%rip),%rax
mov    (%rax),%edi
call   1080 <fflush@plt>
movl   $0x0,-0x4(%rbp)
jmp    1301 <waitForMessage+0x171>
mov    0x2d25(%rip),%rax
mov    (%rax),%edi
mov    -0x28(%rbp),%edx
lea    0xd5c(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1301 <waitForMessage+0x171>
jmp    12f5 <waitForMessage+0x165>
lea    0xd50(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1301 <waitForMessage+0x171>
jmp    12fa <waitForMessage+0x16a>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<waitForMessage>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rcx,%rbx
mov    %rsi,%r14
mov    %edi,%r15d
lea    0x1(%r15),%ebp
movl   $0xa,0x8(%rsp)
movq   $0x0,0x10(%rsp)
mov    %rsi,%rdi
mov    %edx,%esi
call   10a0 <bzero@plt>
lea    0x8(%rsp),%r8
mov    %ebp,%edi
mov    %rbx,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
call   1070 <select@plt>
test   %eax,%eax
je     11ec <waitForMessage+0x5c>
cmp    $0xffffffff,%eax
jne    11ff <waitForMessage+0x6f>
lea    0xe1b(%rip),%rdi
call   1090 <error@plt>
jmp    11f8 <waitForMessage+0x68>
lea    0xe41(%rip),%rdi
call   1040 <puts@plt>
mov    $0xffffffff,%eax
jmp    125e <waitForMessage+0xce>
mov    %r15d,%edi
mov    %r14,%rsi
mov    $0xff,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
cmp    $0x3,%eax
jl     1220 <waitForMessage+0x90>
lea    -0x2(%rax),%ecx
movb   $0x0,(%r14,%rcx,1)
jmp    1226 <waitForMessage+0x96>
test   %eax,%eax
js     1269 <waitForMessage+0xd9>
je     1277 <waitForMessage+0xe7>
movl   $0x0,0x4(%rsp)
lea    0x4(%rsp),%rdx
mov    %r14,%rdi
mov    %eax,%esi
call   1050 <removeCharReturns@plt>
mov    0x2d94(%rip),%rbx
mov    (%rbx),%ecx
mov    %r14,%rdi
mov    $0x1,%esi
mov    %eax,%edx
call   10b0 <fwrite@plt>
mov    (%rbx),%edi
call   1080 <fflush@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd99(%rip),%rdi
call   1090 <error@plt>
jmp    11f8 <waitForMessage+0x68>
mov    0x2d62(%rip),%rax
mov    (%rax),%edi
lea    0xd9c(%rip),%rsi
xor    %edx,%edx
call   1060 <fprintf@plt>
jmp    11f8 <waitForMessage+0x68>

LLVM IR (-O0):
%struct.timeval = type { i32, i64 }
@.str = [9 x i8] c"select()\00"
@.str.1 = [26 x i8] c"ERROR reading from socket\00"
@stdout = global i32 0
@stderr = global i32 0
@.str.2 = [17 x i8] c"Why did n = %d?\0A\00"
@.str.3 = [9 x i8] c"Timeout\0A\00"
define i32 @waitForMessage(i32 %0, i8* %1, i32 %2, i32* %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32*
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca %struct.timeval
%14 = alloca i32
store i32 %0, i32* %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store i32* %3, i32** %9
%15 = load i32, i32* %6
%16 = add nsw i32 %15, 1
store i32 %16, i32* %12
%17 = getelementptr inbounds %struct.timeval, %struct.timeval* %13, i32 0, i32 0
store i32 10, i32* %17
%18 = getelementptr inbounds %struct.timeval, %struct.timeval* %13, i32 0, i32 1
store i64 0, i64* %18
%19 = load i8*, i8** %7
%20 = load i32, i32* %8
%21 = sext i32 %20 to i64
%22 = mul i64 1, %21
%23 = trunc i64 %22 to i32
%24 = call i32 @bzero(i8* %19, i32 %23)
%25 = load i32, i32* %12
%26 = load i32*, i32** %9
%27 = call i32 @select(i32 %25, i32* %26, i32* null, i32* null, %struct.timeval* %13)
store i32 %27, i32* %10
%28 = load i32, i32* %10
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %32
30: ; preds = %4
%31 = call i32 @error(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
br label %73
32: ; preds = %4
%33 = load i32, i32* %10
%34 = icmp ne i32 %33, 0
br i1 %34, label %35, label %70
35: ; preds = %32
%36 = load i32, i32* %6
%37 = load i8*, i8** %7
%38 = call i32 @recv(i32 %36, i8* %37, i32 255, i32 0)
store i32 %38, i32* %11
%39 = load i32, i32* %11
%40 = icmp sgt i32 %39, 2
br i1 %40, label %41, label %47
41: ; preds = %35
%42 = load i8*, i8** %7
%43 = load i32, i32* %11
%44 = sub nsw i32 %43, 2
%45 = sext i32 %44 to i64
%46 = getelementptr inbounds i8, i8* %42, i64 %45
store i8 0, i8* %46
br label %47
47: ; preds = %41, %35
%48 = load i32, i32* %11
%49 = icmp slt i32 %48, 0
br i1 %49, label %50, label %52
50: ; preds = %47
%51 = call i32 @error(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
br label %69
52: ; preds = %47
%53 = load i32, i32* %11
%54 = icmp ne i32 %53, 0
br i1 %54, label %55, label %65
55: ; preds = %52
store i32 0, i32* %14
%56 = load i8*, i8** %7
%57 = load i32, i32* %11
%58 = call i32 @removeCharReturns(i8* %56, i32 %57, i32* %14)
store i32 %58, i32* %11
%59 = load i8*, i8** %7
%60 = load i32, i32* %11
%61 = load i32, i32* @stdout
%62 = call i32 @fwrite(i8* %59, i32 1, i32 %60, i32 %61)
%63 = load i32, i32* @stdout
%64 = call i32 @fflush(i32 %63)
store i32 0, i32* %5
br label %74
65: ; preds = %52
%66 = load i32, i32* @stderr
%67 = load i32, i32* %11
%68 = call i32 @fprintf(i32 %66, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i32 %67)
store i32 -1, i32* %5
br label %74
69: ; preds = %50
br label %72
70: ; preds = %32
%71 = call i32 @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %5
br label %74
72: ; preds = %69
br label %73
73: ; preds = %72, %30
store i32 -1, i32* %5
br label %74
74: ; preds = %73, %70, %65, %55
%75 = load i32, i32* %5
ret i32 %75
}

LLVM IR (-O2):
%struct.timeval = type { i32, i64 }
@.str = [9 x i8] c"select()\00"
@.str.1 = [26 x i8] c"ERROR reading from socket\00"
@stdout = global i32 0
@stderr = global i32 0
@.str.2 = [17 x i8] c"Why did n = %d?\0A\00"
@str = [8 x i8] c"Timeout\00"
define i32 @waitForMessage(i32 %0, i8* %1, i32 %2, i32* %3) {
%5 = alloca %struct.timeval
%6 = alloca i32
%7 = add nsw i32 %0, 1
%8 = bitcast %struct.timeval* %5 to i8*
%9 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i64 0, i32 0
store i32 10, i32* %9
%10 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i64 0, i32 1
store i64 0, i64* %10
%11 = tail call i32 @bzero(i8* %1, i32 %2)
%12 = call i32 @select(i32 %7, i32* %3, i32* null, i32* null, %struct.timeval* %5)
switch i32 %12, label %15 [
i32 -1, label %13
i32 0, label %38
]
13: ; preds = %4
%14 = call i32 @error(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
br label %40
15: ; preds = %4
%16 = call i32 @recv(i32 %0, i8* %1, i32 255, i32 0)
%17 = icmp sgt i32 %16, 2
br i1 %17, label %18, label %22
18: ; preds = %15
%19 = add nsw i32 %16, -2
%20 = zext i32 %19 to i64
%21 = getelementptr inbounds i8, i8* %1, i64 %20
store i8 0, i8* %21
br label %28
22: ; preds = %15
%23 = icmp slt i32 %16, 0
br i1 %23, label %24, label %26
24: ; preds = %22
%25 = call i32 @error(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
br label %40
26: ; preds = %22
%27 = icmp eq i32 %16, 0
br i1 %27, label %35, label %28
28: ; preds = %18, %26
%29 = bitcast i32* %6 to i8*
store i32 0, i32* %6
%30 = call i32 @removeCharReturns(i8* %1, i32 %16, i32* %6)
%31 = load i32, i32* @stdout
%32 = call i32 @fwrite(i8* %1, i32 1, i32 %30, i32 %31)
%33 = load i32, i32* @stdout
%34 = call i32 @fflush(i32 %33)
br label %40
35: ; preds = %26
%36 = load i32, i32* @stderr
%37 = call i32 @fprintf(i32 %36, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i32 0)
br label %40
38: ; preds = %4
%39 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @str, i64 0, i64 0))
br label %40
40: ; preds = %13, %24, %38, %35, %28
%41 = phi i32 [ 0, %28 ], [ -1, %35 ], [ -1, %38 ], [ -1, %24 ], [ -1, %13 ]
ret i32 %41
}