#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 {
  int dummy;
};

/* Variables and functions */
int AT_SYMLINK_NOFOLLOW;
int dir_mutex_lock(int);
int /*<<< orphan*/ dir_mutex_unlock();
int lstat(char const *, struct stat *);
int /*<<< orphan*/ printf(char *, char const *, int);
int stat(char const *, struct stat *);
int fstatat(int dirfd, const char *pathname, struct stat *buf, int flags) {
  int rc;
  rc = dir_mutex_lock(dirfd);
  if (rc < 0) {
    printf("fstatat[%s]: %i\n", pathname, rc);
    return rc;
  }
  if (flags & AT_SYMLINK_NOFOLLOW) {
    rc = lstat(pathname, buf);
  } else {
    rc = stat(pathname, buf);
  }
  dir_mutex_unlock();
  return rc;
}


x86 assembly (clang, -O0):
<fstatat>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    -0x8(%rbp),%edi
call   1030 <dir_mutex_lock@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    1199 <fstatat+0x49>
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%edx
lea    0xe77(%rip),%rdi
call   1040 <printf@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11e0 <fstatat+0x90>
mov    -0x1c(%rbp),%eax
mov    0x2e3d(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     11c3 <fstatat+0x73>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1050 <lstat@plt>
mov    %eax,-0x20(%rbp)
jmp    11d3 <fstatat+0x83>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1060 <stat@plt>
mov    %eax,-0x20(%rbp)
mov    $0x0,%al
call   1070 <dir_mutex_unlock@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fstatat>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %ecx,%ebp
mov    %rdx,%r15
mov    %rsi,%r14
call   1030 <dir_mutex_lock@plt>
test   %eax,%eax
js     1180 <fstatat+0x30>
mov    0x2e71(%rip),%rax
test   %ebp,(%rax)
je     1195 <fstatat+0x45>
mov    %r14,%rdi
mov    %r15,%rsi
call   1050 <lstat@plt>
jmp    11a0 <fstatat+0x50>
mov    %eax,%ebx
lea    0xe77(%rip),%rdi
mov    %r14,%rsi
mov    %eax,%edx
call   1040 <printf@plt>
jmp    11a9 <fstatat+0x59>
mov    %r14,%rdi
mov    %r15,%rsi
call   1060 <stat@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1070 <dir_mutex_unlock@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [17 x i8] c"fstatat[%s]: %i\0A\00"
@AT_SYMLINK_NOFOLLOW = global i32 0
define i32 @fstatat(i32 %0, i8* %1, %struct.stat* %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca %struct.stat*
%9 = alloca i32
%10 = alloca i32
store i32 %0, i32* %6
store i8* %1, i8** %7
store %struct.stat* %2, %struct.stat** %8
store i32 %3, i32* %9
%11 = load i32, i32* %6
%12 = call i32 @dir_mutex_lock(i32 %11)
store i32 %12, i32* %10
%13 = load i32, i32* %10
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %20
15: ; preds = %4
%16 = load i8*, i8** %7
%17 = load i32, i32* %10
%18 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* %16, i32 %17)
%19 = load i32, i32* %10
store i32 %19, i32* %5
br label %36
20: ; preds = %4
%21 = load i32, i32* %9
%22 = load i32, i32* @AT_SYMLINK_NOFOLLOW
%23 = and i32 %21, %22
%24 = icmp ne i32 %23, 0
br i1 %24, label %25, label %29
25: ; preds = %20
%26 = load i8*, i8** %7
%27 = load %struct.stat*, %struct.stat** %8
%28 = call i32 @lstat(i8* %26, %struct.stat* %27)
store i32 %28, i32* %10
br label %33
29: ; preds = %20
%30 = load i8*, i8** %7
%31 = load %struct.stat*, %struct.stat** %8
%32 = call i32 @stat(i8* %30, %struct.stat* %31)
store i32 %32, i32* %10
br label %33
33: ; preds = %29, %25
%34 = call i32 (...) @dir_mutex_unlock()
%35 = load i32, i32* %10
store i32 %35, i32* %5
br label %36
36: ; preds = %33, %15
%37 = load i32, i32* %5
ret i32 %37
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [17 x i8] c"fstatat[%s]: %i\0A\00"
@AT_SYMLINK_NOFOLLOW = global i32 0
define i32 @fstatat(i32 %0, i8* %1, %struct.stat* nocapture %2, i32 %3) {
%5 = tail call i32 @dir_mutex_lock(i32 %0)
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %4
%8 = tail call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* %1, i32 %5)
br label %20
9: ; preds = %4
%10 = load i32, i32* @AT_SYMLINK_NOFOLLOW
%11 = and i32 %10, %3
%12 = icmp eq i32 %11, 0
br i1 %12, label %15, label %13
13: ; preds = %9
%14 = tail call i32 @lstat(i8* %1, %struct.stat* %2)
br label %17
15: ; preds = %9
%16 = tail call i32 @stat(i8* %1, %struct.stat* %2)
br label %17
17: ; preds = %15, %13
%18 = phi i32 [ %14, %13 ], [ %16, %15 ]
%19 = tail call i32 (...) @dir_mutex_unlock()
br label %20
20: ; preds = %17, %7
%21 = phi i32 [ %5, %7 ], [ %18, %17 ]
ret i32 %21
}