#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 */
struct utmp {
  int dummy;
};
struct flock {
  int /*<<< orphan*/ l_type;
  int /*<<< orphan*/ l_whence;
};
typedef int off_t;

/* Variables and functions */
int /*<<< orphan*/ F_SETLK;
int /*<<< orphan*/ F_SETLKW;
int /*<<< orphan*/ F_UNLCK;
int /*<<< orphan*/ F_WRLCK;
int /*<<< orphan*/ O_WRONLY;
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fcntl(int, int /*<<< orphan*/, struct flock *);
int /*<<< orphan*/ ftruncate(int, int);
scalar_t__ lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ memset(struct flock *, char, int);
int open(char const *, int /*<<< orphan*/);
int write(int, struct utmp const *, int);
int updwtmp_file(const char *file, const struct utmp *utmp) {
  int result = -1;
  struct flock fl;
  off_t offset;
  int fd;
  fd = open(file, O_WRONLY);
  if (fd < 0)
    return -1;
  memset(&fl, '\0', sizeof(struct flock));
  fl.l_type = F_WRLCK;
  fl.l_whence = SEEK_SET;
  fcntl(fd, F_SETLK, &fl);
  offset = lseek(fd, 0, SEEK_END);
  if (offset % sizeof(struct utmp) != 0) {
    offset -= offset % sizeof(struct utmp);
    ftruncate(fd, offset);
    if (lseek(fd, 0, SEEK_END) < 0)
      goto unlock_return;
  }
  if (write(fd, utmp, sizeof(struct utmp)) != sizeof(struct utmp)) {
    ftruncate(fd, offset);
    goto unlock_return;
  }
  result = 0;
unlock_return:
  fl.l_type = F_UNLCK;
  fcntl(fd, F_SETLKW, &fl);
  close(fd);
  return result;
}


