#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 {
  int /*<<< orphan*/ ut_time;
  int /*<<< orphan*/ ut_type;
  int /*<<< orphan*/ ut_line;
};

/* Variables and functions */
int /*<<< orphan*/ DEAD_PROCESS;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ SEEK_CUR;
int /*<<< orphan*/ UTMP_FILE;
int close(int);
int lseek(int, int, int /*<<< orphan*/);
int open(int /*<<< orphan*/, int /*<<< orphan*/);
int read(int, struct utmp *, int);
scalar_t__ strncmp(int /*<<< orphan*/, char *, int);
int time(int /*<<< orphan*/ *);
int write(int, struct utmp *, int);
int logout_tty(char *line) {
  int fd;
  struct utmp rec;
  int len = sizeof(struct utmp);
  int retval = -1;
  if ((fd = open(UTMP_FILE, O_RDWR)) == -1)
    return -1;
  while (read(fd, &rec, len) == len) {
    if (strncmp(rec.ut_line, line, sizeof(rec.ut_line)) == 0) {
      rec.ut_type = DEAD_PROCESS;
      if (time(&rec.ut_time) == -1)
        break;
      if (lseek(fd, -len, SEEK_CUR) == -1)
        break;
      if (write(fd, &rec, len) == len)
        retval = 0;
      break;
    }
  }
  if (close(fd) == -1)
    retval = -1;
  return retval;
}


