#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 */
struct random {
  int dummy;
};

/* Variables and functions */
int random_int32(struct random *);
int random_xd10(struct random *r, int nr_die) {
  if (r == NULL)
    return -1;
  int total = 0;
  for (int i = 0; i < nr_die; i++) {
    total += (random_int32(r) % 10) + 1;
  }
  return total;
}


x86 assembly (clang, -O0):
<random_xd10>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1136 <random_xd10+0x26>
movl   $0xffffffff,-0x4(%rbp)
jmp    1180 <random_xd10+0x70>
movl   $0x0,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    117a <random_xd10+0x6a>
mov    -0x10(%rbp),%rdi
call   1030 <random_int32@plt>
mov    $0xa,%ecx
cltd
idiv   %ecx
mov    %edx,%eax
add    $0x1,%eax
add    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1144 <random_xd10+0x34>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<random_xd10>:
push   %rbp
push   %r14
push   %rbx
test   %rdi,%rdi
je     1161 <random_xd10+0x51>
mov    %esi,%ebp
test   %esi,%esi
jle    1168 <random_xd10+0x58>
mov    %rdi,%r14
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %r14,%rdi
call   1030 <random_int32@plt>
cltq
imul   $0x66666667,%rax,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x22,%rcx
add    %edx,%ecx
add    %ecx,%ecx
lea    (%rcx,%rcx,4),%ecx
sub    %ecx,%eax
add    %eax,%ebx
add    $0x1,%ebx
add    $0xffffffff,%ebp
jne    1130 <random_xd10+0x20>
jmp    116a <random_xd10+0x5a>
mov    $0xffffffff,%ebx
jmp    116a <random_xd10+0x5a>
xor    %ebx,%ebx
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.random = type { i32 }
define i32 @random_xd10(%struct.random* %0, i32 %1) {
%3 = alloca i32
%4 = alloca %struct.random*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store %struct.random* %0, %struct.random** %4
store i32 %1, i32* %5
%8 = load %struct.random*, %struct.random** %4
%9 = icmp eq %struct.random* %8, null
br i1 %9, label %10, label %11
10: ; preds = %2
store i32 -1, i32* %3
br label %28
11: ; preds = %2
store i32 0, i32* %6
store i32 0, i32* %7
br label %12
12: ; preds = %23, %11
%13 = load i32, i32* %7
%14 = load i32, i32* %5
%15 = icmp slt i32 %13, %14
br i1 %15, label %16, label %26
16: ; preds = %12
%17 = load %struct.random*, %struct.random** %4
%18 = call i32 @random_int32(%struct.random* %17)
%19 = srem i32 %18, 10
%20 = add nsw i32 %19, 1
%21 = load i32, i32* %6
%22 = add nsw i32 %21, %20
store i32 %22, i32* %6
br label %23
23: ; preds = %16
%24 = load i32, i32* %7
%25 = add nsw i32 %24, 1
store i32 %25, i32* %7
br label %12
26: ; preds = %12
%27 = load i32, i32* %6
store i32 %27, i32* %3
br label %28
28: ; preds = %26, %10
%29 = load i32, i32* %3
ret i32 %29
}

LLVM IR (-O2):
%struct.random = type { i32 }
define i32 @random_xd10(%struct.random* %0, i32 %1) {
%3 = icmp eq %struct.random* %0, null
br i1 %3, label %15, label %4
4: ; preds = %2
%5 = icmp sgt i32 %1, 0
br i1 %5, label %6, label %15
6: ; preds = %4, %6
%7 = phi i32 [ %13, %6 ], [ 0, %4 ]
%8 = phi i32 [ %12, %6 ], [ 0, %4 ]
%9 = tail call i32 @random_int32(%struct.random* %0)
%10 = srem i32 %9, 10
%11 = add i32 %8, 1
%12 = add i32 %11, %10
%13 = add nuw nsw i32 %7, 1
%14 = icmp eq i32 %13, %1
br i1 %14, label %15, label %6
15: ; preds = %6, %4, %2
%16 = phi i32 [ -1, %2 ], [ 0, %4 ], [ %12, %6 ]
ret i32 %16
}