#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 scalar_t__ uid_t;
typedef scalar_t__ time_t;
struct utmpx {
  scalar_t__ ut_type;
  char *ut_user;
  char *ut_line;
};

/* Variables and functions */
int /*<<< orphan*/ MAXPATHLEN;
scalar_t__ USER_PROCESS;
int /*<<< orphan*/ endutxent();
int /*<<< orphan*/ errx(int, char *, char *);
struct utmpx *getutxent();
int /*<<< orphan*/ setutxent();
scalar_t__ strcmp(char *, char *);
int /*<<< orphan*/ strlcpy(char *, char *, int /*<<< orphan*/);
scalar_t__ term_chk(char *, int *, scalar_t__ *, int /*<<< orphan*/);
int /*<<< orphan*/ warnx(char *, char *, char *);
void search_utmp(char *user, char *tty, char *mytty, uid_t myuid) {
  struct utmpx *u;
  time_t bestatime, atime;
  int nloggedttys, nttys, msgsok, user_is_me;
  nloggedttys = nttys = 0;
  bestatime = 0;
  user_is_me = 0;
  setutxent();
  while ((u = getutxent()) != NULL)
    if (u->ut_type == USER_PROCESS && strcmp(user, u->ut_user) == 0) {
      ++nloggedttys;
      if (term_chk(u->ut_line, &msgsok, &atime, 0))
        continue;
      if (myuid && !msgsok)
        continue;
      if (strcmp(u->ut_line, mytty) == 0) {
        user_is_me = 1;
        continue;
      }
      ++nttys;
      if (atime > bestatime) {
        bestatime = atime;
        (void)strlcpy(tty, u->ut_line, MAXPATHLEN);
      }
    }
  endutxent();
  if (nloggedttys == 0)
    errx(1, "%s is not logged in", user);
  if (nttys == 0) {
    if (user_is_me) {
      (void)strlcpy(tty, mytty, MAXPATHLEN);
      return;
    }
    errx(1, "%s has messages disabled", user);
  } else if (nttys > 1) {
    warnx("%s is logged in more than once; writing to %s", user, tty);
  }
}


