#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 next(int *, int, int);
int /*<<< orphan*/ printf(char *, int, ...);
void rm(int a[], int n) {
  int i = 0;
  int m = 1;
  while (next(a, n, i) != i) {
    printf("i %d m %d\n", i, m);
    i = next(a, n, i);
    if (m == 3) {
      a[i] = 0;
      m = 1;
    }
    m++;
  }
  printf("%d\n", i + 1);
}


x86 assembly (clang, -O0):
<rm>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
movl   $0x1,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x10(%rbp),%edx
call   1040 <next@plt>
cmp    -0x10(%rbp),%eax
je     11a9 <rm+0x89>
mov    -0x10(%rbp),%esi
mov    -0x14(%rbp),%edx
lea    0xe9e(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x10(%rbp),%edx
call   1040 <next@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x3,-0x14(%rbp)
jne    119b <rm+0x7b>
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
movl   $0x0,(%rax,%rcx,4)
movl   $0x1,-0x14(%rbp)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    113d <rm+0x1d>
mov    -0x10(%rbp),%esi
add    $0x1,%esi
lea    0xe51(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rm>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %esi,%r14d
mov    %rdi,%r12
xor    %edx,%edx
call   1040 <next@plt>
test   %eax,%eax
je     119d <rm+0x7d>
mov    $0x1,%ebx
lea    0xebb(%rip),%r15
xor    %ebp,%ebp
jmp    1164 <rm+0x44>
nopl   0x0(%rax)
add    $0x1,%ebx
mov    %r12,%rdi
mov    %r14d,%esi
mov    %ebp,%edx
call   1040 <next@plt>
cmp    %ebp,%eax
je     1198 <rm+0x78>
mov    %r15,%rdi
mov    %ebp,%esi
mov    %ebx,%edx
xor    %eax,%eax
call   1030 <printf@plt>
mov    %r12,%rdi
mov    %r14d,%esi
mov    %ebp,%edx
call   1040 <next@plt>
mov    %eax,%ebp
cmp    $0x3,%ebx
jne    1150 <rm+0x30>
movslq %ebp,%rax
movl   $0x0,(%r12,%rax,4)
mov    $0x1,%ebx
jmp    1150 <rm+0x30>
add    $0x1,%ebp
jmp    11a2 <rm+0x82>
mov    $0x1,%ebp
lea    0xe5e(%rip),%rdi
mov    %ebp,%esi
xor    %eax,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <printf@plt>

LLVM IR (-O0):
@.str = [11 x i8] c"i %d m %d\0A\00"
@.str.1 = [4 x i8] c"%d\0A\00"
define void @rm(i32* %0, i32 %1) {
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i32* %0, i32** %3
store i32 %1, i32* %4
store i32 0, i32* %5
store i32 1, i32* %6
br label %7
7: ; preds = %29, %2
%8 = load i32*, i32** %3
%9 = load i32, i32* %4
%10 = load i32, i32* %5
%11 = call i32 @next(i32* %8, i32 %9, i32 %10)
%12 = load i32, i32* %5
%13 = icmp ne i32 %11, %12
br i1 %13, label %14, label %32
14: ; preds = %7
%15 = load i32, i32* %5
%16 = load i32, i32* %6
%17 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %15, i32 %16)
%18 = load i32*, i32** %3
%19 = load i32, i32* %4
%20 = load i32, i32* %5
%21 = call i32 @next(i32* %18, i32 %19, i32 %20)
store i32 %21, i32* %5
%22 = load i32, i32* %6
%23 = icmp eq i32 %22, 3
br i1 %23, label %24, label %29
24: ; preds = %14
%25 = load i32*, i32** %3
%26 = load i32, i32* %5
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds i32, i32* %25, i64 %27
store i32 0, i32* %28
store i32 1, i32* %6
br label %29
29: ; preds = %24, %14
%30 = load i32, i32* %6
%31 = add nsw i32 %30, 1
store i32 %31, i32* %6
br label %7
32: ; preds = %7
%33 = load i32, i32* %5
%34 = add nsw i32 %33, 1
%35 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %34)
ret void
}

LLVM IR (-O2):
@.str = [11 x i8] c"i %d m %d\0A\00"
@.str.1 = [4 x i8] c"%d\0A\00"
define void @rm(i32* %0, i32 %1) {
%3 = tail call i32 @next(i32* %0, i32 %1, i32 0)
%4 = icmp eq i32 %3, 0
br i1 %4, label %21, label %5
5: ; preds = %2, %14
%6 = phi i32 [ %16, %14 ], [ 1, %2 ]
%7 = phi i32 [ %9, %14 ], [ 0, %2 ]
%8 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %7, i32 %6)
%9 = tail call i32 @next(i32* %0, i32 %1, i32 %7)
%10 = icmp eq i32 %6, 3
br i1 %10, label %11, label %14
11: ; preds = %5
%12 = sext i32 %9 to i64
%13 = getelementptr inbounds i32, i32* %0, i64 %12
store i32 0, i32* %13
br label %14
14: ; preds = %11, %5
%15 = phi i32 [ 1, %11 ], [ %6, %5 ]
%16 = add nsw i32 %15, 1
%17 = tail call i32 @next(i32* %0, i32 %1, i32 %9)
%18 = icmp eq i32 %17, %9
br i1 %18, label %19, label %5
19: ; preds = %14
%20 = add i32 %9, 1
br label %21
21: ; preds = %19, %2
%22 = phi i32 [ 1, %2 ], [ %20, %19 ]
%23 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %22)
ret void
}