#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 */
typedef int type;

/* Variables and functions */
int /*<<< orphan*/ printf(char *, ...);
int rand();
int roll_dice(char *name, int nb, int type, int bonus) {
  printf("\n%s : Roll %dd%d (", name, nb, type);
  int result = 0;
  for (int i = 0; i < nb; i++) {
    if (i > 0)
      printf("+");
    int randnb = rand() % (type) + 1;
    printf("%d", randnb);
    result += randnb;
  }
  printf(") ");
  if (bonus > 0)
    printf("+ %d ", bonus);
  else if (bonus < 0)
    printf("%d ", bonus);
  result += bonus;
  printf("= %d\n", result);
  return result;
}


x86 assembly (clang, -O0):
<roll_dice>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %ecx,-0x14(%rbp)
mov    -0x8(%rbp),%rsi
mov    -0xc(%rbp),%edx
mov    -0x10(%rbp),%ecx
lea    0xeba(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    11b8 <roll_dice+0x98>
cmpl   $0x0,-0x1c(%rbp)
jle    117f <roll_dice+0x5f>
lea    0xe9b(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x0,%al
call   1040 <rand@plt>
cltd
idivl  -0x10(%rbp)
add    $0x1,%edx
mov    %edx,-0x20(%rbp)
mov    -0x20(%rbp),%esi
lea    0xe7b(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x20(%rbp),%eax
add    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    115b <roll_dice+0x3b>
lea    0xe59(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
cmpl   $0x0,-0x14(%rbp)
jle    11e6 <roll_dice+0xc6>
mov    -0x14(%rbp),%esi
lea    0xe41(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1206 <roll_dice+0xe6>
cmpl   $0x0,-0x14(%rbp)
jge    1201 <roll_dice+0xe1>
mov    -0x14(%rbp),%esi
lea    0xe23(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1206 <roll_dice+0xe6>
mov    -0x14(%rbp),%eax
add    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%esi
lea    0xe08(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x18(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<roll_dice>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %ecx,%r14d
mov    %edx,%r15d
mov    %esi,%ebp
mov    %rdi,%rsi
lea    0xeb3(%rip),%rdi
xor    %r13d,%r13d
mov    %ebp,%edx
mov    %r15d,%ecx
xor    %eax,%eax
call   1040 <printf@plt>
test   %ebp,%ebp
jle    11d0 <roll_dice+0xa0>
xor    %eax,%eax
call   1050 <rand@plt>
cltd
idiv   %r15d
mov    %edx,%r13d
add    $0x1,%r13d
lea    0xe9a(%rip),%rdi
mov    %r13d,%esi
xor    %eax,%eax
call   1040 <printf@plt>
cmp    $0x1,%ebp
je     11d0 <roll_dice+0xa0>
add    $0xffffffff,%ebp
lea    0xe81(%rip),%r12
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    $0x2b,%edi
call   1030 <putchar@plt>
xor    %eax,%eax
call   1050 <rand@plt>
cltd
idiv   %r15d
mov    %edx,%ebx
lea    0x1(%rbx),%esi
mov    %r12,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
add    %ebx,%r13d
add    $0x1,%r13d
add    $0xffffffff,%ebp
jne    11a0 <roll_dice+0x70>
lea    0xe3f(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
test   %r14d,%r14d
jle    11f6 <roll_dice+0xc6>
lea    0xe2f(%rip),%rdi
mov    %r14d,%esi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    11f8 <roll_dice+0xc8>
js     121e <roll_dice+0xee>
add    %r14d,%r13d
lea    0xe1d(%rip),%rdi
mov    %r13d,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %r13d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdf6(%rip),%rdi
jmp    11ea <roll_dice+0xba>

LLVM IR (-O0):
@.str = [19 x i8] c"\0A%s : Roll %dd%d (\00"
@.str.1 = [2 x i8] c"+\00"
@.str.2 = [3 x i8] c"%d\00"
@.str.3 = [3 x i8] c") \00"
@.str.4 = [6 x i8] c"+ %d \00"
@.str.5 = [4 x i8] c"%d \00"
@.str.6 = [6 x i8] c"= %d\0A\00"
define i32 @roll_dice(i8* %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i32 %3, i32* %8
%12 = load i8*, i8** %5
%13 = load i32, i32* %6
%14 = load i32, i32* %7
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* %12, i32 %13, i32 %14)
store i32 0, i32* %9
store i32 0, i32* %10
br label %16
16: ; preds = %35, %4
%17 = load i32, i32* %10
%18 = load i32, i32* %6
%19 = icmp slt i32 %17, %18
br i1 %19, label %20, label %38
20: ; preds = %16
%21 = load i32, i32* %10
%22 = icmp sgt i32 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %20
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
br label %25
25: ; preds = %23, %20
%26 = call i32 (...) @rand()
%27 = load i32, i32* %7
%28 = srem i32 %26, %27
%29 = add nsw i32 %28, 1
store i32 %29, i32* %11
%30 = load i32, i32* %11
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 %30)
%32 = load i32, i32* %11
%33 = load i32, i32* %9
%34 = add nsw i32 %33, %32
store i32 %34, i32* %9
br label %35
35: ; preds = %25
%36 = load i32, i32* %10
%37 = add nsw i32 %36, 1
store i32 %37, i32* %10
br label %16
38: ; preds = %16
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%40 = load i32, i32* %8
%41 = icmp sgt i32 %40, 0
br i1 %41, label %42, label %45
42: ; preds = %38
%43 = load i32, i32* %8
%44 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i32 %43)
br label %52
45: ; preds = %38
%46 = load i32, i32* %8
%47 = icmp slt i32 %46, 0
br i1 %47, label %48, label %51
48: ; preds = %45
%49 = load i32, i32* %8
%50 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i32 %49)
br label %51
51: ; preds = %48, %45
br label %52
52: ; preds = %51, %42
%53 = load i32, i32* %8
%54 = load i32, i32* %9
%55 = add nsw i32 %54, %53
store i32 %55, i32* %9
%56 = load i32, i32* %9
%57 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i64 0, i64 0), i32 %56)
%58 = load i32, i32* %9
ret i32 %58
}

LLVM IR (-O2):
@.str = [19 x i8] c"\0A%s : Roll %dd%d (\00"
@.str.2 = [3 x i8] c"%d\00"
@.str.3 = [3 x i8] c") \00"
@.str.4 = [6 x i8] c"+ %d \00"
@.str.5 = [4 x i8] c"%d \00"
@.str.6 = [6 x i8] c"= %d\0A\00"
define i32 @roll_dice(i8* %0, i32 %1, i32 %2, i32 %3) {
%5 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* %0, i32 %1, i32 %2)
%6 = icmp sgt i32 %1, 0
br i1 %6, label %7, label %13
7: ; preds = %4
%8 = tail call i32 (...) @rand()
%9 = srem i32 %8, %2
%10 = add nsw i32 %9, 1
%11 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 %10)
%12 = icmp eq i32 %1, 1
br i1 %12, label %13, label %17
13: ; preds = %17, %7, %4
%14 = phi i32 [ 0, %4 ], [ %10, %7 ], [ %25, %17 ]
%15 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%16 = icmp sgt i32 %3, 0
br i1 %16, label %30, label %28
17: ; preds = %7, %17
%18 = phi i32 [ %25, %17 ], [ %10, %7 ]
%19 = phi i32 [ %26, %17 ], [ 1, %7 ]
%20 = tail call i32 @putchar(i32 43)
%21 = tail call i32 (...) @rand()
%22 = srem i32 %21, %2
%23 = add nsw i32 %22, 1
%24 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 %23)
%25 = add nsw i32 %23, %18
%26 = add nuw nsw i32 %19, 1
%27 = icmp eq i32 %26, %1
br i1 %27, label %13, label %17
28: ; preds = %13
%29 = icmp slt i32 %3, 0
br i1 %29, label %30, label %33
30: ; preds = %28, %13
%31 = phi i8* [ getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), %13 ], [ getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), %28 ]
%32 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) %31, i32 %3)
br label %33
33: ; preds = %30, %28
%34 = add nsw i32 %14, %3
%35 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i64 0, i64 0), i32 %34)
ret i32 %34
}