#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 {
  char *ut_id;
  int ut_session;
  int /*<<< orphan*/ ut_user;
  int /*<<< orphan*/ ut_line;
  int /*<<< orphan*/ ut_time;
  int /*<<< orphan*/ ut_pid;
  int /*<<< orphan*/ ut_type;
};
struct passwd {
  char *pw_name;
};

/* Variables and functions */
int /*<<< orphan*/ USER_PROCESS;
int /*<<< orphan*/ UT_LINESIZE;
int /*<<< orphan*/ UT_NAMESIZE;
int /*<<< orphan*/ getpid();
int /*<<< orphan*/ pututline(struct utmp *);
int strlen(char *);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char *, int /*<<< orphan*/);
int /*<<< orphan*/ time(int /*<<< orphan*/ *);
char *ttyname(int /*<<< orphan*/);
void write_utmp(struct passwd *pwd) {
  struct utmp ut = {
      .ut_type = USER_PROCESS,
      .ut_pid = getpid(),
      .ut_id = "1",
      .ut_time = time(NULL),
      .ut_session = 1,
  };
  char *tname = ttyname(0);
  if (tname && strlen(tname) > 5) {
    strncpy(ut.ut_line, tname + 5, UT_LINESIZE);
  }
  strncpy(ut.ut_user, pwd->pw_name, UT_NAMESIZE);
  pututline(&ut);
}


