#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;
typedef int /*<<< orphan*/ buffer;

/* Variables and functions */
int /*<<< orphan*/ bzero(char *, int);
int /*<<< orphan*/ error(char *);
int /*<<< orphan*/ printf(char *, ...);
int recv(int, char *, int, int /*<<< orphan*/);
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int waitForMessage(int sockfd, char *buffer, fd_set *rfds) {
  int retval, n;
  int maxfd = sockfd + 1;
  struct timeval tv;
  tv.tv_sec = 10;
  tv.tv_usec = 0;
  bzero(buffer, sizeof(buffer));
  retval = select(maxfd, rfds, NULL, NULL, &tv);
  if (retval == -1)
    error("select()");
  else if (retval) {
    n = recv(sockfd, buffer, 255, 0);
    if (n < 0)
      error("ERROR reading from socket");
    if (n != 0) {
      printf("%d: %s\n", n, buffer);
      return 0;
    }
  } else {
    printf("Timeout\n");
    return -1;
  }
}


x86 assembly (clang, -O0):
<waitForMessage>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
movl   $0xa,-0x38(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x8,%esi
call   1070 <bzero@plt>
mov    -0x24(%rbp),%edi
mov    -0x18(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x38(%rbp),%r8
mov    %rcx,%rdx
call   1050 <select@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11be <waitForMessage+0x6e>
lea    0xe4c(%rip),%rdi
call   1060 <error@plt>
jmp    1243 <waitForMessage+0xf3>
cmpl   $0x0,-0x1c(%rbp)
je     1224 <waitForMessage+0xd4>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0xff,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    11f4 <waitForMessage+0xa4>
lea    0xe1a(%rip),%rdi
call   1060 <error@plt>
cmpl   $0x0,-0x20(%rbp)
je     121f <waitForMessage+0xcf>
mov    -0x20(%rbp),%esi
mov    -0x10(%rbp),%rdx
lea    0xe17(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1243 <waitForMessage+0xf3>
jmp    123e <waitForMessage+0xee>
lea    0xe00(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1243 <waitForMessage+0xf3>
jmp    1243 <waitForMessage+0xf3>
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<waitForMessage>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%rbp
mov    %rsi,%r14
mov    %edi,%r15d
lea    0x1(%r15),%ebx
movl   $0xa,0x8(%rsp)
movq   $0x0,0x10(%rsp)
mov    %rsi,%rdi
mov    $0x8,%esi
call   1080 <bzero@plt>
lea    0x8(%rsp),%r8
mov    %ebx,%edi
mov    %rbp,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
call   1060 <select@plt>
test   %eax,%eax
je     11bf <waitForMessage+0x5f>
cmp    $0xffffffff,%eax
jne    11d2 <waitForMessage+0x72>
lea    0xe48(%rip),%rdi
call   1070 <error@plt>
jmp    120f <waitForMessage+0xaf>
lea    0xe65(%rip),%rdi
call   1040 <puts@plt>
mov    $0xffffffff,%ebp
jmp    120f <waitForMessage+0xaf>
mov    %r15d,%edi
mov    %r14,%rsi
mov    $0xff,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,%ebx
test   %eax,%eax
js     11ee <waitForMessage+0x8e>
jne    11fa <waitForMessage+0x9a>
jmp    120f <waitForMessage+0xaf>
lea    0xe14(%rip),%rdi
call   1070 <error@plt>
lea    0xe22(%rip),%rdi
xor    %ebp,%ebp
mov    %ebx,%esi
mov    %r14,%rdx
xor    %eax,%eax
call   1050 <printf@plt>
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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"
@.str.2 = [8 x i8] c"%d: %s\0A\00"
@.str.3 = [9 x i8] c"Timeout\0A\00"
define i32 @waitForMessage(i32 %0, i8* %1, i32* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca %struct.timeval
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32* %2, i32** %7
%12 = load i32, i32* %5
%13 = add nsw i32 %12, 1
store i32 %13, i32* %10
%14 = getelementptr inbounds %struct.timeval, %struct.timeval* %11, i32 0, i32 0
store i32 10, i32* %14
%15 = getelementptr inbounds %struct.timeval, %struct.timeval* %11, i32 0, i32 1
store i64 0, i64* %15
%16 = load i8*, i8** %6
%17 = call i32 @bzero(i8* %16, i32 8)
%18 = load i32, i32* %10
%19 = load i32*, i32** %7
%20 = call i32 @select(i32 %18, i32* %19, i32* null, i32* null, %struct.timeval* %11)
store i32 %20, i32* %8
%21 = load i32, i32* %8
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %25
23: ; preds = %3
%24 = call i32 @error(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
br label %47
25: ; preds = %3
%26 = load i32, i32* %8
%27 = icmp ne i32 %26, 0
br i1 %27, label %28, label %44
28: ; preds = %25
%29 = load i32, i32* %5
%30 = load i8*, i8** %6
%31 = call i32 @recv(i32 %29, i8* %30, i32 255, i32 0)
store i32 %31, i32* %9
%32 = load i32, i32* %9
%33 = icmp slt i32 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %28
%35 = call i32 @error(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
br label %36
36: ; preds = %34, %28
%37 = load i32, i32* %9
%38 = icmp ne i32 %37, 0
br i1 %38, label %39, label %43
39: ; preds = %36
%40 = load i32, i32* %9
%41 = load i8*, i8** %6
%42 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i32 %40, i8* %41)
store i32 0, i32* %4
br label %47
43: ; preds = %36
br label %46
44: ; preds = %25
%45 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %4
br label %47
46: ; preds = %43
br label %47
47: ; preds = %39, %44, %46, %23
%48 = load i32, i32* %4
ret i32 %48
}

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"
@.str.2 = [8 x i8] c"%d: %s\0A\00"
@str = [8 x i8] c"Timeout\00"
define i32 @waitForMessage(i32 %0, i8* %1, i32* %2) {
%4 = alloca %struct.timeval
%5 = add nsw i32 %0, 1
%6 = bitcast %struct.timeval* %4 to i8*
%7 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 0
store i32 10, i32* %7
%8 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 1
store i64 0, i64* %8
%9 = tail call i32 @bzero(i8* %1, i32 8)
%10 = call i32 @select(i32 %5, i32* %2, i32* null, i32* null, %struct.timeval* %4)
switch i32 %10, label %13 [
i32 -1, label %11
i32 0, label %22
]
11: ; preds = %3
%12 = call i32 @error(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
br label %24
13: ; preds = %3
%14 = call i32 @recv(i32 %0, i8* %1, i32 255, i32 0)
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %13
%17 = call i32 @error(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
br label %20
18: ; preds = %13
%19 = icmp eq i32 %14, 0
br i1 %19, label %24, label %20
20: ; preds = %16, %18
%21 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i32 %14, i8* %1)
br label %24
22: ; preds = %3
%23 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @str, i64 0, i64 0))
br label %24
24: ; preds = %11, %18, %22, %20
%25 = phi i32 [ 0, %20 ], [ -1, %22 ], [ undef, %18 ], [ undef, %11 ]
ret i32 %25
}