#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_line;
};
typedef int /*<<< orphan*/ _tty;

/* Variables and functions */
int /*<<< orphan*/ STDERR_FILENO;
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ UT_LINESIZE;
int /*<<< orphan*/ _PATH_UTMP;
int /*<<< orphan*/ _PATH_WTMP;
char *basename(char *);
int /*<<< orphan*/ endutent();
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ pututline(struct utmp *);
int /*<<< orphan*/ setutent();
scalar_t__ strncmp(char *, char *, int);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char const *,
                           int /*<<< orphan*/);
int tty_name(int /*<<< orphan*/, char **, int);
int /*<<< orphan*/ updwtmp(int /*<<< orphan*/, struct utmp *);
scalar_t__ utmpname(int /*<<< orphan*/);
void __login(const struct utmp *ut) {
  char _tty[512 + UT_LINESIZE];
  char *tty = _tty;
  int found_tty;
  const char *ttyp;
  struct utmp copy = *ut;
  found_tty = tty_name(STDIN_FILENO, &tty, sizeof(_tty));
  if (found_tty < 0)
    found_tty = tty_name(STDOUT_FILENO, &tty, sizeof(_tty));
  if (found_tty < 0)
    found_tty = tty_name(STDERR_FILENO, &tty, sizeof(_tty));
  if (found_tty >= 0) {
    if (strncmp(tty, "/dev/", 5) == 0)
      ttyp = tty + 5;
    else
      ttyp = basename(tty);
    strncpy(copy.ut_line, ttyp, UT_LINESIZE);
    if (utmpname(_PATH_UTMP) == 0) {
      setutent();
      pututline(&copy);
      endutent();
    }
    if (tty != _tty)
      free(tty);
  } else
    strncpy(copy.ut_line, "???", UT_LINESIZE);
  updwtmp(_PATH_WTMP, &copy);
}


