#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 timeval {
  int tv_usec;
  int /*<<< orphan*/ tv_sec;
};
struct timespec {
  int tv_nsec;
  int /*<<< orphan*/ tv_sec;
};

/* Variables and functions */
int AT_SYMLINK_NOFOLLOW;
int dir_mutex_lock(int);
int /*<<< orphan*/ dir_mutex_unlock();
int lutimes(char const *, struct timeval *);
int utimes(char const *, struct timeval *);
int utimensat(int dirfd, const char *pathname, const struct timespec times[2],
              int flags) {
  int rc;
  struct timeval tv[2];
  tv[0].tv_sec = times[0].tv_sec;
  tv[0].tv_usec = times[0].tv_nsec / 1000;
  tv[1].tv_sec = times[1].tv_sec;
  tv[1].tv_usec = times[1].tv_nsec / 1000;
  rc = dir_mutex_lock(dirfd);
  if (rc < 0)
    return rc;
  if (flags & AT_SYMLINK_NOFOLLOW) {
    rc = lutimes(pathname, tv);
  } else {
    rc = utimes(pathname, tv);
  }
  dir_mutex_unlock();
  return rc;
}


x86 assembly (clang, -O0):
<utimensat>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    -0x18(%rbp),%rax
mov    0x4(%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
mov    %eax,-0x30(%rbp)
mov    -0x18(%rbp),%rax
mov    0xc(%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
mov    %eax,-0x28(%rbp)
mov    -0x8(%rbp),%edi
call   1030 <dir_mutex_lock@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    11ad <utimensat+0x6d>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11f4 <utimensat+0xb4>
mov    -0x1c(%rbp),%eax
mov    0x2e29(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     11d7 <utimensat+0x97>
mov    -0x10(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   1050 <lutimes@plt>
mov    %eax,-0x20(%rbp)
jmp    11e7 <utimensat+0xa7>
mov    -0x10(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   1040 <utimes@plt>
mov    %eax,-0x20(%rbp)
mov    $0x0,%al
call   1060 <dir_mutex_unlock@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<utimensat>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %ecx,%ebp
mov    %rsi,%r14
mov    0x4(%rdx),%eax
mov    %eax,0x4(%rsp)
movslq (%rdx),%rax
imul   $0x10624dd3,%rax,%rax
mov    %rax,%rcx
shr    $0x3f,%rcx
sar    $0x26,%rax
add    %ecx,%eax
mov    %eax,(%rsp)
mov    0xc(%rdx),%eax
mov    %eax,0xc(%rsp)
movslq 0x8(%rdx),%rax
imul   $0x10624dd3,%rax,%rax
mov    %rax,%rcx
shr    $0x3f,%rcx
sar    $0x26,%rax
add    %ecx,%eax
mov    %eax,0x8(%rsp)
call   1030 <dir_mutex_lock@plt>
test   %eax,%eax
js     11b2 <utimensat+0x72>
mov    0x2e3f(%rip),%rax
test   %ebp,(%rax)
je     11b6 <utimensat+0x76>
mov    %rsp,%rsi
mov    %r14,%rdi
call   1050 <lutimes@plt>
jmp    11c1 <utimensat+0x81>
mov    %eax,%ebx
jmp    11ca <utimensat+0x8a>
mov    %rsp,%rsi
mov    %r14,%rdi
call   1040 <utimes@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1060 <dir_mutex_unlock@plt>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.timespec = type { i32, i32 }
%struct.timeval = type { i32, i32 }
@AT_SYMLINK_NOFOLLOW = global i32 0
define i32 @utimensat(i32 %0, i8* %1, %struct.timespec* %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca %struct.timespec*
%9 = alloca i32
%10 = alloca i32
%11 = alloca [2 x %struct.timeval]
store i32 %0, i32* %6
store i8* %1, i8** %7
store %struct.timespec* %2, %struct.timespec** %8
store i32 %3, i32* %9
%12 = load %struct.timespec*, %struct.timespec** %8
%13 = getelementptr inbounds %struct.timespec, %struct.timespec* %12, i64 0
%14 = getelementptr inbounds %struct.timespec, %struct.timespec* %13, i32 0, i32 1
%15 = load i32, i32* %14
%16 = getelementptr inbounds [2 x %struct.timeval], [2 x %struct.timeval]* %11, i64 0, i64 0
%17 = getelementptr inbounds %struct.timeval, %struct.timeval* %16, i32 0, i32 1
store i32 %15, i32* %17
%18 = load %struct.timespec*, %struct.timespec** %8
%19 = getelementptr inbounds %struct.timespec, %struct.timespec* %18, i64 0
%20 = getelementptr inbounds %struct.timespec, %struct.timespec* %19, i32 0, i32 0
%21 = load i32, i32* %20
%22 = sdiv i32 %21, 1000
%23 = getelementptr inbounds [2 x %struct.timeval], [2 x %struct.timeval]* %11, i64 0, i64 0
%24 = getelementptr inbounds %struct.timeval, %struct.timeval* %23, i32 0, i32 0
store i32 %22, i32* %24
%25 = load %struct.timespec*, %struct.timespec** %8
%26 = getelementptr inbounds %struct.timespec, %struct.timespec* %25, i64 1
%27 = getelementptr inbounds %struct.timespec, %struct.timespec* %26, i32 0, i32 1
%28 = load i32, i32* %27
%29 = getelementptr inbounds [2 x %struct.timeval], [2 x %struct.timeval]* %11, i64 0, i64 1
%30 = getelementptr inbounds %struct.timeval, %struct.timeval* %29, i32 0, i32 1
store i32 %28, i32* %30
%31 = load %struct.timespec*, %struct.timespec** %8
%32 = getelementptr inbounds %struct.timespec, %struct.timespec* %31, i64 1
%33 = getelementptr inbounds %struct.timespec, %struct.timespec* %32, i32 0, i32 0
%34 = load i32, i32* %33
%35 = sdiv i32 %34, 1000
%36 = getelementptr inbounds [2 x %struct.timeval], [2 x %struct.timeval]* %11, i64 0, i64 1
%37 = getelementptr inbounds %struct.timeval, %struct.timeval* %36, i32 0, i32 0
store i32 %35, i32* %37
%38 = load i32, i32* %6
%39 = call i32 @dir_mutex_lock(i32 %38)
store i32 %39, i32* %10
%40 = load i32, i32* %10
%41 = icmp slt i32 %40, 0
br i1 %41, label %42, label %44
42: ; preds = %4
%43 = load i32, i32* %10
store i32 %43, i32* %5
br label %60
44: ; preds = %4
%45 = load i32, i32* %9
%46 = load i32, i32* @AT_SYMLINK_NOFOLLOW
%47 = and i32 %45, %46
%48 = icmp ne i32 %47, 0
br i1 %48, label %49, label %53
49: ; preds = %44
%50 = load i8*, i8** %7
%51 = getelementptr inbounds [2 x %struct.timeval], [2 x %struct.timeval]* %11, i64 0, i64 0
%52 = call i32 @lutimes(i8* %50, %struct.timeval* %51)
store i32 %52, i32* %10
br label %57
53: ; preds = %44
%54 = load i8*, i8** %7
%55 = getelementptr inbounds [2 x %struct.timeval], [2 x %struct.timeval]* %11, i64 0, i64 0
%56 = call i32 @utimes(i8* %54, %struct.timeval* %55)
store i32 %56, i32* %10
br label %57
57: ; preds = %53, %49
%58 = call i32 (...) @dir_mutex_unlock()
%59 = load i32, i32* %10
store i32 %59, i32* %5
br label %60
60: ; preds = %57, %42
%61 = load i32, i32* %5
ret i32 %61
}

LLVM IR (-O2):
%struct.timespec = type { i32, i32 }
%struct.timeval = type { i32, i32 }
@AT_SYMLINK_NOFOLLOW = global i32 0
define i32 @utimensat(i32 %0, i8* %1, %struct.timespec* nocapture %2, i32 %3) {
%5 = alloca [2 x %struct.timeval]
%6 = bitcast [2 x %struct.timeval]* %5 to i8*
%7 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 1
%8 = load i32, i32* %7
%9 = getelementptr inbounds [2 x %struct.timeval], [2 x %struct.timeval]* %5, i64 0, i64 0
%10 = getelementptr inbounds [2 x %struct.timeval], [2 x %struct.timeval]* %5, i64 0, i64 0, i32 1
store i32 %8, i32* %10
%11 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 0
%12 = load i32, i32* %11
%13 = sdiv i32 %12, 1000
%14 = getelementptr inbounds [2 x %struct.timeval], [2 x %struct.timeval]* %5, i64 0, i64 0, i32 0
store i32 %13, i32* %14
%15 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 1, i32 1
%16 = load i32, i32* %15
%17 = getelementptr inbounds [2 x %struct.timeval], [2 x %struct.timeval]* %5, i64 0, i64 1, i32 1
store i32 %16, i32* %17
%18 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 1, i32 0
%19 = load i32, i32* %18
%20 = sdiv i32 %19, 1000
%21 = getelementptr inbounds [2 x %struct.timeval], [2 x %struct.timeval]* %5, i64 0, i64 1, i32 0
store i32 %20, i32* %21
%22 = tail call i32 @dir_mutex_lock(i32 %0)
%23 = icmp slt i32 %22, 0
br i1 %23, label %35, label %24
24: ; preds = %4
%25 = load i32, i32* @AT_SYMLINK_NOFOLLOW
%26 = and i32 %25, %3
%27 = icmp eq i32 %26, 0
br i1 %27, label %30, label %28
28: ; preds = %24
%29 = call i32 @lutimes(i8* %1, %struct.timeval* %9)
br label %32
30: ; preds = %24
%31 = call i32 @utimes(i8* %1, %struct.timeval* %9)
br label %32
32: ; preds = %30, %28
%33 = phi i32 [ %29, %28 ], [ %31, %30 ]
%34 = call i32 (...) @dir_mutex_unlock()
br label %35
35: ; preds = %4, %32
%36 = phi i32 [ %33, %32 ], [ %22, %4 ]
ret i32 %36
}