#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 */
char EOF;
char fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, char *);
void OptChar(int nbChar, char *buffer, FILE *f) {
  char c = 'a';
  int j = 0;
  while ((c != EOF) && (j < nbChar)) {
    c = fgetc(f);
    buffer[j] = c;
    j++;
  }
  buffer[j] = '\0';
  printf("%s\n", buffer);
}


x86 assembly (clang, -O0):
<OptChar>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movb   $0x61,-0x19(%rbp)
movl   $0x0,-0x20(%rbp)
movsbl -0x19(%rbp),%ecx
mov    0x2e9f(%rip),%rax
movsbl (%rax),%edx
xor    %eax,%eax
cmp    %edx,%ecx
mov    %al,-0x21(%rbp)
je     1165 <OptChar+0x45>
mov    -0x20(%rbp),%eax
cmp    -0x4(%rbp),%eax
setl   %al
mov    %al,-0x21(%rbp)
mov    -0x21(%rbp),%al
test   $0x1,%al
jne    1175 <OptChar+0x55>
jmp    119d <OptChar+0x7d>
mov    -0x18(%rbp),%rdi
call   1040 <fgetc@plt>
mov    %al,-0x19(%rbp)
mov    -0x19(%rbp),%dl
mov    -0x10(%rbp),%rax
movslq -0x20(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    113e <OptChar+0x1e>
mov    -0x10(%rbp),%rax
movslq -0x20(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rsi
lea    0xe4c(%rip),%rdi
call   1030 <printf@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<OptChar>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rsi,%r14
mov    0x2eb5(%rip),%r12
xor    %ebx,%ebx
cmpb   $0x61,(%r12)
je     116b <OptChar+0x4b>
test   %edi,%edi
jle    116b <OptChar+0x4b>
mov    %rdx,%r15
mov    %edi,%r13d
xor    %ebx,%ebx
nopl   0x0(%rax,%rax,1)
mov    %r15,%rdi
call   1040 <fgetc@plt>
mov    %al,(%r14,%rbx,1)
add    $0x1,%rbx
cmp    (%r12),%al
je     116b <OptChar+0x4b>
cmp    %r13,%rbx
jb     1150 <OptChar+0x30>
movb   $0x0,(%r14,%rbx,1)
lea    0xe89(%rip),%rdi
mov    %r14,%rsi
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
jmp    1030 <printf@plt>

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

LLVM IR (-O2):
@EOF = global i8 0
@.str = [4 x i8] c"%s\0A\00"
define void @OptChar(i32 %0, i8* %1, i32* nocapture %2) {
%4 = load i8, i8* @EOF
%5 = icmp ne i8 %4, 97
%6 = icmp sgt i32 %0, 0
%7 = and i1 %5, %6
br i1 %7, label %8, label %21
8: ; preds = %3
%9 = zext i32 %0 to i64
br label %10
10: ; preds = %8, %10
%11 = phi i64 [ 0, %8 ], [ %14, %10 ]
%12 = tail call signext i8 @fgetc(i32* %2)
%13 = getelementptr inbounds i8, i8* %1, i64 %11
store i8 %12, i8* %13
%14 = add nuw nsw i64 %11, 1
%15 = load i8, i8* @EOF
%16 = icmp ne i8 %12, %15
%17 = icmp ult i64 %14, %9
%18 = select i1 %16, i1 %17, i1 false
br i1 %18, label %10, label %19
19: ; preds = %10
%20 = and i64 %14, 4294967295
br label %21
21: ; preds = %19, %3
%22 = phi i64 [ 0, %3 ], [ %20, %19 ]
%23 = getelementptr inbounds i8, i8* %1, i64 %22
store i8 0, i8* %23
%24 = tail call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* %1)
ret void
}