#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*/ buf;

/* Variables and functions */
scalar_t__ cg_read(char const *, char const *, char *, int);
char *strtok(char *, char const *);
long cg_read_lc(const char *cgroup, const char *control) {
  char buf[4096];
  const char delim[] = "\n";
  char *line;
  long cnt = 0;
  if (cg_read(cgroup, control, buf, sizeof(buf)))
    return -1;
  for (line = strtok(buf, delim); line; line = strtok(NULL, delim))
    cnt++;
  return cnt;
}


x86 assembly (clang, -O0):
<cg_read_lc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1040,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0xec6(%rip),%ax
mov    %ax,-0x1022(%rbp)
movq   $0x0,-0x1038(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
lea    -0x1020(%rbp),%rdx
mov    $0x1000,%ecx
call   1030 <cg_read@plt>
cmp    $0x0,%rax
je     117c <cg_read_lc+0x5c>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
jmp    11dd <cg_read_lc+0xbd>
lea    -0x1020(%rbp),%rdi
lea    -0x1022(%rbp),%rsi
call   1040 <strtok@plt>
mov    %rax,-0x1030(%rbp)
cmpq   $0x0,-0x1030(%rbp)
je     11d2 <cg_read_lc+0xb2>
mov    -0x1038(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x1038(%rbp)
lea    -0x1022(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edi
call   1040 <strtok@plt>
mov    %rax,-0x1030(%rbp)
jmp    1196 <cg_read_lc+0x76>
mov    -0x1038(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x1040,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cg_read_lc>:
push   %r14
push   %rbx
sub    $0x1018,%rsp
movw   $0xa,0xe(%rsp)
lea    0x10(%rsp),%rdx
mov    $0x1000,%ecx
call   1030 <cg_read@plt>
mov    $0xffffffffffffffff,%rbx
test   %rax,%rax
jne    1183 <cg_read_lc+0x63>
lea    0x10(%rsp),%rdi
lea    0xe(%rsp),%rsi
call   1040 <strtok@plt>
xor    %ebx,%ebx
test   %rax,%rax
je     1183 <cg_read_lc+0x63>
lea    0xe(%rsp),%r14
nopw   0x0(%rax,%rax,1)
add    $0x1,%rbx
xor    %edi,%edi
mov    %r14,%rsi
call   1040 <strtok@plt>
test   %rax,%rax
jne    1170 <cg_read_lc+0x50>
mov    %rbx,%rax
add    $0x1018,%rsp
pop    %rbx
pop    %r14
ret

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

LLVM IR (-O2):
define i64 @cg_read_lc(i8* %0, i8* %1) {
%3 = alloca [4096 x i8]
%4 = alloca i16
%5 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%6 = bitcast i16* %4 to i8*
store i16 10, i16* %4
%7 = call i64 @cg_read(i8* %0, i8* %1, i8* %5, i32 4096)
%8 = icmp eq i64 %7, 0
br i1 %8, label %9, label %17
9: ; preds = %2
%10 = call i8* @strtok(i8* %5, i8* %6)
%11 = icmp eq i8* %10, null
br i1 %11, label %17, label %12
12: ; preds = %9, %12
%13 = phi i64 [ %14, %12 ], [ 0, %9 ]
%14 = add nuw nsw i64 %13, 1
%15 = call i8* @strtok(i8* null, i8* %6)
%16 = icmp eq i8* %15, null
br i1 %16, label %17, label %12
17: ; preds = %12, %9, %2
%18 = phi i64 [ -1, %2 ], [ 0, %9 ], [ %14, %12 ]
ret i64 %18
}