#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*/ line;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int strlen(char *);
int count_users() {
  FILE *file = fopen("/etc/passwd", "r");
  char line[1024];
  int lines = 0;
  if (NULL == file)
    return 0;
  while (fgets(line, sizeof(line), file) != NULL) {
    int len = strlen(line) - 1;
    if (line[len] == '\n')
      line[len] = 0;
    lines++;
  }
  fclose(file);
  return lines;
}


x86 assembly (clang, -O0):
<count_users>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
lea    0xeae(%rip),%rdi
lea    0xeb3(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x10(%rbp)
movl   $0x0,-0x414(%rbp)
xor    %eax,%eax
cmp    -0x10(%rbp),%rax
jne    1184 <count_users+0x44>
movl   $0x0,-0x4(%rbp)
jmp    120a <count_users+0xca>
jmp    1189 <count_users+0x49>
lea    -0x410(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    $0x400,%esi
call   1050 <fgets@plt>
cmp    $0x0,%rax
je     11f8 <count_users+0xb8>
lea    -0x410(%rbp),%rdi
call   1040 <strlen@plt>
sub    $0x1,%eax
mov    %eax,-0x418(%rbp)
movslq -0x418(%rbp),%rax
movsbl -0x410(%rbp,%rax,1),%eax
ff
cmp    $0xa,%eax
jne    11e4 <count_users+0xa4>
movslq -0x418(%rbp),%rax
movb   $0x0,-0x410(%rbp,%rax,1)
mov    -0x414(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x414(%rbp)
jmp    1189 <count_users+0x49>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x414(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<count_users>:
push   %rbp
push   %r14
push   %rbx
sub    $0x400,%rsp
lea    0xeae(%rip),%rdi
lea    0xeb3(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     11ba <count_users+0x7a>
mov    %rax,%r14
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x400,%esi
mov    %rax,%rdx
call   1050 <fgets@plt>
xor    %ebp,%ebp
jmp    1193 <count_users+0x53>
nopl   (%rax)
add    $0x1,%ebp
mov    %rbx,%rdi
mov    $0x400,%esi
mov    %r14,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
je     11b0 <count_users+0x70>
mov    %rbx,%rdi
call   1040 <strlen@plt>
cltq
cmpb   $0xa,-0x1(%rax,%rbx,1)
jne    1180 <count_users+0x40>
movb   $0x0,-0x1(%rsp,%rax,1)
jmp    1180 <count_users+0x40>
mov    %r14,%rdi
call   1030 <fclose@plt>
jmp    11bc <count_users+0x7c>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x400,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [12 x i8] c"/etc/passwd\00"
@.str.1 = [2 x i8] c"r\00"
define i32 @count_users() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca [1024 x i8]
%4 = alloca i32
%5 = alloca i32
%6 = call i32* @fopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %6, i32** %2
store i32 0, i32* %4
%7 = load i32*, i32** %2
%8 = icmp eq i32* null, %7
br i1 %8, label %9, label %10
9: ; preds = %0
store i32 0, i32* %1
br label %37
10: ; preds = %0
br label %11
11: ; preds = %30, %10
%12 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%13 = load i32*, i32** %2
%14 = call i32* @fgets(i8* %12, i32 1024, i32* %13)
%15 = icmp ne i32* %14, null
br i1 %15, label %16, label %33
16: ; preds = %11
%17 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%18 = call i32 @strlen(i8* %17)
%19 = sub nsw i32 %18, 1
store i32 %19, i32* %5
%20 = load i32, i32* %5
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 %21
%23 = load i8, i8* %22
%24 = sext i8 %23 to i32
%25 = icmp eq i32 %24, 10
br i1 %25, label %26, label %30
26: ; preds = %16
%27 = load i32, i32* %5
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 %28
store i8 0, i8* %29
br label %30
30: ; preds = %26, %16
%31 = load i32, i32* %4
%32 = add nsw i32 %31, 1
store i32 %32, i32* %4
br label %11
33: ; preds = %11
%34 = load i32*, i32** %2
%35 = call i32 @fclose(i32* %34)
%36 = load i32, i32* %4
store i32 %36, i32* %1
br label %37
37: ; preds = %33, %9
%38 = load i32, i32* %1
ret i32 %38
}

LLVM IR (-O2):
@.str = [12 x i8] c"/etc/passwd\00"
@.str.1 = [2 x i8] c"r\00"
define i32 @count_users() {
%1 = alloca [1024 x i8]
%2 = tail call i32* @fopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%3 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 0
%4 = icmp eq i32* %2, null
br i1 %4, label %24, label %5
5: ; preds = %0
%6 = call i32* @fgets(i8* %3, i32 1024, i32* %2)
%7 = icmp eq i32* %6, null
br i1 %7, label %21, label %8
8: ; preds = %5, %17
%9 = phi i32 [ %18, %17 ], [ 0, %5 ]
%10 = call i32 @strlen(i8* %3)
%11 = add nsw i32 %10, -1
%12 = sext i32 %11 to i64
%13 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 %12
%14 = load i8, i8* %13
%15 = icmp eq i8 %14, 10
br i1 %15, label %16, label %17
16: ; preds = %8
store i8 0, i8* %13
br label %17
17: ; preds = %16, %8
%18 = add nuw nsw i32 %9, 1
%19 = call i32* @fgets(i8* %3, i32 1024, i32* %2)
%20 = icmp eq i32* %19, null
br i1 %20, label %21, label %8
21: ; preds = %17, %5
%22 = phi i32 [ 0, %5 ], [ %18, %17 ]
%23 = call i32 @fclose(i32* %2)
br label %24
24: ; preds = %0, %21
%25 = phi i32 [ %22, %21 ], [ 0, %0 ]
ret i32 %25
}