#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 */
scalar_t__ calloc(int, int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen_read(char *);
int /*<<< orphan*/ free(char *);
int nb_lines(char *file, int M) {
  FILE *fp = fopen_read(file);
  int lines = 0;
  int max_char_per_line = 20 * M + 10;
  char *szbuff = (char *)calloc(max_char_per_line, sizeof(char));
  while (fgets(szbuff, max_char_per_line, fp))
    lines++;
  fclose(fp);
  free(szbuff);
  return lines;
}


x86 assembly (clang, -O0):
<nb_lines>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
call   1070 <fopen_read@plt>
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
imul   $0x14,-0xc(%rbp),%eax
add    $0xa,%eax
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%edi
mov    $0x1,%esi
call   1060 <calloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%esi
mov    -0x18(%rbp),%rdx
call   1050 <fgets@plt>
cmp    $0x0,%rax
je     11b6 <nb_lines+0x66>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    118e <nb_lines+0x3e>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x1c(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<nb_lines>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%ebx
call   1070 <fopen_read@plt>
mov    %rax,%r14
lea    (%rbx,%rbx,4),%eax
lea    0xa(,%rax,4),%r15d
mov    %r15d,%edi
mov    $0x1,%esi
call   1060 <calloc@plt>
mov    %rax,%rbx
mov    $0xffffffff,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    %r15d,%esi
mov    %r14,%rdx
call   1050 <fgets@plt>
add    $0x1,%ebp
test   %rax,%rax
jne    1190 <nb_lines+0x40>
mov    %r14,%rdi
call   1040 <fclose@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @nb_lines(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
store i8* %0, i8** %3
store i32 %1, i32* %4
%9 = load i8*, i8** %3
%10 = call i32* @fopen_read(i8* %9)
store i32* %10, i32** %5
store i32 0, i32* %6
%11 = load i32, i32* %4
%12 = mul nsw i32 20, %11
%13 = add nsw i32 %12, 10
store i32 %13, i32* %7
%14 = load i32, i32* %7
%15 = call i64 @calloc(i32 %14, i32 1)
%16 = inttoptr i64 %15 to i8*
store i8* %16, i8** %8
br label %17
17: ; preds = %23, %2
%18 = load i8*, i8** %8
%19 = load i32, i32* %7
%20 = load i32*, i32** %5
%21 = call i64 @fgets(i8* %18, i32 %19, i32* %20)
%22 = icmp ne i64 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %17
%24 = load i32, i32* %6
%25 = add nsw i32 %24, 1
store i32 %25, i32* %6
br label %17
26: ; preds = %17
%27 = load i32*, i32** %5
%28 = call i32 @fclose(i32* %27)
%29 = load i8*, i8** %8
%30 = call i32 @free(i8* %29)
%31 = load i32, i32* %6
ret i32 %31
}

LLVM IR (-O2):
define i32 @nb_lines(i8* %0, i32 %1) {
%3 = tail call i32* @fopen_read(i8* %0)
%4 = mul nsw i32 %1, 20
%5 = add nsw i32 %4, 10
%6 = tail call i64 @calloc(i32 %5, i32 1)
%7 = inttoptr i64 %6 to i8*
br label %8
8: ; preds = %8, %2
%9 = phi i32 [ 0, %2 ], [ %12, %8 ]
%10 = tail call i64 @fgets(i8* %7, i32 %5, i32* %3)
%11 = icmp eq i64 %10, 0
%12 = add nuw nsw i32 %9, 1
br i1 %11, label %13, label %8
13: ; preds = %8
%14 = tail call i32 @fclose(i32* %3)
%15 = tail call i32 @free(i8* %7)
ret i32 %9
}