#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 scalar_t__ key_t;

/* Variables and functions */
scalar_t__ IPC_PRIVATE;
scalar_t__ RAND_MAX;
int rand();
key_t generate_key(void) {
  key_t key = IPC_PRIVATE;
  long long max = 1LL << (8 * sizeof(key_t) - 2);
  max |= max - 1;
  while (key == IPC_PRIVATE) {
    int rnd = rand();
    key = (key_t)((double)rnd / ((double)RAND_MAX + 1) * (double)(max - 1)) + 1;
  }
  return key;
}


x86 assembly (clang, -O0):
<generate_key>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    0x2ec9(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x8(%rbp)
movabs $0x4000000000000000,%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
sub    $0x1,%rax
or     -0x10(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%rax
mov    0x2e99(%rip),%rcx
cmp    (%rcx),%rax
jne    11a6 <generate_key+0x96>
mov    $0x0,%al
call   1030 <rand@plt>
mov    %eax,-0x14(%rbp)
cvtsi2sdl -0x14(%rbp),%xmm0
mov    0x2e72(%rip),%rax
cvtsi2sdq (%rax),%xmm1
movsd  0xe85(%rip),%xmm2
addsd  %xmm2,%xmm1
divsd  %xmm1,%xmm0
mov    -0x10(%rbp),%rax
sub    $0x1,%rax
cvtsi2sd %rax,%xmm1
mulsd  %xmm1,%xmm0
cvttsd2si %xmm0,%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
jmp    1144 <generate_key+0x34>
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<generate_key>:
push   %r14
push   %rbx
push   %rax
mov    0x2ec5(%rip),%r14
mov    0x2ec6(%rip),%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
xor    %eax,%eax
call   1030 <rand@plt>
xorps  %xmm0,%xmm0
cvtsi2sd %eax,%xmm0
xorps  %xmm1,%xmm1
cvtsi2sdq (%r14),%xmm1
addsd  0xeb2(%rip),%xmm1
divsd  %xmm1,%xmm0
mulsd  0xeae(%rip),%xmm0
cvttsd2si %xmm0,%rax
add    $0x1,%rax
cmp    (%rbx),%rax
je     1130 <generate_key+0x20>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@IPC_PRIVATE = global i64 0
@RAND_MAX = global i64 0
define i64 @generate_key() {
%1 = alloca i64
%2 = alloca i64
%3 = alloca i32
%4 = load i64, i64* @IPC_PRIVATE
store i64 %4, i64* %1
store i64 4611686018427387904, i64* %2
%5 = load i64, i64* %2
%6 = sub nsw i64 %5, 1
%7 = load i64, i64* %2
%8 = or i64 %7, %6
store i64 %8, i64* %2
br label %9
9: ; preds = %13, %0
%10 = load i64, i64* %1
%11 = load i64, i64* @IPC_PRIVATE
%12 = icmp eq i64 %10, %11
br i1 %12, label %13, label %27
13: ; preds = %9
%14 = call i32 (...) @rand()
store i32 %14, i32* %3
%15 = load i32, i32* %3
%16 = sitofp i32 %15 to double
%17 = load i64, i64* @RAND_MAX
%18 = sitofp i64 %17 to double
%19 = fadd double %18, 1.000000e+00
%20 = fdiv double %16, %19
%21 = load i64, i64* %2
%22 = sub nsw i64 %21, 1
%23 = sitofp i64 %22 to double
%24 = fmul double %20, %23
%25 = fptosi double %24 to i64
%26 = add nsw i64 %25, 1
store i64 %26, i64* %1
br label %9
27: ; preds = %9
%28 = load i64, i64* %1
ret i64 %28
}

LLVM IR (-O2):
@IPC_PRIVATE = global i64 0
@RAND_MAX = global i64 0
define i64 @generate_key() {
br label %1
1: ; preds = %0, %1
%2 = tail call i32 (...) @rand()
%3 = sitofp i32 %2 to double
%4 = load i64, i64* @RAND_MAX
%5 = sitofp i64 %4 to double
%6 = fadd double %5, 1.000000e+00
%7 = fdiv double %3, %6
%8 = fmul double %7, 0x43E0000000000000
%9 = fptosi double %8 to i64
%10 = add nsw i64 %9, 1
%11 = load i64, i64* @IPC_PRIVATE
%12 = icmp eq i64 %10, %11
br i1 %12, label %1, label %13
13: ; preds = %1
ret i64 %10
}