#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(int damage, int rate, int per, int val) {
  int diff = 0;
  if (damage <= 0 || rate <= 0)
    return 0;
  if (per && rand() % 100 < rate) {
    diff = (damage * per) / 100;
    if (diff == 0) {
      if (per > 0)
        diff = 1;
      else
        diff = -1;
    }
  }
  if (val && rand() % 100 < rate) {
    diff += val;
  }
  return diff;
}


x86 assembly (clang, -O0):
<battle_calc_drain>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %ecx,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
cmpl   $0x0,-0x8(%rbp)
jle    113f <battle_calc_drain+0x2f>
cmpl   $0x0,-0xc(%rbp)
jg     114b <battle_calc_drain+0x3b>
movl   $0x0,-0x4(%rbp)
jmp    11e1 <battle_calc_drain+0xd1>
cmpl   $0x0,-0x10(%rbp)
je     11b0 <battle_calc_drain+0xa0>
mov    $0x0,%al
call   1030 <rand@plt>
mov    $0x64,%ecx
cltd
idiv   %ecx
cmp    -0xc(%rbp),%edx
jge    11b0 <battle_calc_drain+0xa0>
mov    -0x8(%rbp),%eax
imul   -0x10(%rbp),%eax
mov    $0x64,%ecx
cltd
idiv   %ecx
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jne    11ab <battle_calc_drain+0x9b>
cmpl   $0x0,-0x10(%rbp)
jle    119f <battle_calc_drain+0x8f>
movl   $0x1,-0x18(%rbp)
jmp    11a6 <battle_calc_drain+0x96>
movl   $0xffffffff,-0x18(%rbp)
jmp    11ab <battle_calc_drain+0x9b>
jmp    11b0 <battle_calc_drain+0xa0>
cmpl   $0x0,-0x14(%rbp)
je     11db <battle_calc_drain+0xcb>
mov    $0x0,%al
call   1030 <rand@plt>
mov    $0x64,%ecx
cltd
idiv   %ecx
cmp    -0xc(%rbp),%edx
jge    11db <battle_calc_drain+0xcb>
mov    -0x14(%rbp),%eax
add    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<battle_calc_drain>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
xor    %ebp,%ebp
test   %edi,%edi
jle    11bd <battle_calc_drain+0xad>
mov    %esi,%r14d
test   %esi,%esi
jle    11bd <battle_calc_drain+0xad>
mov    %ecx,%r15d
mov    %edx,%r12d
test   %edx,%edx
je     1185 <battle_calc_drain+0x75>
mov    %edi,%ebx
xor    %ebp,%ebp
xor    %eax,%eax
call   1030 <rand@plt>
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    %r14d,%eax
jge    1187 <battle_calc_drain+0x77>
imul   %r12d,%ebx
lea    0x63(%rbx),%eax
cmp    $0xc6,%eax
ja     11c8 <battle_calc_drain+0xb8>
xor    %eax,%eax
test   %r12d,%r12d
setg   %al
lea    (%rax,%rax,1),%ebp
add    $0xffffffff,%ebp
test   %r15d,%r15d
jne    118c <battle_calc_drain+0x7c>
jmp    11bd <battle_calc_drain+0xad>
xor    %ebp,%ebp
test   %r15d,%r15d
je     11bd <battle_calc_drain+0xad>
xor    %ebx,%ebx
xor    %eax,%eax
call   1030 <rand@plt>
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    %r14d,%eax
cmovge %ebx,%r15d
add    %ebp,%r15d
mov    %r15d,%ebp
mov    %ebp,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
movslq %ebx,%rax
imul   $0x51eb851f,%rax,%rbp
mov    %rbp,%rax
shr    $0x3f,%rax
sar    $0x25,%rbp
add    %eax,%ebp
test   %r15d,%r15d
jne    118c <battle_calc_drain+0x7c>
jmp    11bd <battle_calc_drain+0xad>

LLVM IR (-O0):
define i32 @battle_calc_drain(i32 %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i32 %0, i32* %6
store i32 %1, i32* %7
store i32 %2, i32* %8
store i32 %3, i32* %9
store i32 0, i32* %10
%11 = load i32, i32* %6
%12 = icmp sle i32 %11, 0
br i1 %12, label %16, label %13
13: ; preds = %4
%14 = load i32, i32* %7
%15 = icmp sle i32 %14, 0
br i1 %15, label %16, label %17
16: ; preds = %13, %4
store i32 0, i32* %5
br label %53
17: ; preds = %13
%18 = load i32, i32* %8
%19 = icmp ne i32 %18, 0
br i1 %19, label %20, label %39
20: ; preds = %17
%21 = call i32 (...) @rand()
%22 = srem i32 %21, 100
%23 = load i32, i32* %7
%24 = icmp slt i32 %22, %23
br i1 %24, label %25, label %39
25: ; preds = %20
%26 = load i32, i32* %6
%27 = load i32, i32* %8
%28 = mul nsw i32 %26, %27
%29 = sdiv i32 %28, 100
store i32 %29, i32* %10
%30 = load i32, i32* %10
%31 = icmp eq i32 %30, 0
br i1 %31, label %32, label %38
32: ; preds = %25
%33 = load i32, i32* %8
%34 = icmp sgt i32 %33, 0
br i1 %34, label %35, label %36
35: ; preds = %32
store i32 1, i32* %10
br label %37
36: ; preds = %32
store i32 -1, i32* %10
br label %37
37: ; preds = %36, %35
br label %38
38: ; preds = %37, %25
br label %39
39: ; preds = %38, %20, %17
%40 = load i32, i32* %9
%41 = icmp ne i32 %40, 0
br i1 %41, label %42, label %51
42: ; preds = %39
%43 = call i32 (...) @rand()
%44 = srem i32 %43, 100
%45 = load i32, i32* %7
%46 = icmp slt i32 %44, %45
br i1 %46, label %47, label %51
47: ; preds = %42
%48 = load i32, i32* %9
%49 = load i32, i32* %10
%50 = add nsw i32 %49, %48
store i32 %50, i32* %10
br label %51
51: ; preds = %47, %42, %39
%52 = load i32, i32* %10
store i32 %52, i32* %5
br label %53
53: ; preds = %51, %16
%54 = load i32, i32* %5
ret i32 %54
}

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