#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 = 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     11bd <nb_cols_lfmm+0x4d>
cmpl   $0xa,-0x18(%rbp)
setne  %al
mov    %al,-0x29(%rbp)
mov    -0x29(%rbp),%al
test   $0x1,%al
jne    11cd <nb_cols_lfmm+0x5d>
jmp    11e7 <nb_cols_lfmm+0x77>
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    0xdc6(%rip),%rsi
call   1090 <strtok@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     1270 <nb_cols_lfmm+0x100>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
xor    %eax,%eax
mov    %eax,%edi
lea    0xd9e(%rip),%rsi
call   1090 <strtok@plt>
mov    %rax,-0x28(%rbp)
jmp    1243 <nb_cols_lfmm+0xd3>
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>
xor    %ebx,%ebx
mov    $0x0,%ebp
cmp    $0xa,%eax
je     11c6 <nb_cols_lfmm+0x56>
mov    0x2e4a(%rip),%r12
mov    $0x0,%ebp
cmp    (%r12),%eax
je     11c6 <nb_cols_lfmm+0x56>
xor    %ebp,%ebp
nopl   0x0(%rax,%rax,1)
mov    %r15,%rdi
call   1050 <fgetc@plt>
add    $0x2,%ebp
cmp    $0xa,%eax
je     11c6 <nb_cols_lfmm+0x56>
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
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    0xe04(%rip),%rsi
mov    %r15,%rdi
call   1090 <strtok@plt>
test   %rax,%rax
je     1232 <nb_cols_lfmm+0xc2>
xor    %ebx,%ebx
lea    0xdee(%rip),%rbp
cs nopw 0x0(%rax,%rax,1)
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 = %21, %1
%13 = load i32, i32* %5
%14 = load i32, i32* @EOF
%15 = icmp ne i32 %13, %14
br i1 %15, label %16, label %19
16: ; preds = %12
%17 = load i32, i32* %5
%18 = icmp ne i32 %17, 10
br label %19
19: ; preds = %16, %12
%20 = phi i1 [ false, %12 ], [ %18, %16 ]
br i1 %20, label %21, label %26
21: ; preds = %19
%22 = load i32*, i32** %3
%23 = call i32 @fgetc(i32* %22)
store i32 %23, i32* %5
%24 = load i32, i32* %4
%25 = add nsw i32 %24, 1
store i32 %25, i32* %4
br label %12
26: ; preds = %19
%27 = load i32*, i32** %3
%28 = call i32 @fclose(i32* %27)
%29 = load i8*, i8** %2
%30 = call i32* @fopen_read(i8* %29)
store i32* %30, i32** %3
%31 = load i32, i32* %4
%32 = mul nsw i32 2, %31
%33 = call i64 @calloc(i32 %32, i32 1)
%34 = inttoptr i64 %33 to i8*
store i8* %34, i8** %6
%35 = load i8*, i8** %6
%36 = load i32, i32* %4
%37 = mul nsw i32 2, %36
%38 = load i32*, i32** %3
%39 = call i8* @fgets(i8* %35, i32 %37, i32* %38)
store i8* %39, i8** %7
store i32 0, i32* %4
%40 = load i8*, i8** %6
%41 = call i8* @strtok(i8* %40, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
store i8* %41, i8** %7
br label %42
42: ; preds = %45, %26
%43 = load i8*, i8** %7
%44 = icmp ne i8* %43, null
br i1 %44, label %45, label %49
45: ; preds = %42
%46 = load i32, i32* %4
%47 = add nsw i32 %46, 1
store i32 %47, i32* %4
%48 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
store i8* %48, i8** %7
br label %42
49: ; preds = %42
%50 = load i32*, i32** %3
%51 = call i32 @fclose(i32* %50)
%52 = load i8*, i8** %6
%53 = call i32 @free(i8* %52)
%54 = load i32, i32* %4
ret i32 %54
}

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 = icmp ne i32 %3, %4
%6 = icmp ne i32 %3, 10
%7 = and i1 %6, %5
br i1 %7, label %8, label %18
8: ; preds = %1, %8
%9 = phi i32 [ %11, %8 ], [ 0, %1 ]
%10 = tail call i32 @fgetc(i32* %2)
%11 = add nuw nsw i32 %9, 1
%12 = load i32, i32* @EOF
%13 = icmp ne i32 %10, %12
%14 = icmp ne i32 %10, 10
%15 = and i1 %14, %13
br i1 %15, label %8, label %16
16: ; preds = %8
%17 = shl nuw i32 %11, 1
br label %18
18: ; preds = %16, %1
%19 = phi i32 [ 0, %1 ], [ %17, %16 ]
%20 = tail call i32 @fclose(i32* %2)
%21 = tail call i32* @fopen_read(i8* %0)
%22 = tail call i64 @calloc(i32 %19, i32 1)
%23 = inttoptr i64 %22 to i8*
%24 = tail call i8* @fgets(i8* %23, i32 %19, i32* %21)
%25 = tail call i8* @strtok(i8* %23, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%26 = icmp eq i8* %25, null
br i1 %26, label %32, label %27
27: ; preds = %18, %27
%28 = phi i32 [ %29, %27 ], [ 0, %18 ]
%29 = add nuw nsw i32 %28, 1
%30 = tail call i8* @strtok(i8* null, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%31 = icmp eq i8* %30, null
br i1 %31, label %32, label %27
32: ; preds = %27, %18
%33 = phi i32 [ 0, %18 ], [ %29, %27 ]
%34 = tail call i32 @fclose(i32* %21)
%35 = tail call i32 @free(i8* %23)
ret i32 %33
}