#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 camera_update();
int get_object_count(int);
int /*<<< orphan*/ motor(int, int);
int /*<<< orphan*/ msleep(int);
int /*<<< orphan*/ off(int);
int /*<<< orphan*/ printf(char *, int);
void collectPoms(int duration, int sortLimit) {
  printf("%d \n", camera_update());
  int i = 0;
  int redCount = 0;
  int greenCount = 0;
  while (i < duration) {
    i = i + 1;
    int numObjsRed = 0;
    int numObjsGreen = 0;
    printf("pom try: %d \n", i);
    motor(0, -850);
    msleep(500);
    off(0);
    camera_update();
    numObjsRed = get_object_count(0);
    numObjsGreen = get_object_count(1);
    if (numObjsRed >= 1 && redCount < sortLimit) {
      printf("found red: %d \n", i);
      motor(2, 500);
      msleep(1000);
      off(2);
      i = 0;
      redCount++;
    } else if (numObjsGreen >= 1 && greenCount < sortLimit) {
      printf("found green: %d \n", i);
      motor(2, -500);
      msleep(1000);
      off(2);
      greenCount++;
    } else {
      redCount = 0;
      greenCount = 0;
    }
    printf("pom loop bottom: %d \n", i);
  }
  printf("pom end: %d \n", i);
}


