#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*/ data_addresource(int, int, int);
scalar_t__ data_getresource(int, int);
int data_totresources(int);
int rand();
int robber_steal(int player) {
  int k = rand() % data_totresources(player);
  int i;
  for (i = 0; k >= 0; i++)
    k -= data_getresource(player, i);
  data_addresource(player, --i, -1);
  return i;
}


x86 assembly (clang, -O0):
<robber_steal>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    $0x0,%al
call   1060 <rand@plt>
mov    %eax,-0x10(%rbp)
mov    -0x4(%rbp),%edi
call   1050 <data_totresources@plt>
mov    %eax,%ecx
mov    -0x10(%rbp),%eax
cltd
idiv   %ecx
mov    %edx,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
cmpl   $0x0,-0x8(%rbp)
jl     119f <robber_steal+0x5f>
mov    -0x4(%rbp),%edi
mov    -0xc(%rbp),%esi
call   1040 <data_getresource@plt>
mov    %rax,%rcx
movslq -0x8(%rbp),%rax
sub    %rcx,%rax
mov    %eax,-0x8(%rbp)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    116f <robber_steal+0x2f>
mov    -0x4(%rbp),%edi
mov    -0xc(%rbp),%esi
add    $0xffffffff,%esi
mov    %esi,-0xc(%rbp)
mov    $0xffffffff,%edx
call   1030 <data_addresource@plt>
mov    -0xc(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<robber_steal>:
push   %rbp
push   %r14
push   %rbx
mov    %edi,%r14d
xor    %eax,%eax
call   1060 <rand@plt>
mov    %eax,%ebx
mov    %r14d,%edi
call   1050 <data_totresources@plt>
mov    %eax,%ecx
mov    %ebx,%eax
cltd
idiv   %ecx
test   %edx,%edx
js     1183 <robber_steal+0x43>
mov    %edx,%ebx
mov    $0xffffffff,%ebp
nopw   0x0(%rax,%rax,1)
add    $0x1,%ebp
mov    %r14d,%edi
mov    %ebp,%esi
call   1040 <data_getresource@plt>
sub    %eax,%ebx
jns    1170 <robber_steal+0x30>
jmp    1188 <robber_steal+0x48>
mov    $0xffffffff,%ebp
mov    %r14d,%edi
mov    %ebp,%esi
mov    $0xffffffff,%edx
call   1030 <data_addresource@plt>
mov    %ebp,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define i32 @robber_steal(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
store i32 %0, i32* %2
%5 = call i32 (...) @rand()
%6 = load i32, i32* %2
%7 = call i32 @data_totresources(i32 %6)
%8 = srem i32 %5, %7
store i32 %8, i32* %3
store i32 0, i32* %4
br label %9
9: ; preds = %20, %1
%10 = load i32, i32* %3
%11 = icmp sge i32 %10, 0
br i1 %11, label %12, label %23
12: ; preds = %9
%13 = load i32, i32* %2
%14 = load i32, i32* %4
%15 = call i64 @data_getresource(i32 %13, i32 %14)
%16 = load i32, i32* %3
%17 = sext i32 %16 to i64
%18 = sub nsw i64 %17, %15
%19 = trunc i64 %18 to i32
store i32 %19, i32* %3
br label %20
20: ; preds = %12
%21 = load i32, i32* %4
%22 = add nsw i32 %21, 1
store i32 %22, i32* %4
br label %9
23: ; preds = %9
%24 = load i32, i32* %2
%25 = load i32, i32* %4
%26 = add nsw i32 %25, -1
store i32 %26, i32* %4
%27 = call i32 @data_addresource(i32 %24, i32 %26, i32 -1)
%28 = load i32, i32* %4
ret i32 %28
}

LLVM IR (-O2):
define i32 @robber_steal(i32 %0) {
%2 = tail call i32 (...) @rand()
%3 = tail call i32 @data_totresources(i32 %0)
%4 = srem i32 %2, %3
%5 = icmp sgt i32 %4, -1
br i1 %5, label %6, label %14
6: ; preds = %1, %6
%7 = phi i32 [ %12, %6 ], [ 0, %1 ]
%8 = phi i32 [ %11, %6 ], [ %4, %1 ]
%9 = tail call i64 @data_getresource(i32 %0, i32 %7)
%10 = trunc i64 %9 to i32
%11 = sub i32 %8, %10
%12 = add nuw nsw i32 %7, 1
%13 = icmp sgt i32 %11, -1
br i1 %13, label %6, label %14
14: ; preds = %6, %1
%15 = phi i32 [ -1, %1 ], [ %7, %6 ]
%16 = tail call i32 @data_addresource(i32 %0, i32 %15, i32 -1)
ret i32 %15
}