#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();
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) {
    ttyp = basename(tty);
    strncpy(copy.ut_line, ttyp, UT_LINESIZE);
    if (utmpname(_PATH_UTMP) == 0) {
      setutent();
      pututline(&copy);
      endutent();
    }
    if (tty != _tty)
      free(tty);
  }
  updwtmp(_PATH_WTMP, &copy);
}


x86 assembly (clang, -O0):
<login>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2e35(%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    0x2dd3(%rip),%rcx
mov    (%rcx),%edi
mov    %eax,%edx
lea    -0x20(%rbp),%rsi
call   10b0 <tty_name@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    121a <login+0x8a>
mov    -0x48(%rbp),%rax
mov    0x2de6(%rip),%rcx
mov    (%rcx),%edi
mov    %eax,%edx
lea    -0x20(%rbp),%rsi
call   10b0 <tty_name@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    123f <login+0xaf>
mov    -0x48(%rbp),%rax
mov    0x2d91(%rip),%rcx
mov    (%rcx),%edi
mov    %eax,%edx
lea    -0x20(%rbp),%rsi
call   10b0 <tty_name@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jl     12b6 <login+0x126>
mov    -0x20(%rbp),%rdi
call   1080 <basename@plt>
mov    %rax,-0x30(%rbp)
mov    -0x38(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    0x2d74(%rip),%rax
mov    (%rax),%edx
call   1040 <strncpy@plt>
mov    0x2d6e(%rip),%rax
mov    (%rax),%edi
call   1060 <utmpname@plt>
cmp    $0x0,%rax
jne    129a <login+0x10a>
mov    $0x0,%al
call   1090 <setutent@plt>
lea    -0x38(%rbp),%rdi
call   10a0 <pututline@plt>
mov    $0x0,%al
call   1070 <endutent@plt>
mov    -0x40(%rbp),%rax
cmp    %rax,-0x20(%rbp)
je     12b1 <login+0x121>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
jmp    12b6 <login+0x126>
mov    0x2d0b(%rip),%rax
mov    (%rax),%edi
lea    -0x38(%rbp),%rsi
call   1050 <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    0x2e32(%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,-0x30(%rbp)
mov    (%rdi),%r14d
mov    %r14d,-0x28(%rbp)
mov    0x2de6(%rip),%rax
mov    (%rax),%edi
lea    -0x30(%rbp),%rsi
mov    %ebx,%edx
call   10b0 <tty_name@plt>
test   %eax,%eax
jns    1213 <login+0x83>
mov    0x2e06(%rip),%rax
mov    (%rax),%edi
lea    -0x30(%rbp),%rsi
mov    %ebx,%edx
call   10b0 <tty_name@plt>
test   %eax,%eax
jns    1213 <login+0x83>
mov    0x2dbe(%rip),%rax
mov    (%rax),%edi
lea    -0x30(%rbp),%rsi
mov    %ebx,%edx
call   10b0 <tty_name@plt>
test   %eax,%eax
js     1263 <login+0xd3>
mov    -0x30(%rbp),%rdi
call   1080 <basename@plt>
mov    (%r12),%edx
mov    %r14d,%edi
mov    %rax,%rsi
call   1040 <strncpy@plt>
mov    0x2dae(%rip),%rax
mov    (%rax),%edi
call   1060 <utmpname@plt>
test   %rax,%rax
jne    1255 <login+0xc5>
xor    %eax,%eax
call   1090 <setutent@plt>
lea    -0x28(%rbp),%rdi
call   10a0 <pututline@plt>
xor    %eax,%eax
call   1070 <endutent@plt>
mov    -0x30(%rbp),%rdi
cmp    %r15,%rdi
je     1263 <login+0xd3>
call   1030 <free@plt>
mov    0x2d5e(%rip),%rax
mov    (%rax),%edi
lea    -0x28(%rbp),%rsi
call   1050 <updwtmp@plt>
lea    -0x20(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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
@_PATH_UTMP = global i32 0
@_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 %58
36: ; preds = %33
%37 = load i8*, i8** %5
%38 = call i8* @basename(i8* %37)
store i8* %38, i8** %7
%39 = getelementptr inbounds %struct.utmp, %struct.utmp* %8, i32 0, i32 0
%40 = load i32, i32* %39
%41 = load i8*, i8** %7
%42 = load i32, i32* @UT_LINESIZE
%43 = call i32 @strncpy(i32 %40, i8* %41, i32 %42)
%44 = load i32, i32* @_PATH_UTMP
%45 = call i64 @utmpname(i32 %44)
%46 = icmp eq i64 %45, 0
br i1 %46, label %47, label %51
47: ; preds = %36
%48 = call i32 (...) @setutent()
%49 = call i32 @pututline(%struct.utmp* %8)
%50 = call i32 (...) @endutent()
br label %51
51: ; preds = %47, %36
%52 = load i8*, i8** %5
%53 = icmp ne i8* %52, %13
br i1 %53, label %54, label %57
54: ; preds = %51
%55 = load i8*, i8** %5
%56 = call i32 @free(i8* %55)
br label %57
57: ; preds = %54, %51
br label %58
58: ; preds = %57, %33
%59 = load i32, i32* @_PATH_WTMP
%60 = call i32 @updwtmp(i32 %59, %struct.utmp* %8)
%61 = 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
@_PATH_UTMP = global i32 0
@_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 %41
24: ; preds = %1, %16, %20
%25 = load i8*, i8** %2
%26 = call i8* @basename(i8* %25)
%27 = load i32, i32* @UT_LINESIZE
%28 = call i32 @strncpy(i32 %12, i8* %26, i32 %27)
%29 = load i32, i32* @_PATH_UTMP
%30 = call i64 @utmpname(i32 %29)
%31 = icmp eq i64 %30, 0
br i1 %31, label %32, label %36
32: ; preds = %24
%33 = call i32 (...) @setutent()
%34 = call i32 @pututline(%struct.utmp* %3)
%35 = call i32 (...) @endutent()
br label %36
36: ; preds = %32, %24
%37 = load i8*, i8** %2
%38 = icmp eq i8* %37, %7
br i1 %38, label %41, label %39
39: ; preds = %36
%40 = call i32 @free(i8* %37)
br label %41
41: ; preds = %36, %39, %20
%42 = load i32, i32* @_PATH_WTMP
%43 = call i32 @updwtmp(i32 %42, %struct.utmp* %3)
ret void
}