x86 assembly (clang, -O0):
<updwtmp_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0xffffffff,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e16(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0x0,-0x30(%rbp)
jge    11b2 <updwtmp_file+0x42>
movl   $0xffffffff,-0x4(%rbp)
jmp    12be <updwtmp_file+0x14e>
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1070 <memset@plt>
mov    0x2def(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    0x2deb(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x30(%rbp),%edi
mov    0x2df4(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1030 <fcntl@plt>
mov    -0x30(%rbp),%edi
mov    0x2def(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1060 <lseek@plt>
mov    %eax,-0x2c(%rbp)
movslq -0x2c(%rbp),%rax
and    $0x3,%rax
cmp    $0x0,%rax
je     125b <updwtmp_file+0xeb>
movslq -0x2c(%rbp),%rcx
and    $0x3,%rcx
movslq -0x2c(%rbp),%rax
sub    %rcx,%rax
mov    %eax,-0x2c(%rbp)
mov    -0x30(%rbp),%edi
mov    -0x2c(%rbp),%esi
call   1050 <ftruncate@plt>
mov    -0x30(%rbp),%edi
mov    0x2daa(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1060 <lseek@plt>
cmp    $0x0,%rax
jge    1256 <updwtmp_file+0xe6>
jmp    128f <updwtmp_file+0x11f>
jmp    125b <updwtmp_file+0xeb>
mov    -0x30(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    $0x4,%edx
call   1040 <write@plt>
cltq
cmp    $0x4,%rax
je     1288 <updwtmp_file+0x118>
mov    -0x30(%rbp),%edi
mov    -0x2c(%rbp),%esi
call   1050 <ftruncate@plt>
jmp    128f <updwtmp_file+0x11f>
movl   $0x0,-0x1c(%rbp)
mov    0x2d3a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x30(%rbp),%edi
mov    0x2d3b(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1030 <fcntl@plt>
mov    -0x30(%rbp),%edi
call   1080 <close@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<updwtmp_file>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    0x2e22(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
test   %eax,%eax
js     1264 <updwtmp_file+0xf4>
mov    %eax,%ebx
lea    0x8(%rsp),%rbp
xor    %r15d,%r15d
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1070 <memset@plt>
mov    0x2e03(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    0x2dfe(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rsp)
mov    0x2e09(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %rbp,%rdx
call   1030 <fcntl@plt>
mov    0x2e06(%rip),%r12
mov    (%r12),%edx
mov    %ebx,%edi
xor    %esi,%esi
call   1060 <lseek@plt>
mov    %rax,%rbp
test   $0x3,%bpl
je     1216 <updwtmp_file+0xa6>
and    $0xfffffffc,%ebp
mov    %ebx,%edi
mov    %ebp,%esi
call   1050 <ftruncate@plt>
mov    (%r12),%edx
mov    %ebx,%edi
xor    %esi,%esi
call   1060 <lseek@plt>
test   %rax,%rax
js     1233 <updwtmp_file+0xc3>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x4,%edx
call   1040 <write@plt>
cmp    $0x4,%eax
je     1239 <updwtmp_file+0xc9>
mov    %ebx,%edi
mov    %ebp,%esi
call   1050 <ftruncate@plt>
mov    $0xffffffff,%r15d
mov    0x2d90(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    0x2d93(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebx,%edi
call   1030 <fcntl@plt>
mov    %ebx,%edi
call   1080 <close@plt>
jmp    126a <updwtmp_file+0xfa>
mov    $0xffffffff,%r15d
mov    %r15d,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.utmp = type { i32 }
%struct.flock = type { i32, i32 }
@O_WRONLY = global i32 0
@F_WRLCK = global i32 0
@SEEK_SET = global i32 0
@F_SETLK = global i32 0
@SEEK_END = global i32 0
@F_UNLCK = global i32 0
@F_SETLKW = global i32 0
define i32 @updwtmp_file(i8* %0, %struct.utmp* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca %struct.utmp*
%6 = alloca i32
%7 = alloca %struct.flock
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %4
store %struct.utmp* %1, %struct.utmp** %5
store i32 -1, i32* %6
%10 = load i8*, i8** %4
%11 = load i32, i32* @O_WRONLY
%12 = call i32 @open(i8* %10, i32 %11)
store i32 %12, i32* %9
%13 = load i32, i32* %9
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %16
15: ; preds = %2
store i32 -1, i32* %3
br label %70
16: ; preds = %2
%17 = call i32 @memset(%struct.flock* %7, i8 signext 0, i32 8)
%18 = load i32, i32* @F_WRLCK
%19 = getelementptr inbounds %struct.flock, %struct.flock* %7, i32 0, i32 0
store i32 %18, i32* %19
%20 = load i32, i32* @SEEK_SET
%21 = getelementptr inbounds %struct.flock, %struct.flock* %7, i32 0, i32 1
store i32 %20, i32* %21
%22 = load i32, i32* %9
%23 = load i32, i32* @F_SETLK
%24 = call i32 @fcntl(i32 %22, i32 %23, %struct.flock* %7)
%25 = load i32, i32* %9
%26 = load i32, i32* @SEEK_END
%27 = call i64 @lseek(i32 %25, i32 0, i32 %26)
%28 = trunc i64 %27 to i32
store i32 %28, i32* %8
%29 = load i32, i32* %8
%30 = sext i32 %29 to i64
%31 = urem i64 %30, 4
%32 = icmp ne i64 %31, 0
br i1 %32, label %33, label %50
33: ; preds = %16
%34 = load i32, i32* %8
%35 = sext i32 %34 to i64
%36 = urem i64 %35, 4
%37 = load i32, i32* %8
%38 = sext i32 %37 to i64
%39 = sub i64 %38, %36
%40 = trunc i64 %39 to i32
store i32 %40, i32* %8
%41 = load i32, i32* %9
%42 = load i32, i32* %8
%43 = call i32 @ftruncate(i32 %41, i32 %42)
%44 = load i32, i32* %9
%45 = load i32, i32* @SEEK_END
%46 = call i64 @lseek(i32 %44, i32 0, i32 %45)
%47 = icmp slt i64 %46, 0
br i1 %47, label %48, label %49
48: ; preds = %33
br label %61
49: ; preds = %33
br label %50
50: ; preds = %49, %16
%51 = load i32, i32* %9
%52 = load %struct.utmp*, %struct.utmp** %5
%53 = call i32 @write(i32 %51, %struct.utmp* %52, i32 4)
%54 = sext i32 %53 to i64
%55 = icmp ne i64 %54, 4
br i1 %55, label %56, label %60
56: ; preds = %50
%57 = load i32, i32* %9
%58 = load i32, i32* %8
%59 = call i32 @ftruncate(i32 %57, i32 %58)
br label %61
60: ; preds = %50
store i32 0, i32* %6
br label %61
61: ; preds = %60, %56, %48
%62 = load i32, i32* @F_UNLCK
%63 = getelementptr inbounds %struct.flock, %struct.flock* %7, i32 0, i32 0
store i32 %62, i32* %63
%64 = load i32, i32* %9
%65 = load i32, i32* @F_SETLKW
%66 = call i32 @fcntl(i32 %64, i32 %65, %struct.flock* %7)
%67 = load i32, i32* %9
%68 = call i32 @close(i32 %67)
%69 = load i32, i32* %6
store i32 %69, i32* %3
br label %70
70: ; preds = %61, %15
%71 = load i32, i32* %3
ret i32 %71
}

LLVM IR (-O2):
%struct.utmp = type { i32 }
%struct.flock = type { i32, i32 }
@O_WRONLY = global i32 0
@F_WRLCK = global i32 0
@SEEK_SET = global i32 0
@F_SETLK = global i32 0
@SEEK_END = global i32 0
@F_UNLCK = global i32 0
@F_SETLKW = global i32 0
define i32 @updwtmp_file(i8* nocapture %0, %struct.utmp* nocapture %1) {
%3 = alloca %struct.flock
%4 = bitcast %struct.flock* %3 to i8*
%5 = load i32, i32* @O_WRONLY
%6 = tail call i32 @open(i8* %0, i32 %5)
%7 = icmp slt i32 %6, 0
br i1 %7, label %39, label %8
8: ; preds = %2
%9 = call i32 @memset(%struct.flock* %3, i8 signext 0, i32 8)
%10 = load i32, i32* @F_WRLCK
%11 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 0
store i32 %10, i32* %11
%12 = load i32, i32* @SEEK_SET
%13 = getelementptr inbounds %struct.flock, %struct.flock* %3, i64 0, i32 1
store i32 %12, i32* %13
%14 = load i32, i32* @F_SETLK
%15 = call i32 @fcntl(i32 %6, i32 %14, %struct.flock* %3)
%16 = load i32, i32* @SEEK_END
%17 = call i64 @lseek(i32 %6, i32 0, i32 %16)
%18 = trunc i64 %17 to i32
%19 = and i64 %17, 3
%20 = icmp eq i64 %19, 0
br i1 %20, label %27, label %21
21: ; preds = %8
%22 = and i32 %18, -4
%23 = call i32 @ftruncate(i32 %6, i32 %22)
%24 = load i32, i32* @SEEK_END
%25 = call i64 @lseek(i32 %6, i32 0, i32 %24)
%26 = icmp slt i64 %25, 0
br i1 %26, label %33, label %27
27: ; preds = %21, %8
%28 = phi i32 [ %22, %21 ], [ %18, %8 ]
%29 = call i32 @write(i32 %6, %struct.utmp* %1, i32 4)
%30 = icmp eq i32 %29, 4
br i1 %30, label %33, label %31
31: ; preds = %27
%32 = call i32 @ftruncate(i32 %6, i32 %28)
br label %33
33: ; preds = %27, %21, %31
%34 = phi i32 [ -1, %21 ], [ -1, %31 ], [ 0, %27 ]
%35 = load i32, i32* @F_UNLCK
store i32 %35, i32* %11
%36 = load i32, i32* @F_SETLKW
%37 = call i32 @fcntl(i32 %6, i32 %36, %struct.flock* %3)
%38 = call i32 @close(i32 %6)
br label %39
39: ; preds = %2, %33
%40 = phi i32 [ %34, %33 ], [ -1, %2 ]
ret i32 %40
}