#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 empty_unit_counts(int **, int);
int /*<<< orphan*/ empty_unit_pos_array(int *, int **, int);
int /*<<< orphan*/ free(int *);
scalar_t__ malloc(int);
int /*<<< orphan*/ map_max_val(int **, int);
int random_max(int /*<<< orphan*/);
int random_num(int /*<<< orphan*/, int);
int set_new_num(int **map, int length) {
  int counts, pos, val;
  int *array;
  counts = empty_unit_counts(map, length);
  if (counts == 0) {
    return 0;
  }
  array = (int *)malloc(counts * sizeof(int));
  empty_unit_pos_array(array, map, length);
  pos = random_num(0, counts - 1);
  val = random_max(map_max_val(map, length));
  *(*(map + array[pos] / length) + array[pos] % length) = val;
  free(array);
  return 1;
}


x86 assembly (clang, -O0):
<set_new_num>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1080 <empty_unit_counts@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jne    11a4 <set_new_num+0x34>
movl   $0x0,-0x4(%rbp)
jmp    1249 <set_new_num+0xd9>
movslq -0x18(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edi
call   1060 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1070 <empty_unit_pos_array@plt>
mov    -0x18(%rbp),%esi
sub    $0x1,%esi
xor    %edi,%edi
call   1050 <random_num@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1090 <map_max_val@plt>
mov    %eax,%edi
call   1040 <random_max@plt>
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x28(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
cltd
idivl  -0x14(%rbp)
mov    %eax,%ecx
mov    -0x40(%rbp),%rax
movslq %ecx,%rcx
mov    (%rax,%rcx,8),%rax
mov    %rax,-0x38(%rbp)
mov    -0x28(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,4),%eax
cltd
idivl  -0x14(%rbp)
mov    -0x38(%rbp),%rax
mov    %edx,%ecx
mov    -0x2c(%rbp),%edx
movslq %ecx,%rcx
mov    %edx,(%rax,%rcx,4)
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_new_num>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %rdi,%r15
call   1080 <empty_unit_counts@plt>
test   %eax,%eax
je     11e9 <set_new_num+0x79>
mov    %eax,%ebx
lea    0x0(,%rbx,4),%edi
call   1060 <malloc@plt>
mov    %rax,%r14
mov    %rax,%rdi
mov    %r15,%rsi
mov    %ebp,%edx
call   1070 <empty_unit_pos_array@plt>
add    $0xffffffff,%ebx
xor    %edi,%edi
mov    %ebx,%esi
call   1050 <random_num@plt>
mov    %eax,%ebx
mov    %r15,%rdi
mov    %ebp,%esi
call   1090 <map_max_val@plt>
mov    %eax,%edi
call   1040 <random_max@plt>
mov    %eax,%ecx
movslq %ebx,%rax
mov    (%r14,%rax,4),%eax
cltd
idiv   %ebp
cltq
mov    (%r15,%rax,8),%rax
movslq %edx,%rdx
mov    %ecx,(%rax,%rdx,4)
mov    %r14,%rdi
call   1030 <free@plt>
mov    $0x1,%eax
jmp    11eb <set_new_num+0x7b>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @set_new_num(i32** %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32**
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32*
store i32** %0, i32*** %4
store i32 %1, i32* %5
%10 = load i32**, i32*** %4
%11 = load i32, i32* %5
%12 = call i32 @empty_unit_counts(i32** %10, i32 %11)
store i32 %12, i32* %6
%13 = load i32, i32* %6
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %16
15: ; preds = %2
store i32 0, i32* %3
br label %57
16: ; preds = %2
%17 = load i32, i32* %6
%18 = sext i32 %17 to i64
%19 = mul i64 %18, 4
%20 = trunc i64 %19 to i32
%21 = call i64 @malloc(i32 %20)
%22 = inttoptr i64 %21 to i32*
store i32* %22, i32** %9
%23 = load i32*, i32** %9
%24 = load i32**, i32*** %4
%25 = load i32, i32* %5
%26 = call i32 @empty_unit_pos_array(i32* %23, i32** %24, i32 %25)
%27 = load i32, i32* %6
%28 = sub nsw i32 %27, 1
%29 = call i32 @random_num(i32 0, i32 %28)
store i32 %29, i32* %7
%30 = load i32**, i32*** %4
%31 = load i32, i32* %5
%32 = call i32 @map_max_val(i32** %30, i32 %31)
%33 = call i32 @random_max(i32 %32)
store i32 %33, i32* %8
%34 = load i32, i32* %8
%35 = load i32**, i32*** %4
%36 = load i32*, i32** %9
%37 = load i32, i32* %7
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds i32, i32* %36, i64 %38
%40 = load i32, i32* %39
%41 = load i32, i32* %5
%42 = sdiv i32 %40, %41
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds i32*, i32** %35, i64 %43
%45 = load i32*, i32** %44
%46 = load i32*, i32** %9
%47 = load i32, i32* %7
%48 = sext i32 %47 to i64
%49 = getelementptr inbounds i32, i32* %46, i64 %48
%50 = load i32, i32* %49
%51 = load i32, i32* %5
%52 = srem i32 %50, %51
%53 = sext i32 %52 to i64
%54 = getelementptr inbounds i32, i32* %45, i64 %53
store i32 %34, i32* %54
%55 = load i32*, i32** %9
%56 = call i32 @free(i32* %55)
store i32 1, i32* %3
br label %57
57: ; preds = %16, %15
%58 = load i32, i32* %3
ret i32 %58
}

LLVM IR (-O2):
define i32 @set_new_num(i32** %0, i32 %1) {
%3 = tail call i32 @empty_unit_counts(i32** %0, i32 %1)
%4 = icmp eq i32 %3, 0
br i1 %4, label %25, label %5
5: ; preds = %2
%6 = shl i32 %3, 2
%7 = tail call i64 @malloc(i32 %6)
%8 = inttoptr i64 %7 to i32*
%9 = tail call i32 @empty_unit_pos_array(i32* %8, i32** %0, i32 %1)
%10 = add nsw i32 %3, -1
%11 = tail call i32 @random_num(i32 0, i32 %10)
%12 = tail call i32 @map_max_val(i32** %0, i32 %1)
%13 = tail call i32 @random_max(i32 %12)
%14 = sext i32 %11 to i64
%15 = getelementptr inbounds i32, i32* %8, i64 %14
%16 = load i32, i32* %15
%17 = sdiv i32 %16, %1
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds i32*, i32** %0, i64 %18
%20 = load i32*, i32** %19
%21 = srem i32 %16, %1
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds i32, i32* %20, i64 %22
store i32 %13, i32* %23
%24 = tail call i32 @free(i32* %8)
br label %25
25: ; preds = %2, %5
%26 = phi i32 [ 1, %5 ], [ 0, %2 ]
ret i32 %26
}