#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 */
double RAND_MAX;
double rand();
void init_spins(int *s, int N, double p) {
  int i;
  double val;
  for (i = 0; i < N; i++) {
    val = 1.0 * rand() / RAND_MAX;
    if (val < p) {
      s[i] = 1;
    } else
      s[i] = -1;
  }
}


x86 assembly (clang, -O0):
<init_spins>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movsd  %xmm0,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    11a7 <init_spins+0x97>
mov    $0x0,%al
call   1030 <rand@plt>
movaps %xmm0,%xmm1
movsd  0xeb7(%rip),%xmm0
mulsd  %xmm1,%xmm0
mov    0x2e94(%rip),%rax
divsd  (%rax),%xmm0
movsd  %xmm0,-0x28(%rbp)
movsd  -0x28(%rbp),%xmm1
movsd  -0x18(%rbp),%xmm0
ucomisd %xmm1,%xmm0
jbe    1185 <init_spins+0x75>
mov    -0x8(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movl   $0x1,(%rax,%rcx,4)
jmp    1194 <init_spins+0x84>
mov    -0x8(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movl   $0xffffffff,(%rax,%rcx,4)
jmp    1199 <init_spins+0x89>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    112b <init_spins+0x1b>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<init_spins>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
test   %esi,%esi
jle    115c <init_spins+0x4c>
mov    %rdi,%r14
mov    %esi,%r12d
mov    0x2ebf(%rip),%r15
xor    %ebx,%ebx
movsd  %xmm0,(%rsp)
xor    %eax,%eax
call   1030 <rand@plt>
divsd  (%r15),%xmm0
xor    %eax,%eax
movsd  (%rsp),%xmm1
ucomisd %xmm0,%xmm1
seta   %al
add    %eax,%eax
add    $0xffffffff,%eax
mov    %eax,(%r14,%rbx,4)
add    $0x1,%rbx
cmp    %rbx,%r12
jne    1130 <init_spins+0x20>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@RAND_MAX = global double 0.000000e+00
define void @init_spins(i32* %0, i32 %1, double %2) {
%4 = alloca i32*
%5 = alloca i32
%6 = alloca double
%7 = alloca i32
%8 = alloca double
store i32* %0, i32** %4
store i32 %1, i32* %5
store double %2, double* %6
store i32 0, i32* %7
br label %9
9: ; preds = %32, %3
%10 = load i32, i32* %7
%11 = load i32, i32* %5
%12 = icmp slt i32 %10, %11
br i1 %12, label %13, label %35
13: ; preds = %9
%14 = call double (...) @rand()
%15 = fmul double 1.000000e+00, %14
%16 = load double, double* @RAND_MAX
%17 = fdiv double %15, %16
store double %17, double* %8
%18 = load double, double* %8
%19 = load double, double* %6
%20 = fcmp olt double %18, %19
br i1 %20, label %21, label %26
21: ; preds = %13
%22 = load i32*, i32** %4
%23 = load i32, i32* %7
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds i32, i32* %22, i64 %24
store i32 1, i32* %25
br label %31
26: ; preds = %13
%27 = load i32*, i32** %4
%28 = load i32, i32* %7
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds i32, i32* %27, i64 %29
store i32 -1, i32* %30
br label %31
31: ; preds = %26, %21
br label %32
32: ; preds = %31
%33 = load i32, i32* %7
%34 = add nsw i32 %33, 1
store i32 %34, i32* %7
br label %9
35: ; preds = %9
ret void
}

LLVM IR (-O2):
@RAND_MAX = global double 0.000000e+00
define void @init_spins(i32* nocapture writeonly %0, i32 %1, double %2) {
%4 = icmp sgt i32 %1, 0
br i1 %4, label %5, label %17
5: ; preds = %3
%6 = zext i32 %1 to i64
br label %7
7: ; preds = %5, %7
%8 = phi i64 [ 0, %5 ], [ %15, %7 ]
%9 = tail call double (...) @rand()
%10 = load double, double* @RAND_MAX
%11 = fdiv double %9, %10
%12 = fcmp olt double %11, %2
%13 = select i1 %12, i32 1, i32 -1
%14 = getelementptr inbounds i32, i32* %0, i64 %8
store i32 %13, i32* %14
%15 = add nuw nsw i64 %8, 1
%16 = icmp eq i64 %15, %6
br i1 %16, label %17, label %7
17: ; preds = %7, %3
ret void
}