#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 utsname {
  char *release;
};
struct utmp {
  int ut_pid;
  int ut_type;
  int /*<<< orphan*/ ut_host;
  int /*<<< orphan*/ ut_line;
  int /*<<< orphan*/ ut_id;
  int /*<<< orphan*/ ut_name;
  int /*<<< orphan*/ ut_time;
};
struct timeval {
  int dummy;
};

/* Variables and functions */
int O_APPEND;
int O_WRONLY;
int /*<<< orphan*/ WTMP_FILE;
int /*<<< orphan*/ W_OK;
scalar_t__ access(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ memset(struct utmp *, int /*<<< orphan*/, int);
int open(int /*<<< orphan*/, int);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ time(int /*<<< orphan*/ *);
scalar_t__ uname(struct utsname *);
int /*<<< orphan*/ write(int, char *, int);
void write_wtmp(char *user, char *id, int pid, int type, char *line) {
  int fd;
  struct utmp utmp;
  struct utsname uname_buf;
  struct timeval tv;
  if (access(WTMP_FILE, W_OK) < 0)
    return;
  if ((fd = open(WTMP_FILE, O_WRONLY | O_APPEND)) < 0)
    return;
  memset(&utmp, 0, sizeof(utmp));
  time(&utmp.ut_time);
  utmp.ut_pid = pid;
  utmp.ut_type = type;
  strncpy(utmp.ut_name, user, sizeof(utmp.ut_name));
  strncpy(utmp.ut_id, id, sizeof(utmp.ut_id));
  strncpy(utmp.ut_line, line, sizeof(utmp.ut_line));
  if (uname(&uname_buf) == 0)
    strncpy(utmp.ut_host, uname_buf.release, sizeof(utmp.ut_host));
  write(fd, (char *)&utmp, sizeof(utmp));
  close(fd);
}


x86 assembly (clang, -O0):
<write_wtmp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
mov    %r8,-0x20(%rbp)
mov    0x2e37(%rip),%rax
mov    (%rax),%edi
mov    0x2e3e(%rip),%rax
mov    (%rax),%esi
call   10a0 <access@plt>
cmp    $0x0,%rax
jge    11c0 <write_wtmp+0x40>
jmp    128a <write_wtmp+0x10a>
mov    0x2e11(%rip),%rax
mov    (%rax),%edi
mov    0x2df8(%rip),%rax
mov    (%rax),%esi
mov    0x2de7(%rip),%rax
or     (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jge    11f1 <write_wtmp+0x71>
jmp    128a <write_wtmp+0x10a>
lea    -0x40(%rbp),%rdi
xor    %esi,%esi
mov    $0x1c,%edx
call   1060 <memset@plt>
lea    -0x40(%rbp),%rdi
add    $0x18,%rdi
call   1080 <time@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x40(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x2c(%rbp),%edi
mov    -0x8(%rbp),%rsi
mov    $0x4,%edx
call   1030 <strncpy@plt>
mov    -0x30(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x4,%edx
call   1030 <strncpy@plt>
mov    -0x34(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    $0x4,%edx
call   1030 <strncpy@plt>
lea    -0x48(%rbp),%rdi
call   1050 <uname@plt>
cmp    $0x0,%rax
jne    1271 <write_wtmp+0xf1>
mov    -0x38(%rbp),%edi
mov    -0x48(%rbp),%rsi
mov    $0x4,%edx
call   1030 <strncpy@plt>
mov    -0x24(%rbp),%edi
lea    -0x40(%rbp),%rsi
mov    $0x1c,%edx
call   1040 <write@plt>
mov    -0x24(%rbp),%edi
call   1070 <close@plt>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_wtmp>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %r8,%r14
mov    %ecx,%r13d
mov    %edx,%ebp
mov    %rsi,%r15
mov    %rdi,%r12
mov    0x2e35(%rip),%rbx
mov    (%rbx),%edi
mov    0x2e3c(%rip),%rax
mov    (%rax),%esi
call   10a0 <access@plt>
test   %rax,%rax
js     126b <write_wtmp+0xeb>
mov    (%rbx),%edi
mov    0x2e03(%rip),%rax
mov    0x2df4(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
call   1090 <open@plt>
test   %eax,%eax
js     126b <write_wtmp+0xeb>
mov    %eax,%ebx
mov    %rsp,%rdi
xor    %esi,%esi
mov    $0x1c,%edx
call   1060 <memset@plt>
lea    0x18(%rsp),%rdi
call   1080 <time@plt>
mov    %ebp,(%rsp)
mov    %r13d,0x4(%rsp)
mov    0x14(%rsp),%edi
mov    %r12,%rsi
mov    $0x4,%edx
call   1030 <strncpy@plt>
mov    0x10(%rsp),%edi
mov    %r15,%rsi
mov    $0x4,%edx
call   1030 <strncpy@plt>
mov    0xc(%rsp),%edi
mov    %r14,%rsi
mov    $0x4,%edx
call   1030 <strncpy@plt>
lea    0x20(%rsp),%rdi
call   1050 <uname@plt>
test   %rax,%rax
jne    1255 <write_wtmp+0xd5>
mov    0x8(%rsp),%edi
mov    0x20(%rsp),%rsi
mov    $0x4,%edx
call   1030 <strncpy@plt>
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0x1c,%edx
call   1040 <write@plt>
mov    %ebx,%edi
call   1070 <close@plt>
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.utmp = type { i32, i32, i32, i32, i32, i32, i32 }
%struct.utsname = type { i8* }
%struct.timeval = type { i32 }
@WTMP_FILE = global i32 0
@W_OK = global i32 0
@O_WRONLY = global i32 0
@O_APPEND = global i32 0
define void @write_wtmp(i8* %0, i8* %1, i32 %2, i32 %3, i8* %4) {
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i32
%12 = alloca %struct.utmp
%13 = alloca %struct.utsname
%14 = alloca %struct.timeval
store i8* %0, i8** %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store i32 %3, i32* %9
store i8* %4, i8** %10
%15 = load i32, i32* @WTMP_FILE
%16 = load i32, i32* @W_OK
%17 = call i64 @access(i32 %15, i32 %16)
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %20
19: ; preds = %5
br label %62
20: ; preds = %5
%21 = load i32, i32* @WTMP_FILE
%22 = load i32, i32* @O_WRONLY
%23 = load i32, i32* @O_APPEND
%24 = or i32 %22, %23
%25 = call i32 @open(i32 %21, i32 %24)
store i32 %25, i32* %11
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %28
27: ; preds = %20
br label %62
28: ; preds = %20
%29 = call i32 @memset(%struct.utmp* %12, i32 0, i32 28)
%30 = getelementptr inbounds %struct.utmp, %struct.utmp* %12, i32 0, i32 6
%31 = call i32 @time(i32* %30)
%32 = load i32, i32* %8
%33 = getelementptr inbounds %struct.utmp, %struct.utmp* %12, i32 0, i32 0
store i32 %32, i32* %33
%34 = load i32, i32* %9
%35 = getelementptr inbounds %struct.utmp, %struct.utmp* %12, i32 0, i32 1
store i32 %34, i32* %35
%36 = getelementptr inbounds %struct.utmp, %struct.utmp* %12, i32 0, i32 5
%37 = load i32, i32* %36
%38 = load i8*, i8** %6
%39 = call i32 @strncpy(i32 %37, i8* %38, i32 4)
%40 = getelementptr inbounds %struct.utmp, %struct.utmp* %12, i32 0, i32 4
%41 = load i32, i32* %40
%42 = load i8*, i8** %7
%43 = call i32 @strncpy(i32 %41, i8* %42, i32 4)
%44 = getelementptr inbounds %struct.utmp, %struct.utmp* %12, i32 0, i32 3
%45 = load i32, i32* %44
%46 = load i8*, i8** %10
%47 = call i32 @strncpy(i32 %45, i8* %46, i32 4)
%48 = call i64 @uname(%struct.utsname* %13)
%49 = icmp eq i64 %48, 0
br i1 %49, label %50, label %56
50: ; preds = %28
%51 = getelementptr inbounds %struct.utmp, %struct.utmp* %12, i32 0, i32 2
%52 = load i32, i32* %51
%53 = getelementptr inbounds %struct.utsname, %struct.utsname* %13, i32 0, i32 0
%54 = load i8*, i8** %53
%55 = call i32 @strncpy(i32 %52, i8* %54, i32 4)
br label %56
56: ; preds = %50, %28
%57 = load i32, i32* %11
%58 = bitcast %struct.utmp* %12 to i8*
%59 = call i32 @write(i32 %57, i8* %58, i32 28)
%60 = load i32, i32* %11
%61 = call i32 @close(i32 %60)
br label %62
62: ; preds = %56, %27, %19
ret void
}

LLVM IR (-O2):
%struct.utmp = type { i32, i32, i32, i32, i32, i32, i32 }
%struct.utsname = type { i8* }
@WTMP_FILE = global i32 0
@W_OK = global i32 0
@O_WRONLY = global i32 0
@O_APPEND = global i32 0
define void @write_wtmp(i8* %0, i8* %1, i32 %2, i32 %3, i8* %4) {
%6 = alloca %struct.utmp
%7 = alloca %struct.utsname
%8 = bitcast %struct.utmp* %6 to i8*
%9 = bitcast %struct.utsname* %7 to i8*
%10 = load i32, i32* @WTMP_FILE
%11 = load i32, i32* @W_OK
%12 = tail call i64 @access(i32 %10, i32 %11)
%13 = icmp slt i64 %12, 0
br i1 %13, label %47, label %14
14: ; preds = %5
%15 = load i32, i32* @WTMP_FILE
%16 = load i32, i32* @O_WRONLY
%17 = load i32, i32* @O_APPEND
%18 = or i32 %17, %16
%19 = tail call i32 @open(i32 %15, i32 %18)
%20 = icmp slt i32 %19, 0
br i1 %20, label %47, label %21
21: ; preds = %14
%22 = call i32 @memset(%struct.utmp* %6, i32 0, i32 28)
%23 = getelementptr inbounds %struct.utmp, %struct.utmp* %6, i64 0, i32 6
%24 = call i32 @time(i32* %23)
%25 = getelementptr inbounds %struct.utmp, %struct.utmp* %6, i64 0, i32 0
store i32 %2, i32* %25
%26 = getelementptr inbounds %struct.utmp, %struct.utmp* %6, i64 0, i32 1
store i32 %3, i32* %26
%27 = getelementptr inbounds %struct.utmp, %struct.utmp* %6, i64 0, i32 5
%28 = load i32, i32* %27
%29 = call i32 @strncpy(i32 %28, i8* %0, i32 4)
%30 = getelementptr inbounds %struct.utmp, %struct.utmp* %6, i64 0, i32 4
%31 = load i32, i32* %30
%32 = call i32 @strncpy(i32 %31, i8* %1, i32 4)
%33 = getelementptr inbounds %struct.utmp, %struct.utmp* %6, i64 0, i32 3
%34 = load i32, i32* %33
%35 = call i32 @strncpy(i32 %34, i8* %4, i32 4)
%36 = call i64 @uname(%struct.utsname* %7)
%37 = icmp eq i64 %36, 0
br i1 %37, label %38, label %44
38: ; preds = %21
%39 = getelementptr inbounds %struct.utmp, %struct.utmp* %6, i64 0, i32 2
%40 = load i32, i32* %39
%41 = getelementptr inbounds %struct.utsname, %struct.utsname* %7, i64 0, i32 0
%42 = load i8*, i8** %41
%43 = call i32 @strncpy(i32 %40, i8* %42, i32 4)
br label %44
44: ; preds = %38, %21
%45 = call i32 @write(i32 %19, i8* %8, i32 28)
%46 = call i32 @close(i32 %19)
br label %47
47: ; preds = %14, %5, %44
ret void
}