#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*/ tty_name;
struct utmp {
  char *ut_line;
};

/* Variables and functions */
int /*<<< orphan*/ _PATH_WTMP;
int /*<<< orphan*/ endutent();
int /*<<< orphan*/ pututline(struct utmp *);
int /*<<< orphan*/ setutent();
scalar_t__ strncmp(char *, char *, int);
int /*<<< orphan*/ strncpy(char *, char *, int);
scalar_t__ ttyname_r(int, char *, int);
int /*<<< orphan*/ updwtmp(int /*<<< orphan*/, struct utmp *);
void login(const struct utmp *entry) {
  struct utmp copy;
  char tty_name[sizeof(copy.ut_line) + 6];
  int fd;
  copy = *entry;
  fd = 0;
  while (fd != 3 && ttyname_r(fd, tty_name, sizeof(tty_name)) != 0)
    fd++;
  if (fd != 3) {
    if (strncmp(tty_name, "/dev/", 5) == 0)
      strncpy(copy.ut_line, tty_name + 5, sizeof(copy.ut_line) - 1);
    else
      strncpy(copy.ut_line, tty_name, sizeof(copy.ut_line) - 1);
    copy.ut_line[sizeof(copy.ut_line) - 1] = '\0';
    setutent();
    pututline(&copy);
    endutent();
  } else {
    strncpy(copy.ut_line, "???", sizeof(copy.ut_line));
  }
  updwtmp(_PATH_WTMP, &copy);
}


x86 assembly (clang, -O0):
<login>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x10(%rbp)
movl   $0x0,-0x24(%rbp)
xor    %eax,%eax
cmpl   $0x3,-0x24(%rbp)
mov    %al,-0x25(%rbp)
je     11b8 <login+0x48>
mov    -0x24(%rbp),%edi
lea    -0x1e(%rbp),%rsi
mov    $0xe,%edx
call   1070 <ttyname_r@plt>
cmp    $0x0,%rax
setne  %al
mov    %al,-0x25(%rbp)
mov    -0x25(%rbp),%al
test   $0x1,%al
jne    11c8 <login+0x58>
jmp    11d6 <login+0x66>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    118e <login+0x1e>
cmpl   $0x3,-0x24(%rbp)
je     1250 <login+0xe0>
lea    -0x1e(%rbp),%rdi
lea    0xe15(%rip),%rsi
mov    $0x5,%edx
call   1040 <strncmp@plt>
cmp    $0x0,%rax
jne    121a <login+0xaa>
mov    -0x10(%rbp),%rdi
lea    -0x1e(%rbp),%rsi
add    $0x5,%rsi
mov    $0x7,%edx
call   1030 <strncpy@plt>
jmp    122c <login+0xbc>
mov    -0x10(%rbp),%rdi
lea    -0x1e(%rbp),%rsi
mov    $0x7,%edx
call   1030 <strncpy@plt>
mov    -0x10(%rbp),%rax
movb   $0x0,0x7(%rax)
mov    $0x0,%al
call   1080 <setutent@plt>
lea    -0x10(%rbp),%rdi
call   1090 <pututline@plt>
mov    $0x0,%al
call   1060 <endutent@plt>
jmp    1265 <login+0xf5>
mov    -0x10(%rbp),%rdi
lea    0xdab(%rip),%rsi
mov    $0x8,%edx
call   1030 <strncpy@plt>
mov    0x2d74(%rip),%rax
mov    (%rax),%edi
lea    -0x10(%rbp),%rsi
call   1050 <updwtmp@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<login>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    (%rdi),%rbx
mov    %rbx,(%rsp)
lea    0xa(%rsp),%r14
xor    %edi,%edi
mov    %r14,%rsi
mov    $0xe,%edx
call   1070 <ttyname_r@plt>
test   %rax,%rax
je     11df <login+0x6f>
lea    0xa(%rsp),%rsi
mov    $0x1,%edi
mov    $0xe,%edx
call   1070 <ttyname_r@plt>
test   %rax,%rax
je     11df <login+0x6f>
lea    0xa(%rsp),%rsi
mov    $0x2,%edi
mov    $0xe,%edx
call   1070 <ttyname_r@plt>
test   %rax,%rax
je     11df <login+0x6f>
lea    0xe36(%rip),%rsi
mov    %rbx,%rdi
mov    $0x8,%edx
call   1030 <strncpy@plt>
jmp    1226 <login+0xb6>
lea    0xe1a(%rip),%rsi
mov    %r14,%rdi
mov    $0x5,%edx
call   1040 <strncmp@plt>
test   %rax,%rax
lea    0xf(%rsp),%rsi
cmovne %r14,%rsi
mov    %rbx,%rdi
mov    $0x7,%edx
call   1030 <strncpy@plt>
movb   $0x0,0x7(%rbx)
xor    %eax,%eax
call   1080 <setutent@plt>
mov    %rsp,%rdi
call   1090 <pututline@plt>
xor    %eax,%eax
call   1060 <endutent@plt>
mov    0x2db3(%rip),%rax
mov    (%rax),%edi
mov    %rsp,%rsi
call   1050 <updwtmp@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret

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

LLVM IR (-O2):
%struct.utmp = type { i8* }
@.str = [6 x i8] c"/dev/\00"
@.str.1 = [4 x i8] c"???\00"
@_PATH_WTMP = global i32 0
define void @login(%struct.utmp* nocapture %0) {
%2 = alloca %struct.utmp
%3 = alloca [14 x i8]
%4 = bitcast %struct.utmp* %2 to i8*
%5 = getelementptr inbounds [14 x i8], [14 x i8]* %3, i64 0, i64 0
%6 = bitcast %struct.utmp* %0 to i64*
%7 = bitcast %struct.utmp* %2 to i64*
%8 = load i64, i64* %6
store i64 %8, i64* %7
%9 = call i64 @ttyname_r(i32 0, i8* %5, i32 14)
%10 = icmp eq i64 %9, 0
%11 = inttoptr i64 %8 to i8*
br i1 %10, label %20, label %12
12: ; preds = %1
%13 = call i64 @ttyname_r(i32 1, i8* %5, i32 14)
%14 = icmp eq i64 %13, 0
br i1 %14, label %20, label %15
15: ; preds = %12
%16 = call i64 @ttyname_r(i32 2, i8* %5, i32 14)
%17 = icmp eq i64 %16, 0
br i1 %17, label %20, label %18
18: ; preds = %15
%19 = call i32 @strncpy(i8* %11, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 8)
br label %30
20: ; preds = %15, %12, %1
%21 = call i64 @strncmp(i8* %5, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 5)
%22 = icmp eq i64 %21, 0
%23 = getelementptr inbounds [14 x i8], [14 x i8]* %3, i64 0, i64 5
%24 = select i1 %22, i8* %23, i8* %5
%25 = call i32 @strncpy(i8* %11, i8* %24, i32 7)
%26 = getelementptr inbounds i8, i8* %11, i64 7
store i8 0, i8* %26
%27 = call i32 (...) @setutent()
%28 = call i32 @pututline(%struct.utmp* %2)
%29 = call i32 (...) @endutent()
br label %30
30: ; preds = %18, %20
%31 = load i32, i32* @_PATH_WTMP
%32 = call i32 @updwtmp(i32 %31, %struct.utmp* %2)
ret void
}