#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;
};
struct timeval {
  int dummy;
};

/* Variables and functions */
int DEAD_PROCESS;
int /*<<< orphan*/ UTMP_FILE;
int UT_LINESIZE;
int UT_NAMESIZE;
int /*<<< orphan*/ W_OK;
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;
  struct timeval tv;
  if (access(UTMP_FILE, W_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    $0x90,%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    0x2e17(%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    0x2d8e(%rip),%rax
mov    (%rax),%edx
call   1030 <strncpy@plt>
mov    -0x18(%rbp),%eax
mov    0x2d6d(%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    0x2d32(%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    0x2d0d(%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    $0x90,%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    0x2e29(%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    0x2db4(%rip),%rax
mov    (%rax),%edx
mov    %r15,%rsi
call   1030 <strncpy@plt>
mov    0x2d93(%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    0x2d57(%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 }
%struct.timeval = type { i32 }
@UTMP_FILE = global i32 0
@W_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*
%16 = alloca %struct.timeval
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
%17 = load i32, i32* @UTMP_FILE
%18 = load i32, i32* @W_OK
%19 = call i64 @access(i32 %17, i32 %18)
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %22
21: ; preds = %6
br label %80
22: ; preds = %6
%23 = call i32 @memset(%struct.utmp* %13, i32 0, i32 40)
%24 = load i32, i32* %10
%25 = getelementptr inbounds %struct.utmp, %struct.utmp* %13, i32 0, i32 0
store i32 %24, i32* %25
%26 = load i32, i32* %9
%27 = getelementptr inbounds %struct.utmp, %struct.utmp* %13, i32 0, i32 1
store i32 %26, i32* %27
%28 = getelementptr inbounds %struct.utmp, %struct.utmp* %13, i32 0, i32 2
%29 = load i8*, i8** %28
%30 = load i8*, i8** %8
%31 = call i32 @strncpy(i8* %29, i8* %30, i32 8)
%32 = getelementptr inbounds %struct.utmp, %struct.utmp* %13, i32 0, i32 5
%33 = call i32 @time(i32* %32)
%34 = getelementptr inbounds %struct.utmp, %struct.utmp* %13, i32 0, i32 3
%35 = load i8*, i8** %34
%36 = load i8*, i8** %7
%37 = load i32, i32* @UT_NAMESIZE
%38 = call i32 @strncpy(i8* %35, i8* %36, i32 %37)
%39 = load i8*, i8** %11
%40 = icmp ne i8* %39, null
br i1 %40, label %41, label %47
41: ; preds = %22
%42 = getelementptr inbounds %struct.utmp, %struct.utmp* %13, i32 0, i32 4
%43 = load i8*, i8** %42
%44 = load i8*, i8** %11
%45 = load i32, i32* @UT_LINESIZE
%46 = call i32 @strncpy(i8* %43, i8* %44, i32 %45)
br label %47
47: ; preds = %41, %22
%48 = load i32, i32* %10
%49 = load i32, i32* @DEAD_PROCESS
%50 = icmp eq i32 %48, %49
br i1 %50, label %51, label %76
51: ; preds = %47
%52 = call i32 (...) @setutent()
%53 = bitcast %struct.utmp* %14 to i8*
%54 = bitcast %struct.utmp* %13 to i8*
%55 = call %struct.utmp* @getutid(%struct.utmp* %14)
store %struct.utmp* %55, %struct.utmp** %15
%56 = icmp ne %struct.utmp* %55, null
br i1 %56, label %57, label %75
57: ; preds = %51
%58 = getelementptr inbounds %struct.utmp, %struct.utmp* %13, i32 0, i32 4
%59 = load i8*, i8** %58
%60 = load %struct.utmp*, %struct.utmp** %15
%61 = getelementptr inbounds %struct.utmp, %struct.utmp* %60, i32 0, i32 4
%62 = load i8*, i8** %61
%63 = load i32, i32* @UT_LINESIZE
%64 = call i32 @strncpy(i8* %59, i8* %62, i32 %63)
%65 = load i8*, i8** %12
%66 = icmp ne i8* %65, null
br i1 %66, label %67, label %74
67: ; preds = %57
%68 = load i8*, i8** %12
%69 = load %struct.utmp*, %struct.utmp** %15
%70 = getelementptr inbounds %struct.utmp, %struct.utmp* %69, i32 0, i32 4
%71 = load i8*, i8** %70
%72 = load i32, i32* @UT_LINESIZE
%73 = call i32 @strncpy(i8* %68, i8* %71, i32 %72)
br label %74
74: ; preds = %67, %57
br label %75
75: ; preds = %74, %51
br label %76
76: ; preds = %75, %47
%77 = call i32 (...) @setutent()
%78 = call i32 @pututline(%struct.utmp* %13)
%79 = call i32 (...) @endutent()
br label %80
80: ; preds = %76, %21
ret void
}

LLVM IR (-O2):
%struct.utmp = type { i32, i32, i8*, i8*, i8*, i32 }
@UTMP_FILE = global i32 0
@W_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* @W_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
}