#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*/ utmp;
struct utmp {
  char *ut_name;
  int /*<<< orphan*/ ut_line;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ UT_LINESIZE;
char *_PATH_UTMP;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
scalar_t__ fread(struct utmp *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ ttystat(int /*<<< orphan*/, int /*<<< orphan*/);
int num_users() {
  FILE *ut;
  struct utmp utmp;
  int nusers;
  if ((ut = fopen(_PATH_UTMP, "r")) == NULL) {
    printf("Error calculating users logged in, could not read file %s\n",
           _PATH_UTMP);
    exit(EXIT_FAILURE);
  }
  nusers = 0;
  for (; fread(&utmp, sizeof(utmp), 1, ut);) {
    if (utmp.ut_name[0] == '\0') {
      continue;
    }
    if (!ttystat(utmp.ut_line, UT_LINESIZE)) {
      continue;
    }
    nusers++;
  }
  fclose(ut);
  return nusers;
}


x86 assembly (clang, -O0):
<num_users>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    0x2e79(%rip),%rax
mov    (%rax),%rdi
lea    0xe87(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x8(%rbp)
cmp    $0x0,%rax
jne    11b0 <num_users+0x50>
mov    0x2e55(%rip),%rax
mov    (%rax),%rsi
lea    0xe65(%rip),%rdi
call   1050 <printf@plt>
mov    0x2e27(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
movl   $0x0,-0x1c(%rbp)
mov    -0x8(%rbp),%rcx
lea    -0x18(%rbp),%rdi
mov    $0x10,%esi
mov    $0x1,%edx
call   1030 <fread@plt>
cmp    $0x0,%rax
je     121a <num_users+0xba>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    11ed <num_users+0x8d>
jmp    11b7 <num_users+0x57>
mov    -0x10(%rbp),%edi
mov    0x2de9(%rip),%rax
mov    (%rax),%esi
call   1060 <ttystat@plt>
cmp    $0x0,%eax
jne    120c <num_users+0xac>
jmp    11b7 <num_users+0x57>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11b7 <num_users+0x57>
mov    -0x8(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x1c(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<num_users>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    0x2e77(%rip),%rbp
mov    0x0(%rbp),%rdi
lea    0xe84(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     11ef <num_users+0x8f>
mov    %rax,%rbx
xor    %r14d,%r14d
lea    0x8(%rsp),%rbp
mov    0x2e48(%rip),%r15
nopl   0x0(%rax,%rax,1)
mov    %rbp,%rdi
mov    $0x10,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1030 <fread@plt>
test   %rax,%rax
je     11d9 <num_users+0x79>
mov    0x8(%rsp),%rax
cmpb   $0x0,(%rax)
je     11a0 <num_users+0x40>
mov    0x10(%rsp),%edi
mov    (%r15),%esi
call   1060 <ttystat@plt>
cmp    $0x1,%eax
sbb    $0xffffffff,%r14d
jmp    11a0 <num_users+0x40>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    %r14d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x0(%rbp),%rsi
lea    0xe08(%rip),%rdi
call   1050 <printf@plt>
mov    0x2dca(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

LLVM IR (-O0):
%struct.utmp = type { i8*, i32 }
@_PATH_UTMP = global i8* null
@.str = [2 x i8] c"r\00"
@.str.1 = [59 x i8] c"Error calculating users logged in, could not read file %s\0A\00"
@EXIT_FAILURE = global i32 0
@UT_LINESIZE = global i32 0
define i32 @num_users() {
%1 = alloca i32*
%2 = alloca %struct.utmp
%3 = alloca i32
%4 = load i8*, i8** @_PATH_UTMP
%5 = call i32* @fopen(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %5, i32** %1
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %12
7: ; preds = %0
%8 = load i8*, i8** @_PATH_UTMP
%9 = call i32 @printf(i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str.1, i64 0, i64 0), i8* %8)
%10 = load i32, i32* @EXIT_FAILURE
%11 = call i32 @exit(i32 %10)
unreachable
12: ; preds = %0
store i32 0, i32* %3
br label %13
13: ; preds = %32, %31, %24, %12
%14 = load i32*, i32** %1
%15 = call i64 @fread(%struct.utmp* %2, i32 16, i32 1, i32* %14)
%16 = icmp ne i64 %15, 0
br i1 %16, label %17, label %35
17: ; preds = %13
%18 = getelementptr inbounds %struct.utmp, %struct.utmp* %2, i32 0, i32 0
%19 = load i8*, i8** %18
%20 = getelementptr inbounds i8, i8* %19, i64 0
%21 = load i8, i8* %20
%22 = sext i8 %21 to i32
%23 = icmp eq i32 %22, 0
br i1 %23, label %24, label %25
24: ; preds = %17
br label %13
25: ; preds = %17
%26 = getelementptr inbounds %struct.utmp, %struct.utmp* %2, i32 0, i32 1
%27 = load i32, i32* %26
%28 = load i32, i32* @UT_LINESIZE
%29 = call i32 @ttystat(i32 %27, i32 %28)
%30 = icmp ne i32 %29, 0
br i1 %30, label %32, label %31
31: ; preds = %25
br label %13
32: ; preds = %25
%33 = load i32, i32* %3
%34 = add nsw i32 %33, 1
store i32 %34, i32* %3
br label %13
35: ; preds = %13
%36 = load i32*, i32** %1
%37 = call i32 @fclose(i32* %36)
%38 = load i32, i32* %3
ret i32 %38
}

LLVM IR (-O2):
%struct.utmp = type { i8*, i32 }
@_PATH_UTMP = global i8* null
@.str = [2 x i8] c"r\00"
@.str.1 = [59 x i8] c"Error calculating users logged in, could not read file %s\0A\00"
@EXIT_FAILURE = global i32 0
@UT_LINESIZE = global i32 0
define i32 @num_users() {
%1 = alloca %struct.utmp
%2 = bitcast %struct.utmp* %1 to i8*
%3 = load i8*, i8** @_PATH_UTMP
%4 = tail call i32* @fopen(i8* %3, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %9, label %6
6: ; preds = %0
%7 = getelementptr inbounds %struct.utmp, %struct.utmp* %1, i64 0, i32 0
%8 = getelementptr inbounds %struct.utmp, %struct.utmp* %1, i64 0, i32 1
br label %28
9: ; preds = %0
%10 = load i8*, i8** @_PATH_UTMP
%11 = tail call i32 @printf(i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str.1, i64 0, i64 0), i8* %10)
%12 = load i32, i32* @EXIT_FAILURE
%13 = tail call i32 @exit(i32 %12)
unreachable
14: ; preds = %28, %17
%15 = call i64 @fread(%struct.utmp* %1, i32 16, i32 1, i32* %4)
%16 = icmp eq i64 %15, 0
br i1 %16, label %30, label %17
17: ; preds = %14
%18 = load i8*, i8** %7
%19 = load i8, i8* %18
%20 = icmp eq i8 %19, 0
br i1 %20, label %14, label %21
21: ; preds = %17
%22 = load i32, i32* %8
%23 = load i32, i32* @UT_LINESIZE
%24 = tail call i32 @ttystat(i32 %22, i32 %23)
%25 = icmp ne i32 %24, 0
%26 = zext i1 %25 to i32
%27 = add nuw nsw i32 %29, %26
br label %28
28: ; preds = %6, %21
%29 = phi i32 [ 0, %6 ], [ %27, %21 ]
br label %14
30: ; preds = %14
%31 = tail call i32 @fclose(i32* %4)
ret i32 %29
}