#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 */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int log(int);
double pow(int, int);
int /*<<< orphan*/ stderr;
double mydbern(int x, double p, int l) {
  if (p < 0 || p > 1) {
    fprintf(stderr, "p not in [0, 1]");
    exit(EXIT_FAILURE);
  }
  if (x != 0 && x != 1) {
    if (l == 1) {
      fprintf(stderr, "x => log(0)=nan in Bernoulli");
      exit(EXIT_FAILURE);
    }
    return 0.0;
  }
  if (l == 1) {
    if (p == 0 || p == 1) {
      fprintf(stderr, "p => log(0)=nan in Bernoulli");
      exit(EXIT_FAILURE);
    }
    return x * log(p) + (1 - x) * log(1 - p);
  }
  return pow(p, x) * pow(1 - p, 1 - x);
}


x86 assembly (clang, -O0):
<mydbern>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0xc(%rbp)
movsd  %xmm0,-0x18(%rbp)
mov    %esi,-0x1c(%rbp)
xorps  %xmm0,%xmm0
ucomisd -0x18(%rbp),%xmm0
ja     1178 <mydbern+0x38>
movsd  -0x18(%rbp),%xmm0
movsd  0xe92(%rip),%xmm1
ucomisd %xmm1,%xmm0
jbe    119b <mydbern+0x5b>
mov    0x2e61(%rip),%rax
mov    (%rax),%edi
lea    0xe80(%rip),%rsi
call   1030 <fprintf@plt>
mov    0x2e44(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
cmpl   $0x0,-0xc(%rbp)
je     11e9 <mydbern+0xa9>
cmpl   $0x1,-0xc(%rbp)
je     11e9 <mydbern+0xa9>
cmpl   $0x1,-0x1c(%rbp)
jne    11dc <mydbern+0x9c>
mov    0x2e20(%rip),%rax
mov    (%rax),%edi
lea    0xe4f(%rip),%rsi
call   1030 <fprintf@plt>
mov    0x2e03(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    12e8 <mydbern+0x1a8>
cmpl   $0x1,-0x1c(%rbp)
jne    12a7 <mydbern+0x167>
movsd  -0x18(%rbp),%xmm0
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jne    1210 <mydbern+0xd0>
jp     1210 <mydbern+0xd0>
jmp    122d <mydbern+0xed>
movsd  -0x18(%rbp),%xmm0
movsd  0xde3(%rip),%xmm1
ucomisd %xmm1,%xmm0
jne    1250 <mydbern+0x110>
jp     1250 <mydbern+0x110>
mov    0x2dac(%rip),%rax
mov    (%rax),%edi
lea    0xdf8(%rip),%rsi
call   1030 <fprintf@plt>
mov    0x2d8f(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
mov    -0xc(%rbp),%eax
mov    %eax,-0x28(%rbp)
cvttsd2si -0x18(%rbp),%edi
call   1040 <log@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%eax
imul   %ecx,%eax
mov    %eax,-0x20(%rbp)
mov    $0x1,%eax
sub    -0xc(%rbp),%eax
mov    %eax,-0x24(%rbp)
movsd  0xd82(%rip),%xmm0
subsd  -0x18(%rbp),%xmm0
cvttsd2si %xmm0,%edi
call   1040 <log@plt>
mov    -0x24(%rbp),%ecx
mov    %eax,%edx
mov    -0x20(%rbp),%eax
imul   %edx,%ecx
add    %ecx,%eax
cvtsi2sd %eax,%xmm0
movsd  %xmm0,-0x8(%rbp)
jmp    12e8 <mydbern+0x1a8>
cvttsd2si -0x18(%rbp),%edi
mov    -0xc(%rbp),%esi
call   1050 <pow@plt>
movsd  %xmm0,-0x30(%rbp)
movsd  0xd3f(%rip),%xmm0
subsd  -0x18(%rbp),%xmm0
cvttsd2si %xmm0,%edi
mov    $0x1,%esi
sub    -0xc(%rbp),%esi
call   1050 <pow@plt>
movaps %xmm0,%xmm1
movsd  -0x30(%rbp),%xmm0
mulsd  %xmm1,%xmm0
movsd  %xmm0,-0x8(%rbp)
movsd  -0x8(%rbp),%xmm0
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mydbern>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
xorpd  %xmm1,%xmm1
ucomisd %xmm0,%xmm1
ja     1229 <mydbern+0xe9>
ucomisd 0xea2(%rip),%xmm0
ja     1229 <mydbern+0xe9>
mov    %edi,%ebx
cmp    $0x2,%edi
jae    11d1 <mydbern+0x91>
cmp    $0x1,%esi
movsd  %xmm0,(%rsp)
jne    11ec <mydbern+0xac>
xorpd  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jne    1185 <mydbern+0x45>
jnp    123b <mydbern+0xfb>
ucomisd 0xe73(%rip),%xmm0
jne    1195 <mydbern+0x55>
jnp    123b <mydbern+0xfb>
cvttsd2si %xmm0,%edi
call   1040 <log@plt>
mov    %eax,%r14d
imul   %ebx,%r14d
mov    $0x1,%ebp
sub    %ebx,%ebp
movsd  0xe4c(%rip),%xmm0
subsd  (%rsp),%xmm0
cvttsd2si %xmm0,%edi
call   1040 <log@plt>
imul   %ebp,%eax
add    %r14d,%eax
xorps  %xmm0,%xmm0
cvtsi2sd %eax,%xmm0
jmp    1220 <mydbern+0xe0>
xorpd  %xmm0,%xmm0
cmp    $0x1,%esi
jne    1220 <mydbern+0xe0>
mov    0x2dff(%rip),%rax
mov    (%rax),%edi
lea    0xe2e(%rip),%rsi
jmp    124b <mydbern+0x10b>
cvttsd2si %xmm0,%edi
mov    %ebx,%esi
call   1050 <pow@plt>
movsd  %xmm0,0x8(%rsp)
movsd  0xdfb(%rip),%xmm0
subsd  (%rsp),%xmm0
cvttsd2si %xmm0,%edi
mov    $0x1,%esi
sub    %ebx,%esi
call   1050 <pow@plt>
mulsd  0x8(%rsp),%xmm0
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2db0(%rip),%rax
mov    (%rax),%edi
lea    0xdcf(%rip),%rsi
jmp    124b <mydbern+0x10b>
mov    0x2d9e(%rip),%rax
mov    (%rax),%edi
lea    0xdea(%rip),%rsi
call   1030 <fprintf@plt>
mov    0x2d81(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [16 x i8] c"p not in [0, 1]\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [29 x i8] c"x => log(0)=nan in Bernoulli\00"
@.str.2 = [29 x i8] c"p => log(0)=nan in Bernoulli\00"
define double @mydbern(i32 %0, double %1, i32 %2) {
%4 = alloca double
%5 = alloca i32
%6 = alloca double
%7 = alloca i32
store i32 %0, i32* %5
store double %1, double* %6
store i32 %2, i32* %7
%8 = load double, double* %6
%9 = fcmp olt double %8, 0.000000e+00
br i1 %9, label %13, label %10
10: ; preds = %3
%11 = load double, double* %6
%12 = fcmp ogt double %11, 1.000000e+00
br i1 %12, label %13, label %18
13: ; preds = %10, %3
%14 = load i32, i32* @stderr
%15 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%16 = load i32, i32* @EXIT_FAILURE
%17 = call i32 @exit(i32 %16)
unreachable
18: ; preds = %10
%19 = load i32, i32* %5
%20 = icmp ne i32 %19, 0
br i1 %20, label %21, label %33
21: ; preds = %18
%22 = load i32, i32* %5
%23 = icmp ne i32 %22, 1
br i1 %23, label %24, label %33
24: ; preds = %21
%25 = load i32, i32* %7
%26 = icmp eq i32 %25, 1
br i1 %26, label %27, label %32
27: ; preds = %24
%28 = load i32, i32* @stderr
%29 = call i32 @fprintf(i32 %28, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
%30 = load i32, i32* @EXIT_FAILURE
%31 = call i32 @exit(i32 %30)
unreachable
32: ; preds = %24
store double 0.000000e+00, double* %4
br label %74
33: ; preds = %21, %18
%34 = load i32, i32* %7
%35 = icmp eq i32 %34, 1
br i1 %35, label %36, label %62
36: ; preds = %33
%37 = load double, double* %6
%38 = fcmp oeq double %37, 0.000000e+00
br i1 %38, label %42, label %39
39: ; preds = %36
%40 = load double, double* %6
%41 = fcmp oeq double %40, 1.000000e+00
br i1 %41, label %42, label %47
42: ; preds = %39, %36
%43 = load i32, i32* @stderr
%44 = call i32 @fprintf(i32 %43, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0))
%45 = load i32, i32* @EXIT_FAILURE
%46 = call i32 @exit(i32 %45)
unreachable
47: ; preds = %39
%48 = load i32, i32* %5
%49 = load double, double* %6
%50 = fptosi double %49 to i32
%51 = call i32 @log(i32 %50)
%52 = mul nsw i32 %48, %51
%53 = load i32, i32* %5
%54 = sub nsw i32 1, %53
%55 = load double, double* %6
%56 = fsub double 1.000000e+00, %55
%57 = fptosi double %56 to i32
%58 = call i32 @log(i32 %57)
%59 = mul nsw i32 %54, %58
%60 = add nsw i32 %52, %59
%61 = sitofp i32 %60 to double
store double %61, double* %4
br label %74
62: ; preds = %33
%63 = load double, double* %6
%64 = fptosi double %63 to i32
%65 = load i32, i32* %5
%66 = call double @pow(i32 %64, i32 %65)
%67 = load double, double* %6
%68 = fsub double 1.000000e+00, %67
%69 = fptosi double %68 to i32
%70 = load i32, i32* %5
%71 = sub nsw i32 1, %70
%72 = call double @pow(i32 %69, i32 %71)
%73 = fmul double %66, %72
store double %73, double* %4
br label %74
74: ; preds = %62, %47, %32
%75 = load double, double* %4
ret double %75
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [16 x i8] c"p not in [0, 1]\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [29 x i8] c"x => log(0)=nan in Bernoulli\00"
@.str.2 = [29 x i8] c"p => log(0)=nan in Bernoulli\00"
define double @mydbern(i32 %0, double %1, i32 %2) {
%4 = fcmp olt double %1, 0.000000e+00
%5 = fcmp ogt double %1, 1.000000e+00
%6 = or i1 %4, %5
br i1 %6, label %7, label %12
7: ; preds = %3
%8 = load i32, i32* @stderr
%9 = tail call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%10 = load i32, i32* @EXIT_FAILURE
%11 = tail call i32 @exit(i32 %10)
unreachable
12: ; preds = %3
%13 = icmp ult i32 %0, 2
%14 = icmp eq i32 %2, 1
br i1 %13, label %21, label %15
15: ; preds = %12
br i1 %14, label %16, label %50
16: ; preds = %15
%17 = load i32, i32* @stderr
%18 = tail call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
%19 = load i32, i32* @EXIT_FAILURE
%20 = tail call i32 @exit(i32 %19)
unreachable
21: ; preds = %12
br i1 %14, label %22, label %42
22: ; preds = %21
%23 = fcmp oeq double %1, 0.000000e+00
%24 = fcmp oeq double %1, 1.000000e+00
%25 = or i1 %23, %24
br i1 %25, label %26, label %31
26: ; preds = %22
%27 = load i32, i32* @stderr
%28 = tail call i32 @fprintf(i32 %27, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0))
%29 = load i32, i32* @EXIT_FAILURE
%30 = tail call i32 @exit(i32 %29)
unreachable
31: ; preds = %22
%32 = fptosi double %1 to i32
%33 = tail call i32 @log(i32 %32)
%34 = mul nuw nsw i32 %33, %0
%35 = sub nuw nsw i32 1, %0
%36 = fsub double 1.000000e+00, %1
%37 = fptosi double %36 to i32
%38 = tail call i32 @log(i32 %37)
%39 = mul nuw nsw i32 %38, %35
%40 = add nsw i32 %39, %34
%41 = sitofp i32 %40 to double
br label %50
42: ; preds = %21
%43 = fptosi double %1 to i32
%44 = tail call double @pow(i32 %43, i32 %0)
%45 = fsub double 1.000000e+00, %1
%46 = fptosi double %45 to i32
%47 = sub nuw nsw i32 1, %0
%48 = tail call double @pow(i32 %46, i32 %47)
%49 = fmul double %44, %48
br label %50
50: ; preds = %15, %42, %31
%51 = phi double [ %41, %31 ], [ %49, %42 ], [ 0.000000e+00, %15 ]
ret double %51
}