#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 stat {
  scalar_t__ st_dev;
  scalar_t__ st_ino;
};

/* Variables and functions */
int /*<<< orphan*/ LOCK_EX;
int O_CLOEXEC;
int O_NOFOLLOW;
int O_RDWR;
int /*<<< orphan*/ close(int);
scalar_t__ flock(int, int /*<<< orphan*/);
scalar_t__ fstat(int, struct stat *);
int open(char const *, int);
scalar_t__ stat(char const *, struct stat *);
int /*<<< orphan*/ warn(char *, ...);
int /*<<< orphan*/ warnx(char *);
int open_wtmp(const char *wtmppath, struct stat *st) {
  int fd;
  struct stat st2;
  fd = open(wtmppath, O_RDWR | O_NOFOLLOW | O_CLOEXEC);
  if (fd < 0) {
    warn("open(%s)", wtmppath);
    return -1;
  }
  if (flock(fd, LOCK_EX) < 0) {
    warn("flock()");
    goto err;
  }
  if (fstat(fd, st) < 0) {
    warn("fstat()");
    goto err;
  }
  if (stat(wtmppath, &st2) < 0) {
    warn("stat()");
    goto err;
  }
  if (st->st_dev != st2.st_dev || st->st_ino != st2.st_ino) {
    warnx("lost race with other process");
    goto err;
  }
  return fd;
err:
  (void)close(fd);
  return -1;
}


