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

/* Variables and functions */
scalar_t__ USER_PROCESS;
int /*<<< orphan*/ endutxent();
struct utmpx *getutxline(struct utmpx *);
int /*<<< orphan*/ setutxent();
scalar_t__ strcmp(char *, int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char *, int);
int utmp_chk(char *user, char *tty) {
  struct utmpx lu, *u;
  strncpy(lu.ut_line, tty, sizeof lu.ut_line);
  setutxent();
  while ((u = getutxline(&lu)) != NULL)
    if (u->ut_type == USER_PROCESS && strcmp(user, u->ut_user) == 0) {
      endutxent();
      return (0);
    }
  endutxent();
  return (1);
}


x86 assembly (clang, -O0):
<utmp_chk>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x1c(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    $0x4,%edx
call   1030 <strncpy@plt>
mov    $0x0,%al
call   1070 <setutxent@plt>
lea    -0x28(%rbp),%rdi
call   1040 <getutxline@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
je     11d8 <utmp_chk+0x88>
mov    -0x30(%rbp),%rax
mov    (%rax),%rax
mov    0x2e4b(%rip),%rcx
cmp    (%rcx),%rax
jne    11d3 <utmp_chk+0x83>
mov    -0x10(%rbp),%rdi
mov    -0x30(%rbp),%rax
mov    0x8(%rax),%esi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
jne    11d3 <utmp_chk+0x83>
mov    $0x0,%al
call   1050 <endutxent@plt>
movl   $0x0,-0x4(%rbp)
jmp    11e6 <utmp_chk+0x96>
jmp    1178 <utmp_chk+0x28>
mov    $0x0,%al
call   1050 <endutxent@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<utmp_chk>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%r15
mov    $0x4,%edx
call   1030 <strncpy@plt>
xor    %eax,%eax
call   1070 <setutxent@plt>
lea    0x8(%rsp),%rdi
call   1040 <getutxline@plt>
mov    $0x1,%r14d
test   %rax,%rax
je     11c8 <utmp_chk+0x78>
mov    0x2e5e(%rip),%rbx
lea    0x8(%rsp),%rbp
jmp    11ad <utmp_chk+0x5d>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %rbp,%rdi
call   1040 <getutxline@plt>
test   %rax,%rax
je     11c8 <utmp_chk+0x78>
mov    (%rax),%rcx
cmp    (%rbx),%rcx
jne    11a0 <utmp_chk+0x50>
mov    0x8(%rax),%esi
mov    %r15,%rdi
call   1060 <strcmp@plt>
test   %rax,%rax
jne    11a0 <utmp_chk+0x50>
xor    %r14d,%r14d
xor    %eax,%eax
call   1050 <endutxent@plt>
mov    %r14d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.utmpx = type { i64, i32, i32 }
@USER_PROCESS = global i64 0
define i32 @utmp_chk(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca %struct.utmpx
%7 = alloca %struct.utmpx*
store i8* %0, i8** %4
store i8* %1, i8** %5
%8 = getelementptr inbounds %struct.utmpx, %struct.utmpx* %6, i32 0, i32 2
%9 = load i32, i32* %8
%10 = load i8*, i8** %5
%11 = call i32 @strncpy(i32 %9, i8* %10, i32 4)
%12 = call i32 (...) @setutxent()
br label %13
13: ; preds = %31, %2
%14 = call %struct.utmpx* @getutxline(%struct.utmpx* %6)
store %struct.utmpx* %14, %struct.utmpx** %7
%15 = icmp ne %struct.utmpx* %14, null
br i1 %15, label %16, label %32
16: ; preds = %13
%17 = load %struct.utmpx*, %struct.utmpx** %7
%18 = getelementptr inbounds %struct.utmpx, %struct.utmpx* %17, i32 0, i32 0
%19 = load i64, i64* %18
%20 = load i64, i64* @USER_PROCESS
%21 = icmp eq i64 %19, %20
br i1 %21, label %22, label %31
22: ; preds = %16
%23 = load i8*, i8** %4
%24 = load %struct.utmpx*, %struct.utmpx** %7
%25 = getelementptr inbounds %struct.utmpx, %struct.utmpx* %24, i32 0, i32 1
%26 = load i32, i32* %25
%27 = call i64 @strcmp(i8* %23, i32 %26)
%28 = icmp eq i64 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %22
%30 = call i32 (...) @endutxent()
store i32 0, i32* %3
br label %34
31: ; preds = %22, %16
br label %13
32: ; preds = %13
%33 = call i32 (...) @endutxent()
store i32 1, i32* %3
br label %34
34: ; preds = %32, %29
%35 = load i32, i32* %3
ret i32 %35
}

LLVM IR (-O2):
%struct.utmpx = type { i64, i32, i32 }
@USER_PROCESS = global i64 0
define i32 @utmp_chk(i8* %0, i8* %1) {
%3 = alloca %struct.utmpx
%4 = bitcast %struct.utmpx* %3 to i8*
%5 = tail call i32 @strncpy(i32 undef, i8* %1, i32 4)
%6 = tail call i32 (...) @setutxent()
%7 = call %struct.utmpx* @getutxline(%struct.utmpx* %3)
%8 = icmp eq %struct.utmpx* %7, null
br i1 %8, label %23, label %9
9: ; preds = %2, %20
%10 = phi %struct.utmpx* [ %21, %20 ], [ %7, %2 ]
%11 = getelementptr inbounds %struct.utmpx, %struct.utmpx* %10, i64 0, i32 0
%12 = load i64, i64* %11
%13 = load i64, i64* @USER_PROCESS
%14 = icmp eq i64 %12, %13
br i1 %14, label %15, label %20
15: ; preds = %9
%16 = getelementptr inbounds %struct.utmpx, %struct.utmpx* %10, i64 0, i32 1
%17 = load i32, i32* %16
%18 = call i64 @strcmp(i8* %0, i32 %17)
%19 = icmp eq i64 %18, 0
br i1 %19, label %23, label %20
20: ; preds = %15, %9
%21 = call %struct.utmpx* @getutxline(%struct.utmpx* %3)
%22 = icmp eq %struct.utmpx* %21, null
br i1 %22, label %23, label %9
23: ; preds = %20, %15, %2
%24 = phi i32 [ 1, %2 ], [ 0, %15 ], [ 1, %20 ]
%25 = call i32 (...) @endutxent()
ret i32 %24
}