x86 assembly (clang, -O0):
<search_utmp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
movl   $0x0,-0x40(%rbp)
movl   $0x0,-0x3c(%rbp)
movq   $0x0,-0x30(%rbp)
movl   $0x0,-0x48(%rbp)
mov    $0x0,%al
call   1080 <setutxent@plt>
mov    $0x0,%al
call   10a0 <getutxent@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     12b6 <search_utmp+0x136>
mov    -0x28(%rbp),%rax
mov    (%rax),%rax
mov    0x2e09(%rip),%rcx
cmp    (%rcx),%rax
jne    12b1 <search_utmp+0x131>
mov    -0x8(%rbp),%rdi
mov    -0x28(%rbp),%rax
mov    0x8(%rax),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
jne    12b1 <search_utmp+0x131>
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
mov    -0x28(%rbp),%rax
mov    0x10(%rax),%rdi
lea    -0x44(%rbp),%rsi
lea    -0x38(%rbp),%rdx
xor    %ecx,%ecx
call   1040 <term_chk@plt>
cmp    $0x0,%rax
je     1232 <search_utmp+0xb2>
jmp    11bc <search_utmp+0x3c>
cmpq   $0x0,-0x20(%rbp)
je     124c <search_utmp+0xcc>
cmpl   $0x0,-0x44(%rbp)
jne    124c <search_utmp+0xcc>
jmp    11bc <search_utmp+0x3c>
mov    -0x28(%rbp),%rax
mov    0x10(%rax),%rdi
mov    -0x18(%rbp),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
jne    1273 <search_utmp+0xf3>
movl   $0x1,-0x48(%rbp)
jmp    11bc <search_utmp+0x3c>
mov    -0x40(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x40(%rbp)
mov    -0x38(%rbp),%rax
cmp    -0x30(%rbp),%rax
jle    12ac <search_utmp+0x12c>
mov    -0x38(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%rax
mov    0x10(%rax),%rsi
mov    0x2d3b(%rip),%rax
mov    (%rax),%edx
call   1090 <strlcpy@plt>
jmp    12b1 <search_utmp+0x131>
jmp    11bc <search_utmp+0x3c>
mov    $0x0,%al
call   1030 <endutxent@plt>
cmpl   $0x0,-0x3c(%rbp)
jne    12dc <search_utmp+0x15c>
mov    -0x8(%rbp),%rdx
mov    $0x1,%edi
lea    0xd29(%rip),%rsi
call   1050 <errx@plt>
cmpl   $0x0,-0x40(%rbp)
jne    1325 <search_utmp+0x1a5>
cmpl   $0x0,-0x48(%rbp)
je     130b <search_utmp+0x18b>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    0x2ce1(%rip),%rax
mov    (%rax),%edx
call   1090 <strlcpy@plt>
jmp    1348 <search_utmp+0x1c8>
mov    -0x8(%rbp),%rdx
mov    $0x1,%edi
lea    0xcf9(%rip),%rsi
call   1050 <errx@plt>
jmp    1348 <search_utmp+0x1c8>
cmpl   $0x1,-0x40(%rbp)
jle    1343 <search_utmp+0x1c3>
mov    -0x8(%rbp),%rsi
mov    -0x10(%rbp),%rdx
lea    0xcef(%rip),%rdi
call   1060 <warnx@plt>
jmp    1348 <search_utmp+0x1c8>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<search_utmp>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %rcx,0x28(%rsp)
mov    %rdx,0x20(%rsp)
mov    %rsi,0x8(%rsp)
mov    %rdi,%r12
xor    %eax,%eax
call   1080 <setutxent@plt>
xor    %eax,%eax
mov    %rax,0x18(%rsp)
xor    %eax,%eax
movl   $0x0,0x4(%rsp)
xor    %ecx,%ecx
mov    %eax,%r15d
mov    %r15d,%r13d
mov    %ecx,(%rsp)
xor    %eax,%eax
call   10a0 <getutxent@plt>
test   %rax,%rax
je     12cf <search_utmp+0x14f>
mov    %rax,%r14
mov    %r13d,%r15d
mov    0x28(%rsp),%r13
lea    0x14(%rsp),%rbx
mov    0x2dfe(%rip),%rbp
jmp    1207 <search_utmp+0x87>
nopl   0x0(%rax)
xor    %eax,%eax
call   10a0 <getutxent@plt>
mov    %rax,%r14
add    $0x1,%r15d
test   %rax,%rax
je     12d2 <search_utmp+0x152>
mov    (%r14),%rax
cmp    0x0(%rbp),%rax
jne    1270 <search_utmp+0xf0>
mov    0x8(%r14),%rsi
mov    %r12,%rdi
call   1070 <strcmp@plt>
test   %rax,%rax
jne    1280 <search_utmp+0x100>
mov    0x10(%r14),%rdi
mov    %rbx,%rsi
lea    0x30(%rsp),%rdx
xor    %ecx,%ecx
call   1040 <term_chk@plt>
test   %rax,%rax
jne    11f0 <search_utmp+0x70>
test   %r13,%r13
je     1245 <search_utmp+0xc5>
cmpl   $0x0,0x14(%rsp)
je     11f0 <search_utmp+0x70>
mov    0x10(%r14),%rdi
mov    0x20(%rsp),%rsi
call   1070 <strcmp@plt>
lea    0x1(%r15),%r13d
mov    $0x1,%ecx
test   %rax,%rax
je     11c0 <search_utmp+0x40>
jmp    1288 <search_utmp+0x108>
nopw   0x0(%rax,%rax,1)
mov    (%rsp),%ecx
jmp    11bd <search_utmp+0x3d>
nopl   0x0(%rax,%rax,1)
mov    (%rsp),%ecx
jmp    11bd <search_utmp+0x3d>
addl   $0x1,0x4(%rsp)
mov    0x30(%rsp),%r13
lea    0x1(%r15),%eax
cmp    0x18(%rsp),%r13
mov    (%rsp),%ecx
jle    11ba <search_utmp+0x3a>
mov    0x10(%r14),%rsi
mov    0x2d31(%rip),%rax
mov    (%rax),%edx
mov    0x8(%rsp),%rdi
call   1090 <strlcpy@plt>
mov    (%rsp),%ecx
add    $0x1,%r15d
mov    %r13,0x18(%rsp)
mov    %r15d,%eax
jmp    11ba <search_utmp+0x3a>
mov    %r13d,%r15d
xor    %eax,%eax
call   1030 <endutxent@plt>
test   %r15d,%r15d
jne    12f2 <search_utmp+0x172>
lea    0xd1b(%rip),%rsi
mov    $0x1,%edi
mov    %r12,%rdx
call   1050 <errx@plt>
mov    0x4(%rsp),%eax
test   %eax,%eax
je     1315 <search_utmp+0x195>
cmp    $0x2,%eax
jl     1349 <search_utmp+0x1c9>
lea    0xd27(%rip),%rdi
mov    %r12,%rsi
mov    0x8(%rsp),%rdx
call   1060 <warnx@plt>
jmp    1349 <search_utmp+0x1c9>
cmpl   $0x0,(%rsp)
je     1335 <search_utmp+0x1b5>
mov    0x2cbe(%rip),%rax
mov    (%rax),%edx
mov    0x8(%rsp),%rdi
mov    0x20(%rsp),%rsi
call   1090 <strlcpy@plt>
jmp    1349 <search_utmp+0x1c9>
lea    0xcd8(%rip),%rsi
mov    $0x1,%edi
mov    %r12,%rdx
call   1050 <errx@plt>
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.utmpx = type { i64, i8*, i8* }
@USER_PROCESS = global i64 0
@MAXPATHLEN = global i32 0
@.str = [20 x i8] c"%s is not logged in\00"
@.str.1 = [25 x i8] c"%s has messages disabled\00"
@.str.2 = [46 x i8] c"%s is logged in more than once; writing to %s\00"
define void @search_utmp(i8* %0, i8* %1, i8* %2, i64 %3) {
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i64
%9 = alloca %struct.utmpx*
%10 = alloca i64
%11 = alloca i64
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i8* %2, i8** %7
store i64 %3, i64* %8
store i32 0, i32* %13
store i32 0, i32* %12
store i64 0, i64* %10
store i32 0, i32* %15
%16 = call i32 (...) @setutxent()
br label %17
17: ; preds = %72, %56, %48, %41, %4
%18 = call %struct.utmpx* (...) @getutxent()
store %struct.utmpx* %18, %struct.utmpx** %9
%19 = icmp ne %struct.utmpx* %18, null
br i1 %19, label %20, label %73
20: ; preds = %17
%21 = load %struct.utmpx*, %struct.utmpx** %9
%22 = getelementptr inbounds %struct.utmpx, %struct.utmpx* %21, i32 0, i32 0
%23 = load i64, i64* %22
%24 = load i64, i64* @USER_PROCESS
%25 = icmp eq i64 %23, %24
br i1 %25, label %26, label %72
26: ; preds = %20
%27 = load i8*, i8** %5
%28 = load %struct.utmpx*, %struct.utmpx** %9
%29 = getelementptr inbounds %struct.utmpx, %struct.utmpx* %28, i32 0, i32 1
%30 = load i8*, i8** %29
%31 = call i64 @strcmp(i8* %27, i8* %30)
%32 = icmp eq i64 %31, 0
br i1 %32, label %33, label %72
33: ; preds = %26
%34 = load i32, i32* %12
%35 = add nsw i32 %34, 1
store i32 %35, i32* %12
%36 = load %struct.utmpx*, %struct.utmpx** %9
%37 = getelementptr inbounds %struct.utmpx, %struct.utmpx* %36, i32 0, i32 2
%38 = load i8*, i8** %37
%39 = call i64 @term_chk(i8* %38, i32* %14, i64* %11, i32 0)
%40 = icmp ne i64 %39, 0
br i1 %40, label %41, label %42
41: ; preds = %33
br label %17
42: ; preds = %33
%43 = load i64, i64* %8
%44 = icmp ne i64 %43, 0
br i1 %44, label %45, label %49
45: ; preds = %42
%46 = load i32, i32* %14
%47 = icmp ne i32 %46, 0
br i1 %47, label %49, label %48
48: ; preds = %45
br label %17
49: ; preds = %45, %42
%50 = load %struct.utmpx*, %struct.utmpx** %9
%51 = getelementptr inbounds %struct.utmpx, %struct.utmpx* %50, i32 0, i32 2
%52 = load i8*, i8** %51
%53 = load i8*, i8** %7
%54 = call i64 @strcmp(i8* %52, i8* %53)
%55 = icmp eq i64 %54, 0
br i1 %55, label %56, label %57
56: ; preds = %49
store i32 1, i32* %15
br label %17
57: ; preds = %49
%58 = load i32, i32* %13
%59 = add nsw i32 %58, 1
store i32 %59, i32* %13
%60 = load i64, i64* %11
%61 = load i64, i64* %10
%62 = icmp sgt i64 %60, %61
br i1 %62, label %63, label %71
63: ; preds = %57
%64 = load i64, i64* %11
store i64 %64, i64* %10
%65 = load i8*, i8** %6
%66 = load %struct.utmpx*, %struct.utmpx** %9
%67 = getelementptr inbounds %struct.utmpx, %struct.utmpx* %66, i32 0, i32 2
%68 = load i8*, i8** %67
%69 = load i32, i32* @MAXPATHLEN
%70 = call i32 @strlcpy(i8* %65, i8* %68, i32 %69)
br label %71
71: ; preds = %63, %57
br label %72
72: ; preds = %71, %26, %20
br label %17
73: ; preds = %17
%74 = call i32 (...) @endutxent()
%75 = load i32, i32* %12
%76 = icmp eq i32 %75, 0
br i1 %76, label %77, label %80
77: ; preds = %73
%78 = load i8*, i8** %5
%79 = call i32 @errx(i32 1, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %78)
br label %80
80: ; preds = %77, %73
%81 = load i32, i32* %13
%82 = icmp eq i32 %81, 0
br i1 %82, label %83, label %94
83: ; preds = %80
%84 = load i32, i32* %15
%85 = icmp ne i32 %84, 0
br i1 %85, label %86, label %91
86: ; preds = %83
%87 = load i8*, i8** %6
%88 = load i8*, i8** %7
%89 = load i32, i32* @MAXPATHLEN
%90 = call i32 @strlcpy(i8* %87, i8* %88, i32 %89)
br label %102
91: ; preds = %83
%92 = load i8*, i8** %5
%93 = call i32 @errx(i32 1, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %92)
br label %102
94: ; preds = %80
%95 = load i32, i32* %13
%96 = icmp sgt i32 %95, 1
br i1 %96, label %97, label %101
97: ; preds = %94
%98 = load i8*, i8** %5
%99 = load i8*, i8** %6
%100 = call i32 @warnx(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.2, i64 0, i64 0), i8* %98, i8* %99)
br label %101
101: ; preds = %97, %94
br label %102
102: ; preds = %86, %101, %91
ret void
}

LLVM IR (-O2):
%struct.utmpx = type { i64, i8*, i8* }
@USER_PROCESS = global i64 0
@MAXPATHLEN = global i32 0
@.str = [20 x i8] c"%s is not logged in\00"
@.str.1 = [25 x i8] c"%s has messages disabled\00"
@.str.2 = [46 x i8] c"%s is logged in more than once; writing to %s\00"
define void @search_utmp(i8* %0, i8* %1, i8* %2, i64 %3) {
%5 = alloca i64
%6 = alloca i32
%7 = bitcast i64* %5 to i8*
%8 = bitcast i32* %6 to i8*
%9 = tail call i32 (...) @setutxent()
%10 = icmp eq i64 %3, 0
br label %11
11: ; preds = %63, %4
%12 = phi i64 [ %61, %63 ], [ 0, %4 ]
%13 = phi i32 [ %42, %63 ], [ 0, %4 ]
%14 = phi i32 [ %60, %63 ], [ 0, %4 ]
%15 = phi i32 [ %26, %63 ], [ 0, %4 ]
br label %16
16: ; preds = %11, %59
%17 = phi i32 [ %13, %11 ], [ %42, %59 ]
%18 = phi i32 [ %14, %11 ], [ %60, %59 ]
%19 = phi i32 [ %15, %11 ], [ %26, %59 ]
br label %21
20: ; preds = %36, %29
br label %21
21: ; preds = %16, %20
%22 = phi i32 [ %31, %20 ], [ %17, %16 ]
%23 = phi i32 [ %26, %20 ], [ %19, %16 ]
br label %24
24: ; preds = %21, %54
%25 = phi i32 [ %22, %21 ], [ %42, %54 ]
%26 = phi i32 [ %23, %21 ], [ 1, %54 ]
%27 = call %struct.utmpx* (...) @getutxent()
%28 = icmp eq %struct.utmpx* %27, null
br i1 %28, label %68, label %29
29: ; preds = %24, %47
%30 = phi %struct.utmpx* [ %48, %47 ], [ %27, %24 ]
%31 = phi i32 [ %42, %47 ], [ %25, %24 ]
%32 = getelementptr inbounds %struct.utmpx, %struct.utmpx* %30, i64 0, i32 0
%33 = load i64, i64* %32
%34 = load i64, i64* @USER_PROCESS
%35 = icmp eq i64 %33, %34
br i1 %35, label %36, label %20
36: ; preds = %29
%37 = getelementptr inbounds %struct.utmpx, %struct.utmpx* %30, i64 0, i32 1
%38 = load i8*, i8** %37
%39 = call i64 @strcmp(i8* %0, i8* %38)
%40 = icmp eq i64 %39, 0
br i1 %40, label %41, label %20
41: ; preds = %36
%42 = add nsw i32 %31, 1
%43 = getelementptr inbounds %struct.utmpx, %struct.utmpx* %30, i64 0, i32 2
%44 = load i8*, i8** %43
%45 = call i64 @term_chk(i8* %44, i32* %6, i64* %5, i32 0)
%46 = icmp eq i64 %45, 0
br i1 %46, label %50, label %47
47: ; preds = %41, %50
%48 = call %struct.utmpx* (...) @getutxent()
%49 = icmp eq %struct.utmpx* %48, null
br i1 %49, label %68, label %29
50: ; preds = %41
%51 = load i32, i32* %6
%52 = icmp ne i32 %51, 0
%53 = select i1 %10, i1 true, i1 %52
br i1 %53, label %54, label %47
54: ; preds = %50
%55 = getelementptr inbounds %struct.utmpx, %struct.utmpx* %30, i64 0, i32 2
%56 = load i8*, i8** %55
%57 = call i64 @strcmp(i8* %56, i8* %2)
%58 = icmp eq i64 %57, 0
br i1 %58, label %24, label %59
59: ; preds = %54
%60 = add nsw i32 %18, 1
%61 = load i64, i64* %5
%62 = icmp sgt i64 %61, %12
br i1 %62, label %63, label %16
63: ; preds = %59
%64 = getelementptr inbounds %struct.utmpx, %struct.utmpx* %30, i64 0, i32 2
%65 = load i8*, i8** %64
%66 = load i32, i32* @MAXPATHLEN
%67 = call i32 @strlcpy(i8* %1, i8* %65, i32 %66)
br label %11
68: ; preds = %24, %47
%69 = phi i32 [ %42, %47 ], [ %25, %24 ]
%70 = call i32 (...) @endutxent()
%71 = icmp eq i32 %69, 0
br i1 %71, label %72, label %74
72: ; preds = %68
%73 = call i32 @errx(i32 1, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %74
74: ; preds = %72, %68
%75 = icmp eq i32 %18, 0
br i1 %75, label %76, label %83
76: ; preds = %74
%77 = icmp eq i32 %26, 0
br i1 %77, label %81, label %78
78: ; preds = %76
%79 = load i32, i32* @MAXPATHLEN
%80 = call i32 @strlcpy(i8* %1, i8* %2, i32 %79)
br label %87
81: ; preds = %76
%82 = call i32 @errx(i32 1, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %87
83: ; preds = %74
%84 = icmp sgt i32 %18, 1
br i1 %84, label %85, label %87
85: ; preds = %83
%86 = call i32 @warnx(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.2, i64 0, i64 0), i8* %0, i8* %1)
br label %87
87: ; preds = %81, %85, %83, %78
ret void
}