#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 {
  char *ut_name;
  int ut_time;
};

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ _PATH_WTMP;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int open(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, char *, int);
int read(int, struct utmp *, int);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  int fd = open(_PATH_WTMP, O_RDONLY);
  if (fd == -1) {
    fprintf(stderr, "Error opening file: %s", strerror(errno));
    return 1;
  }
  do {
    struct utmp record;
    int nbytes = read(fd, &record, sizeof(struct utmp));
    if (nbytes == -1) {
      fprintf(stderr, "Error reading from file: %s", strerror(errno));
      break;
    } else if (nbytes == 0) {
      break;
    }
    printf("Login/logout from user %s at %d\n", record.ut_name, record.ut_time);
  } while (1);
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e53(%rip),%rax
mov    (%rax),%edi
mov    0x2e62(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11d2 <main+0x72>
mov    0x2e37(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    0x2e1b(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x30(%rbp),%edi
mov    %rax,%rdx
lea    0xe3f(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    126b <main+0x10b>
jmp    11d7 <main+0x77>
mov    -0x14(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0x10,%edx
call   1050 <read@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0xffffffff,-0x2c(%rbp)
jne    1226 <main+0xc6>
mov    0x2ddc(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    0x2dc0(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x34(%rbp),%edi
mov    %rax,%rdx
lea    0xdfb(%rip),%rsi
call   1060 <fprintf@plt>
jmp    125c <main+0xfc>
cmpl   $0x0,-0x2c(%rbp)
jne    1235 <main+0xd5>
jmp    125c <main+0xfc>
jmp    123a <main+0xda>
mov    -0x28(%rbp),%rsi
mov    -0x20(%rbp),%edx
lea    0xdeb(%rip),%rdi
call   1030 <printf@plt>
mov    $0x1,%al
test   $0x1,%al
jne    11d7 <main+0x77>
jmp    125c <main+0xfc>
mov    -0x14(%rbp),%edi
call   1040 <close@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   %rbx
sub    $0x18,%rsp
mov    0x2e5f(%rip),%rax
mov    (%rax),%edi
mov    0x2e6e(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     11cb <main+0x6b>
mov    %eax,%ebx
lea    0x8(%rsp),%r14
lea    0xe9f(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x10,%edx
call   1050 <read@plt>
test   %eax,%eax
je     1222 <main+0xc2>
cmp    $0xffffffff,%eax
je     11fa <main+0x9a>
mov    0x8(%rsp),%rsi
mov    0x10(%rsp),%edx
mov    %r15,%rdi
call   1030 <printf@plt>
jmp    11a0 <main+0x40>
mov    0x2e06(%rip),%rax
mov    (%rax),%ebx
mov    0x2ded(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xe17(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1060 <fprintf@plt>
mov    $0x1,%eax
jmp    122b <main+0xcb>
mov    0x2dd7(%rip),%rax
mov    (%rax),%ebp
mov    0x2dbe(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xdff(%rip),%rsi
mov    %ebp,%edi
mov    %rax,%rdx
call   1060 <fprintf@plt>
mov    %ebx,%edi
call   1040 <close@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.utmp = type { i8*, i32 }
@_PATH_WTMP = global i32 0
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str = [23 x i8] c"Error opening file: %s\00"
@errno = global i32 0
@.str.1 = [28 x i8] c"Error reading from file: %s\00"
@.str.2 = [33 x i8] c"Login/logout from user %s at %d\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca %struct.utmp
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* @_PATH_WTMP
%10 = load i32, i32* @O_RDONLY
%11 = call i32 @open(i32 %9, i32 %10)
store i32 %11, i32* %6
%12 = load i32, i32* %6
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %19
14: ; preds = %2
%15 = load i32, i32* @stderr
%16 = load i32, i32* @errno
%17 = call i8* @strerror(i32 %16)
%18 = call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* %17)
store i32 1, i32* %3
br label %45
19: ; preds = %2
br label %20
20: ; preds = %41, %19
%21 = load i32, i32* %6
%22 = call i32 @read(i32 %21, %struct.utmp* %7, i32 16)
store i32 %22, i32* %8
%23 = load i32, i32* %8
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %30
25: ; preds = %20
%26 = load i32, i32* @stderr
%27 = load i32, i32* @errno
%28 = call i8* @strerror(i32 %27)
%29 = call i32 @fprintf(i32 %26, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i8* %28)
br label %42
30: ; preds = %20
%31 = load i32, i32* %8
%32 = icmp eq i32 %31, 0
br i1 %32, label %33, label %34
33: ; preds = %30
br label %42
34: ; preds = %30
br label %35
35: ; preds = %34
%36 = getelementptr inbounds %struct.utmp, %struct.utmp* %7, i32 0, i32 0
%37 = load i8*, i8** %36
%38 = getelementptr inbounds %struct.utmp, %struct.utmp* %7, i32 0, i32 1
%39 = load i32, i32* %38
%40 = call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i8* %37, i32 %39)
br label %41
41: ; preds = %35
br i1 true, label %20, label %42
42: ; preds = %41, %33, %25
%43 = load i32, i32* %6
%44 = call i32 @close(i32 %43)
store i32 0, i32* %3
br label %45
45: ; preds = %42, %14
%46 = load i32, i32* %3
ret i32 %46
}

LLVM IR (-O2):
%struct.utmp = type { i8*, i32 }
@_PATH_WTMP = global i32 0
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str = [23 x i8] c"Error opening file: %s\00"
@errno = global i32 0
@.str.1 = [28 x i8] c"Error reading from file: %s\00"
@.str.2 = [33 x i8] c"Login/logout from user %s at %d\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca %struct.utmp
%4 = load i32, i32* @_PATH_WTMP
%5 = load i32, i32* @O_RDONLY
%6 = tail call i32 @open(i32 %4, i32 %5)
%7 = icmp eq i32 %6, -1
br i1 %7, label %12, label %8
8: ; preds = %2
%9 = bitcast %struct.utmp* %3 to i8*
%10 = getelementptr inbounds %struct.utmp, %struct.utmp* %3, i64 0, i32 0
%11 = getelementptr inbounds %struct.utmp, %struct.utmp* %3, i64 0, i32 1
br label %17
12: ; preds = %2
%13 = load i32, i32* @stderr
%14 = load i32, i32* @errno
%15 = tail call i8* @strerror(i32 %14)
%16 = tail call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* %15)
br label %30
17: ; preds = %8, %24
%18 = call i32 @read(i32 %6, %struct.utmp* %3, i32 16)
switch i32 %18, label %24 [
i32 -1, label %19
i32 0, label %28
]
19: ; preds = %17
%20 = load i32, i32* @stderr
%21 = load i32, i32* @errno
%22 = tail call i8* @strerror(i32 %21)
%23 = tail call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i8* %22)
br label %28
24: ; preds = %17
%25 = load i8*, i8** %10
%26 = load i32, i32* %11
%27 = tail call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i8* %25, i32 %26)
br label %17
28: ; preds = %17, %19
%29 = tail call i32 @close(i32 %6)
br label %30
30: ; preds = %28, %12
%31 = phi i32 [ 1, %12 ], [ 0, %28 ]
ret i32 %31
}