#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 EOF;
scalar_t__ calloc(int, int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int fgetc(int /*<<< orphan*/ *);
char *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen_read(char *);
int /*<<< orphan*/ free(char *);
char *strtok(char *, char *);
int nb_cols_lfmm(char *file) {
  FILE *fp = fopen_read(file);
  int cols = 0;
  int c;
  char *szbuff;
  char *token;
  c = fgetc(fp);
  while ((c != EOF) && (c != 10) && (c != 13)) {
    c = fgetc(fp);
    cols++;
  }
  fclose(fp);
  fp = fopen_read(file);
  szbuff = (char *)calloc(2 * cols, sizeof(char));
  token = fgets(szbuff, 2 * cols, fp);
  cols = 0;
  token = strtok(szbuff, " \t\n");
  while (token) {
    cols++;
    token = strtok(NULL, " \t\n");
  }
  fclose(fp);
  free(szbuff);
  return cols;
}


x86 assembly (clang, -O0):
<nb_cols_lfmm>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1080 <fopen_read@plt>
mov    %rax,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <fgetc@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%ecx
xor    %eax,%eax
mov    0x2e40(%rip),%rdx
cmp    (%rdx),%ecx
mov    %al,-0x29(%rbp)
je     11cc <nb_cols_lfmm+0x5c>
xor    %eax,%eax
cmpl   $0xa,-0x18(%rbp)
mov    %al,-0x29(%rbp)
je     11cc <nb_cols_lfmm+0x5c>
cmpl   $0xd,-0x18(%rbp)
setne  %al
mov    %al,-0x29(%rbp)
mov    -0x29(%rbp),%al
test   $0x1,%al
jne    11dc <nb_cols_lfmm+0x6c>
jmp    11f6 <nb_cols_lfmm+0x86>
mov    -0x10(%rbp),%rdi
call   1050 <fgetc@plt>
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    119c <nb_cols_lfmm+0x2c>
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x8(%rbp),%rdi
call   1080 <fopen_read@plt>
mov    %rax,-0x10(%rbp)
mov    -0x14(%rbp),%edi
shl    $0x1,%edi
mov    $0x1,%esi
call   1070 <calloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x14(%rbp),%esi
shl    $0x1,%esi
mov    -0x10(%rbp),%rdx
call   1060 <fgets@plt>
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x20(%rbp),%rdi
lea    0xdb7(%rip),%rsi
call   1090 <strtok@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     127f <nb_cols_lfmm+0x10f>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
xor    %eax,%eax
mov    %eax,%edi
lea    0xd8f(%rip),%rsi
call   1090 <strtok@plt>
mov    %rax,-0x28(%rbp)
jmp    1252 <nb_cols_lfmm+0xe2>
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x14(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<nb_cols_lfmm>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdi,%r14
call   1080 <fopen_read@plt>
mov    %rax,%r15
mov    %rax,%rdi
call   1050 <fgetc@plt>
mov    0x2e56(%rip),%r12
xor    %ebx,%ebx
mov    $0x0,%ebp
cmp    (%r12),%eax
je     11cb <nb_cols_lfmm+0x5b>
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
cmp    $0xa,%eax
je     11cb <nb_cols_lfmm+0x5b>
cmp    $0xd,%eax
je     11cb <nb_cols_lfmm+0x5b>
mov    %r15,%rdi
call   1050 <fgetc@plt>
add    $0x1,%ebp
cmp    (%r12),%eax
jne    11b0 <nb_cols_lfmm+0x40>
mov    %r15,%rdi
call   1040 <fclose@plt>
mov    %r14,%rdi
call   1080 <fopen_read@plt>
mov    %rax,%r14
add    %ebp,%ebp
mov    %ebp,%edi
mov    $0x1,%esi
call   1070 <calloc@plt>
mov    %rax,%r15
mov    %rax,%rdi
mov    %ebp,%esi
mov    %r14,%rdx
call   1060 <fgets@plt>
lea    0xdfd(%rip),%rsi
mov    %r15,%rdi
call   1090 <strtok@plt>
test   %rax,%rax
je     1232 <nb_cols_lfmm+0xc2>
xor    %ebx,%ebx
lea    0xde7(%rip),%rbp
nopl   0x0(%rax)
add    $0x1,%ebx
xor    %edi,%edi
mov    %rbp,%rsi
call   1090 <strtok@plt>
test   %rax,%rax
jne    1220 <nb_cols_lfmm+0xb0>
mov    %r14,%rdi
call   1040 <fclose@plt>
mov    %r15,%rdi
call   1030 <free@plt>
mov    %ebx,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@EOF = global i32 0
@.str = [4 x i8] c" \09\0A\00"
define i32 @nb_cols_lfmm(i8* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
store i8* %0, i8** %2
%8 = load i8*, i8** %2
%9 = call i32* @fopen_read(i8* %8)
store i32* %9, i32** %3
store i32 0, i32* %4
%10 = load i32*, i32** %3
%11 = call i32 @fgetc(i32* %10)
store i32 %11, i32* %5
br label %12
12: ; preds = %24, %1
%13 = load i32, i32* %5
%14 = load i32, i32* @EOF
%15 = icmp ne i32 %13, %14
br i1 %15, label %16, label %22
16: ; preds = %12
%17 = load i32, i32* %5
%18 = icmp ne i32 %17, 10
br i1 %18, label %19, label %22
19: ; preds = %16
%20 = load i32, i32* %5
%21 = icmp ne i32 %20, 13
br label %22
22: ; preds = %19, %16, %12
%23 = phi i1 [ false, %16 ], [ false, %12 ], [ %21, %19 ]
br i1 %23, label %24, label %29
24: ; preds = %22
%25 = load i32*, i32** %3
%26 = call i32 @fgetc(i32* %25)
store i32 %26, i32* %5
%27 = load i32, i32* %4
%28 = add nsw i32 %27, 1
store i32 %28, i32* %4
br label %12
29: ; preds = %22
%30 = load i32*, i32** %3
%31 = call i32 @fclose(i32* %30)
%32 = load i8*, i8** %2
%33 = call i32* @fopen_read(i8* %32)
store i32* %33, i32** %3
%34 = load i32, i32* %4
%35 = mul nsw i32 2, %34
%36 = call i64 @calloc(i32 %35, i32 1)
%37 = inttoptr i64 %36 to i8*
store i8* %37, i8** %6
%38 = load i8*, i8** %6
%39 = load i32, i32* %4
%40 = mul nsw i32 2, %39
%41 = load i32*, i32** %3
%42 = call i8* @fgets(i8* %38, i32 %40, i32* %41)
store i8* %42, i8** %7
store i32 0, i32* %4
%43 = load i8*, i8** %6
%44 = call i8* @strtok(i8* %43, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
store i8* %44, i8** %7
br label %45
45: ; preds = %48, %29
%46 = load i8*, i8** %7
%47 = icmp ne i8* %46, null
br i1 %47, label %48, label %52
48: ; preds = %45
%49 = load i32, i32* %4
%50 = add nsw i32 %49, 1
store i32 %50, i32* %4
%51 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
store i8* %51, i8** %7
br label %45
52: ; preds = %45
%53 = load i32*, i32** %3
%54 = call i32 @fclose(i32* %53)
%55 = load i8*, i8** %6
%56 = call i32 @free(i8* %55)
%57 = load i32, i32* %4
ret i32 %57
}

LLVM IR (-O2):
@EOF = global i32 0
@.str = [4 x i8] c" \09\0A\00"
define i32 @nb_cols_lfmm(i8* %0) {
%2 = tail call i32* @fopen_read(i8* %0)
%3 = tail call i32 @fgetc(i32* %2)
%4 = load i32, i32* @EOF
%5 = freeze i32 %4
%6 = icmp eq i32 %3, %5
br i1 %6, label %16, label %7
7: ; preds = %1, %10
%8 = phi i32 [ %11, %10 ], [ %3, %1 ]
%9 = phi i32 [ %12, %10 ], [ 0, %1 ]
switch i32 %8, label %10 [
i32 13, label %16
i32 10, label %16
]
10: ; preds = %7
%11 = tail call i32 @fgetc(i32* %2)
%12 = add nuw nsw i32 %9, 1
%13 = load i32, i32* @EOF
%14 = freeze i32 %13
%15 = icmp eq i32 %11, %14
br i1 %15, label %16, label %7
16: ; preds = %10, %7, %7, %1
%17 = phi i32 [ 0, %1 ], [ %9, %7 ], [ %9, %7 ], [ %12, %10 ]
%18 = tail call i32 @fclose(i32* %2)
%19 = tail call i32* @fopen_read(i8* %0)
%20 = shl nuw nsw i32 %17, 1
%21 = tail call i64 @calloc(i32 %20, i32 1)
%22 = inttoptr i64 %21 to i8*
%23 = tail call i8* @fgets(i8* %22, i32 %20, i32* %19)
%24 = tail call i8* @strtok(i8* %22, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%25 = icmp eq i8* %24, null
br i1 %25, label %31, label %26
26: ; preds = %16, %26
%27 = phi i32 [ %28, %26 ], [ 0, %16 ]
%28 = add nuw nsw i32 %27, 1
%29 = tail call i8* @strtok(i8* null, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%30 = icmp eq i8* %29, null
br i1 %30, label %31, label %26
31: ; preds = %26, %16
%32 = phi i32 [ 0, %16 ], [ %28, %26 ]
%33 = tail call i32 @fclose(i32* %19)
%34 = tail call i32 @free(i8* %22)
ret i32 %32
}