#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 */

/* Variables and functions */
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_RCVTIMEO;
int recv(int, void *, int, int /*<<< orphan*/);
int recvfromTimeOut(int, long, long);
int /*<<< orphan*/ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/,
                              unsigned long *, int);
int receiveMessage(int sock, void *message, int maxLength,
                   unsigned long timeout) {
  unsigned long val;
  long timeout_s;
  long timeout_us;
  int bytesReceived;
  if (sock < 0) {
    return -1;
  }
  val = timeout + 100;
  setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &val, sizeof(unsigned long));
  timeout_s = timeout / 1000;
  timeout_us = (timeout - (timeout_s * 1000)) * 1000;
  bytesReceived = 0;
  switch (recvfromTimeOut(sock, timeout_s, timeout_us)) {
  case 0:
  case -1:
    return -1;
    break;
  default:
    bytesReceived = recv(sock, message, maxLength, 0);
    break;
  }
  return bytesReceived;
}


x86 assembly (clang, -O0):
<receiveMessage>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %rcx,-0x20(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    115c <receiveMessage+0x2c>
movl   $0xffffffff,-0x4(%rbp)
jmp    1214 <receiveMessage+0xe4>
mov    -0x20(%rbp),%rax
add    $0x64,%rax
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e76(%rip),%rax
mov    (%rax),%esi
mov    0x2e5d(%rip),%rax
mov    (%rax),%edx
lea    -0x28(%rbp),%rcx
mov    $0x8,%r8d
call   1040 <setsockopt@plt>
mov    -0x20(%rbp),%rax
shr    $0x3,%rax
movabs $0x20c49ba5e353f7cf,%rcx
9b c4 20
mul    %rcx
shr    $0x4,%rdx
mov    %rdx,-0x30(%rbp)
mov    -0x20(%rbp),%rax
mov    -0x30(%rbp),%rcx
imul   $0x3e8,%rcx,%rcx
sub    %rcx,%rax
imul   $0x3e8,%rax,%rax
mov    %rax,-0x38(%rbp)
movl   $0x0,-0x3c(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    -0x38(%rbp),%rdx
call   1050 <recvfromTimeOut@plt>
add    $0x1,%eax
sub    $0x1,%eax
ja     11fa <receiveMessage+0xca>
jmp    11ee <receiveMessage+0xbe>
movl   $0xffffffff,-0x4(%rbp)
jmp    1214 <receiveMessage+0xe4>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<receiveMessage>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    $0xffffffff,%r12d
test   %edi,%edi
js     11d0 <receiveMessage+0xa0>
mov    %rcx,%rbp
mov    %edx,%r14d
mov    %rsi,%r15
mov    %edi,%ebx
lea    0x64(%rcx),%rax
mov    %rax,0x8(%rsp)
mov    0x2e83(%rip),%rax
mov    (%rax),%esi
mov    0x2e6a(%rip),%rax
mov    (%rax),%edx
lea    0x8(%rsp),%rcx
mov    $0x8,%r8d
call   1040 <setsockopt@plt>
mov    %rbp,%rax
shr    $0x3,%rax
movabs $0x20c49ba5e353f7cf,%rcx
9b c4 20
mul    %rcx
shr    $0x4,%rdx
imul   $0xfffffffffffffc18,%rdx,%rax
add    %rbp,%rax
imul   $0x3e8,%rax,%rax
mov    %ebx,%edi
mov    %rdx,%rsi
mov    %rax,%rdx
call   1050 <recvfromTimeOut@plt>
add    $0x1,%eax
cmp    $0x2,%eax
jb     11d0 <receiveMessage+0xa0>
mov    %ebx,%edi
mov    %r15,%rsi
mov    %r14d,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,%r12d
mov    %r12d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@SOL_SOCKET = global i32 0
@SO_RCVTIMEO = global i32 0
define i32 @receiveMessage(i32 %0, i8* %1, i32 %2, i64 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i64
%10 = alloca i64
%11 = alloca i64
%12 = alloca i64
%13 = alloca i32
store i32 %0, i32* %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store i64 %3, i64* %9
%14 = load i32, i32* %6
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %17
16: ; preds = %4
store i32 -1, i32* %5
br label %43
17: ; preds = %4
%18 = load i64, i64* %9
%19 = add i64 %18, 100
store i64 %19, i64* %10
%20 = load i32, i32* %6
%21 = load i32, i32* @SOL_SOCKET
%22 = load i32, i32* @SO_RCVTIMEO
%23 = call i32 @setsockopt(i32 %20, i32 %21, i32 %22, i64* %10, i32 8)
%24 = load i64, i64* %9
%25 = udiv i64 %24, 1000
store i64 %25, i64* %11
%26 = load i64, i64* %9
%27 = load i64, i64* %11
%28 = mul nsw i64 %27, 1000
%29 = sub i64 %26, %28
%30 = mul i64 %29, 1000
store i64 %30, i64* %12
store i32 0, i32* %13
%31 = load i32, i32* %6
%32 = load i64, i64* %11
%33 = load i64, i64* %12
%34 = call i32 @recvfromTimeOut(i32 %31, i64 %32, i64 %33)
switch i32 %34, label %36 [
i32 0, label %35
i32 -1, label %35
]
35: ; preds = %17, %17
store i32 -1, i32* %5
br label %43
36: ; preds = %17
%37 = load i32, i32* %6
%38 = load i8*, i8** %7
%39 = load i32, i32* %8
%40 = call i32 @recv(i32 %37, i8* %38, i32 %39, i32 0)
store i32 %40, i32* %13
br label %41
41: ; preds = %36
%42 = load i32, i32* %13
store i32 %42, i32* %5
br label %43
43: ; preds = %41, %35, %16
%44 = load i32, i32* %5
ret i32 %44
}

LLVM IR (-O2):
@SOL_SOCKET = global i32 0
@SO_RCVTIMEO = global i32 0
define i32 @receiveMessage(i32 %0, i8* %1, i32 %2, i64 %3) {
%5 = alloca i64
%6 = bitcast i64* %5 to i8*
%7 = icmp slt i32 %0, 0
br i1 %7, label %20, label %8
8: ; preds = %4
%9 = add i64 %3, 100
store i64 %9, i64* %5
%10 = load i32, i32* @SOL_SOCKET
%11 = load i32, i32* @SO_RCVTIMEO
%12 = call i32 @setsockopt(i32 %0, i32 %10, i32 %11, i64* %5, i32 8)
%13 = udiv i64 %3, 1000
%14 = mul i64 %13, -1000
%15 = add i64 %14, %3
%16 = mul i64 %15, 1000
%17 = call i32 @recvfromTimeOut(i32 %0, i64 %13, i64 %16)
switch i32 %17, label %18 [
i32 0, label %20
i32 -1, label %20
]
18: ; preds = %8
%19 = call i32 @recv(i32 %0, i8* %1, i32 %2, i32 0)
br label %20
20: ; preds = %8, %8, %4, %18
%21 = phi i32 [ %19, %18 ], [ -1, %4 ], [ -1, %8 ], [ -1, %8 ]
ret i32 %21
}