#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 fscanf(int /*<<< orphan*/ *, char *, char *);
unsigned numberOfNeedle(FILE *in, char needle, unsigned limit) {
  int counter = 0;
  char buffer;
  for (unsigned i = 0; fscanf(in, "%c", &buffer) != -1 && i < limit; i++) {
    if (buffer == needle) {
      counter += 1;
    }
  }
  return counter;
}


x86 assembly (clang, -O0):
<numberOfNeedle>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %sil,%al
mov    %rdi,-0x8(%rbp)
mov    %al,-0x9(%rbp)
mov    %edx,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xec2(%rip),%rsi
lea    -0x15(%rbp),%rdx
call   1030 <fscanf@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0xffffffff,%ecx
mov    %al,-0x1d(%rbp)
je     1163 <numberOfNeedle+0x53>
mov    -0x1c(%rbp),%eax
cmp    -0x10(%rbp),%eax
setb   %al
mov    %al,-0x1d(%rbp)
mov    -0x1d(%rbp),%al
test   $0x1,%al
jne    1173 <numberOfNeedle+0x63>
jmp    119f <numberOfNeedle+0x8f>
movsbl -0x15(%rbp),%eax
movsbl -0x9(%rbp),%ecx
cmp    %ecx,%eax
jne    118c <numberOfNeedle+0x7c>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1191 <numberOfNeedle+0x81>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1133 <numberOfNeedle+0x23>
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<numberOfNeedle>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%r15d
mov    %esi,%r14d
mov    %rdi,%rbp
lea    0xed5(%rip),%rsi
lea    0x7(%rsp),%rdx
call   1030 <fscanf@plt>
xor    %ebx,%ebx
cmp    $0xffffffff,%eax
je     1182 <numberOfNeedle+0x72>
test   %r15d,%r15d
je     1182 <numberOfNeedle+0x72>
xor    %ebx,%ebx
mov    $0x1,%r12d
lea    0x7(%rsp),%r13
xchg   %ax,%ax
mov    %ebx,%eax
xor    %ebx,%ebx
cmp    %r14b,0x7(%rsp)
sete   %bl
add    %eax,%ebx
mov    %rbp,%rdi
lea    0xe98(%rip),%rsi
mov    %r13,%rdx
call   1030 <fscanf@plt>
cmp    $0xffffffff,%eax
je     1182 <numberOfNeedle+0x72>
lea    0x1(%r12),%eax
cmp    %r15d,%r12d
mov    %eax,%r12d
jb     1150 <numberOfNeedle+0x40>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [3 x i8] c"%c\00"
define i32 @numberOfNeedle(i32* nocapture %0, i8 signext %1, i32 %2) {
%4 = alloca i8
%5 = call i32 @fscanf(i32* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %4)
%6 = icmp ne i32 %5, -1
%7 = icmp ne i32 %2, 0
%8 = and i1 %6, %7
br i1 %8, label %11, label %9
9: ; preds = %11, %3
%10 = phi i32 [ 0, %3 ], [ %17, %11 ]
ret i32 %10
11: ; preds = %3, %11
%12 = phi i32 [ %18, %11 ], [ 0, %3 ]
%13 = phi i32 [ %17, %11 ], [ 0, %3 ]
%14 = load i8, i8* %4
%15 = icmp eq i8 %14, %1
%16 = zext i1 %15 to i32
%17 = add nuw nsw i32 %13, %16
%18 = add nuw i32 %12, 1
%19 = call i32 @fscanf(i32* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %4)
%20 = icmp ne i32 %19, -1
%21 = icmp ult i32 %18, %2
%22 = select i1 %20, i1 %21, i1 false
br i1 %22, label %11, label %9
}