#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 atoi(char const *);
int /*<<< orphan*/ fork();
int /*<<< orphan*/ getpid();
int /*<<< orphan*/ pause();
int rand();
int /*<<< orphan*/ setpgid(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ srand(int /*<<< orphan*/);
int main(int argc, const char **argv) {
  if (argc < 2)
    return 0;
  int credits = atoi(argv[1]), n;
startover:
  if (!credits) {
    goto end;
  }
  srand(getpid());
  n = rand() % ((3) < (credits) ? (3) : (credits)) + 1;
  credits -= n;
  int childcredits = credits / n;
  for (int i = 0; i < n; i++) {
    if (!fork()) {
      setpgid(0, 0);
      credits = childcredits + (i < credits % n);
      goto startover;
    }
  }
end:
  pause();
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    119c <main+0x2c>
movl   $0x0,-0x4(%rbp)
jmp    128b <main+0x11b>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1060 <atoi@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jne    11bb <main+0x4b>
jmp    1284 <main+0x114>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%edi
call   1040 <srand@plt>
mov    $0x0,%al
call   1090 <rand@plt>
mov    %eax,-0x24(%rbp)
mov    $0x3,%eax
cmp    -0x14(%rbp),%eax
jge    11ee <main+0x7e>
mov    $0x3,%eax
mov    %eax,-0x28(%rbp)
jmp    11f4 <main+0x84>
mov    -0x14(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x24(%rbp),%eax
mov    -0x28(%rbp),%ecx
cltd
idiv   %ecx
add    $0x1,%edx
mov    %edx,-0x18(%rbp)
mov    -0x18(%rbp),%ecx
mov    -0x14(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cltd
idivl  -0x18(%rbp)
mov    %eax,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    127f <main+0x10f>
mov    $0x0,%al
call   1080 <fork@plt>
cmp    $0x0,%eax
jne    126c <main+0xfc>
xor    %esi,%esi
mov    %esi,%edi
call   1070 <setpgid@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x20(%rbp),%ecx
mov    -0x14(%rbp),%eax
cltd
idivl  -0x18(%rbp)
mov    -0x2c(%rbp),%eax
cmp    %edx,%ecx
setl   %cl
and    $0x1,%cl
movzbl %cl,%ecx
add    %ecx,%eax
mov    %eax,-0x14(%rbp)
jmp    11ac <main+0x3c>
jmp    1271 <main+0x101>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    121f <main+0xaf>
jmp    1284 <main+0x114>
mov    $0x0,%al
call   1050 <pause@plt>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
cmp    $0x2,%edi
jl     1217 <main+0xa7>
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    0x8(%rsi),%rdi
call   1060 <atoi@plt>
test   %eax,%eax
je     1206 <main+0x96>
mov    %eax,%r15d
xor    %eax,%eax
call   1030 <getpid@plt>
mov    %eax,%edi
call   1040 <srand@plt>
xor    %eax,%eax
call   1090 <rand@plt>
cmp    $0x3,%r15d
mov    $0x3,%ecx
cmovl  %r15d,%ecx
cltd
idiv   %ecx
mov    %edx,%ecx
lea    0x1(%rcx),%ebp
sub    %ebp,%r15d
mov    %r15d,%eax
cltd
idiv   %ebp
test   %ecx,%ecx
js     1206 <main+0x96>
mov    %edx,%r14d
mov    %eax,%r15d
xor    %ebx,%ebx
nop
xor    %eax,%eax
call   1080 <fork@plt>
test   %eax,%eax
je     11f0 <main+0x80>
add    $0x1,%ebx
cmp    %ebx,%ebp
jne    11d0 <main+0x60>
jmp    1206 <main+0x96>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
xor    %edi,%edi
xor    %esi,%esi
call   1070 <setpgid@plt>
xor    %eax,%eax
cmp    %r14d,%ebx
setl   %al
add    %eax,%r15d
jne    1190 <main+0x20>
xor    %eax,%eax
call   1050 <pause@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
xor    %eax,%eax
ret

LLVM IR (-O0):
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* %4
%11 = icmp slt i32 %10, 2
br i1 %11, label %12, label %13
12: ; preds = %2
store i32 0, i32* %3
br label %65
13: ; preds = %2
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 1
%16 = load i8*, i8** %15
%17 = call i32 @atoi(i8* %16)
store i32 %17, i32* %6
br label %18
18: ; preds = %48, %13
%19 = load i32, i32* %6
%20 = icmp ne i32 %19, 0
br i1 %20, label %22, label %21
21: ; preds = %18
br label %63
22: ; preds = %18
%23 = call i32 (...) @getpid()
%24 = call i32 @srand(i32 %23)
%25 = call i32 (...) @rand()
%26 = load i32, i32* %6
%27 = icmp slt i32 3, %26
br i1 %27, label %28, label %29
28: ; preds = %22
br label %31
29: ; preds = %22
%30 = load i32, i32* %6
br label %31
31: ; preds = %29, %28
%32 = phi i32 [ 3, %28 ], [ %30, %29 ]
%33 = srem i32 %25, %32
%34 = add nsw i32 %33, 1
store i32 %34, i32* %7
%35 = load i32, i32* %7
%36 = load i32, i32* %6
%37 = sub nsw i32 %36, %35
store i32 %37, i32* %6
%38 = load i32, i32* %6
%39 = load i32, i32* %7
%40 = sdiv i32 %38, %39
store i32 %40, i32* %8
store i32 0, i32* %9
br label %41
41: ; preds = %59, %31
%42 = load i32, i32* %9
%43 = load i32, i32* %7
%44 = icmp slt i32 %42, %43
br i1 %44, label %45, label %62
45: ; preds = %41
%46 = call i32 (...) @fork()
%47 = icmp ne i32 %46, 0
br i1 %47, label %58, label %48
48: ; preds = %45
%49 = call i32 @setpgid(i32 0, i32 0)
%50 = load i32, i32* %8
%51 = load i32, i32* %9
%52 = load i32, i32* %6
%53 = load i32, i32* %7
%54 = srem i32 %52, %53
%55 = icmp slt i32 %51, %54
%56 = zext i1 %55 to i32
%57 = add nsw i32 %50, %56
store i32 %57, i32* %6
br label %18
58: ; preds = %45
br label %59
59: ; preds = %58
%60 = load i32, i32* %9
%61 = add nsw i32 %60, 1
store i32 %61, i32* %9
br label %41
62: ; preds = %41
br label %63
63: ; preds = %62, %21
%64 = call i32 (...) @pause()
br label %65
65: ; preds = %63, %12
%66 = load i32, i32* %3
ret i32 %66
}

LLVM IR (-O2):
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp slt i32 %0, 2
br i1 %3, label %37, label %4
4: ; preds = %2
%5 = getelementptr inbounds i8*, i8** %1, i64 1
%6 = load i8*, i8** %5
%7 = tail call i32 @atoi(i8* %6)
%8 = icmp eq i32 %7, 0
br i1 %8, label %35, label %9
9: ; preds = %4, %29
%10 = phi i32 [ %33, %29 ], [ %7, %4 ]
%11 = tail call i32 (...) @getpid()
%12 = tail call i32 @srand(i32 %11)
%13 = tail call i32 (...) @rand()
%14 = icmp slt i32 %10, 3
%15 = select i1 %14, i32 %10, i32 3
%16 = srem i32 %13, %15
%17 = add i32 %16, 1
%18 = sub nsw i32 %10, %17
%19 = sdiv i32 %18, %17
%20 = srem i32 %18, %17
%21 = icmp slt i32 %16, 0
br i1 %21, label %35, label %22
22: ; preds = %9, %26
%23 = phi i32 [ %27, %26 ], [ 0, %9 ]
%24 = tail call i32 (...) @fork()
%25 = icmp eq i32 %24, 0
br i1 %25, label %29, label %26
26: ; preds = %22
%27 = add nuw i32 %23, 1
%28 = icmp eq i32 %23, %16
br i1 %28, label %35, label %22
29: ; preds = %22
%30 = tail call i32 @setpgid(i32 0, i32 0)
%31 = icmp slt i32 %23, %20
%32 = zext i1 %31 to i32
%33 = add nsw i32 %19, %32
%34 = icmp eq i32 %33, 0
br i1 %34, label %35, label %9
35: ; preds = %29, %9, %26, %4
%36 = tail call i32 (...) @pause()
br label %37
37: ; preds = %2, %35
ret i32 0
}