x86 assembly (clang, -O0):
<open_wtmp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e55(%rip),%rax
mov    (%rax),%esi
mov    0x2e64(%rip),%rax
or     (%rax),%esi
mov    0x2e23(%rip),%rax
or     (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11cf <open_wtmp+0x5f>
mov    -0x10(%rbp),%rsi
lea    0xe44(%rip),%rdi
mov    $0x0,%al
call   1030 <warn@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    129e <open_wtmp+0x12e>
mov    -0x1c(%rbp),%edi
mov    0x2dff(%rip),%rax
mov    (%rax),%esi
call   1080 <flock@plt>
cmp    $0x0,%rax
jge    11fd <open_wtmp+0x8d>
lea    0xe18(%rip),%rdi
mov    $0x0,%al
call   1030 <warn@plt>
jmp    128f <open_wtmp+0x11f>
mov    -0x1c(%rbp),%edi
mov    -0x18(%rbp),%rsi
call   1090 <fstat@plt>
cmp    $0x0,%rax
jge    1226 <open_wtmp+0xb6>
lea    0xdf7(%rip),%rdi
mov    $0x0,%al
call   1030 <warn@plt>
jmp    128f <open_wtmp+0x11f>
mov    -0x10(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   1060 <stat@plt>
cmp    $0x0,%rax
jge    1250 <open_wtmp+0xe0>
lea    0xdce(%rip),%rdi
mov    $0x0,%al
call   1030 <warn@plt>
jmp    128f <open_wtmp+0x11f>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
cmp    -0x30(%rbp),%rax
jne    1273 <open_wtmp+0x103>
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%rax
cmp    -0x28(%rbp),%rax
je     1284 <open_wtmp+0x114>
lea    0xd9f(%rip),%rdi
call   1040 <warnx@plt>
jmp    128f <open_wtmp+0x11f>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    129e <open_wtmp+0x12e>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_wtmp>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    0x2e60(%rip),%rcx
mov    0x2e71(%rip),%rax
mov    (%rax),%eax
or     (%rcx),%eax
mov    %rsi,%r14
mov    %rdi,%r15
mov    0x2e28(%rip),%rcx
or     (%rcx),%eax
mov    %eax,%esi
call   1070 <open@plt>
test   %eax,%eax
js     11fd <open_wtmp+0x8d>
mov    %eax,%ebx
mov    0x2e2a(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1080 <flock@plt>
test   %rax,%rax
js     1210 <open_wtmp+0xa0>
mov    %ebx,%edi
mov    %r14,%rsi
call   1090 <fstat@plt>
test   %rax,%rax
js     1219 <open_wtmp+0xa9>
mov    %rsp,%rsi
mov    %r15,%rdi
call   1060 <stat@plt>
test   %rax,%rax
js     1222 <open_wtmp+0xb2>
mov    (%r14),%rax
cmp    (%rsp),%rax
jne    11ef <open_wtmp+0x7f>
mov    0x8(%r14),%rax
cmp    0x8(%rsp),%rax
je     123c <open_wtmp+0xcc>
lea    0xe23(%rip),%rdi
call   1040 <warnx@plt>
jmp    1230 <open_wtmp+0xc0>
lea    0xdfc(%rip),%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   1030 <warn@plt>
jmp    1237 <open_wtmp+0xc7>
lea    0xdf2(%rip),%rdi
jmp    1229 <open_wtmp+0xb9>
lea    0xdf1(%rip),%rdi
jmp    1229 <open_wtmp+0xb9>
lea    0xde9(%rip),%rdi
xor    %eax,%eax
call   1030 <warn@plt>
mov    %ebx,%edi
call   1050 <close@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.stat = type { i64, i64 }
@O_RDWR = global i32 0
@O_NOFOLLOW = global i32 0
@O_CLOEXEC = global i32 0
@.str = [9 x i8] c"open(%s)\00"
@LOCK_EX = global i32 0
@.str.1 = [8 x i8] c"flock()\00"
@.str.2 = [8 x i8] c"fstat()\00"
@.str.3 = [7 x i8] c"stat()\00"
@.str.4 = [29 x i8] c"lost race with other process\00"
define i32 @open_wtmp(i8* %0, %struct.stat* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca %struct.stat*
%6 = alloca i32
%7 = alloca %struct.stat
store i8* %0, i8** %4
store %struct.stat* %1, %struct.stat** %5
%8 = load i8*, i8** %4
%9 = load i32, i32* @O_RDWR
%10 = load i32, i32* @O_NOFOLLOW
%11 = or i32 %9, %10
%12 = load i32, i32* @O_CLOEXEC
%13 = or i32 %11, %12
%14 = call i32 @open(i8* %8, i32 %13)
store i32 %14, i32* %6
%15 = load i32, i32* %6
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %2
%18 = load i8*, i8** %4
%19 = call i32 (i8*, ...) @warn(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* %18)
store i32 -1, i32* %3
br label %61
20: ; preds = %2
%21 = load i32, i32* %6
%22 = load i32, i32* @LOCK_EX
%23 = call i64 @flock(i32 %21, i32 %22)
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %20
%26 = call i32 (i8*, ...) @warn(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
br label %58
27: ; preds = %20
%28 = load i32, i32* %6
%29 = load %struct.stat*, %struct.stat** %5
%30 = call i64 @fstat(i32 %28, %struct.stat* %29)
%31 = icmp slt i64 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %27
%33 = call i32 (i8*, ...) @warn(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
br label %58
34: ; preds = %27
%35 = load i8*, i8** %4
%36 = call i64 @stat(i8* %35, %struct.stat* %7)
%37 = icmp slt i64 %36, 0
br i1 %37, label %38, label %40
38: ; preds = %34
%39 = call i32 (i8*, ...) @warn(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
br label %58
40: ; preds = %34
%41 = load %struct.stat*, %struct.stat** %5
%42 = getelementptr inbounds %struct.stat, %struct.stat* %41, i32 0, i32 0
%43 = load i64, i64* %42
%44 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%45 = load i64, i64* %44
%46 = icmp ne i64 %43, %45
br i1 %46, label %54, label %47
47: ; preds = %40
%48 = load %struct.stat*, %struct.stat** %5
%49 = getelementptr inbounds %struct.stat, %struct.stat* %48, i32 0, i32 1
%50 = load i64, i64* %49
%51 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 1
%52 = load i64, i64* %51
%53 = icmp ne i64 %50, %52
br i1 %53, label %54, label %56
54: ; preds = %47, %40
%55 = call i32 @warnx(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0))
br label %58
56: ; preds = %47
%57 = load i32, i32* %6
store i32 %57, i32* %3
br label %61
58: ; preds = %54, %38, %32, %25
%59 = load i32, i32* %6
%60 = call i32 @close(i32 %59)
store i32 -1, i32* %3
br label %61
61: ; preds = %58, %56, %17
%62 = load i32, i32* %3
ret i32 %62
}

LLVM IR (-O2):
%struct.stat = type { i64, i64 }
@O_RDWR = global i32 0
@O_NOFOLLOW = global i32 0
@O_CLOEXEC = global i32 0
@.str = [9 x i8] c"open(%s)\00"
@LOCK_EX = global i32 0
@.str.1 = [8 x i8] c"flock()\00"
@.str.2 = [8 x i8] c"fstat()\00"
@.str.3 = [7 x i8] c"stat()\00"
@.str.4 = [29 x i8] c"lost race with other process\00"
define i32 @open_wtmp(i8* %0, %struct.stat* nocapture %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = load i32, i32* @O_RDWR
%6 = load i32, i32* @O_NOFOLLOW
%7 = or i32 %6, %5
%8 = load i32, i32* @O_CLOEXEC
%9 = or i32 %7, %8
%10 = tail call i32 @open(i8* %0, i32 %9)
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = tail call i32 (i8*, ...) @warn(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %46
14: ; preds = %2
%15 = load i32, i32* @LOCK_EX
%16 = tail call i64 @flock(i32 %10, i32 %15)
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %14
%19 = tail call i32 (i8*, ...) @warn(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
br label %44
20: ; preds = %14
%21 = tail call i64 @fstat(i32 %10, %struct.stat* %1)
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %20
%24 = tail call i32 (i8*, ...) @warn(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
br label %44
25: ; preds = %20
%26 = call i64 @stat(i8* %0, %struct.stat* %3)
%27 = icmp slt i64 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %25
%29 = call i32 (i8*, ...) @warn(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
br label %44
30: ; preds = %25
%31 = getelementptr inbounds %struct.stat, %struct.stat* %1, i64 0, i32 0
%32 = load i64, i64* %31
%33 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%34 = load i64, i64* %33
%35 = icmp eq i64 %32, %34
br i1 %35, label %36, label %42
36: ; preds = %30
%37 = getelementptr inbounds %struct.stat, %struct.stat* %1, i64 0, i32 1
%38 = load i64, i64* %37
%39 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 1
%40 = load i64, i64* %39
%41 = icmp eq i64 %38, %40
br i1 %41, label %46, label %42
42: ; preds = %36, %30
%43 = call i32 @warnx(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0))
br label %44
44: ; preds = %42, %28, %23, %18
%45 = call i32 @close(i32 %10)
br label %46
46: ; preds = %36, %44, %12
%47 = phi i32 [ -1, %12 ], [ -1, %44 ], [ %10, %36 ]
ret i32 %47
}