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

/* Variables and functions */
int DEAD_PROCESS;
int /*<<< orphan*/ F_OK;
int /*<<< orphan*/ UTMP_FILE;
int UT_LINESIZE;
int UT_NAMESIZE;
scalar_t__ access(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ endutent();
struct utmp *getutid(struct utmp *);
int /*<<< orphan*/ memset(struct utmp *, int /*<<< orphan*/, int);
int /*<<< orphan*/ pututline(struct utmp *);
int /*<<< orphan*/ setutent();
int /*<<< orphan*/ strncpy(char *, char *, int);
int /*<<< orphan*/ time(int /*<<< orphan*/ *);
void write_utmp(char *user, char *id, int pid, int type, char *line,
                char *oldline) {
  struct utmp utmp;
  struct utmp tmp;
  struct utmp *utmptr;
  if (access(UTMP_FILE, F_OK) < 0)
    return;
  memset(&utmp, 0, sizeof(utmp));
  utmp.ut_type = type;
  utmp.ut_pid = pid;
  strncpy(utmp.ut_id, id, sizeof(utmp.ut_id));
  time(&utmp.ut_time);
  strncpy(utmp.ut_user, user, UT_NAMESIZE);
  if (line)
    strncpy(utmp.ut_line, line, UT_LINESIZE);
  if (type == DEAD_PROCESS) {
    setutent();
    tmp = utmp;
    if ((utmptr = getutid(&tmp)) != NULL) {
      strncpy(utmp.ut_line, utmptr->ut_line, UT_LINESIZE);
      if (oldline)
        strncpy(oldline, utmptr->ut_line, UT_LINESIZE);
    }
  }
  setutent();
  pututline(&utmp);
  endutent();
}


x86 assembly (clang, -O0):
<write_utmp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
mov    %r8,-0x20(%rbp)
mov    %r9,-0x28(%rbp)
mov    0x2e30(%rip),%rax
mov    (%rax),%edi
mov    0x2df7(%rip),%rax
mov    (%rax),%esi
call   1090 <access@plt>
cmp    $0x0,%rax
jge    11d7 <write_utmp+0x47>
jmp    12eb <write_utmp+0x15b>
lea    -0x50(%rbp),%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1040 <memset@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x50(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x48(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    $0x8,%edx
call   1030 <strncpy@plt>
lea    -0x50(%rbp),%rdi
add    $0x20,%rdi
call   1070 <time@plt>
mov    -0x40(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    0x2dd7(%rip),%rax
mov    (%rax),%edx
call   1030 <strncpy@plt>
cmpq   $0x0,-0x20(%rbp)
je     1249 <write_utmp+0xb9>
mov    -0x38(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    0x2d96(%rip),%rax
mov    (%rax),%edx
call   1030 <strncpy@plt>
mov    -0x18(%rbp),%eax
mov    0x2d75(%rip),%rcx
cmp    (%rcx),%eax
jne    12d4 <write_utmp+0x144>
mov    $0x0,%al
call   1080 <setutent@plt>
lea    -0x78(%rbp),%rdi
lea    -0x50(%rbp),%rsi
mov    $0x28,%edx
call   1060 <memcpy@plt>
lea    -0x78(%rbp),%rdi
call   10b0 <getutid@plt>
mov    %rax,-0x80(%rbp)
cmp    $0x0,%rax
je     12cf <write_utmp+0x13f>
mov    -0x38(%rbp),%rdi
mov    -0x80(%rbp),%rax
mov    0x18(%rax),%rsi
mov    0x2d3a(%rip),%rax
mov    (%rax),%edx
call   1030 <strncpy@plt>
cmpq   $0x0,-0x28(%rbp)
je     12ca <write_utmp+0x13a>
mov    -0x28(%rbp),%rdi
mov    -0x80(%rbp),%rax
mov    0x18(%rax),%rsi
mov    0x2d15(%rip),%rax
mov    (%rax),%edx
call   1030 <strncpy@plt>
jmp    12cf <write_utmp+0x13f>
jmp    12d4 <write_utmp+0x144>
mov    $0x0,%al
call   1080 <setutent@plt>
lea    -0x50(%rbp),%rdi
call   10a0 <pututline@plt>
mov    $0x0,%al
call   1050 <endutent@plt>
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_utmp>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x58,%rsp
mov    %r9,%r14
mov    %r8,%r15
mov    %ecx,%r13d
mov    %edx,%ebp
mov    %rsi,%rbx
mov    %rdi,%r12
mov    0x2e42(%rip),%rax
mov    (%rax),%edi
mov    0x2e09(%rip),%rax
mov    (%rax),%esi
call   1080 <access@plt>
test   %rax,%rax
js     12ad <write_utmp+0x12d>
lea    0x8(%rsp),%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1040 <memset@plt>
mov    %r13d,0x8(%rsp)
mov    %ebp,0xc(%rsp)
mov    0x10(%rsp),%rdi
mov    %rbx,%rsi
mov    $0x8,%edx
call   1030 <strncpy@plt>
lea    0x28(%rsp),%rdi
call   1060 <time@plt>
mov    0x18(%rsp),%rdi
mov    0x2df7(%rip),%rax
mov    (%rax),%edx
mov    %r12,%rsi
call   1030 <strncpy@plt>
test   %r15,%r15
je     1226 <write_utmp+0xa6>
mov    0x20(%rsp),%rdi
mov    0x2dbc(%rip),%rax
mov    (%rax),%edx
mov    %r15,%rsi
call   1030 <strncpy@plt>
mov    0x2d9b(%rip),%rax
cmp    %r13d,(%rax)
jne    1295 <write_utmp+0x115>
xor    %eax,%eax
call   1070 <setutent@plt>
movups 0x8(%rsp),%xmm0
movups 0x18(%rsp),%xmm1
movaps %xmm0,0x30(%rsp)
movaps %xmm1,0x40(%rsp)
mov    0x28(%rsp),%rax
mov    %rax,0x50(%rsp)
lea    0x30(%rsp),%rdi
call   10a0 <getutid@plt>
test   %rax,%rax
je     1295 <write_utmp+0x115>
mov    %rax,%rbx
mov    0x20(%rsp),%rdi
mov    0x18(%rax),%rsi
mov    0x2d5f(%rip),%rbp
mov    0x0(%rbp),%edx
call   1030 <strncpy@plt>
test   %r14,%r14
je     1295 <write_utmp+0x115>
mov    0x18(%rbx),%rsi
mov    0x0(%rbp),%edx
mov    %r14,%rdi
call   1030 <strncpy@plt>
xor    %eax,%eax
call   1070 <setutent@plt>
lea    0x8(%rsp),%rdi
call   1090 <pututline@plt>
xor    %eax,%eax
call   1050 <endutent@plt>
add    $0x58,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.utmp = type { i32, i32, i8*, i8*, i8*, i32 }
@UTMP_FILE = global i32 0
@F_OK = global i32 0
@UT_NAMESIZE = global i32 0
@UT_LINESIZE = global i32 0
@DEAD_PROCESS = global i32 0
define void @write_utmp(i8* %0, i8* %1, i32 %2, i32 %3, i8* %4, i8* %5) {
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i8*
%12 = alloca i8*
%13 = alloca %struct.utmp
%14 = alloca %struct.utmp
%15 = alloca %struct.utmp*
store i8* %0, i8** %7
store i8* %1, i8** %8
store i32 %2, i32* %9
store i32 %3, i32* %10
store i8* %4, i8** %11
store i8* %5, i8** %12
%16 = load i32, i32* @UTMP_FILE
%17 = load i32, i32* @F_OK
%18 = call i64 @access(i32 %16, i32 %17)
%19 = icmp slt i64 %18, 0
br i1 %19, label %20, label %21
20: ; preds = %6
br label %79
21: ; preds = %6
%22 = call i32 @memset(%struct.utmp* %13, i32 0, i32 40)
%23 = load i32, i32* %10
%24 = getelementptr inbounds %struct.utmp, %struct.utmp* %13, i32 0, i32 0
store i32 %23, i32* %24
%25 = load i32, i32* %9
%26 = getelementptr inbounds %struct.utmp, %struct.utmp* %13, i32 0, i32 1
store i32 %25, i32* %26
%27 = getelementptr inbounds %struct.utmp, %struct.utmp* %13, i32 0, i32 2
%28 = load i8*, i8** %27
%29 = load i8*, i8** %8
%30 = call i32 @strncpy(i8* %28, i8* %29, i32 8)
%31 = getelementptr inbounds %struct.utmp, %struct.utmp* %13, i32 0, i32 5
%32 = call i32 @time(i32* %31)
%33 = getelementptr inbounds %struct.utmp, %struct.utmp* %13, i32 0, i32 3
%34 = load i8*, i8** %33
%35 = load i8*, i8** %7
%36 = load i32, i32* @UT_NAMESIZE
%37 = call i32 @strncpy(i8* %34, i8* %35, i32 %36)
%38 = load i8*, i8** %11
%39 = icmp ne i8* %38, null
br i1 %39, label %40, label %46
40: ; preds = %21
%41 = getelementptr inbounds %struct.utmp, %struct.utmp* %13, i32 0, i32 4
%42 = load i8*, i8** %41
%43 = load i8*, i8** %11
%44 = load i32, i32* @UT_LINESIZE
%45 = call i32 @strncpy(i8* %42, i8* %43, i32 %44)
br label %46
46: ; preds = %40, %21
%47 = load i32, i32* %10
%48 = load i32, i32* @DEAD_PROCESS
%49 = icmp eq i32 %47, %48
br i1 %49, label %50, label %75
50: ; preds = %46
%51 = call i32 (...) @setutent()
%52 = bitcast %struct.utmp* %14 to i8*
%53 = bitcast %struct.utmp* %13 to i8*
%54 = call %struct.utmp* @getutid(%struct.utmp* %14)
store %struct.utmp* %54, %struct.utmp** %15
%55 = icmp ne %struct.utmp* %54, null
br i1 %55, label %56, label %74
56: ; preds = %50
%57 = getelementptr inbounds %struct.utmp, %struct.utmp* %13, i32 0, i32 4
%58 = load i8*, i8** %57
%59 = load %struct.utmp*, %struct.utmp** %15
%60 = getelementptr inbounds %struct.utmp, %struct.utmp* %59, i32 0, i32 4
%61 = load i8*, i8** %60
%62 = load i32, i32* @UT_LINESIZE
%63 = call i32 @strncpy(i8* %58, i8* %61, i32 %62)
%64 = load i8*, i8** %12
%65 = icmp ne i8* %64, null
br i1 %65, label %66, label %73
66: ; preds = %56
%67 = load i8*, i8** %12
%68 = load %struct.utmp*, %struct.utmp** %15
%69 = getelementptr inbounds %struct.utmp, %struct.utmp* %68, i32 0, i32 4
%70 = load i8*, i8** %69
%71 = load i32, i32* @UT_LINESIZE
%72 = call i32 @strncpy(i8* %67, i8* %70, i32 %71)
br label %73
73: ; preds = %66, %56
br label %74
74: ; preds = %73, %50
br label %75
75: ; preds = %74, %46
%76 = call i32 (...) @setutent()
%77 = call i32 @pututline(%struct.utmp* %13)
%78 = call i32 (...) @endutent()
br label %79
79: ; preds = %75, %20
ret void
}

LLVM IR (-O2):
%struct.utmp = type { i32, i32, i8*, i8*, i8*, i32 }
@UTMP_FILE = global i32 0
@F_OK = global i32 0
@UT_NAMESIZE = global i32 0
@UT_LINESIZE = global i32 0
@DEAD_PROCESS = global i32 0
define void @write_utmp(i8* %0, i8* %1, i32 %2, i32 %3, i8* %4, i8* %5) {
%7 = alloca %struct.utmp
%8 = alloca %struct.utmp
%9 = bitcast %struct.utmp* %7 to i8*
%10 = bitcast %struct.utmp* %8 to i8*
%11 = load i32, i32* @UTMP_FILE
%12 = load i32, i32* @F_OK
%13 = tail call i64 @access(i32 %11, i32 %12)
%14 = icmp slt i64 %13, 0
br i1 %14, label %57, label %15
15: ; preds = %6
%16 = call i32 @memset(%struct.utmp* %7, i32 0, i32 40)
%17 = getelementptr inbounds %struct.utmp, %struct.utmp* %7, i64 0, i32 0
store i32 %3, i32* %17
%18 = getelementptr inbounds %struct.utmp, %struct.utmp* %7, i64 0, i32 1
store i32 %2, i32* %18
%19 = getelementptr inbounds %struct.utmp, %struct.utmp* %7, i64 0, i32 2
%20 = load i8*, i8** %19
%21 = call i32 @strncpy(i8* %20, i8* %1, i32 8)
%22 = getelementptr inbounds %struct.utmp, %struct.utmp* %7, i64 0, i32 5
%23 = call i32 @time(i32* %22)
%24 = getelementptr inbounds %struct.utmp, %struct.utmp* %7, i64 0, i32 3
%25 = load i8*, i8** %24
%26 = load i32, i32* @UT_NAMESIZE
%27 = call i32 @strncpy(i8* %25, i8* %0, i32 %26)
%28 = icmp eq i8* %4, null
br i1 %28, label %34, label %29
29: ; preds = %15
%30 = getelementptr inbounds %struct.utmp, %struct.utmp* %7, i64 0, i32 4
%31 = load i8*, i8** %30
%32 = load i32, i32* @UT_LINESIZE
%33 = call i32 @strncpy(i8* %31, i8* %4, i32 %32)
br label %34
34: ; preds = %29, %15
%35 = load i32, i32* @DEAD_PROCESS
%36 = icmp eq i32 %35, %3
br i1 %36, label %37, label %53
37: ; preds = %34
%38 = call i32 (...) @setutent()
%39 = call %struct.utmp* @getutid(%struct.utmp* %8)
%40 = icmp eq %struct.utmp* %39, null
br i1 %40, label %53, label %41
41: ; preds = %37
%42 = getelementptr inbounds %struct.utmp, %struct.utmp* %7, i64 0, i32 4
%43 = load i8*, i8** %42
%44 = getelementptr inbounds %struct.utmp, %struct.utmp* %39, i64 0, i32 4
%45 = load i8*, i8** %44
%46 = load i32, i32* @UT_LINESIZE
%47 = call i32 @strncpy(i8* %43, i8* %45, i32 %46)
%48 = icmp eq i8* %5, null
br i1 %48, label %53, label %49
49: ; preds = %41
%50 = load i8*, i8** %44
%51 = load i32, i32* @UT_LINESIZE
%52 = call i32 @strncpy(i8* %5, i8* %50, i32 %51)
br label %53
53: ; preds = %37, %49, %41, %34
%54 = call i32 (...) @setutent()
%55 = call i32 @pututline(%struct.utmp* %7)
%56 = call i32 (...) @endutent()
br label %57
57: ; preds = %6, %53
ret void
}