#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 /*<<< orphan*/ st_mode;
};

/* Variables and functions */
scalar_t__ EEXIST;
scalar_t__ EISDIR;
scalar_t__ ENOENT;
scalar_t__ S_ISDIR(int /*<<< orphan*/);
scalar_t__ errno;
int lstat(char const *, struct stat *);
scalar_t__ rename(char const *, char const *);
int ag_move(const char *src, const char *dest, int check) {
  struct stat st;
  int err = 0;
  if (!check || ((err = lstat(dest, &st)) && errno == ENOENT)) {
    return rename(src, dest) ? -2 : 0;
  } else if (!err) {
    if (S_ISDIR(st.st_mode)) {
      errno = EISDIR;
    } else {
      errno = EEXIST;
    }
  }
  return -1;
}


x86 assembly (clang, -O0):
<ag_move>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
movl   $0x0,-0x24(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     1187 <ag_move+0x57>
mov    -0x18(%rbp),%rdi
lea    -0x20(%rbp),%rsi
call   1040 <lstat@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
je     11ad <ag_move+0x7d>
mov    0x2e5c(%rip),%rax
mov    (%rax),%rax
mov    0x2e42(%rip),%rcx
cmp    (%rcx),%rax
jne    11ad <ag_move+0x7d>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1050 <rename@plt>
mov    %rax,%rdx
xor    %eax,%eax
mov    $0xfffffffe,%ecx
cmp    $0x0,%rdx
cmovne %ecx,%eax
mov    %eax,-0x4(%rbp)
jmp    1207 <ag_move+0xd7>
cmpl   $0x0,-0x24(%rbp)
jne    11fb <ag_move+0xcb>
mov    -0x20(%rbp),%edi
call   1030 <S_ISDIR@plt>
cmp    $0x0,%rax
je     11e2 <ag_move+0xb2>
mov    0x2e18(%rip),%rax
mov    (%rax),%rcx
mov    0x2df6(%rip),%rax
mov    %rcx,(%rax)
jmp    11f6 <ag_move+0xc6>
mov    0x2def(%rip),%rax
mov    (%rax),%rcx
mov    0x2ddd(%rip),%rax
mov    %rcx,(%rax)
jmp    11fb <ag_move+0xcb>
jmp    1200 <ag_move+0xd0>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ag_move>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %rdi,%r14
test   %edx,%edx
je     1168 <ag_move+0x38>
mov    %rsp,%rsi
mov    %rbx,%rdi
call   1040 <lstat@plt>
test   %eax,%eax
je     1183 <ag_move+0x53>
mov    0x2e7c(%rip),%rax
mov    (%rax),%rcx
mov    0x2e62(%rip),%rdx
mov    $0xffffffff,%eax
cmp    (%rdx),%rcx
jne    11b2 <ag_move+0x82>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1050 <rename@plt>
xor    %ecx,%ecx
test   %rax,%rax
sete   %cl
lea    (%rcx,%rcx,1),%eax
add    $0xfffffffe,%eax
jmp    11b2 <ag_move+0x82>
mov    (%rsp),%edi
call   1030 <S_ISDIR@plt>
test   %rax,%rax
je     1199 <ag_move+0x69>
mov    0x2e51(%rip),%rax
jmp    11a0 <ag_move+0x70>
mov    0x2e38(%rip),%rax
mov    (%rax),%rax
mov    0x2e26(%rip),%rcx
mov    %rax,(%rcx)
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@errno = global i64 0
@ENOENT = global i64 0
@EISDIR = global i64 0
@EEXIST = global i64 0
define i32 @ag_move(i8* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca %struct.stat
%9 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i32 0, i32* %9
%10 = load i32, i32* %7
%11 = icmp ne i32 %10, 0
br i1 %11, label %12, label %20
12: ; preds = %3
%13 = load i8*, i8** %6
%14 = call i32 @lstat(i8* %13, %struct.stat* %8)
store i32 %14, i32* %9
%15 = icmp ne i32 %14, 0
br i1 %15, label %16, label %27
16: ; preds = %12
%17 = load i64, i64* @errno
%18 = load i64, i64* @ENOENT
%19 = icmp eq i64 %17, %18
br i1 %19, label %20, label %27
20: ; preds = %16, %3
%21 = load i8*, i8** %5
%22 = load i8*, i8** %6
%23 = call i64 @rename(i8* %21, i8* %22)
%24 = icmp ne i64 %23, 0
%25 = zext i1 %24 to i64
%26 = select i1 %24, i32 -2, i32 0
store i32 %26, i32* %4
br label %42
27: ; preds = %16, %12
%28 = load i32, i32* %9
%29 = icmp ne i32 %28, 0
br i1 %29, label %40, label %30
30: ; preds = %27
%31 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%32 = load i32, i32* %31
%33 = call i64 @S_ISDIR(i32 %32)
%34 = icmp ne i64 %33, 0
br i1 %34, label %35, label %37
35: ; preds = %30
%36 = load i64, i64* @EISDIR
store i64 %36, i64* @errno
br label %39
37: ; preds = %30
%38 = load i64, i64* @EEXIST
store i64 %38, i64* @errno
br label %39
39: ; preds = %37, %35
br label %40
40: ; preds = %39, %27
br label %41
41: ; preds = %40
store i32 -1, i32* %4
br label %42
42: ; preds = %41, %20
%43 = load i32, i32* %4
ret i32 %43
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@errno = global i64 0
@ENOENT = global i64 0
@EISDIR = global i64 0
@EEXIST = global i64 0
define i32 @ag_move(i8* nocapture %0, i8* nocapture %1, i32 %2) {
%4 = alloca %struct.stat
%5 = bitcast %struct.stat* %4 to i8*
%6 = icmp eq i32 %2, 0
br i1 %6, label %14, label %7
7: ; preds = %3
%8 = call i32 @lstat(i8* %1, %struct.stat* %4)
%9 = icmp eq i32 %8, 0
br i1 %9, label %18, label %10
10: ; preds = %7
%11 = load i64, i64* @errno
%12 = load i64, i64* @ENOENT
%13 = icmp eq i64 %11, %12
br i1 %13, label %14, label %27
14: ; preds = %10, %3
%15 = tail call i64 @rename(i8* %0, i8* %1)
%16 = icmp eq i64 %15, 0
%17 = select i1 %16, i32 0, i32 -2
br label %27
18: ; preds = %7
%19 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%20 = load i32, i32* %19
%21 = tail call i64 @S_ISDIR(i32 %20)
%22 = icmp eq i64 %21, 0
br i1 %22, label %25, label %23
23: ; preds = %18
%24 = load i64, i64* @EISDIR
store i64 %24, i64* @errno
br label %27
25: ; preds = %18
%26 = load i64, i64* @EEXIST
store i64 %26, i64* @errno
br label %27
27: ; preds = %10, %23, %25, %14
%28 = phi i32 [ %17, %14 ], [ -1, %25 ], [ -1, %23 ], [ -1, %10 ]
ret i32 %28
}