#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 /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, int, ...);
int rand();
void gen_rand(char *name, int n, int s) {
  FILE *fp = fopen(name, "w");
  fprintf(fp, "%d %d\n", n, s);
  char *on[s];
  for (int i = 0; i < n; i++) {
    int slot = rand() % s;
    if (on[slot]) {
      fprintf(fp, "f %d\n", slot);
      on[slot] = NULL;
    } else {
      int size = rand() % (131072 / 2 - 4) + 4;
      fprintf(fp, "a %d %d\n", slot, size);
    }
  }
  for (int top = 0; top < s; top++) {
    if (on[top]) {
      fprintf(fp, "f %d\n", top);
    }
  }
}


x86 assembly (clang, -O0):
<gen_rand>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xeb3(%rip),%rsi
call   1040 <fopen@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0xc(%rbp),%edx
mov    -0x10(%rbp),%ecx
lea    0xea3(%rip),%rsi
xor    %eax,%eax
call   1030 <fprintf@plt>
mov    -0x10(%rbp),%eax
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
lea    0xf(,%rax,8),%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x40(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    1233 <gen_rand+0x103>
mov    $0x0,%al
call   1050 <rand@plt>
cltd
idivl  -0x10(%rbp)
mov    -0x40(%rbp),%rax
mov    %edx,-0x30(%rbp)
movslq -0x30(%rbp),%rcx
cmpq   $0x0,(%rax,%rcx,8)
je     11f3 <gen_rand+0xc3>
mov    -0x18(%rbp),%rdi
mov    -0x30(%rbp),%edx
lea    0xe2b(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
mov    -0x40(%rbp),%rax
movslq -0x30(%rbp),%rcx
movq   $0x0,(%rax,%rcx,8)
jmp    1220 <gen_rand+0xf0>
mov    $0x0,%al
call   1050 <rand@plt>
mov    $0xfffc,%ecx
cltd
idiv   %ecx
add    $0x4,%edx
mov    %edx,-0x34(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x30(%rbp),%edx
mov    -0x34(%rbp),%ecx
lea    0xdef(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
jmp    1225 <gen_rand+0xf5>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    119c <gen_rand+0x6c>
movl   $0x0,-0x38(%rbp)
mov    -0x38(%rbp),%eax
cmp    -0x10(%rbp),%eax
jge    1281 <gen_rand+0x151>
mov    -0x40(%rbp),%rax
movslq -0x38(%rbp),%rcx
cmpq   $0x0,(%rax,%rcx,8)
je     126e <gen_rand+0x13e>
mov    -0x18(%rbp),%rdi
mov    -0x38(%rbp),%edx
lea    0xd9b(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
jmp    1273 <gen_rand+0x143>
mov    -0x38(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x38(%rbp)
jmp    123a <gen_rand+0x10a>
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gen_rand>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edx,%r14d
mov    %esi,%ebx
lea    0xeb3(%rip),%rsi
call   1040 <fopen@plt>
mov    %rax,%r15
lea    0xeae(%rip),%rsi
mov    %rax,%rdi
mov    %ebx,%edx
mov    %r14d,%ecx
xor    %eax,%eax
call   1030 <fprintf@plt>
mov    %r14d,-0x2c(%rbp)
mov    %r14d,%eax
mov    %rsp,%r14
mov    %rax,-0x38(%rbp)
lea    0xf(,%rax,8),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r14
mov    %r14,%rsp
test   %ebx,%ebx
jg     122d <gen_rand+0xfd>
cmpl   $0x0,-0x2c(%rbp)
jle    1260 <gen_rand+0x130>
lea    0xe5e(%rip),%r12
xor    %ebx,%ebx
jmp    11be <gen_rand+0x8e>
nopl   0x0(%rax,%rax,1)
add    $0x1,%rbx
cmp    %rbx,-0x38(%rbp)
je     1260 <gen_rand+0x130>
cmpq   $0x0,(%r14,%rbx,8)
je     11b0 <gen_rand+0x80>
mov    %r15,%rdi
mov    %r12,%rsi
mov    %ebx,%edx
xor    %eax,%eax
call   1030 <fprintf@plt>
jmp    11b0 <gen_rand+0x80>
cs nopw 0x0(%rax,%rax,1)
mov    %edx,%r12d
xor    %eax,%eax
call   1050 <rand@plt>
cltq
imul   $0xffffffff80020009,%rax,%rcx
shr    $0x20,%rcx
add    %eax,%ecx
mov    %ecx,%edx
shr    $0x1f,%edx
sar    $0xf,%ecx
add    %edx,%ecx
imul   $0xfffc,%ecx,%ecx
neg    %ecx
add    %eax,%ecx
add    $0x4,%ecx
mov    %r15,%rdi
lea    0xdee(%rip),%rsi
mov    %r12d,%edx
xor    %eax,%eax
call   1030 <fprintf@plt>
add    $0xffffffff,%ebx
je     1193 <gen_rand+0x63>
xor    %eax,%eax
call   1050 <rand@plt>
cltd
idivl  -0x2c(%rbp)
movslq %edx,%r13
cmpq   $0x0,(%r14,%r13,8)
je     11e0 <gen_rand+0xb0>
mov    %r15,%rdi
lea    0xdb6(%rip),%rsi
mov    %r13d,%edx
xor    %eax,%eax
call   1030 <fprintf@plt>
movq   $0x0,(%r14,%r13,8)
jmp    1224 <gen_rand+0xf4>
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"w\00"
@.str.1 = [7 x i8] c"%d %d\0A\00"
@.str.2 = [6 x i8] c"f %d\0A\00"
@.str.3 = [9 x i8] c"a %d %d\0A\00"
define void @gen_rand(i8* %0, i32 %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32 %2, i32* %6
%14 = load i8*, i8** %4
%15 = call i32* @fopen(i8* %14, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %15, i32** %7
%16 = load i32*, i32** %7
%17 = load i32, i32* %5
%18 = load i32, i32* %6
%19 = call i32 (i32*, i8*, i32, ...) @fprintf(i32* %16, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32 %17, i32 %18)
%20 = load i32, i32* %6
%21 = zext i32 %20 to i64
%22 = call i8* @llvm.stacksave()
store i8* %22, i8** %8
%23 = alloca i8*, i64 %21
store i64 %21, i64* %9
store i32 0, i32* %10
br label %24
24: ; preds = %53, %3
%25 = load i32, i32* %10
%26 = load i32, i32* %5
%27 = icmp slt i32 %25, %26
br i1 %27, label %28, label %56
28: ; preds = %24
%29 = call i32 (...) @rand()
%30 = load i32, i32* %6
%31 = srem i32 %29, %30
store i32 %31, i32* %11
%32 = load i32, i32* %11
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds i8*, i8** %23, i64 %33
%35 = load i8*, i8** %34
%36 = icmp ne i8* %35, null
br i1 %36, label %37, label %44
37: ; preds = %28
%38 = load i32*, i32** %7
%39 = load i32, i32* %11
%40 = call i32 (i32*, i8*, i32, ...) @fprintf(i32* %38, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 %39)
%41 = load i32, i32* %11
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds i8*, i8** %23, i64 %42
store i8* null, i8** %43
br label %52
44: ; preds = %28
%45 = call i32 (...) @rand()
%46 = srem i32 %45, 65532
%47 = add nsw i32 %46, 4
store i32 %47, i32* %12
%48 = load i32*, i32** %7
%49 = load i32, i32* %11
%50 = load i32, i32* %12
%51 = call i32 (i32*, i8*, i32, ...) @fprintf(i32* %48, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i32 %49, i32 %50)
br label %52
52: ; preds = %44, %37
br label %53
53: ; preds = %52
%54 = load i32, i32* %10
%55 = add nsw i32 %54, 1
store i32 %55, i32* %10
br label %24
56: ; preds = %24
store i32 0, i32* %13
br label %57
57: ; preds = %72, %56
%58 = load i32, i32* %13
%59 = load i32, i32* %6
%60 = icmp slt i32 %58, %59
br i1 %60, label %61, label %75
61: ; preds = %57
%62 = load i32, i32* %13
%63 = sext i32 %62 to i64
%64 = getelementptr inbounds i8*, i8** %23, i64 %63
%65 = load i8*, i8** %64
%66 = icmp ne i8* %65, null
br i1 %66, label %67, label %71
67: ; preds = %61
%68 = load i32*, i32** %7
%69 = load i32, i32* %13
%70 = call i32 (i32*, i8*, i32, ...) @fprintf(i32* %68, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 %69)
br label %71
71: ; preds = %67, %61
br label %72
72: ; preds = %71
%73 = load i32, i32* %13
%74 = add nsw i32 %73, 1
store i32 %74, i32* %13
br label %57
75: ; preds = %57
%76 = load i8*, i8** %8
ret void
}

LLVM IR (-O2):
@.str = [2 x i8] c"w\00"
@.str.1 = [7 x i8] c"%d %d\0A\00"
@.str.2 = [6 x i8] c"f %d\0A\00"
@.str.3 = [9 x i8] c"a %d %d\0A\00"
define void @gen_rand(i8* nocapture %0, i32 %1, i32 %2) {
%4 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%5 = tail call i32 (i32*, i8*, i32, ...) @fprintf(i32* %4, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32 %1, i32 %2)
%6 = zext i32 %2 to i64
%7 = alloca i8*, i64 %6
%8 = icmp sgt i32 %1, 0
br i1 %8, label %11, label %9
9: ; preds = %26, %3
%10 = icmp sgt i32 %2, 0
br i1 %10, label %30, label %29
11: ; preds = %3, %26
%12 = phi i32 [ %27, %26 ], [ 0, %3 ]
%13 = tail call i32 (...) @rand()
%14 = srem i32 %13, %2
%15 = sext i32 %14 to i64
%16 = getelementptr inbounds i8*, i8** %7, i64 %15
%17 = load i8*, i8** %16
%18 = icmp eq i8* %17, null
br i1 %18, label %21, label %19
19: ; preds = %11
%20 = tail call i32 (i32*, i8*, i32, ...) @fprintf(i32* %4, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 %14)
store i8* null, i8** %16
br label %26
21: ; preds = %11
%22 = tail call i32 (...) @rand()
%23 = srem i32 %22, 65532
%24 = add nsw i32 %23, 4
%25 = tail call i32 (i32*, i8*, i32, ...) @fprintf(i32* %4, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i32 %14, i32 %24)
br label %26
26: ; preds = %21, %19
%27 = add nuw nsw i32 %12, 1
%28 = icmp eq i32 %27, %1
br i1 %28, label %9, label %11
29: ; preds = %38, %9
ret void
30: ; preds = %9, %38
%31 = phi i64 [ %39, %38 ], [ 0, %9 ]
%32 = getelementptr inbounds i8*, i8** %7, i64 %31
%33 = load i8*, i8** %32
%34 = icmp eq i8* %33, null
br i1 %34, label %38, label %35
35: ; preds = %30
%36 = trunc i64 %31 to i32
%37 = tail call i32 (i32*, i8*, i32, ...) @fprintf(i32* %4, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 %36)
br label %38
38: ; preds = %30, %35
%39 = add nuw nsw i64 %31, 1
%40 = icmp eq i64 %39, %6
br i1 %40, label %29, label %30
}