#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 */
typedef int uint8_t;
typedef int /*<<< orphan*/ uint64_t;
typedef scalar_t__ uint32_t;

/* Variables and functions */
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
scalar_t__ pollit(void *, int /*<<< orphan*/, scalar_t__);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ putchar(char);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ stdout;
int wait_for_doorbell(void *base, uint64_t offset, uint32_t doorbell_val,
                      uint32_t timeout, const char *ok_msg) {
  uint8_t ping_recvd = 0;
  while (timeout) {
    if (pollit(base, offset, doorbell_val)) {
      ping_recvd = 1;
      break;
    }
    putchar('.');
    fflush(stdout);
    sleep(1);
    --timeout;
  }
  if (ping_recvd) {
    printf("OK: %s\n", ok_msg);
  } else {
    printf("FAIL: timeout\n");
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<wait_for_doorbell>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8,-0x30(%rbp)
movl   $0x0,-0x34(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     11d6 <wait_for_doorbell+0x86>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x20(%rbp),%rdx
call   1050 <pollit@plt>
cmp    $0x0,%rax
je     11a3 <wait_for_doorbell+0x53>
movl   $0x1,-0x34(%rbp)
jmp    11d6 <wait_for_doorbell+0x86>
mov    $0x2e,%edi
call   1030 <putchar@plt>
mov    0x2e2c(%rip),%rax
mov    (%rax),%edi
call   1060 <fflush@plt>
mov    $0x1,%edi
call   1070 <sleep@plt>
mov    -0x28(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x28(%rbp)
jmp    1172 <wait_for_doorbell+0x22>
cmpl   $0x0,-0x34(%rbp)
je     11f7 <wait_for_doorbell+0xa7>
mov    -0x30(%rbp),%rsi
lea    0xe15(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1211 <wait_for_doorbell+0xc1>
lea    0xe0a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1218 <wait_for_doorbell+0xc8>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<wait_for_doorbell>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %rcx,%rcx
je     11c6 <wait_for_doorbell+0x66>
mov    %r8,%r14
mov    %rcx,%rbx
mov    %rdx,%r15
mov    %esi,%r12d
mov    %rdi,%rbp
mov    0x2e5a(%rip),%r13
cs nopw 0x0(%rax,%rax,1)
mov    %rbp,%rdi
mov    %r12d,%esi
mov    %r15,%rdx
call   1060 <pollit@plt>
test   %rax,%rax
jne    11d9 <wait_for_doorbell+0x79>
mov    $0x2e,%edi
call   1030 <putchar@plt>
mov    0x0(%r13),%edi
call   1070 <fflush@plt>
mov    $0x1,%edi
call   1080 <sleep@plt>
add    $0xffffffffffffffff,%rbx
jne    1190 <wait_for_doorbell+0x30>
lea    0xe3b(%rip),%rdi
call   1040 <puts@plt>
mov    $0xffffffff,%ebx
jmp    11ec <wait_for_doorbell+0x8c>
lea    0xe20(%rip),%rdi
xor    %ebx,%ebx
mov    %r14,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stdout = global i32 0
@.str = [8 x i8] c"OK: %s\0A\00"
@.str.1 = [15 x i8] c"FAIL: timeout\0A\00"
define i32 @wait_for_doorbell(i8* %0, i32 %1, i64 %2, i64 %3, i8* %4) {
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i64
%10 = alloca i64
%11 = alloca i8*
%12 = alloca i32
store i8* %0, i8** %7
store i32 %1, i32* %8
store i64 %2, i64* %9
store i64 %3, i64* %10
store i8* %4, i8** %11
store i32 0, i32* %12
br label %13
13: ; preds = %23, %5
%14 = load i64, i64* %10
%15 = icmp ne i64 %14, 0
br i1 %15, label %16, label %30
16: ; preds = %13
%17 = load i8*, i8** %7
%18 = load i32, i32* %8
%19 = load i64, i64* %9
%20 = call i64 @pollit(i8* %17, i32 %18, i64 %19)
%21 = icmp ne i64 %20, 0
br i1 %21, label %22, label %23
22: ; preds = %16
store i32 1, i32* %12
br label %30
23: ; preds = %16
%24 = call i32 @putchar(i8 signext 46)
%25 = load i32, i32* @stdout
%26 = call i32 @fflush(i32 %25)
%27 = call i32 @sleep(i32 1)
%28 = load i64, i64* %10
%29 = add nsw i64 %28, -1
store i64 %29, i64* %10
br label %13
30: ; preds = %22, %13
%31 = load i32, i32* %12
%32 = icmp ne i32 %31, 0
br i1 %32, label %33, label %36
33: ; preds = %30
%34 = load i8*, i8** %11
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* %34)
br label %38
36: ; preds = %30
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %6
br label %39
38: ; preds = %33
store i32 0, i32* %6
br label %39
39: ; preds = %38, %36
%40 = load i32, i32* %6
ret i32 %40
}

LLVM IR (-O2):
@stdout = global i32 0
@.str = [8 x i8] c"OK: %s\0A\00"
@str = [14 x i8] c"FAIL: timeout\00"
define i32 @wait_for_doorbell(i8* %0, i32 %1, i64 %2, i64 %3, i8* %4) {
%6 = icmp eq i64 %3, 0
br i1 %6, label %20, label %7
7: ; preds = %5, %11
%8 = phi i64 [ %16, %11 ], [ %3, %5 ]
%9 = tail call i64 @pollit(i8* %0, i32 %1, i64 %2)
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %18
11: ; preds = %7
%12 = tail call i32 @putchar(i8 signext 46)
%13 = load i32, i32* @stdout
%14 = tail call i32 @fflush(i32 %13)
%15 = tail call i32 @sleep(i32 1)
%16 = add nsw i64 %8, -1
%17 = icmp eq i64 %16, 0
br i1 %17, label %20, label %7
18: ; preds = %7
%19 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* %4)
br label %22
20: ; preds = %11, %5
%21 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str, i64 0, i64 0))
br label %22
22: ; preds = %18, %20
%23 = phi i32 [ 0, %18 ], [ -1, %20 ]
ret i32 %23
}