#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 */
unsigned char *calloc(int, int);
int random();
unsigned char *bruiteNB(unsigned char *img, int xs, int ys, double proportion,
                        int *nc, int *nl) {
  unsigned char *res = NULL;
  int i, adr;
  res = calloc(xs * ys, 1);
  for (i = 0; i < xs * ys * proportion; i = i + 1) {
    adr = random() % (xs * ys);
    if (res[adr] < 128)
      res[adr] = 255;
    else
      res[adr] = 0;
  }
  *nc = xs;
  *nl = ys;
  return res;
}


x86 assembly (clang, -O0):
<bruiteNB>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
movsd  %xmm0,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0xc(%rbp),%edi
imul   -0x10(%rbp),%edi
mov    $0x1,%esi
call   1030 <calloc@plt>
mov    %rax,-0x30(%rbp)
movl   $0x0,-0x34(%rbp)
cvtsi2sdl -0x34(%rbp),%xmm1
mov    -0xc(%rbp),%eax
imul   -0x10(%rbp),%eax
cvtsi2sd %eax,%xmm0
mulsd  -0x18(%rbp),%xmm0
ucomisd %xmm1,%xmm0
jbe    11dd <bruiteNB+0xbd>
mov    $0x0,%al
call   1040 <random@plt>
mov    -0xc(%rbp),%ecx
imul   -0x10(%rbp),%ecx
cltd
idiv   %ecx
mov    %edx,-0x38(%rbp)
mov    -0x30(%rbp),%rax
movslq -0x38(%rbp),%rcx
movzbl (%rax,%rcx,1),%eax
cmp    $0x80,%eax
jge    11be <bruiteNB+0x9e>
mov    -0x30(%rbp),%rax
movslq -0x38(%rbp),%rcx
movb   $0xff,(%rax,%rcx,1)
jmp    11ca <bruiteNB+0xaa>
mov    -0x30(%rbp),%rax
movslq -0x38(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    11cf <bruiteNB+0xaf>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    1163 <bruiteNB+0x43>
mov    -0xc(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x10(%rbp),%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x30(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bruiteNB>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %r8,0x10(%rsp)
mov    %rcx,%r15
movsd  %xmm0,0x8(%rsp)
mov    %edx,%r12d
mov    %esi,%r13d
mov    %edx,%ebp
imul   %esi,%ebp
mov    $0x1,%r14d
mov    %ebp,%edi
mov    $0x1,%esi
call   1030 <calloc@plt>
mov    %rax,%rbx
cvtsi2sd %ebp,%xmm1
mulsd  0x8(%rsp),%xmm1
xorpd  %xmm0,%xmm0
ucomisd %xmm0,%xmm1
jbe    11b1 <bruiteNB+0x91>
movsd  %xmm1,0x8(%rsp)
cs nopw 0x0(%rax,%rax,1)
xor    %eax,%eax
call   1040 <random@plt>
movsd  0x8(%rsp),%xmm1
cltd
idiv   %ebp
movslq %edx,%rax
movzbl (%rbx,%rax,1),%ecx
sar    $0x7,%cl
not    %cl
xorps  %xmm0,%xmm0
cvtsi2sd %r14d,%xmm0
mov    %cl,(%rbx,%rax,1)
add    $0x1,%r14d
ucomisd %xmm0,%xmm1
ja     1180 <bruiteNB+0x60>
mov    %r13d,(%r15)
mov    0x10(%rsp),%rax
mov    %r12d,(%rax)
mov    %rbx,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define noalias i8* @bruiteNB(i8* nocapture readnone %0, i32 %1, i32 %2, double %3, i32* nocapture writeonly %4, i32* nocapture writeonly %5) {
%7 = mul nsw i32 %2, %1
%8 = tail call i8* @calloc(i32 %7, i32 1)
%9 = sitofp i32 %7 to double
%10 = fmul double %9, %3
%11 = fcmp ogt double %10, 0.000000e+00
br i1 %11, label %12, label %24
12: ; preds = %6, %12
%13 = phi i32 [ %21, %12 ], [ 0, %6 ]
%14 = tail call i32 (...) @random()
%15 = srem i32 %14, %7
%16 = sext i32 %15 to i64
%17 = getelementptr inbounds i8, i8* %8, i64 %16
%18 = load i8, i8* %17
%19 = ashr i8 %18, 7
%20 = xor i8 %19, -1
store i8 %20, i8* %17
%21 = add nuw nsw i32 %13, 1
%22 = sitofp i32 %21 to double
%23 = fcmp ogt double %10, %22
br i1 %23, label %12, label %24
24: ; preds = %12, %6
store i32 %1, i32* %4
store i32 %2, i32* %5
ret i8* %8
}