#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*/ tv;
struct timeval {
  int dummy;
};
struct stat {
  int st_mode;
};

/* Variables and functions */
int O_CREAT;
int O_WRONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fsync(int);
int /*<<< orphan*/ gettimeofday(struct timeval *, int /*<<< orphan*/ *);
int open(char *, int, int);
int /*<<< orphan*/ sprintf(char *, char *, char const *);
int stat(char const *, struct stat *);
int /*<<< orphan*/ unlink(char *);
int /*<<< orphan*/ write(int, struct timeval *, int);
void wake(const char *disk) {
  struct timeval tv;
  char name[1024];
  struct stat buf;
  int fd;
  int rc = stat(disk, &buf);
  if (rc != 0)
    return;
  if (!(buf.st_mode & 0222))
    return;
  gettimeofday(&tv, NULL);
  sprintf(name, "%s/.pks_keepalive", disk);
  fd = open(name, O_WRONLY | O_CREAT, 0600);
  if (fd == -1)
    return;
  write(fd, &tv, sizeof(tv));
  fsync(fd);
  close(fd);
  unlink(name);
}


x86 assembly (clang, -O0):
<wake>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
lea    -0x418(%rbp),%rsi
call   1070 <stat@plt>
mov    %eax,-0x420(%rbp)
cmpl   $0x0,-0x420(%rbp)
je     11b7 <wake+0x37>
jmp    1265 <wake+0xe5>
mov    -0x418(%rbp),%eax
and    $0x92,%eax
cmp    $0x0,%eax
jne    11d0 <wake+0x50>
jmp    1265 <wake+0xe5>
lea    -0x10(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1050 <gettimeofday@plt>
lea    -0x410(%rbp),%rdi
mov    -0x8(%rbp),%rdx
lea    0xe11(%rip),%rsi
call   10a0 <sprintf@plt>
lea    -0x410(%rbp),%rdi
mov    0x2dce(%rip),%rax
mov    (%rax),%esi
mov    0x2ded(%rip),%rax
or     (%rax),%esi
mov    $0x180,%edx
call   1090 <open@plt>
mov    %eax,-0x41c(%rbp)
cmpl   $0xffffffff,-0x41c(%rbp)
jne    122f <wake+0xaf>
jmp    1265 <wake+0xe5>
mov    -0x41c(%rbp),%edi
lea    -0x10(%rbp),%rsi
mov    $0x4,%edx
call   1040 <write@plt>
mov    -0x41c(%rbp),%edi
call   1080 <fsync@plt>
mov    -0x41c(%rbp),%edi
call   1060 <close@plt>
lea    -0x410(%rbp),%rdi
call   1030 <unlink@plt>
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<wake>:
push   %r14
push   %rbx
sub    $0x418,%rsp
mov    %rdi,%rbx
mov    %rsp,%rsi
call   1070 <stat@plt>
test   %eax,%eax
jne    1211 <wake+0x91>
testb  $0x92,(%rsp)
je     1211 <wake+0x91>
lea    0x8(%rsp),%rdi
xor    %esi,%esi
call   1050 <gettimeofday@plt>
lea    0xe4e(%rip),%rsi
lea    0x10(%rsp),%r14
mov    %r14,%rdi
mov    %rbx,%rdx
call   10a0 <sprintf@plt>
mov    0x2e07(%rip),%rax
mov    0x2e28(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %r14,%rdi
mov    $0x180,%edx
call   1090 <open@plt>
cmp    $0xffffffff,%eax
je     1211 <wake+0x91>
mov    %eax,%ebx
lea    0x8(%rsp),%rsi
mov    %eax,%edi
mov    $0x4,%edx
call   1040 <write@plt>
mov    %ebx,%edi
call   1080 <fsync@plt>
mov    %ebx,%edi
call   1060 <close@plt>
lea    0x10(%rsp),%rdi
call   1030 <unlink@plt>
add    $0x418,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.timeval = type { i32 }
%struct.stat = type { i32 }
@.str = [18 x i8] c"%s/.pks_keepalive\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
define void @wake(i8* %0) {
%2 = alloca i8*
%3 = alloca %struct.timeval
%4 = alloca [1024 x i8]
%5 = alloca %struct.stat
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %2
%8 = load i8*, i8** %2
%9 = call i32 @stat(i8* %8, %struct.stat* %5)
store i32 %9, i32* %7
%10 = load i32, i32* %7
%11 = icmp ne i32 %10, 0
br i1 %11, label %12, label %13
12: ; preds = %1
br label %41
13: ; preds = %1
%14 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 0
%15 = load i32, i32* %14
%16 = and i32 %15, 146
%17 = icmp ne i32 %16, 0
br i1 %17, label %19, label %18
18: ; preds = %13
br label %41
19: ; preds = %13
%20 = call i32 @gettimeofday(%struct.timeval* %3, i32* null)
%21 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%22 = load i8*, i8** %2
%23 = call i32 @sprintf(i8* %21, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %22)
%24 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%25 = load i32, i32* @O_WRONLY
%26 = load i32, i32* @O_CREAT
%27 = or i32 %25, %26
%28 = call i32 @open(i8* %24, i32 %27, i32 384)
store i32 %28, i32* %6
%29 = load i32, i32* %6
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %32
31: ; preds = %19
br label %41
32: ; preds = %19
%33 = load i32, i32* %6
%34 = call i32 @write(i32 %33, %struct.timeval* %3, i32 4)
%35 = load i32, i32* %6
%36 = call i32 @fsync(i32 %35)
%37 = load i32, i32* %6
%38 = call i32 @close(i32 %37)
%39 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%40 = call i32 @unlink(i8* %39)
br label %41
41: ; preds = %32, %31, %18, %12
ret void
}

LLVM IR (-O2):
%struct.timeval = type { i32 }
%struct.stat = type { i32 }
@.str = [18 x i8] c"%s/.pks_keepalive\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
define void @wake(i8* %0) {
%2 = alloca %struct.timeval
%3 = alloca [1024 x i8]
%4 = alloca %struct.stat
%5 = bitcast %struct.timeval* %2 to i8*
%6 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%7 = bitcast %struct.stat* %4 to i8*
%8 = call i32 @stat(i8* %0, %struct.stat* %4)
%9 = icmp eq i32 %8, 0
br i1 %9, label %10, label %28
10: ; preds = %1
%11 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%12 = load i32, i32* %11
%13 = and i32 %12, 146
%14 = icmp eq i32 %13, 0
br i1 %14, label %28, label %15
15: ; preds = %10
%16 = call i32 @gettimeofday(%struct.timeval* %2, i32* null)
%17 = call i32 @sprintf(i8* %6, i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %0)
%18 = load i32, i32* @O_WRONLY
%19 = load i32, i32* @O_CREAT
%20 = or i32 %19, %18
%21 = call i32 @open(i8* %6, i32 %20, i32 384)
%22 = icmp eq i32 %21, -1
br i1 %22, label %28, label %23
23: ; preds = %15
%24 = call i32 @write(i32 %21, %struct.timeval* %2, i32 4)
%25 = tail call i32 @fsync(i32 %21)
%26 = tail call i32 @close(i32 %21)
%27 = call i32 @unlink(i8* %6)
br label %28
28: ; preds = %15, %10, %1, %23
ret void
}