#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;
};

/* Variables and functions */
int O_APPEND;
int O_WRONLY;
int /*<<< orphan*/ WTMP_FILE;
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;
  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    0x2e4f(%rip),%rax
mov    (%rax),%edi
mov    0x2e36(%rip),%rax
mov    (%rax),%esi
mov    0x2e25(%rip),%rax
or     (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jge    11bb <write_wtmp+0x4b>
jmp    1254 <write_wtmp+0xe4>
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    123b <write_wtmp+0xcb>
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    0x2e4d(%rip),%rax
mov    (%rax),%edi
mov    0x2e34(%rip),%rax
mov    0x2e25(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
call   1090 <open@plt>
test   %eax,%eax
js     1242 <write_wtmp+0xd2>
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    122c <write_wtmp+0xbc>
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* }
@WTMP_FILE = 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
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
%14 = load i32, i32* @WTMP_FILE
%15 = load i32, i32* @O_WRONLY
%16 = load i32, i32* @O_APPEND
%17 = or i32 %15, %16
%18 = call i32 @open(i32 %14, i32 %17)
store i32 %18, i32* %11
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %21
20: ; preds = %5
br label %55
21: ; preds = %5
%22 = call i32 @memset(%struct.utmp* %12, i32 0, i32 28)
%23 = getelementptr inbounds %struct.utmp, %struct.utmp* %12, i32 0, i32 6
%24 = call i32 @time(i32* %23)
%25 = load i32, i32* %8
%26 = getelementptr inbounds %struct.utmp, %struct.utmp* %12, i32 0, i32 0
store i32 %25, i32* %26
%27 = load i32, i32* %9
%28 = getelementptr inbounds %struct.utmp, %struct.utmp* %12, i32 0, i32 1
store i32 %27, i32* %28
%29 = getelementptr inbounds %struct.utmp, %struct.utmp* %12, i32 0, i32 5
%30 = load i32, i32* %29
%31 = load i8*, i8** %6
%32 = call i32 @strncpy(i32 %30, i8* %31, i32 4)
%33 = getelementptr inbounds %struct.utmp, %struct.utmp* %12, i32 0, i32 4
%34 = load i32, i32* %33
%35 = load i8*, i8** %7
%36 = call i32 @strncpy(i32 %34, i8* %35, i32 4)
%37 = getelementptr inbounds %struct.utmp, %struct.utmp* %12, i32 0, i32 3
%38 = load i32, i32* %37
%39 = load i8*, i8** %10
%40 = call i32 @strncpy(i32 %38, i8* %39, i32 4)
%41 = call i64 @uname(%struct.utsname* %13)
%42 = icmp eq i64 %41, 0
br i1 %42, label %43, label %49
43: ; preds = %21
%44 = getelementptr inbounds %struct.utmp, %struct.utmp* %12, i32 0, i32 2
%45 = load i32, i32* %44
%46 = getelementptr inbounds %struct.utsname, %struct.utsname* %13, i32 0, i32 0
%47 = load i8*, i8** %46
%48 = call i32 @strncpy(i32 %45, i8* %47, i32 4)
br label %49
49: ; preds = %43, %21
%50 = load i32, i32* %11
%51 = bitcast %struct.utmp* %12 to i8*
%52 = call i32 @write(i32 %50, i8* %51, i32 28)
%53 = load i32, i32* %11
%54 = call i32 @close(i32 %53)
br label %55
55: ; preds = %49, %20
ret void
}

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