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

/* Variables and functions */
int /*<<< orphan*/ FD_SET(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ FD_ZERO(int /*<<< orphan*/ *);
int applid2fd(unsigned int);
int capi20_isinstalled();
int select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
           int /*<<< orphan*/ *, struct timeval *);
int /*<<< orphan*/ validapplid(unsigned int);
unsigned capi20_waitformessage(unsigned ApplID, struct timeval *TimeOut) {
  int fd;
  fd_set rfds;
  FD_ZERO(&rfds);
  if (capi20_isinstalled() != 0x0000)
    return 0x1009;
  if (!validapplid(ApplID))
    return 0x1101;
  fd = applid2fd(ApplID);
  FD_SET(fd, &rfds);
  if (select(fd + 1, &rfds, NULL, NULL, TimeOut) < 1)
    return 0x1104;
  return 0x0000;
}


x86 assembly (clang, -O0):
<capi20_waitformessage>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x18(%rbp),%rdi
call   1050 <FD_ZERO@plt>
mov    $0x0,%al
call   1040 <capi20_isinstalled@plt>
cmp    $0x0,%eax
je     1194 <capi20_waitformessage+0x34>
movl   $0x1009,-0x4(%rbp)
jmp    11fe <capi20_waitformessage+0x9e>
mov    -0x8(%rbp),%edi
call   1080 <validapplid@plt>
cmp    $0x0,%eax
jne    11b1 <capi20_waitformessage+0x51>
movl   $0x1101,-0x4(%rbp)
jmp    11fe <capi20_waitformessage+0x9e>
mov    -0x8(%rbp),%edi
call   1030 <applid2fd@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   1070 <FD_SET@plt>
mov    -0x14(%rbp),%edi
add    $0x1,%edi
mov    -0x10(%rbp),%r8
lea    -0x18(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%ecx
mov    %rcx,%rdx
call   1060 <select@plt>
cmp    $0x1,%eax
jge    11f7 <capi20_waitformessage+0x97>
movl   $0x1104,-0x4(%rbp)
jmp    11fe <capi20_waitformessage+0x9e>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<capi20_waitformessage>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %edi,%ebp
lea    0x4(%rsp),%rdi
call   1050 <FD_ZERO@plt>
xor    %eax,%eax
call   1040 <capi20_isinstalled@plt>
mov    %eax,%ecx
mov    $0x1009,%eax
test   %ecx,%ecx
jne    11d4 <capi20_waitformessage+0x74>
mov    %ebp,%edi
call   1080 <validapplid@plt>
test   %eax,%eax
je     11cf <capi20_waitformessage+0x6f>
mov    %ebp,%edi
call   1030 <applid2fd@plt>
mov    %eax,%ebp
lea    0x4(%rsp),%r14
mov    %eax,%edi
mov    %r14,%rsi
call   1070 <FD_SET@plt>
add    $0x1,%ebp
xor    %r15d,%r15d
mov    %ebp,%edi
mov    %r14,%rsi
xor    %edx,%edx
xor    %ecx,%ecx
mov    %rbx,%r8
call   1060 <select@plt>
test   %eax,%eax
mov    $0x1104,%eax
cmovg  %r15d,%eax
jmp    11d4 <capi20_waitformessage+0x74>
mov    $0x1101,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i32 }
define i32 @capi20_waitformessage(i32 %0, %struct.timeval* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.timeval*
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %4
store %struct.timeval* %1, %struct.timeval** %5
%8 = call i32 @FD_ZERO(i32* %7)
%9 = call i32 (...) @capi20_isinstalled()
%10 = icmp ne i32 %9, 0
br i1 %10, label %11, label %12
11: ; preds = %2
store i32 4105, i32* %3
br label %29
12: ; preds = %2
%13 = load i32, i32* %4
%14 = call i32 @validapplid(i32 %13)
%15 = icmp ne i32 %14, 0
br i1 %15, label %17, label %16
16: ; preds = %12
store i32 4353, i32* %3
br label %29
17: ; preds = %12
%18 = load i32, i32* %4
%19 = call i32 @applid2fd(i32 %18)
store i32 %19, i32* %6
%20 = load i32, i32* %6
%21 = call i32 @FD_SET(i32 %20, i32* %7)
%22 = load i32, i32* %6
%23 = add nsw i32 %22, 1
%24 = load %struct.timeval*, %struct.timeval** %5
%25 = call i32 @select(i32 %23, i32* %7, i32* null, i32* null, %struct.timeval* %24)
%26 = icmp slt i32 %25, 1
br i1 %26, label %27, label %28
27: ; preds = %17
store i32 4356, i32* %3
br label %29
28: ; preds = %17
store i32 0, i32* %3
br label %29
29: ; preds = %28, %27, %16, %11
%30 = load i32, i32* %3
ret i32 %30
}

LLVM IR (-O2):
%struct.timeval = type { i32 }
define i32 @capi20_waitformessage(i32 %0, %struct.timeval* %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = call i32 @FD_ZERO(i32* %3)
%6 = call i32 (...) @capi20_isinstalled()
%7 = icmp eq i32 %6, 0
br i1 %7, label %8, label %18
8: ; preds = %2
%9 = call i32 @validapplid(i32 %0)
%10 = icmp eq i32 %9, 0
br i1 %10, label %18, label %11
11: ; preds = %8
%12 = call i32 @applid2fd(i32 %0)
%13 = call i32 @FD_SET(i32 %12, i32* %3)
%14 = add nsw i32 %12, 1
%15 = call i32 @select(i32 %14, i32* %3, i32* null, i32* null, %struct.timeval* %1)
%16 = icmp sgt i32 %15, 0
%17 = select i1 %16, i32 0, i32 4356
br label %18
18: ; preds = %11, %8, %2
%19 = phi i32 [ 4105, %2 ], [ 4353, %8 ], [ %17, %11 ]
ret i32 %19
}