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

/* Variables and functions */
int EOF;
scalar_t__ USER_PROCESS;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int getc(int /*<<< orphan*/ *);
int /*<<< orphan*/ memset(struct utmp *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, char *, char *);
int /*<<< orphan*/ stderr;
int main(void) {
  FILE *fp;
  int c;
  char *p;
  struct utmp record;
  unsigned int i = 0;
  unsigned int index = 0;
  fp = fopen("/var/run/utmp", "r");
  if (!fp) {
    fprintf(stderr, "Cannot open /var/run/utmp\n");
    return 1;
  }
  memset(&record, 0, sizeof record);
  p = (char *)&record;
  while ((c = getc(fp)) != EOF) {
    p[index] = (char)c;
    i++;
    index = i % sizeof record;
    if (index == 0) {
      if (record.ut_type == USER_PROCESS) {
        printf("%5s\t%10s\n", record.ut_line, record.ut_user);
      }
      memset(&record, 0, sizeof record);
    }
  }
  fclose(fp);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x3c(%rbp)
movl   $0x0,-0x40(%rbp)
lea    0xe7c(%rip),%rdi
lea    0xe83(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11c0 <main+0x60>
mov    0x2e2a(%rip),%rax
mov    (%rax),%edi
lea    0xe61(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1274 <main+0x114>
lea    -0x38(%rbp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1050 <memset@plt>
lea    -0x38(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1080 <getc@plt>
mov    %eax,-0x14(%rbp)
mov    0x2df5(%rip),%rcx
cmp    (%rcx),%eax
je     1264 <main+0x104>
mov    -0x14(%rbp),%eax
mov    %al,%dl
mov    -0x20(%rbp),%rax
mov    -0x40(%rbp),%ecx
mov    %dl,(%rax,%rcx,1)
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
mov    $0x18,%ecx
xor    %edx,%edx
div    %rcx
mov    %edx,%eax
mov    %eax,-0x40(%rbp)
cmpl   $0x0,-0x40(%rbp)
jne    125f <main+0xff>
mov    -0x38(%rbp),%rax
mov    0x2db6(%rip),%rcx
cmp    (%rcx),%rax
jne    124f <main+0xef>
mov    -0x30(%rbp),%rsi
mov    -0x28(%rbp),%rdx
lea    0xde1(%rip),%rdi
call   1040 <printf@plt>
lea    -0x38(%rbp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1050 <memset@plt>
jmp    11d8 <main+0x78>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
lea    0xe8b(%rip),%rdi
lea    0xe92(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     11b9 <main+0x59>
mov    %rax,%rbx
mov    %rsp,%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1050 <memset@plt>
mov    %rbx,%rdi
call   1080 <getc@plt>
mov    0x2e39(%rip),%r13
cmp    0x0(%r13),%eax
jne    11e2 <main+0x82>
mov    %rbx,%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
jmp    11d3 <main+0x73>
mov    0x2e10(%rip),%rax
mov    (%rax),%edi
lea    0xe47(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0x1,%ebp
xor    %ecx,%ecx
mov    $0xaaaaaaab,%r14d
mov    %rsp,%r12
jmp    1223 <main+0xc3>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %r12,%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1050 <memset@plt>
mov    %rbx,%rdi
call   1080 <getc@plt>
add    $0x1,%ebp
mov    %r15,%rcx
cmp    0x0(%r13),%eax
je     11ad <main+0x4d>
mov    %ebp,%edx
imul   %r14,%rdx
shr    $0x24,%rdx
shl    $0x3,%edx
lea    (%rdx,%rdx,2),%edx
mov    %ebp,%r15d
mov    %al,(%rsp,%rcx,1)
sub    %edx,%r15d
jne    120f <main+0xaf>
mov    (%rsp),%rax
mov    0x2d9f(%rip),%rcx
cmp    (%rcx),%rax
jne    1200 <main+0xa0>
mov    0x8(%rsp),%rsi
mov    0x10(%rsp),%rdx
lea    0xdcc(%rip),%rdi
call   1040 <printf@plt>
jmp    1200 <main+0xa0>

LLVM IR (-O0):
%struct.utmp = type { i64, i8*, i8* }
@.str = [14 x i8] c"/var/run/utmp\00"
@.str.1 = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.2 = [27 x i8] c"Cannot open /var/run/utmp\0A\00"
@EOF = global i32 0
@USER_PROCESS = global i64 0
@.str.3 = [10 x i8] c"%5s\09%10s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca i32
%4 = alloca i8*
%5 = alloca %struct.utmp
%6 = alloca i32
%7 = alloca i32
store i32 0, i32* %1
store i32 0, i32* %6
store i32 0, i32* %7
%8 = call i32* @fopen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %8, i32** %2
%9 = load i32*, i32** %2
%10 = icmp ne i32* %9, null
br i1 %10, label %14, label %11
11: ; preds = %0
%12 = load i32, i32* @stderr
%13 = call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %1
br label %54
14: ; preds = %0
%15 = call i32 @memset(%struct.utmp* %5, i32 0, i32 24)
%16 = bitcast %struct.utmp* %5 to i8*
store i8* %16, i8** %4
br label %17
17: ; preds = %50, %14
%18 = load i32*, i32** %2
%19 = call i32 @getc(i32* %18)
store i32 %19, i32* %3
%20 = load i32, i32* @EOF
%21 = icmp ne i32 %19, %20
br i1 %21, label %22, label %51
22: ; preds = %17
%23 = load i32, i32* %3
%24 = trunc i32 %23 to i8
%25 = load i8*, i8** %4
%26 = load i32, i32* %7
%27 = zext i32 %26 to i64
%28 = getelementptr inbounds i8, i8* %25, i64 %27
store i8 %24, i8* %28
%29 = load i32, i32* %6
%30 = add i32 %29, 1
store i32 %30, i32* %6
%31 = load i32, i32* %6
%32 = zext i32 %31 to i64
%33 = urem i64 %32, 24
%34 = trunc i64 %33 to i32
store i32 %34, i32* %7
%35 = load i32, i32* %7
%36 = icmp eq i32 %35, 0
br i1 %36, label %37, label %50
37: ; preds = %22
%38 = getelementptr inbounds %struct.utmp, %struct.utmp* %5, i32 0, i32 0
%39 = load i64, i64* %38
%40 = load i64, i64* @USER_PROCESS
%41 = icmp eq i64 %39, %40
br i1 %41, label %42, label %48
42: ; preds = %37
%43 = getelementptr inbounds %struct.utmp, %struct.utmp* %5, i32 0, i32 1
%44 = load i8*, i8** %43
%45 = getelementptr inbounds %struct.utmp, %struct.utmp* %5, i32 0, i32 2
%46 = load i8*, i8** %45
%47 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i8* %44, i8* %46)
br label %48
48: ; preds = %42, %37
%49 = call i32 @memset(%struct.utmp* %5, i32 0, i32 24)
br label %50
50: ; preds = %48, %22
br label %17
51: ; preds = %17
%52 = load i32*, i32** %2
%53 = call i32 @fclose(i32* %52)
store i32 0, i32* %1
br label %54
54: ; preds = %51, %11
%55 = load i32, i32* %1
ret i32 %55
}

LLVM IR (-O2):
%struct.utmp = type { i64, i8*, i8* }
@.str = [14 x i8] c"/var/run/utmp\00"
@.str.1 = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.2 = [27 x i8] c"Cannot open /var/run/utmp\0A\00"
@EOF = global i32 0
@USER_PROCESS = global i64 0
@.str.3 = [10 x i8] c"%5s\09%10s\0A\00"
define i32 @main() {
%1 = alloca %struct.utmp
%2 = bitcast %struct.utmp* %1 to i8*
%3 = tail call i32* @fopen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %5, label %8
5: ; preds = %0
%6 = load i32, i32* @stderr
%7 = tail call i32 @fprintf(i32 %6, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0))
br label %43
8: ; preds = %0
%9 = call i32 @memset(%struct.utmp* %1, i32 0, i32 24)
%10 = call i32 @getc(i32* %3)
%11 = load i32, i32* @EOF
%12 = icmp eq i32 %10, %11
br i1 %12, label %41, label %13
13: ; preds = %8
%14 = getelementptr inbounds %struct.utmp, %struct.utmp* %1, i64 0, i32 0
%15 = getelementptr inbounds %struct.utmp, %struct.utmp* %1, i64 0, i32 1
%16 = getelementptr inbounds %struct.utmp, %struct.utmp* %1, i64 0, i32 2
br label %17
17: ; preds = %13, %37
%18 = phi i32 [ %10, %13 ], [ %38, %37 ]
%19 = phi i64 [ 0, %13 ], [ %25, %37 ]
%20 = phi i32 [ 0, %13 ], [ %23, %37 ]
%21 = trunc i32 %18 to i8
%22 = getelementptr inbounds i8, i8* %2, i64 %19
store i8 %21, i8* %22
%23 = add i32 %20, 1
%24 = urem i32 %23, 24
%25 = zext i32 %24 to i64
%26 = icmp eq i32 %24, 0
br i1 %26, label %27, label %37
27: ; preds = %17
%28 = load i64, i64* %14
%29 = load i64, i64* @USER_PROCESS
%30 = icmp eq i64 %28, %29
br i1 %30, label %31, label %35
31: ; preds = %27
%32 = load i8*, i8** %15
%33 = load i8*, i8** %16
%34 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i8* %32, i8* %33)
br label %35
35: ; preds = %31, %27
%36 = call i32 @memset(%struct.utmp* %1, i32 0, i32 24)
br label %37
37: ; preds = %35, %17
%38 = call i32 @getc(i32* %3)
%39 = load i32, i32* @EOF
%40 = icmp eq i32 %38, %39
br i1 %40, label %41, label %17
41: ; preds = %37, %8
%42 = call i32 @fclose(i32* %3)
br label %43
43: ; preds = %41, %5
%44 = phi i32 [ 0, %41 ], [ 1, %5 ]
ret i32 %44
}