#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 rand();
int battle_calc_drain_per(int damage, int rate, int per) {
  int diff = 0;
  if (damage <= 0 || rate <= 0)
    return 0;
  if (per && (rand() ^ (rand() >> 8)) % 100 < rate) {
    diff = damage * per / 100;
    if (diff == 0)
      diff = (per > 0) ? 1 : -1;
  }
  return diff;
}


x86 assembly (clang, -O0):
<battle_calc_drain_per>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
cmpl   $0x0,-0x8(%rbp)
jle    113c <battle_calc_drain_per+0x2c>
cmpl   $0x0,-0xc(%rbp)
jg     1148 <battle_calc_drain_per+0x38>
movl   $0x0,-0x4(%rbp)
jmp    11bb <battle_calc_drain_per+0xab>
cmpl   $0x0,-0x10(%rbp)
je     11b5 <battle_calc_drain_per+0xa5>
mov    $0x0,%al
call   1030 <rand@plt>
mov    %eax,-0x18(%rbp)
mov    $0x0,%al
call   1030 <rand@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%eax
sar    $0x8,%ecx
xor    %ecx,%eax
mov    $0x64,%ecx
cltd
idiv   %ecx
cmp    -0xc(%rbp),%edx
jge    11b5 <battle_calc_drain_per+0xa5>
mov    -0x8(%rbp),%eax
imul   -0x10(%rbp),%eax
mov    $0x64,%ecx
cltd
idiv   %ecx
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jne    11b0 <battle_calc_drain_per+0xa0>
mov    -0x10(%rbp),%edx
mov    $0xffffffff,%eax
mov    $0x1,%ecx
cmp    $0x0,%edx
cmovg  %ecx,%eax
mov    %eax,-0x14(%rbp)
jmp    11b5 <battle_calc_drain_per+0xa5>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<battle_calc_drain_per>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
xor    %ebx,%ebx
test   %edi,%edi
jle    119c <battle_calc_drain_per+0x8c>
mov    %esi,%r15d
test   %esi,%esi
jle    119c <battle_calc_drain_per+0x8c>
mov    %edx,%r14d
test   %edx,%edx
je     119c <battle_calc_drain_per+0x8c>
mov    %edi,%ebp
xor    %ebx,%ebx
xor    %eax,%eax
call   1030 <rand@plt>
mov    %eax,%r12d
xor    %eax,%eax
call   1030 <rand@plt>
sar    $0x8,%eax
xor    %r12d,%eax
cltq
imul   $0x51eb851f,%rax,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x25,%rcx
add    %edx,%ecx
imul   $0x64,%ecx,%ecx
sub    %ecx,%eax
cmp    %r15d,%eax
jge    119c <battle_calc_drain_per+0x8c>
imul   %r14d,%ebp
lea    0x63(%rbp),%eax
cmp    $0xc6,%eax
ja     1185 <battle_calc_drain_per+0x75>
xor    %eax,%eax
test   %r14d,%r14d
setg   %al
lea    (%rax,%rax,1),%ebx
add    $0xffffffff,%ebx
jmp    119c <battle_calc_drain_per+0x8c>
movslq %ebp,%rax
imul   $0x51eb851f,%rax,%rbx
mov    %rbx,%rax
shr    $0x3f,%rax
sar    $0x25,%rbx
add    %eax,%ebx
mov    %ebx,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @battle_calc_drain_per(i32 %0, i32 %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i32 0, i32* %8
%9 = load i32, i32* %5
%10 = icmp sle i32 %9, 0
br i1 %10, label %14, label %11
11: ; preds = %3
%12 = load i32, i32* %6
%13 = icmp sle i32 %12, 0
br i1 %13, label %14, label %15
14: ; preds = %11, %3
store i32 0, i32* %4
br label %41
15: ; preds = %11
%16 = load i32, i32* %7
%17 = icmp ne i32 %16, 0
br i1 %17, label %18, label %39
18: ; preds = %15
%19 = call i32 (...) @rand()
%20 = call i32 (...) @rand()
%21 = ashr i32 %20, 8
%22 = xor i32 %19, %21
%23 = srem i32 %22, 100
%24 = load i32, i32* %6
%25 = icmp slt i32 %23, %24
br i1 %25, label %26, label %39
26: ; preds = %18
%27 = load i32, i32* %5
%28 = load i32, i32* %7
%29 = mul nsw i32 %27, %28
%30 = sdiv i32 %29, 100
store i32 %30, i32* %8
%31 = load i32, i32* %8
%32 = icmp eq i32 %31, 0
br i1 %32, label %33, label %38
33: ; preds = %26
%34 = load i32, i32* %7
%35 = icmp sgt i32 %34, 0
%36 = zext i1 %35 to i64
%37 = select i1 %35, i32 1, i32 -1
store i32 %37, i32* %8
br label %38
38: ; preds = %33, %26
br label %39
39: ; preds = %38, %18, %15
%40 = load i32, i32* %8
store i32 %40, i32* %4
br label %41
41: ; preds = %39, %14
%42 = load i32, i32* %4
ret i32 %42
}

LLVM IR (-O2):
define i32 @battle_calc_drain_per(i32 %0, i32 %1, i32 %2) {
%4 = icmp slt i32 %0, 1
%5 = icmp slt i32 %1, 1
%6 = or i1 %4, %5
%7 = icmp eq i32 %2, 0
%8 = or i1 %6, %7
br i1 %8, label %24, label %9
9: ; preds = %3
%10 = tail call i32 (...) @rand()
%11 = tail call i32 (...) @rand()
%12 = ashr i32 %11, 8
%13 = xor i32 %12, %10
%14 = srem i32 %13, 100
%15 = icmp slt i32 %14, %1
br i1 %15, label %16, label %24
16: ; preds = %9
%17 = mul nsw i32 %2, %0
%18 = sdiv i32 %17, 100
%19 = add i32 %17, 99
%20 = icmp ult i32 %19, 199
br i1 %20, label %21, label %24
21: ; preds = %16
%22 = icmp slt i32 %2, 1
%23 = select i1 %22, i32 -1, i32 1
br label %24
24: ; preds = %9, %21, %16, %3
%25 = phi i32 [ 0, %3 ], [ %23, %21 ], [ %18, %16 ], [ 0, %9 ]
ret i32 %25
}