#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 abs(int);
int rand();
void add_num(int num[4][4]) {
  int i = abs(rand() % 4);
  int j = abs(rand() % 4);
  while (num[i][j] != 0) {
    i = abs(rand() % 4);
    j = abs(rand() % 4);
  }
  num[i][j] = (abs(rand()) % 2 + 1) * 2;
}


x86 assembly (clang, -O0):
<add_num>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    $0x0,%al
call   1040 <rand@plt>
mov    $0x4,%ecx
cltd
idiv   %ecx
mov    %edx,%edi
call   1030 <abs@plt>
mov    %eax,-0xc(%rbp)
mov    $0x0,%al
call   1040 <rand@plt>
mov    $0x4,%ecx
cltd
idiv   %ecx
mov    %edx,%edi
call   1030 <abs@plt>
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
shl    $0x4,%rcx
add    %rcx,%rax
movslq -0x10(%rbp),%rcx
cmpl   $0x0,(%rax,%rcx,4)
je     11b2 <add_num+0x92>
mov    $0x0,%al
call   1040 <rand@plt>
mov    $0x4,%ecx
cltd
idiv   %ecx
mov    %edx,%edi
call   1030 <abs@plt>
mov    %eax,-0xc(%rbp)
mov    $0x0,%al
call   1040 <rand@plt>
mov    $0x4,%ecx
cltd
idiv   %ecx
mov    %edx,%edi
call   1030 <abs@plt>
mov    %eax,-0x10(%rbp)
jmp    115e <add_num+0x3e>
mov    $0x0,%al
call   1040 <rand@plt>
mov    %eax,%edi
call   1030 <abs@plt>
mov    $0x2,%ecx
cltd
idiv   %ecx
add    $0x1,%edx
shl    $0x1,%edx
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
shl    $0x4,%rcx
add    %rcx,%rax
movslq -0x10(%rbp),%rcx
mov    %edx,(%rax,%rcx,4)
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<add_num>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r15
xor    %eax,%eax
call   1030 <rand@plt>
lea    0x3(%rax),%ecx
test   %eax,%eax
cmovns %eax,%ecx
and    $0xfffffffc,%ecx
sub    %ecx,%eax
mov    %eax,%r14d
neg    %r14d
cmovs  %eax,%r14d
xor    %eax,%eax
call   1030 <rand@plt>
lea    0x3(%rax),%ecx
test   %eax,%eax
cmovns %eax,%ecx
and    $0xfffffffc,%ecx
sub    %ecx,%eax
mov    %eax,%ecx
neg    %ecx
cmovs  %eax,%ecx
shl    $0x4,%r14
add    %r15,%r14
cmpl   $0x0,(%r14,%rcx,4)
je     11a9 <add_num+0x99>
nop
xor    %eax,%eax
call   1030 <rand@plt>
lea    0x3(%rax),%ecx
test   %eax,%eax
cmovns %eax,%ecx
and    $0xfffffffc,%ecx
sub    %ecx,%eax
mov    %eax,%ebx
neg    %ebx
cmovs  %eax,%ebx
xor    %eax,%eax
call   1030 <rand@plt>
lea    0x3(%rax),%ecx
test   %eax,%eax
cmovns %eax,%ecx
and    $0xfffffffc,%ecx
sub    %ecx,%eax
mov    %eax,%ecx
neg    %ecx
cmovs  %eax,%ecx
shl    $0x4,%rbx
add    %r15,%rbx
cmpl   $0x0,(%rbx,%rcx,4)
jne    1160 <add_num+0x50>
lea    (%rbx,%rcx,4),%rbx
jmp    11ad <add_num+0x9d>
lea    (%r14,%rcx,4),%rbx
xor    %eax,%eax
call   1030 <rand@plt>
and    $0x1,%eax
add    %eax,%eax
add    $0x2,%eax
mov    %eax,(%rbx)
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define void @add_num([4 x i32]* %0) {
%2 = alloca [4 x i32]*
%3 = alloca i32
%4 = alloca i32
store [4 x i32]* %0, [4 x i32]** %2
%5 = call i32 (...) @rand()
%6 = srem i32 %5, 4
%7 = call i32 @abs(i32 %6)
store i32 %7, i32* %3
%8 = call i32 (...) @rand()
%9 = srem i32 %8, 4
%10 = call i32 @abs(i32 %9)
store i32 %10, i32* %4
br label %11
11: ; preds = %21, %1
%12 = load [4 x i32]*, [4 x i32]** %2
%13 = load i32, i32* %3
%14 = sext i32 %13 to i64
%15 = getelementptr inbounds [4 x i32], [4 x i32]* %12, i64 %14
%16 = load i32, i32* %4
%17 = sext i32 %16 to i64
%18 = getelementptr inbounds [4 x i32], [4 x i32]* %15, i64 0, i64 %17
%19 = load i32, i32* %18
%20 = icmp ne i32 %19, 0
br i1 %20, label %21, label %28
21: ; preds = %11
%22 = call i32 (...) @rand()
%23 = srem i32 %22, 4
%24 = call i32 @abs(i32 %23)
store i32 %24, i32* %3
%25 = call i32 (...) @rand()
%26 = srem i32 %25, 4
%27 = call i32 @abs(i32 %26)
store i32 %27, i32* %4
br label %11
28: ; preds = %11
%29 = call i32 (...) @rand()
%30 = call i32 @abs(i32 %29)
%31 = srem i32 %30, 2
%32 = add nsw i32 %31, 1
%33 = mul nsw i32 %32, 2
%34 = load [4 x i32]*, [4 x i32]** %2
%35 = load i32, i32* %3
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds [4 x i32], [4 x i32]* %34, i64 %36
%38 = load i32, i32* %4
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds [4 x i32], [4 x i32]* %37, i64 0, i64 %39
store i32 %33, i32* %40
ret void
}

LLVM IR (-O2):
define void @add_num([4 x i32]* nocapture %0) {
%2 = tail call i32 (...) @rand()
%3 = srem i32 %2, 4
%4 = tail call i32 @llvm.abs.i32(i32 %3, i1 true)
%5 = tail call i32 (...) @rand()
%6 = srem i32 %5, 4
%7 = tail call i32 @llvm.abs.i32(i32 %6, i1 true)
%8 = zext i32 %4 to i64
%9 = zext i32 %7 to i64
%10 = getelementptr inbounds [4 x i32], [4 x i32]* %0, i64 %8, i64 %9
%11 = load i32, i32* %10
%12 = icmp eq i32 %11, 0
br i1 %12, label %25, label %13
13: ; preds = %1, %13
%14 = tail call i32 (...) @rand()
%15 = srem i32 %14, 4
%16 = tail call i32 @llvm.abs.i32(i32 %15, i1 true)
%17 = tail call i32 (...) @rand()
%18 = srem i32 %17, 4
%19 = tail call i32 @llvm.abs.i32(i32 %18, i1 true)
%20 = zext i32 %16 to i64
%21 = zext i32 %19 to i64
%22 = getelementptr inbounds [4 x i32], [4 x i32]* %0, i64 %20, i64 %21
%23 = load i32, i32* %22
%24 = icmp eq i32 %23, 0
br i1 %24, label %25, label %13
25: ; preds = %13, %1
%26 = phi i32* [ %10, %1 ], [ %22, %13 ]
%27 = tail call i32 (...) @rand()
%28 = shl i32 %27, 1
%29 = and i32 %28, 2
%30 = add nuw nsw i32 %29, 2
store i32 %30, i32* %26
ret void
}