x86 assembly (clang, -O0):
<__login>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2e25(%rip),%rax
mov    (%rax),%eax
add    $0x200,%eax
mov    %rax,-0x48(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x10(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x40(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x8(%rbp),%rcx
mov    (%rcx),%ecx
mov    %ecx,-0x38(%rbp)
mov    0x2dc3(%rip),%rcx
mov    (%rcx),%edi
mov    %eax,%edx
lea    -0x20(%rbp),%rsi
call   10c0 <tty_name@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    122a <__login+0x8a>
mov    -0x48(%rbp),%rax
mov    0x2dd6(%rip),%rcx
mov    (%rcx),%edi
mov    %eax,%edx
lea    -0x20(%rbp),%rsi
call   10c0 <tty_name@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    124f <__login+0xaf>
mov    -0x48(%rbp),%rax
mov    0x2d81(%rip),%rcx
mov    (%rcx),%edi
mov    %eax,%edx
lea    -0x20(%rbp),%rsi
call   10c0 <tty_name@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jl     12f6 <__login+0x156>
mov    -0x20(%rbp),%rdi
lea    0xd9c(%rip),%rsi
mov    $0x5,%edx
call   1050 <strncmp@plt>
cmp    $0x0,%rax
jne    1289 <__login+0xe9>
mov    -0x20(%rbp),%rax
add    $0x5,%rax
mov    %rax,-0x30(%rbp)
jmp    1296 <__login+0xf6>
mov    -0x20(%rbp),%rdi
call   1090 <basename@plt>
mov    %rax,-0x30(%rbp)
mov    -0x38(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    0x2d34(%rip),%rax
mov    (%rax),%edx
call   1040 <strncpy@plt>
mov    0x2d2e(%rip),%rax
mov    (%rax),%edi
call   1070 <utmpname@plt>
cmp    $0x0,%rax
jne    12da <__login+0x13a>
mov    $0x0,%al
call   10a0 <setutent@plt>
lea    -0x38(%rbp),%rdi
call   10b0 <pututline@plt>
mov    $0x0,%al
call   1080 <endutent@plt>
mov    -0x40(%rbp),%rax
cmp    %rax,-0x20(%rbp)
je     12f1 <__login+0x151>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
jmp    130e <__login+0x16e>
mov    -0x38(%rbp),%edi
mov    0x2cd8(%rip),%rax
mov    (%rax),%edx
lea    0xcfd(%rip),%rsi
call   1040 <strncpy@plt>
mov    0x2cb3(%rip),%rax
mov    (%rax),%edi
lea    -0x38(%rbp),%rsi
call   1060 <updwtmp@plt>
mov    -0x10(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<__login>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    0x2e22(%rip),%r12
mov    $0x200,%ebx
add    (%r12),%ebx
lea    0xf(%rbx),%rax
and    $0xfffffffffffffff0,%rax
mov    %rsp,%r15
sub    %rax,%r15
mov    %r15,%rsp
mov    %r15,-0x28(%rbp)
mov    (%rdi),%r14d
mov    %r14d,-0x30(%rbp)
mov    0x2dd6(%rip),%rax
mov    (%rax),%edi
lea    -0x28(%rbp),%rsi
mov    %ebx,%edx
call   10c0 <tty_name@plt>
test   %eax,%eax
jns    1227 <__login+0x87>
mov    0x2df6(%rip),%rax
mov    (%rax),%edi
lea    -0x28(%rbp),%rsi
mov    %ebx,%edx
call   10c0 <tty_name@plt>
test   %eax,%eax
jns    1227 <__login+0x87>
mov    0x2dae(%rip),%rax
mov    (%rax),%edi
lea    -0x28(%rbp),%rsi
mov    %ebx,%edx
call   10c0 <tty_name@plt>
test   %eax,%eax
js     12b9 <__login+0x119>
mov    -0x28(%rbp),%rdi
lea    0xdce(%rip),%rsi
mov    $0x5,%edx
call   1050 <strncmp@plt>
mov    -0x28(%rbp),%rsi
test   %rax,%rax
je     1252 <__login+0xb2>
mov    %rsi,%rdi
call   1090 <basename@plt>
mov    %rax,%rsi
jmp    1256 <__login+0xb6>
add    $0x5,%rsi
mov    (%r12),%edx
mov    %r14d,%edi
call   1040 <strncpy@plt>
mov    0x2d77(%rip),%rax
mov    (%rax),%edi
call   1070 <utmpname@plt>
test   %rax,%rax
jne    128c <__login+0xec>
xor    %eax,%eax
call   10a0 <setutent@plt>
lea    -0x30(%rbp),%rdi
call   10b0 <pututline@plt>
xor    %eax,%eax
call   1080 <endutent@plt>
mov    -0x28(%rbp),%rdi
cmp    %r15,%rdi
je     129a <__login+0xfa>
call   1030 <free@plt>
mov    0x2d27(%rip),%rax
mov    (%rax),%edi
lea    -0x30(%rbp),%rsi
call   1060 <updwtmp@plt>
lea    -0x20(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    (%r12),%edx
lea    0xd42(%rip),%rsi
mov    %r14d,%edi
call   1040 <strncpy@plt>
jmp    129a <__login+0xfa>

LLVM IR (-O0):
%struct.utmp = type { i32 }
@UT_LINESIZE = global i32 0
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
@.str = [6 x i8] c"/dev/\00"
@_PATH_UTMP = global i32 0
@.str.1 = [4 x i8] c"???\00"
@_PATH_WTMP = global i32 0
define void @__login(%struct.utmp* %0) {
%2 = alloca %struct.utmp*
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca %struct.utmp
store %struct.utmp* %0, %struct.utmp** %2
%9 = load i32, i32* @UT_LINESIZE
%10 = add nsw i32 512, %9
%11 = zext i32 %10 to i64
%12 = call i8* @llvm.stacksave()
store i8* %12, i8** %3
%13 = alloca i8, i64 %11
store i64 %11, i64* %4
store i8* %13, i8** %5
%14 = load %struct.utmp*, %struct.utmp** %2
%15 = bitcast %struct.utmp* %8 to i8*
%16 = bitcast %struct.utmp* %14 to i8*
%17 = load i32, i32* @STDIN_FILENO
%18 = trunc i64 %11 to i32
%19 = call i32 @tty_name(i32 %17, i8** %5, i32 %18)
store i32 %19, i32* %6
%20 = load i32, i32* %6
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %26
22: ; preds = %1
%23 = load i32, i32* @STDOUT_FILENO
%24 = trunc i64 %11 to i32
%25 = call i32 @tty_name(i32 %23, i8** %5, i32 %24)
store i32 %25, i32* %6
br label %26
26: ; preds = %22, %1
%27 = load i32, i32* %6
%28 = icmp slt i32 %27, 0
br i1 %28, label %29, label %33
29: ; preds = %26
%30 = load i32, i32* @STDERR_FILENO
%31 = trunc i64 %11 to i32
%32 = call i32 @tty_name(i32 %30, i8** %5, i32 %31)
store i32 %32, i32* %6
br label %33
33: ; preds = %29, %26
%34 = load i32, i32* %6
%35 = icmp sge i32 %34, 0
br i1 %35, label %36, label %66
36: ; preds = %33
%37 = load i8*, i8** %5
%38 = call i64 @strncmp(i8* %37, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 5)
%39 = icmp eq i64 %38, 0
br i1 %39, label %40, label %43
40: ; preds = %36
%41 = load i8*, i8** %5
%42 = getelementptr inbounds i8, i8* %41, i64 5
store i8* %42, i8** %7
br label %46
43: ; preds = %36
%44 = load i8*, i8** %5
%45 = call i8* @basename(i8* %44)
store i8* %45, i8** %7
br label %46
46: ; preds = %43, %40
%47 = getelementptr inbounds %struct.utmp, %struct.utmp* %8, i32 0, i32 0
%48 = load i32, i32* %47
%49 = load i8*, i8** %7
%50 = load i32, i32* @UT_LINESIZE
%51 = call i32 @strncpy(i32 %48, i8* %49, i32 %50)
%52 = load i32, i32* @_PATH_UTMP
%53 = call i64 @utmpname(i32 %52)
%54 = icmp eq i64 %53, 0
br i1 %54, label %55, label %59
55: ; preds = %46
%56 = call i32 (...) @setutent()
%57 = call i32 @pututline(%struct.utmp* %8)
%58 = call i32 (...) @endutent()
br label %59
59: ; preds = %55, %46
%60 = load i8*, i8** %5
%61 = icmp ne i8* %60, %13
br i1 %61, label %62, label %65
62: ; preds = %59
%63 = load i8*, i8** %5
%64 = call i32 @free(i8* %63)
br label %65
65: ; preds = %62, %59
br label %71
66: ; preds = %33
%67 = getelementptr inbounds %struct.utmp, %struct.utmp* %8, i32 0, i32 0
%68 = load i32, i32* %67
%69 = load i32, i32* @UT_LINESIZE
%70 = call i32 @strncpy(i32 %68, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %69)
br label %71
71: ; preds = %66, %65
%72 = load i32, i32* @_PATH_WTMP
%73 = call i32 @updwtmp(i32 %72, %struct.utmp* %8)
%74 = load i8*, i8** %3
ret void
}

LLVM IR (-O2):
%struct.utmp = type { i32 }
@UT_LINESIZE = global i32 0
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@STDERR_FILENO = global i32 0
@.str = [6 x i8] c"/dev/\00"
@_PATH_UTMP = global i32 0
@.str.1 = [4 x i8] c"???\00"
@_PATH_WTMP = global i32 0
define void @__login(%struct.utmp* nocapture %0) {
%2 = alloca i8*
%3 = alloca %struct.utmp
%4 = load i32, i32* @UT_LINESIZE
%5 = add nsw i32 %4, 512
%6 = zext i32 %5 to i64
%7 = alloca i8, i64 %6
%8 = bitcast i8** %2 to i8*
store i8* %7, i8** %2
%9 = bitcast %struct.utmp* %3 to i8*
%10 = getelementptr %struct.utmp, %struct.utmp* %0, i64 0, i32 0
%11 = getelementptr inbounds %struct.utmp, %struct.utmp* %3, i64 0, i32 0
%12 = load i32, i32* %10
store i32 %12, i32* %11
%13 = load i32, i32* @STDIN_FILENO
%14 = call i32 @tty_name(i32 %13, i8** %2, i32 %5)
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %24
16: ; preds = %1
%17 = load i32, i32* @STDOUT_FILENO
%18 = call i32 @tty_name(i32 %17, i8** %2, i32 %5)
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %24
20: ; preds = %16
%21 = load i32, i32* @STDERR_FILENO
%22 = call i32 @tty_name(i32 %21, i8** %2, i32 %5)
%23 = icmp sgt i32 %22, -1
br i1 %23, label %24, label %49
24: ; preds = %1, %16, %20
%25 = load i8*, i8** %2
%26 = call i64 @strncmp(i8* %25, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 5)
%27 = icmp eq i64 %26, 0
%28 = load i8*, i8** %2
br i1 %27, label %29, label %31
29: ; preds = %24
%30 = getelementptr inbounds i8, i8* %28, i64 5
br label %33
31: ; preds = %24
%32 = call i8* @basename(i8* %28)
br label %33
33: ; preds = %31, %29
%34 = phi i8* [ %30, %29 ], [ %32, %31 ]
%35 = load i32, i32* @UT_LINESIZE
%36 = call i32 @strncpy(i32 %12, i8* %34, i32 %35)
%37 = load i32, i32* @_PATH_UTMP
%38 = call i64 @utmpname(i32 %37)
%39 = icmp eq i64 %38, 0
br i1 %39, label %40, label %44
40: ; preds = %33
%41 = call i32 (...) @setutent()
%42 = call i32 @pututline(%struct.utmp* %3)
%43 = call i32 (...) @endutent()
br label %44
44: ; preds = %40, %33
%45 = load i8*, i8** %2
%46 = icmp eq i8* %45, %7
br i1 %46, label %52, label %47
47: ; preds = %44
%48 = call i32 @free(i8* %45)
br label %52
49: ; preds = %20
%50 = load i32, i32* @UT_LINESIZE
%51 = call i32 @strncpy(i32 %12, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %50)
br label %52
52: ; preds = %44, %47, %49
%53 = load i32, i32* @_PATH_WTMP
%54 = call i32 @updwtmp(i32 %53, %struct.utmp* %3)
ret void
}