#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_line;
  char *ut_name;
  char *ut_host;
  int /*<<< orphan*/ ut_time;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ SEEK_CUR;
int /*<<< orphan*/ SEEK_END;
char *ctime(int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ ferror(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int fread(struct utmp *, int, int, int /*<<< orphan*/ *);
scalar_t__ fseek(int /*<<< orphan*/ *, int, int /*<<< orphan*/);
char *ftell(int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ stderr;
int main() {
  int nbytes;
  struct utmp u;
  FILE *utfd;
  printf("sizeof(struct utmp): %d\n", sizeof(struct utmp));
  if ((utfd = fopen("/var/log/wtmp", "r")) < 0) {
    perror("Couldn't open utmp file");
    exit(1);
  }
  if (fseek(utfd, -sizeof(struct utmp), SEEK_END) < 0) {
    perror("Can't rewind into file!");
    exit(1);
  }
  do {
    printf("File pos after SEEK_CUR: %ld\n", ftell(utfd));
    nbytes = fread(&u, 1, sizeof(struct utmp), utfd);
    if (nbytes < sizeof(struct utmp)) {
      perror("Problem reading from file");
      fprintf(stderr, "Nbytes: %d\n", nbytes);
      fprintf(stderr, "problem reading utmp structure - truncated.\n");
      exit(1);
    } else {
      printf("ut_line:  %s\n", u.ut_line);
      printf("ut_name:  %s\n", u.ut_name);
      printf("ut_host:  %s\n", u.ut_host);
      printf("ut_time:  %s\n", ctime(&u.ut_time));
    }
  } while (fseek(utfd, -2 * sizeof(struct utmp), SEEK_CUR) == 0 &&
           !ferror(utfd));
  fclose(utfd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe3a(%rip),%rdi
mov    $0x20,%esi
mov    $0x0,%al
call   1070 <printf@plt>
lea    0xe40(%rip),%rdi
lea    0xe47(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
jae    1209 <main+0x59>
lea    0xe2f(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    -0x30(%rbp),%rdi
mov    0x2dd4(%rip),%rax
mov    (%rax),%edx
mov    $0xffffffe0,%esi
call   10a0 <fseek@plt>
cmp    $0x0,%rax
jge    1240 <main+0x90>
lea    0xe10(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
jmp    1245 <main+0x95>
mov    -0x30(%rbp),%rdi
call   1090 <ftell@plt>
mov    %rax,%rsi
lea    0xe01(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x30(%rbp),%rcx
lea    -0x28(%rbp),%rdi
mov    $0x1,%esi
mov    $0x20,%edx
call   1040 <fread@plt>
mov    %eax,-0x8(%rbp)
movslq -0x8(%rbp),%rax
cmp    $0x20,%rax
jae    12ce <main+0x11e>
lea    0xde9(%rip),%rdi
call   10c0 <perror@plt>
mov    0x2d36(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%edx
lea    0xdeb(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    0x2d1c(%rip),%rax
mov    (%rax),%edi
lea    0xde0(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    -0x28(%rbp),%rsi
lea    0xdf1(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x20(%rbp),%rsi
lea    0xded(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x18(%rbp),%rsi
lea    0xde9(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
lea    -0x28(%rbp),%rdi
add    $0x18,%rdi
call   1060 <ctime@plt>
mov    %rax,%rsi
lea    0xdd9(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
jmp    1327 <main+0x177>
mov    -0x30(%rbp),%rdi
mov    0x2cae(%rip),%rax
mov    (%rax),%edx
mov    $0xffffffc0,%esi
call   10a0 <fseek@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    $0x0,%rcx
mov    %al,-0x31(%rbp)
jne    1364 <main+0x1b4>
mov    -0x30(%rbp),%rdi
call   1030 <ferror@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
mov    %al,-0x31(%rbp)
mov    -0x31(%rbp),%al
test   $0x1,%al
jne    1245 <main+0x95>
mov    -0x30(%rbp),%rdi
call   1050 <fclose@plt>
xor    %eax,%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    $0x28,%rsp
lea    0xe3b(%rip),%rdi
mov    $0x20,%esi
xor    %eax,%eax
call   1070 <printf@plt>
lea    0xe41(%rip),%rdi
lea    0xe48(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,%rbx
mov    0x2dfa(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
mov    $0xffffffe0,%esi
call   10a0 <fseek@plt>
test   %rax,%rax
js     1333 <main+0x183>
lea    0xeb3(%rip),%rbp
lea    0xeba(%rip),%r14
lea    0xec1(%rip),%r15
mov    0x2dbe(%rip),%r13
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %rbx,%rdi
call   1090 <ftell@plt>
lea    0xe02(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
lea    0x8(%rsp),%rdi
mov    $0x1,%esi
mov    $0x20,%edx
mov    %rbx,%rcx
call   1040 <fread@plt>
cmp    $0x1f,%eax
jbe    12f0 <main+0x140>
mov    0x8(%rsp),%rsi
lea    0xe3d(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
mov    0x10(%rsp),%rsi
mov    %rbp,%rdi
xor    %eax,%eax
call   1070 <printf@plt>
mov    0x18(%rsp),%rsi
mov    %r14,%rdi
xor    %eax,%eax
call   1070 <printf@plt>
lea    0x20(%rsp),%rdi
call   1060 <ctime@plt>
mov    %r15,%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
mov    0x0(%r13),%edx
mov    %rbx,%rdi
mov    $0xffffffc0,%esi
call   10a0 <fseek@plt>
test   %rax,%rax
jne    12d7 <main+0x127>
mov    %rbx,%rdi
call   1030 <ferror@plt>
test   %eax,%eax
je     1230 <main+0x80>
mov    %rbx,%rdi
call   1050 <fclose@plt>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %eax,%r12d
lea    0xd65(%rip),%rdi
call   10c0 <perror@plt>
mov    0x2cca(%rip),%rbx
mov    (%rbx),%edi
lea    0xd6a(%rip),%rsi
mov    %r12d,%edx
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    (%rbx),%edi
lea    0xd63(%rip),%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
lea    0xcef(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
%struct.utmp = type { i8*, i8*, i8*, i32 }
@.str = [25 x i8] c"sizeof(struct utmp): %d\0A\00"
@.str.1 = [14 x i8] c"/var/log/wtmp\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [24 x i8] c"Couldn't open utmp file\00"
@SEEK_END = global i32 0
@.str.4 = [24 x i8] c"Can't rewind into file!\00"
@.str.5 = [30 x i8] c"File pos after SEEK_CUR: %ld\0A\00"
@.str.6 = [26 x i8] c"Problem reading from file\00"
@stderr = global i32 0
@.str.7 = [12 x i8] c"Nbytes: %d\0A\00"
@.str.8 = [45 x i8] c"problem reading utmp structure - truncated.\0A\00"
@.str.9 = [14 x i8] c"ut_line: %s\0A\00"
@.str.10 = [14 x i8] c"ut_name: %s\0A\00"
@.str.11 = [14 x i8] c"ut_host: %s\0A\00"
@.str.12 = [14 x i8] c"ut_time: %s\0A\00"
@SEEK_CUR = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca %struct.utmp
%4 = alloca i32*
store i32 0, i32* %1
%5 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i64 32)
%6 = call i32* @fopen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %6, i32** %4
%7 = icmp ult i32* %6, null
br i1 %7, label %8, label %11
8: ; preds = %0
%9 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0))
%10 = call i32 @exit(i32 1)
unreachable
11: ; preds = %0
%12 = load i32*, i32** %4
%13 = load i32, i32* @SEEK_END
%14 = call i64 @fseek(i32* %12, i32 -32, i32 %13)
%15 = icmp slt i64 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %11
%17 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0))
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %11
br label %20
20: ; preds = %61, %19
%21 = load i32*, i32** %4
%22 = call i8* @ftell(i32* %21)
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.5, i64 0, i64 0), i8* %22)
%24 = load i32*, i32** %4
%25 = call i32 @fread(%struct.utmp* %3, i32 1, i32 32, i32* %24)
store i32 %25, i32* %2
%26 = load i32, i32* %2
%27 = sext i32 %26 to i64
%28 = icmp ult i64 %27, 32
br i1 %28, label %29, label %37
29: ; preds = %20
%30 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.6, i64 0, i64 0))
%31 = load i32, i32* @stderr
%32 = load i32, i32* %2
%33 = call i32 (i32, i8*, ...) @fprintf(i32 %31, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.7, i64 0, i64 0), i32 %32)
%34 = load i32, i32* @stderr
%35 = call i32 (i32, i8*, ...) @fprintf(i32 %34, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.8, i64 0, i64 0))
%36 = call i32 @exit(i32 1)
unreachable
37: ; preds = %20
%38 = getelementptr inbounds %struct.utmp, %struct.utmp* %3, i32 0, i32 0
%39 = load i8*, i8** %38
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.9, i64 0, i64 0), i8* %39)
%41 = getelementptr inbounds %struct.utmp, %struct.utmp* %3, i32 0, i32 1
%42 = load i8*, i8** %41
%43 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.10, i64 0, i64 0), i8* %42)
%44 = getelementptr inbounds %struct.utmp, %struct.utmp* %3, i32 0, i32 2
%45 = load i8*, i8** %44
%46 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.11, i64 0, i64 0), i8* %45)
%47 = getelementptr inbounds %struct.utmp, %struct.utmp* %3, i32 0, i32 3
%48 = call i8* @ctime(i32* %47)
%49 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.12, i64 0, i64 0), i8* %48)
br label %50
50: ; preds = %37
br label %51
51: ; preds = %50
%52 = load i32*, i32** %4
%53 = load i32, i32* @SEEK_CUR
%54 = call i64 @fseek(i32* %52, i32 -64, i32 %53)
%55 = icmp eq i64 %54, 0
br i1 %55, label %56, label %61
56: ; preds = %51
%57 = load i32*, i32** %4
%58 = call i32 @ferror(i32* %57)
%59 = icmp ne i32 %58, 0
%60 = xor i1 %59, true
br label %61
61: ; preds = %56, %51
%62 = phi i1 [ false, %51 ], [ %60, %56 ]
br i1 %62, label %20, label %63
63: ; preds = %61
%64 = load i32*, i32** %4
%65 = call i32 @fclose(i32* %64)
ret i32 0
}

LLVM IR (-O2):
%struct.utmp = type { i8*, i8*, i8*, i32 }
@.str = [25 x i8] c"sizeof(struct utmp): %d\0A\00"
@.str.1 = [14 x i8] c"/var/log/wtmp\00"
@.str.2 = [2 x i8] c"r\00"
@SEEK_END = global i32 0
@.str.4 = [24 x i8] c"Can't rewind into file!\00"
@.str.5 = [30 x i8] c"File pos after SEEK_CUR: %ld\0A\00"
@.str.6 = [26 x i8] c"Problem reading from file\00"
@stderr = global i32 0
@.str.7 = [12 x i8] c"Nbytes: %d\0A\00"
@.str.8 = [45 x i8] c"problem reading utmp structure - truncated.\0A\00"
@.str.9 = [14 x i8] c"ut_line: %s\0A\00"
@.str.10 = [14 x i8] c"ut_name: %s\0A\00"
@.str.11 = [14 x i8] c"ut_host: %s\0A\00"
@.str.12 = [14 x i8] c"ut_time: %s\0A\00"
@SEEK_CUR = global i32 0
define i32 @main() {
%1 = alloca %struct.utmp
%2 = bitcast %struct.utmp* %1 to i8*
%3 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i64 32)
%4 = tail call i32* @fopen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%5 = load i32, i32* @SEEK_END
%6 = tail call i64 @fseek(i32* %4, i32 -32, i32 %5)
%7 = icmp slt i64 %6, 0
br i1 %7, label %13, label %8
8: ; preds = %0
%9 = getelementptr inbounds %struct.utmp, %struct.utmp* %1, i64 0, i32 0
%10 = getelementptr inbounds %struct.utmp, %struct.utmp* %1, i64 0, i32 1
%11 = getelementptr inbounds %struct.utmp, %struct.utmp* %1, i64 0, i32 2
%12 = getelementptr inbounds %struct.utmp, %struct.utmp* %1, i64 0, i32 3
br label %16
13: ; preds = %0
%14 = tail call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0))
%15 = tail call i32 @exit(i32 1)
unreachable
16: ; preds = %8, %40
%17 = call i8* @ftell(i32* %4)
%18 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str.5, i64 0, i64 0), i8* %17)
%19 = call i32 @fread(%struct.utmp* %1, i32 1, i32 32, i32* %4)
%20 = icmp ult i32 %19, 32
br i1 %20, label %21, label %28
21: ; preds = %16
%22 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.6, i64 0, i64 0))
%23 = load i32, i32* @stderr
%24 = call i32 (i32, i8*, ...) @fprintf(i32 %23, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.7, i64 0, i64 0), i32 %19)
%25 = load i32, i32* @stderr
%26 = call i32 (i32, i8*, ...) @fprintf(i32 %25, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.8, i64 0, i64 0))
%27 = call i32 @exit(i32 1)
unreachable
28: ; preds = %16
%29 = load i8*, i8** %9
%30 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.9, i64 0, i64 0), i8* %29)
%31 = load i8*, i8** %10
%32 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.10, i64 0, i64 0), i8* %31)
%33 = load i8*, i8** %11
%34 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.11, i64 0, i64 0), i8* %33)
%35 = call i8* @ctime(i32* %12)
%36 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.12, i64 0, i64 0), i8* %35)
%37 = load i32, i32* @SEEK_CUR
%38 = call i64 @fseek(i32* %4, i32 -64, i32 %37)
%39 = icmp eq i64 %38, 0
br i1 %39, label %40, label %43
40: ; preds = %28
%41 = call i32 @ferror(i32* %4)
%42 = icmp eq i32 %41, 0
br i1 %42, label %16, label %43
43: ; preds = %28, %40
%44 = call i32 @fclose(i32* %4)
ret i32 0
}