#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 drand48();
void populate_bonds(int **vbonds, int **hbonds, int M, int N, double p) {
  int i, j;
  for (i = 0; i < M; i++) {
    for (j = 0; j < N; j++) {
      vbonds[i][j] = (drand48() < p) ? 1 : 0;
      hbonds[i][j] = (drand48() < p) ? 1 : 0;
    }
  }
}


x86 assembly (clang, -O0):
<populate_bonds>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
movsd  %xmm0,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11d2 <populate_bonds+0xc2>
movl   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    11bf <populate_bonds+0xaf>
mov    $0x0,%al
call   1030 <drand48@plt>
movaps %xmm0,%xmm1
movsd  -0x20(%rbp),%xmm0
xor    %edx,%edx
mov    $0x1,%eax
ucomisd %xmm1,%xmm0
cmova  %eax,%edx
mov    -0x8(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
movslq -0x28(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    $0x0,%al
call   1030 <drand48@plt>
movaps %xmm0,%xmm1
movsd  -0x20(%rbp),%xmm0
xor    %edx,%edx
mov    $0x1,%eax
ucomisd %xmm1,%xmm0
cmova  %eax,%edx
mov    -0x10(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
movslq -0x28(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    1145 <populate_bonds+0x35>
jmp    11c4 <populate_bonds+0xb4>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1132 <populate_bonds+0x22>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<populate_bonds>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %ecx,0xc(%rsp)
test   %edx,%edx
jle    11b8 <populate_bonds+0xa8>
mov    %rsi,%r12
mov    %rdi,%r13
mov    %edx,%r15d
mov    0xc(%rsp),%ebx
xor    %ebp,%ebp
movsd  %xmm0,0x10(%rsp)
jmp    1159 <populate_bonds+0x49>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
add    $0x1,%rbp
cmp    %r15,%rbp
je     11b8 <populate_bonds+0xa8>
cmpl   $0x0,0xc(%rsp)
jle    1150 <populate_bonds+0x40>
xor    %r14d,%r14d
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
xor    %eax,%eax
call   1030 <drand48@plt>
xor    %eax,%eax
movsd  0x10(%rsp),%xmm1
ucomisd %xmm0,%xmm1
seta   %al
mov    0x0(%r13,%rbp,8),%rcx
mov    %eax,(%rcx,%r14,4)
xor    %eax,%eax
call   1030 <drand48@plt>
xor    %eax,%eax
movsd  0x10(%rsp),%xmm1
ucomisd %xmm0,%xmm1
seta   %al
mov    (%r12,%rbp,8),%rcx
mov    %eax,(%rcx,%r14,4)
add    $0x1,%r14
cmp    %r14,%rbx
jne    1170 <populate_bonds+0x60>
jmp    1150 <populate_bonds+0x40>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @populate_bonds(i32** %0, i32** %1, i32 %2, i32 %3, double %4) {
%6 = alloca i32**
%7 = alloca i32**
%8 = alloca i32
%9 = alloca i32
%10 = alloca double
%11 = alloca i32
%12 = alloca i32
store i32** %0, i32*** %6
store i32** %1, i32*** %7
store i32 %2, i32* %8
store i32 %3, i32* %9
store double %4, double* %10
store i32 0, i32* %11
br label %13
13: ; preds = %53, %5
%14 = load i32, i32* %11
%15 = load i32, i32* %8
%16 = icmp slt i32 %14, %15
br i1 %16, label %17, label %56
17: ; preds = %13
store i32 0, i32* %12
br label %18
18: ; preds = %49, %17
%19 = load i32, i32* %12
%20 = load i32, i32* %9
%21 = icmp slt i32 %19, %20
br i1 %21, label %22, label %52
22: ; preds = %18
%23 = call double (...) @drand48()
%24 = load double, double* %10
%25 = fcmp olt double %23, %24
%26 = zext i1 %25 to i64
%27 = select i1 %25, i32 1, i32 0
%28 = load i32**, i32*** %6
%29 = load i32, i32* %11
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds i32*, i32** %28, i64 %30
%32 = load i32*, i32** %31
%33 = load i32, i32* %12
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds i32, i32* %32, i64 %34
store i32 %27, i32* %35
%36 = call double (...) @drand48()
%37 = load double, double* %10
%38 = fcmp olt double %36, %37
%39 = zext i1 %38 to i64
%40 = select i1 %38, i32 1, i32 0
%41 = load i32**, i32*** %7
%42 = load i32, i32* %11
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds i32*, i32** %41, i64 %43
%45 = load i32*, i32** %44
%46 = load i32, i32* %12
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds i32, i32* %45, i64 %47
store i32 %40, i32* %48
br label %49
49: ; preds = %22
%50 = load i32, i32* %12
%51 = add nsw i32 %50, 1
store i32 %51, i32* %12
br label %18
52: ; preds = %18
br label %53
53: ; preds = %52
%54 = load i32, i32* %11
%55 = add nsw i32 %54, 1
store i32 %55, i32* %11
br label %13
56: ; preds = %13
ret void
}

LLVM IR (-O2):
define void @populate_bonds(i32** nocapture %0, i32** nocapture %1, i32 %2, i32 %3, double %4) {
%6 = icmp sgt i32 %2, 0
br i1 %6, label %7, label %33
7: ; preds = %5
%8 = icmp sgt i32 %3, 0
%9 = zext i32 %2 to i64
%10 = zext i32 %3 to i64
br label %11
11: ; preds = %7, %30
%12 = phi i64 [ 0, %7 ], [ %31, %30 ]
br i1 %8, label %13, label %30
13: ; preds = %11
%14 = getelementptr inbounds i32*, i32** %0, i64 %12
%15 = getelementptr inbounds i32*, i32** %1, i64 %12
br label %16
16: ; preds = %13, %16
%17 = phi i64 [ 0, %13 ], [ %28, %16 ]
%18 = tail call double (...) @drand48()
%19 = fcmp olt double %18, %4
%20 = zext i1 %19 to i32
%21 = load i32*, i32** %14
%22 = getelementptr inbounds i32, i32* %21, i64 %17
store i32 %20, i32* %22
%23 = tail call double (...) @drand48()
%24 = fcmp olt double %23, %4
%25 = zext i1 %24 to i32
%26 = load i32*, i32** %15
%27 = getelementptr inbounds i32, i32* %26, i64 %17
store i32 %25, i32* %27
%28 = add nuw nsw i64 %17, 1
%29 = icmp eq i64 %28, %10
br i1 %29, label %30, label %16
30: ; preds = %16, %11
%31 = add nuw nsw i64 %12, 1
%32 = icmp eq i64 %31, %9
br i1 %32, label %33, label %11
33: ; preds = %30, %5
ret void
}