x86 assembly (clang, -O0):
<write_utmp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
lea    0xe8d(%rip),%rax
mov    %rax,-0x28(%rbp)
movl   $0x1,-0x20(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x18(%rbp)
xor    %eax,%eax
mov    %eax,%edi
call   1060 <time@plt>
mov    %eax,-0x14(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,-0x10(%rbp)
mov    0x2e37(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
xor    %edi,%edi
call   1070 <ttyname@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     11ef <write_utmp+0x8f>
mov    -0x30(%rbp),%rdi
call   1050 <strlen@plt>
cmp    $0x5,%eax
jle    11ef <write_utmp+0x8f>
mov    -0x18(%rbp),%edi
mov    -0x30(%rbp),%rsi
add    $0x5,%rsi
mov    0x2df0(%rip),%rax
mov    (%rax),%edx
call   1030 <strncpy@plt>
mov    -0x1c(%rbp),%edi
mov    -0x8(%rbp),%rax
mov    (%rax),%rsi
mov    0x2df8(%rip),%rax
mov    (%rax),%edx
call   1030 <strncpy@plt>
lea    -0x28(%rbp),%rdi
call   1080 <pututline@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_utmp>:
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %rdi,%r14
lea    0xe8f(%rip),%rax
mov    %rax,0x8(%rsp)
movq   $0x1,0x10(%rsp)
movl   $0x0,0x18(%rsp)
xor    %edi,%edi
call   1060 <time@plt>
mov    %eax,0x1c(%rsp)
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %eax,0x20(%rsp)
mov    0x2e3c(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x24(%rsp)
xor    %edi,%edi
call   1070 <ttyname@plt>
test   %rax,%rax
je     11dd <write_utmp+0x7d>
mov    %rax,%rbx
mov    %rax,%rdi
call   1050 <strlen@plt>
cmp    $0x6,%eax
jl     11dd <write_utmp+0x7d>
add    $0x5,%rbx
mov    0x2e07(%rip),%rax
mov    (%rax),%edx
xor    %edi,%edi
mov    %rbx,%rsi
call   1030 <strncpy@plt>
mov    (%r14),%rsi
mov    0x2e11(%rip),%rax
mov    (%rax),%edx
xor    %edi,%edi
call   1030 <strncpy@plt>
lea    0x8(%rsp),%rdi
call   1080 <pututline@plt>
add    $0x28,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.passwd = type { i8* }
%struct.utmp = type { i8*, i32, i32, i32, i32, i32, i32 }
@.str = [2 x i8] c"1\00"
@USER_PROCESS = global i32 0
@UT_LINESIZE = global i32 0
@UT_NAMESIZE = global i32 0
define void @write_utmp(%struct.passwd* %0) {
%2 = alloca %struct.passwd*
%3 = alloca %struct.utmp
%4 = alloca i8*
store %struct.passwd* %0, %struct.passwd** %2
%5 = getelementptr inbounds %struct.utmp, %struct.utmp* %3, i32 0, i32 0
store i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8** %5
%6 = getelementptr inbounds %struct.utmp, %struct.utmp* %3, i32 0, i32 1
store i32 1, i32* %6
%7 = getelementptr inbounds %struct.utmp, %struct.utmp* %3, i32 0, i32 2
store i32 0, i32* %7
%8 = getelementptr inbounds %struct.utmp, %struct.utmp* %3, i32 0, i32 3
store i32 0, i32* %8
%9 = getelementptr inbounds %struct.utmp, %struct.utmp* %3, i32 0, i32 4
%10 = call i32 @time(i32* null)
store i32 %10, i32* %9
%11 = getelementptr inbounds %struct.utmp, %struct.utmp* %3, i32 0, i32 5
%12 = call i32 (...) @getpid()
store i32 %12, i32* %11
%13 = getelementptr inbounds %struct.utmp, %struct.utmp* %3, i32 0, i32 6
%14 = load i32, i32* @USER_PROCESS
store i32 %14, i32* %13
%15 = call i8* @ttyname(i32 0)
store i8* %15, i8** %4
%16 = load i8*, i8** %4
%17 = icmp ne i8* %16, null
br i1 %17, label %18, label %29
18: ; preds = %1
%19 = load i8*, i8** %4
%20 = call i32 @strlen(i8* %19)
%21 = icmp sgt i32 %20, 5
br i1 %21, label %22, label %29
22: ; preds = %18
%23 = getelementptr inbounds %struct.utmp, %struct.utmp* %3, i32 0, i32 3
%24 = load i32, i32* %23
%25 = load i8*, i8** %4
%26 = getelementptr inbounds i8, i8* %25, i64 5
%27 = load i32, i32* @UT_LINESIZE
%28 = call i32 @strncpy(i32 %24, i8* %26, i32 %27)
br label %29
29: ; preds = %22, %18, %1
%30 = getelementptr inbounds %struct.utmp, %struct.utmp* %3, i32 0, i32 2
%31 = load i32, i32* %30
%32 = load %struct.passwd*, %struct.passwd** %2
%33 = getelementptr inbounds %struct.passwd, %struct.passwd* %32, i32 0, i32 0
%34 = load i8*, i8** %33
%35 = load i32, i32* @UT_NAMESIZE
%36 = call i32 @strncpy(i32 %31, i8* %34, i32 %35)
%37 = call i32 @pututline(%struct.utmp* %3)
ret void
}

LLVM IR (-O2):
%struct.passwd = type { i8* }
%struct.utmp = type { i8*, i32, i32, i32, i32, i32, i32 }
@.str = [2 x i8] c"1\00"
@USER_PROCESS = global i32 0
@UT_LINESIZE = global i32 0
@UT_NAMESIZE = global i32 0
define void @write_utmp(%struct.passwd* nocapture %0) {
%2 = alloca %struct.utmp
%3 = bitcast %struct.utmp* %2 to i8*
%4 = getelementptr inbounds %struct.utmp, %struct.utmp* %2, i64 0, i32 0
store i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8** %4
%5 = getelementptr inbounds %struct.utmp, %struct.utmp* %2, i64 0, i32 1
store i32 1, i32* %5
%6 = getelementptr inbounds %struct.utmp, %struct.utmp* %2, i64 0, i32 2
store i32 0, i32* %6
%7 = getelementptr inbounds %struct.utmp, %struct.utmp* %2, i64 0, i32 3
store i32 0, i32* %7
%8 = getelementptr inbounds %struct.utmp, %struct.utmp* %2, i64 0, i32 4
%9 = tail call i32 @time(i32* null)
store i32 %9, i32* %8
%10 = getelementptr inbounds %struct.utmp, %struct.utmp* %2, i64 0, i32 5
%11 = tail call i32 (...) @getpid()
store i32 %11, i32* %10
%12 = getelementptr inbounds %struct.utmp, %struct.utmp* %2, i64 0, i32 6
%13 = load i32, i32* @USER_PROCESS
store i32 %13, i32* %12
%14 = tail call i8* @ttyname(i32 0)
%15 = icmp eq i8* %14, null
br i1 %15, label %23, label %16
16: ; preds = %1
%17 = tail call i32 @strlen(i8* %14)
%18 = icmp sgt i32 %17, 5
br i1 %18, label %19, label %23
19: ; preds = %16
%20 = getelementptr inbounds i8, i8* %14, i64 5
%21 = load i32, i32* @UT_LINESIZE
%22 = tail call i32 @strncpy(i32 0, i8* %20, i32 %21)
br label %23
23: ; preds = %19, %16, %1
%24 = getelementptr inbounds %struct.passwd, %struct.passwd* %0, i64 0, i32 0
%25 = load i8*, i8** %24
%26 = load i32, i32* @UT_NAMESIZE
%27 = tail call i32 @strncpy(i32 0, i8* %25, i32 %26)
%28 = call i32 @pututline(%struct.utmp* %2)
ret void
}