#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 */
typedef int /*<<< orphan*/ u;
struct utmp {
  int /*<<< orphan*/ ut_line;
  int /*<<< orphan*/ ut_name;
};

/* Variables and functions */
int /*<<< orphan*/ endutent();
struct utmp *getutent();
int /*<<< orphan*/ memcpy(struct utmp *, struct utmp *, int);
int /*<<< orphan*/ setutent();
scalar_t__ strncmp(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ utmpname(char *);
int utmp_chk(char *user, char *tty) {
  struct utmp u;
  struct utmp *uptr;
  int res = 1;
  utmpname("/var/run/utmp");
  setutent();
  while ((uptr = getutent())) {
    memcpy(&u, uptr, sizeof(u));
    if (strncmp(user, u.ut_name, sizeof(u.ut_name)) == 0 &&
        strncmp(tty, u.ut_line, sizeof(u.ut_line)) == 0) {
      res = 0;
      break;
    }
  }
  endutent();
  return (res);
}


x86 assembly (clang, -O0):
<utmp_chk>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x1,-0x24(%rbp)
lea    0xe82(%rip),%rdi
call   1040 <utmpname@plt>
mov    $0x0,%al
call   1080 <setutent@plt>
mov    $0x0,%al
call   1070 <getutent@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     11f8 <utmp_chk+0x98>
mov    -0x20(%rbp),%rsi
lea    -0x18(%rbp),%rdi
mov    $0x8,%edx
call   1060 <memcpy@plt>
mov    -0x8(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    $0x4,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
jne    11f3 <utmp_chk+0x93>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%esi
mov    $0x4,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
jne    11f3 <utmp_chk+0x93>
movl   $0x0,-0x24(%rbp)
jmp    11f8 <utmp_chk+0x98>
jmp    118a <utmp_chk+0x2a>
mov    $0x0,%al
call   1050 <endutent@plt>
mov    -0x24(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<utmp_chk>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
lea    0xe8c(%rip),%rdi
call   1040 <utmpname@plt>
xor    %eax,%eax
call   1080 <setutent@plt>
xor    %eax,%eax
call   1070 <getutent@plt>
mov    $0x1,%r15d
test   %rax,%rax
je     11ea <utmp_chk+0x8a>
mov    %rsp,%rbp
jmp    11ac <utmp_chk+0x4c>
nopw   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1070 <getutent@plt>
test   %rax,%rax
je     11ea <utmp_chk+0x8a>
mov    %rbp,%rdi
mov    %rax,%rsi
mov    $0x8,%edx
call   1060 <memcpy@plt>
mov    0x4(%rsp),%esi
mov    %rbx,%rdi
mov    $0x4,%edx
call   1030 <strncmp@plt>
test   %rax,%rax
jne    11a0 <utmp_chk+0x40>
mov    (%rsp),%esi
mov    %r14,%rdi
mov    $0x4,%edx
call   1030 <strncmp@plt>
test   %rax,%rax
jne    11a0 <utmp_chk+0x40>
xor    %r15d,%r15d
xor    %eax,%eax
call   1050 <endutent@plt>
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.utmp = type { i32, i32 }
@.str = [14 x i8] c"/var/run/utmp\00"
define i32 @utmp_chk(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca %struct.utmp
%6 = alloca %struct.utmp*
%7 = alloca i32
store i8* %0, i8** %3
store i8* %1, i8** %4
store i32 1, i32* %7
%8 = call i32 @utmpname(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%9 = call i32 (...) @setutent()
br label %10
10: ; preds = %28, %2
%11 = call %struct.utmp* (...) @getutent()
store %struct.utmp* %11, %struct.utmp** %6
%12 = icmp ne %struct.utmp* %11, null
br i1 %12, label %13, label %29
13: ; preds = %10
%14 = load %struct.utmp*, %struct.utmp** %6
%15 = call i32 @memcpy(%struct.utmp* %5, %struct.utmp* %14, i32 8)
%16 = load i8*, i8** %3
%17 = getelementptr inbounds %struct.utmp, %struct.utmp* %5, i32 0, i32 1
%18 = load i32, i32* %17
%19 = call i64 @strncmp(i8* %16, i32 %18, i32 4)
%20 = icmp eq i64 %19, 0
br i1 %20, label %21, label %28
21: ; preds = %13
%22 = load i8*, i8** %4
%23 = getelementptr inbounds %struct.utmp, %struct.utmp* %5, i32 0, i32 0
%24 = load i32, i32* %23
%25 = call i64 @strncmp(i8* %22, i32 %24, i32 4)
%26 = icmp eq i64 %25, 0
br i1 %26, label %27, label %28
27: ; preds = %21
store i32 0, i32* %7
br label %29
28: ; preds = %21, %13
br label %10
29: ; preds = %27, %10
%30 = call i32 (...) @endutent()
%31 = load i32, i32* %7
ret i32 %31
}

LLVM IR (-O2):
%struct.utmp = type { i32, i32 }
@.str = [14 x i8] c"/var/run/utmp\00"
define i32 @utmp_chk(i8* %0, i8* %1) {
%3 = alloca %struct.utmp
%4 = bitcast %struct.utmp* %3 to i8*
%5 = tail call i32 @utmpname(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%6 = tail call i32 (...) @setutent()
%7 = getelementptr inbounds %struct.utmp, %struct.utmp* %3, i64 0, i32 0
%8 = call %struct.utmp* (...) @getutent()
%9 = icmp eq %struct.utmp* %8, null
br i1 %9, label %25, label %10
10: ; preds = %2
%11 = getelementptr inbounds %struct.utmp, %struct.utmp* %3, i64 0, i32 1
br label %12
12: ; preds = %10, %22
%13 = phi %struct.utmp* [ %8, %10 ], [ %23, %22 ]
%14 = call i32 @memcpy(%struct.utmp* %3, %struct.utmp* %13, i32 8)
%15 = load i32, i32* %11
%16 = call i64 @strncmp(i8* %0, i32 %15, i32 4)
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %22
18: ; preds = %12
%19 = load i32, i32* %7
%20 = call i64 @strncmp(i8* %1, i32 %19, i32 4)
%21 = icmp eq i64 %20, 0
br i1 %21, label %25, label %22
22: ; preds = %18, %12
%23 = call %struct.utmp* (...) @getutent()
%24 = icmp eq %struct.utmp* %23, null
br i1 %24, label %25, label %12
25: ; preds = %22, %18, %2
%26 = phi i32 [ 1, %2 ], [ 0, %18 ], [ 1, %22 ]
%27 = call i32 (...) @endutent()
ret i32 %26
}