x86 assembly (clang, -O0):
<logout_tty>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0xc,-0x24(%rbp)
movl   $0xffffffff,-0x28(%rbp)
mov    0x2e5f(%rip),%rax
mov    (%rax),%edi
mov    0x2e46(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    11b9 <logout_tty+0x49>
movl   $0xffffffff,-0x4(%rbp)
jmp    127f <logout_tty+0x10f>
jmp    11be <logout_tty+0x4e>
mov    -0x14(%rbp),%edi
mov    -0x24(%rbp),%edx
lea    -0x20(%rbp),%rsi
call   1070 <read@plt>
cmp    -0x24(%rbp),%eax
jne    1261 <logout_tty+0xf1>
mov    -0x18(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x4,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
jne    125c <logout_tty+0xec>
mov    0x2dd0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
lea    -0x20(%rbp),%rdi
call   1080 <time@plt>
cmp    $0xffffffff,%eax
jne    1214 <logout_tty+0xa4>
jmp    1261 <logout_tty+0xf1>
mov    -0x14(%rbp),%edi
xor    %esi,%esi
sub    -0x24(%rbp),%esi
mov    0x2db5(%rip),%rax
mov    (%rax),%edx
call   1050 <lseek@plt>
cmp    $0xffffffff,%eax
jne    1238 <logout_tty+0xc8>
jmp    1261 <logout_tty+0xf1>
mov    -0x14(%rbp),%edi
mov    -0x24(%rbp),%edx
lea    -0x20(%rbp),%rsi
call   1040 <write@plt>
cmp    -0x24(%rbp),%eax
jne    1257 <logout_tty+0xe7>
movl   $0x0,-0x28(%rbp)
jmp    1261 <logout_tty+0xf1>
jmp    11be <logout_tty+0x4e>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
cmp    $0xffffffff,%eax
jne    1279 <logout_tty+0x109>
movl   $0xffffffff,-0x28(%rbp)
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<logout_tty>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%rbx
mov    0x2e6c(%rip),%rax
mov    (%rax),%edi
mov    0x2e53(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
cmp    $0xffffffff,%eax
je     123f <logout_tty+0xcf>
mov    %eax,%r14d
lea    0x8(%rsp),%r15
mov    $0xffffffff,%ebp
nopw   0x0(%rax,%rax,1)
mov    %r14d,%edi
mov    %r15,%rsi
mov    $0xc,%edx
call   1070 <read@plt>
cmp    $0xc,%eax
jne    122f <logout_tty+0xbf>
mov    0x10(%rsp),%edi
mov    %rbx,%rsi
mov    $0x4,%edx
call   1030 <strncmp@plt>
test   %rax,%rax
jne    11b0 <logout_tty+0x40>
mov    0x2de6(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
lea    0x8(%rsp),%rdi
call   1080 <time@plt>
cmp    $0xffffffff,%eax
je     122f <logout_tty+0xbf>
mov    0x2dda(%rip),%rax
mov    (%rax),%edx
mov    %r14d,%edi
mov    $0xfffffff4,%esi
call   1050 <lseek@plt>
cmp    $0xffffffff,%eax
je     122f <logout_tty+0xbf>
lea    0x8(%rsp),%rsi
mov    %r14d,%edi
mov    $0xc,%edx
call   1040 <write@plt>
xor    %ebp,%ebp
cmp    $0xc,%eax
setne  %bpl
neg    %ebp
mov    %r14d,%edi
call   1060 <close@plt>
cmp    $0xffffffff,%eax
cmove  %eax,%ebp
jmp    1244 <logout_tty+0xd4>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.utmp = type { i32, i32, i32 }
@UTMP_FILE = global i32 0
@O_RDWR = global i32 0
@DEAD_PROCESS = global i32 0
@SEEK_CUR = global i32 0
define i32 @logout_tty(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca %struct.utmp
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %3
store i32 12, i32* %6
store i32 -1, i32* %7
%8 = load i32, i32* @UTMP_FILE
%9 = load i32, i32* @O_RDWR
%10 = call i32 @open(i32 %8, i32 %9)
store i32 %10, i32* %4
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %13
12: ; preds = %1
store i32 -1, i32* %2
br label %57
13: ; preds = %1
br label %14
14: ; preds = %49, %13
%15 = load i32, i32* %4
%16 = load i32, i32* %6
%17 = call i32 @read(i32 %15, %struct.utmp* %5, i32 %16)
%18 = load i32, i32* %6
%19 = icmp eq i32 %17, %18
br i1 %19, label %20, label %50
20: ; preds = %14
%21 = getelementptr inbounds %struct.utmp, %struct.utmp* %5, i32 0, i32 2
%22 = load i32, i32* %21
%23 = load i8*, i8** %3
%24 = call i64 @strncmp(i32 %22, i8* %23, i32 4)
%25 = icmp eq i64 %24, 0
br i1 %25, label %26, label %49
26: ; preds = %20
%27 = load i32, i32* @DEAD_PROCESS
%28 = getelementptr inbounds %struct.utmp, %struct.utmp* %5, i32 0, i32 1
store i32 %27, i32* %28
%29 = getelementptr inbounds %struct.utmp, %struct.utmp* %5, i32 0, i32 0
%30 = call i32 @time(i32* %29)
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %33
32: ; preds = %26
br label %50
33: ; preds = %26
%34 = load i32, i32* %4
%35 = load i32, i32* %6
%36 = sub nsw i32 0, %35
%37 = load i32, i32* @SEEK_CUR
%38 = call i32 @lseek(i32 %34, i32 %36, i32 %37)
%39 = icmp eq i32 %38, -1
br i1 %39, label %40, label %41
40: ; preds = %33
br label %50
41: ; preds = %33
%42 = load i32, i32* %4
%43 = load i32, i32* %6
%44 = call i32 @write(i32 %42, %struct.utmp* %5, i32 %43)
%45 = load i32, i32* %6
%46 = icmp eq i32 %44, %45
br i1 %46, label %47, label %48
47: ; preds = %41
store i32 0, i32* %7
br label %48
48: ; preds = %47, %41
br label %50
49: ; preds = %20
br label %14
50: ; preds = %48, %40, %32, %14
%51 = load i32, i32* %4
%52 = call i32 @close(i32 %51)
%53 = icmp eq i32 %52, -1
br i1 %53, label %54, label %55
54: ; preds = %50
store i32 -1, i32* %7
br label %55
55: ; preds = %54, %50
%56 = load i32, i32* %7
store i32 %56, i32* %2
br label %57
57: ; preds = %55, %12
%58 = load i32, i32* %2
ret i32 %58
}

LLVM IR (-O2):
%struct.utmp = type { i32, i32, i32 }
@UTMP_FILE = global i32 0
@O_RDWR = global i32 0
@DEAD_PROCESS = global i32 0
@SEEK_CUR = global i32 0
define i32 @logout_tty(i8* %0) {
%2 = alloca %struct.utmp
%3 = bitcast %struct.utmp* %2 to i8*
%4 = load i32, i32* @UTMP_FILE
%5 = load i32, i32* @O_RDWR
%6 = tail call i32 @open(i32 %4, i32 %5)
%7 = icmp eq i32 %6, -1
br i1 %7, label %36, label %8
8: ; preds = %1
%9 = getelementptr inbounds %struct.utmp, %struct.utmp* %2, i64 0, i32 2
br label %10
10: ; preds = %8, %13
%11 = call i32 @read(i32 %6, %struct.utmp* %2, i32 12)
%12 = icmp eq i32 %11, 12
br i1 %12, label %13, label %31
13: ; preds = %10
%14 = load i32, i32* %9
%15 = tail call i64 @strncmp(i32 %14, i8* %0, i32 4)
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %10
17: ; preds = %13
%18 = load i32, i32* @DEAD_PROCESS
%19 = getelementptr inbounds %struct.utmp, %struct.utmp* %2, i64 0, i32 1
store i32 %18, i32* %19
%20 = getelementptr inbounds %struct.utmp, %struct.utmp* %2, i64 0, i32 0
%21 = call i32 @time(i32* %20)
%22 = icmp eq i32 %21, -1
br i1 %22, label %31, label %23
23: ; preds = %17
%24 = load i32, i32* @SEEK_CUR
%25 = call i32 @lseek(i32 %6, i32 -12, i32 %24)
%26 = icmp eq i32 %25, -1
br i1 %26, label %31, label %27
27: ; preds = %23
%28 = call i32 @write(i32 %6, %struct.utmp* %2, i32 12)
%29 = icmp ne i32 %28, 12
%30 = sext i1 %29 to i32
br label %31
31: ; preds = %10, %27, %23, %17
%32 = phi i32 [ -1, %17 ], [ -1, %23 ], [ %30, %27 ], [ -1, %10 ]
%33 = call i32 @close(i32 %6)
%34 = icmp eq i32 %33, -1
%35 = select i1 %34, i32 -1, i32 %32
br label %36
36: ; preds = %1, %31
%37 = phi i32 [ %35, %31 ], [ -1, %1 ]
ret i32 %37
}