x86 assembly (clang, -O0):
<collectPoms>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    $0x0,%al
call   1040 <camera_update@plt>
mov    %eax,%esi
lea    0xe9b(%rip),%rdi
call   1070 <printf@plt>
movl   $0x0,-0xc(%rbp)
movl   $0x0,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0xc(%rbp),%eax
cmp    -0x4(%rbp),%eax
jge    12df <collectPoms+0x17f>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
movl   $0x0,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0xc(%rbp),%esi
lea    0xe3b(%rip),%rdi
call   1070 <printf@plt>
xor    %edi,%edi
mov    $0xfffffcae,%esi
call   1030 <motor@plt>
mov    $0x1f4,%edi
call   1060 <msleep@plt>
xor    %edi,%edi
call   1050 <off@plt>
mov    $0x0,%al
call   1040 <camera_update@plt>
xor    %edi,%edi
call   1080 <get_object_count@plt>
mov    %eax,-0x18(%rbp)
mov    $0x1,%edi
call   1080 <get_object_count@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x1,-0x18(%rbp)
jl     1262 <collectPoms+0x102>
mov    -0x10(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    1262 <collectPoms+0x102>
mov    -0xc(%rbp),%esi
lea    0xde9(%rip),%rdi
call   1070 <printf@plt>
mov    $0x2,%edi
mov    $0x1f4,%esi
call   1030 <motor@plt>
mov    $0x3e8,%edi
call   1060 <msleep@plt>
mov    $0x2,%edi
call   1050 <off@plt>
movl   $0x0,-0xc(%rbp)
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    12cb <collectPoms+0x16b>
cmpl   $0x1,-0x1c(%rbp)
jl     12b8 <collectPoms+0x158>
mov    -0x14(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    12b8 <collectPoms+0x158>
mov    -0xc(%rbp),%esi
lea    0xd9c(%rip),%rdi
call   1070 <printf@plt>
mov    $0x2,%edi
mov    $0xfffffe0c,%esi
call   1030 <motor@plt>
mov    $0x3e8,%edi
call   1060 <msleep@plt>
mov    $0x2,%edi
call   1050 <off@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    12c6 <collectPoms+0x166>
movl   $0x0,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
jmp    12cb <collectPoms+0x16b>
mov    -0xc(%rbp),%esi
lea    0xd5b(%rip),%rdi
call   1070 <printf@plt>
jmp    1198 <collectPoms+0x38>
mov    -0xc(%rbp),%esi
lea    0xd5d(%rip),%rdi
call   1070 <printf@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<collectPoms>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %esi,%ebx
mov    %edi,%ebp
xor    %r13d,%r13d
xor    %eax,%eax
call   1040 <camera_update@plt>
lea    0xe99(%rip),%rdi
mov    %eax,%esi
call   1070 <printf@plt>
test   %ebp,%ebp
jle    12ac <collectPoms+0x14c>
xor    %r13d,%r13d
xor    %r12d,%r12d
xor    %r14d,%r14d
jmp    11be <collectPoms+0x5e>
nopw   0x0(%rax,%rax,1)
xor    %r14d,%r14d
xor    %r12d,%r12d
lea    0xe83(%rip),%rdi
mov    %r13d,%esi
call   1070 <printf@plt>
cmp    %ebp,%r13d
jge    12ac <collectPoms+0x14c>
add    $0x1,%r13d
lea    0xe37(%rip),%rdi
mov    %r13d,%esi
call   1070 <printf@plt>
xor    %edi,%edi
mov    $0xfffffcae,%esi
call   1030 <motor@plt>
mov    $0x1f4,%edi
call   1060 <msleep@plt>
xor    %edi,%edi
call   1050 <off@plt>
xor    %eax,%eax
call   1040 <camera_update@plt>
xor    %edi,%edi
call   1080 <get_object_count@plt>
mov    %eax,%r15d
mov    $0x1,%edi
call   1080 <get_object_count@plt>
test   %r15d,%r15d
jle    1260 <collectPoms+0x100>
cmp    %ebx,%r14d
jge    1260 <collectPoms+0x100>
lea    0xdf4(%rip),%rdi
mov    %r13d,%esi
call   1070 <printf@plt>
mov    $0x2,%edi
mov    $0x1f4,%esi
call   1030 <motor@plt>
mov    $0x3e8,%edi
call   1060 <msleep@plt>
mov    $0x2,%edi
call   1050 <off@plt>
add    $0x1,%r14d
xor    %r13d,%r13d
jmp    11a6 <collectPoms+0x46>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
test   %eax,%eax
jle    11a0 <collectPoms+0x40>
cmp    %ebx,%r12d
jge    11a0 <collectPoms+0x40>
lea    0xda6(%rip),%rdi
mov    %r13d,%esi
call   1070 <printf@plt>
mov    $0x2,%edi
mov    $0xfffffe0c,%esi
call   1030 <motor@plt>
mov    $0x3e8,%edi
call   1060 <msleep@plt>
mov    $0x2,%edi
call   1050 <off@plt>
add    $0x1,%r12d
jmp    11a6 <collectPoms+0x46>
lea    0xd93(%rip),%rdi
mov    %r13d,%esi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1070 <printf@plt>

LLVM IR (-O0):
@.str = [5 x i8] c"%d \0A\00"
@.str.1 = [14 x i8] c"pom try: %d \0A\00"
@.str.2 = [16 x i8] c"found red: %d \0A\00"
@.str.3 = [18 x i8] c"found green: %d \0A\00"
@.str.4 = [22 x i8] c"pom loop bottom: %d \0A\00"
@.str.5 = [14 x i8] c"pom end: %d \0A\00"
define void @collectPoms(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* %3
store i32 %1, i32* %4
%10 = call i32 (...) @camera_update()
%11 = call i32 @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %10)
store i32 0, i32* %5
store i32 0, i32* %6
store i32 0, i32* %7
br label %12
12: ; preds = %58, %2
%13 = load i32, i32* %5
%14 = load i32, i32* %3
%15 = icmp slt i32 %13, %14
br i1 %15, label %16, label %61
16: ; preds = %12
%17 = load i32, i32* %5
%18 = add nsw i32 %17, 1
store i32 %18, i32* %5
store i32 0, i32* %8
store i32 0, i32* %9
%19 = load i32, i32* %5
%20 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 %19)
%21 = call i32 @motor(i32 0, i32 -850)
%22 = call i32 @msleep(i32 500)
%23 = call i32 @off(i32 0)
%24 = call i32 (...) @camera_update()
%25 = call i32 @get_object_count(i32 0)
store i32 %25, i32* %8
%26 = call i32 @get_object_count(i32 1)
store i32 %26, i32* %9
%27 = load i32, i32* %8
%28 = icmp sge i32 %27, 1
br i1 %28, label %29, label %41
29: ; preds = %16
%30 = load i32, i32* %6
%31 = load i32, i32* %4
%32 = icmp slt i32 %30, %31
br i1 %32, label %33, label %41
33: ; preds = %29
%34 = load i32, i32* %5
%35 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i32 %34)
%36 = call i32 @motor(i32 2, i32 500)
%37 = call i32 @msleep(i32 1000)
%38 = call i32 @off(i32 2)
store i32 0, i32* %5
%39 = load i32, i32* %6
%40 = add nsw i32 %39, 1
store i32 %40, i32* %6
br label %58
41: ; preds = %29, %16
%42 = load i32, i32* %9
%43 = icmp sge i32 %42, 1
br i1 %43, label %44, label %56
44: ; preds = %41
%45 = load i32, i32* %7
%46 = load i32, i32* %4
%47 = icmp slt i32 %45, %46
br i1 %47, label %48, label %56
48: ; preds = %44
%49 = load i32, i32* %5
%50 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i32 %49)
%51 = call i32 @motor(i32 2, i32 -500)
%52 = call i32 @msleep(i32 1000)
%53 = call i32 @off(i32 2)
%54 = load i32, i32* %7
%55 = add nsw i32 %54, 1
store i32 %55, i32* %7
br label %57
56: ; preds = %44, %41
store i32 0, i32* %6
store i32 0, i32* %7
br label %57
57: ; preds = %56, %48
br label %58
58: ; preds = %57, %33
%59 = load i32, i32* %5
%60 = call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0), i32 %59)
br label %12
61: ; preds = %12
%62 = load i32, i32* %5
%63 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0), i32 %62)
ret void
}

