#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*/ ut;
struct utmp {
  int /*<<< orphan*/ ut_time;
  int /*<<< orphan*/ ut_name;
  int /*<<< orphan*/ ut_line;
};
struct stat {
  int /*<<< orphan*/ st_size;
};

/* Variables and functions */
int O_APPEND;
int O_WRONLY;
int /*<<< orphan*/ _PATH_WTMP;
scalar_t__ fstat(int, struct stat *);
int /*<<< orphan*/ ftruncate(int, int /*<<< orphan*/);
int /*<<< orphan*/ memset(struct utmp *, int /*<<< orphan*/, int);
int open(int /*<<< orphan*/, int, int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char const *, int);
int /*<<< orphan*/ time(int /*<<< orphan*/ *);
int write(int, char *, int);
void logwtmp(const char *line, const char *name, const char *host) {
  struct utmp ut;
  memset(&ut, 0, sizeof(ut));
  strncpy(ut.ut_line, line, sizeof ut.ut_line);
  strncpy(ut.ut_name, name, sizeof ut.ut_name);
  time(&ut.ut_time);
  {
    struct utmp ut;
    struct stat buf;
    static int fd = -1;
    if (fd < 0 && (fd = open(_PATH_WTMP, O_WRONLY | O_APPEND, 0)) < 0)
      return;
    if (fstat(fd, &buf) == 0) {
      if (write(fd, (char *)&ut, sizeof(struct utmp)) != sizeof(struct utmp))
        ftruncate(fd, buf.st_size);
    }
  }
}


x86 assembly (clang, -O0):
<logwtmp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    -0x20(%rbp),%edi
mov    -0x8(%rbp),%rsi
mov    $0x4,%edx
call   1030 <strncpy@plt>
mov    -0x24(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x4,%edx
call   1030 <strncpy@plt>
lea    -0x28(%rbp),%rdi
call   1070 <time@plt>
cmpl   $0x0,0x2e92(%rip)
jge    1202 <logwtmp+0x92>
mov    0x2e0d(%rip),%rax
mov    (%rax),%edi
mov    0x2df4(%rip),%rax
mov    (%rax),%esi
mov    0x2de3(%rip),%rax
or     (%rax),%esi
xor    %edx,%edx
call   1080 <open@plt>
mov    %eax,0x2e64(%rip)
cmp    $0x0,%eax
jge    1202 <logwtmp+0x92>
jmp    124e <logwtmp+0xde>
mov    0x2e50(%rip),%edi
lea    -0x40(%rbp),%rsi
call   1090 <fstat@plt>
cmp    $0x0,%rax
jne    124e <logwtmp+0xde>
mov    0x2e37(%rip),%edi
lea    -0x38(%rbp),%rsi
mov    $0xc,%edx
call   1040 <write@plt>
cltq
cmp    $0xc,%rax
je     1249 <logwtmp+0xd9>
mov    0x2e17(%rip),%edi
mov    -0x40(%rbp),%esi
call   1050 <ftruncate@plt>
jmp    124e <logwtmp+0xde>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<logwtmp>:
push   %r15
push   %r14
push   %rbx
sub    $0x30,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
lea    0x10(%rsp),%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x18(%rsp),%edi
mov    %rbx,%rsi
mov    $0x4,%edx
call   1030 <strncpy@plt>
mov    0x14(%rsp),%edi
mov    %r14,%rsi
mov    $0x4,%edx
call   1030 <strncpy@plt>
mov    %r15,%rdi
call   1070 <time@plt>
mov    0x2e95(%rip),%edi
test   %edi,%edi
jns    11f5 <logwtmp+0x85>
mov    0x2e12(%rip),%rax
mov    (%rax),%edi
mov    0x2df9(%rip),%rax
mov    0x2dea(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
xor    %edx,%edx
call   1080 <open@plt>
mov    %eax,%edi
mov    %eax,0x2e67(%rip)
test   %eax,%eax
js     122d <logwtmp+0xbd>
lea    0x8(%rsp),%rsi
call   1090 <fstat@plt>
test   %rax,%rax
jne    122d <logwtmp+0xbd>
mov    0x2e4e(%rip),%edi
lea    0x20(%rsp),%rsi
mov    $0xc,%edx
call   1040 <write@plt>
cmp    $0xc,%eax
je     122d <logwtmp+0xbd>
mov    0x2e34(%rip),%edi
mov    0x8(%rsp),%esi
call   1050 <ftruncate@plt>
add    $0x30,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
%struct.utmp = type { i32, i32, i32 }
%struct.stat = type { i32 }
@logwtmp.fd = internal unnamed_addr global i32 -1
@_PATH_WTMP = global i32 0
@O_WRONLY = global i32 0
@O_APPEND = global i32 0
define void @logwtmp(i8* %0, i8* %1, i8* nocapture readnone %2) {
%4 = alloca %struct.utmp
%5 = alloca %struct.utmp
%6 = alloca %struct.stat
%7 = bitcast %struct.utmp* %4 to i8*
%8 = call i32 @memset(%struct.utmp* %4, i32 0, i32 12)
%9 = getelementptr inbounds %struct.utmp, %struct.utmp* %4, i64 0, i32 2
%10 = load i32, i32* %9
%11 = call i32 @strncpy(i32 %10, i8* %0, i32 4)
%12 = getelementptr inbounds %struct.utmp, %struct.utmp* %4, i64 0, i32 1
%13 = load i32, i32* %12
%14 = call i32 @strncpy(i32 %13, i8* %1, i32 4)
%15 = getelementptr inbounds %struct.utmp, %struct.utmp* %4, i64 0, i32 0
%16 = call i32 @time(i32* %15)
%17 = bitcast %struct.utmp* %5 to i8*
%18 = bitcast %struct.stat* %6 to i8*
%19 = load i32, i32* @logwtmp.fd
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %28
21: ; preds = %3
%22 = load i32, i32* @_PATH_WTMP
%23 = load i32, i32* @O_WRONLY
%24 = load i32, i32* @O_APPEND
%25 = or i32 %24, %23
%26 = call i32 @open(i32 %22, i32 %25, i32 0)
store i32 %26, i32* @logwtmp.fd
%27 = icmp slt i32 %26, 0
br i1 %27, label %41, label %28
28: ; preds = %21, %3
%29 = phi i32 [ %26, %21 ], [ %19, %3 ]
%30 = call i64 @fstat(i32 %29, %struct.stat* %6)
%31 = icmp eq i64 %30, 0
br i1 %31, label %32, label %41
32: ; preds = %28
%33 = load i32, i32* @logwtmp.fd
%34 = call i32 @write(i32 %33, i8* %17, i32 12)
%35 = icmp eq i32 %34, 12
br i1 %35, label %41, label %36
36: ; preds = %32
%37 = load i32, i32* @logwtmp.fd
%38 = getelementptr inbounds %struct.stat, %struct.stat* %6, i64 0, i32 0
%39 = load i32, i32* %38
%40 = call i32 @ftruncate(i32 %37, i32 %39)
br label %41
41: ; preds = %28, %36, %32, %21
ret void
}