LLVM IR (-O2):
@.str = [5 x i8] c"%d \0A\00"
@.str.1 = [14 x i8] c"pom try: %d \0A\00"
@.str.2 = [16 x i8] c"found red: %d \0A\00"
@.str.3 = [18 x i8] c"found green: %d \0A\00"
@.str.4 = [22 x i8] c"pom loop bottom: %d \0A\00"
@.str.5 = [14 x i8] c"pom end: %d \0A\00"
define void @collectPoms(i32 %0, i32 %1) {
%3 = tail call i32 (...) @camera_update()
%4 = tail call i32 @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = icmp sgt i32 %0, 0
br i1 %5, label %6, label %43
6: ; preds = %2, %37
%7 = phi i32 [ %40, %37 ], [ 0, %2 ]
%8 = phi i32 [ %39, %37 ], [ 0, %2 ]
%9 = phi i32 [ %38, %37 ], [ 0, %2 ]
%10 = add nsw i32 %7, 1
%11 = tail call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 %10)
%12 = tail call i32 @motor(i32 0, i32 -850)
%13 = tail call i32 @msleep(i32 500)
%14 = tail call i32 @off(i32 0)
%15 = tail call i32 (...) @camera_update()
%16 = tail call i32 @get_object_count(i32 0)
%17 = tail call i32 @get_object_count(i32 1)
%18 = icmp sgt i32 %16, 0
%19 = icmp slt i32 %9, %1
%20 = select i1 %18, i1 %19, i1 false
br i1 %20, label %21, label %27
21: ; preds = %6
%22 = tail call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0), i32 %10)
%23 = tail call i32 @motor(i32 2, i32 500)
%24 = tail call i32 @msleep(i32 1000)
%25 = tail call i32 @off(i32 2)
%26 = add nsw i32 %9, 1
br label %37
27: ; preds = %6
%28 = icmp sgt i32 %17, 0
%29 = icmp slt i32 %8, %1
%30 = select i1 %28, i1 %29, i1 false
br i1 %30, label %31, label %37
31: ; preds = %27
%32 = tail call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i32 %10)
%33 = tail call i32 @motor(i32 2, i32 -500)
%34 = tail call i32 @msleep(i32 1000)
%35 = tail call i32 @off(i32 2)
%36 = add nsw i32 %8, 1
br label %37
37: ; preds = %27, %31, %21
%38 = phi i32 [ %26, %21 ], [ %9, %31 ], [ 0, %27 ]
%39 = phi i32 [ %8, %21 ], [ %36, %31 ], [ 0, %27 ]
%40 = phi i32 [ 0, %21 ], [ %10, %31 ], [ %10, %27 ]
%41 = tail call i32 @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0), i32 %40)
%42 = icmp slt i32 %40, %0
br i1 %42, label %6, label %43
43: ; preds = %37, %2
%44 = phi i32 [ 0, %2 ], [ %40, %37 ]
%45 = tail call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0), i32 %44)
ret void
}