#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__ S_ISDIR(int /*<<< orphan*/);
scalar_t__ lstat(char const *, struct stat *);
int rmdir(char const *);
int unlink(char const *);
int remove(const char *file) {
  struct stat sb;
  if (lstat(file, &sb) < 0)
    return (-1);
  if (S_ISDIR(sb.st_mode))
    return (rmdir(file));
  return (unlink(file));
}


x86 assembly (clang, -O0):
<remove>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1060 <lstat@plt>
cmp    $0x0,%rax
jge    116f <remove+0x2f>
movl   $0xffffffff,-0x4(%rbp)
jmp    119e <remove+0x5e>
mov    -0x18(%rbp),%edi
call   1050 <S_ISDIR@plt>
cmp    $0x0,%rax
je     1192 <remove+0x52>
mov    -0x10(%rbp),%rdi
call   1040 <rmdir@plt>
mov    %eax,-0x4(%rbp)
jmp    119e <remove+0x5e>
mov    -0x10(%rbp),%rdi
call   1030 <unlink@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<remove>:
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
lea    0x8(%rsp),%rsi
call   1060 <lstat@plt>
test   %rax,%rax
js     1172 <remove+0x32>
mov    0x8(%rsp),%edi
call   1050 <S_ISDIR@plt>
mov    %rbx,%rdi
add    $0x10,%rsp
test   %rax,%rax
je     117d <remove+0x3d>
pop    %rbx
jmp    1040 <rmdir@plt>
mov    $0xffffffff,%eax
add    $0x10,%rsp
pop    %rbx
ret
pop    %rbx
jmp    1030 <unlink@plt>

LLVM IR (-O0):
%struct.stat = type { i32 }
define i32 @remove(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.stat
store i8* %0, i8** %3
%5 = load i8*, i8** %3
%6 = call i64 @lstat(i8* %5, %struct.stat* %4)
%7 = icmp slt i64 %6, 0
br i1 %7, label %8, label %9
8: ; preds = %1
store i32 -1, i32* %2
br label %20
9: ; preds = %1
%10 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%11 = load i32, i32* %10
%12 = call i64 @S_ISDIR(i32 %11)
%13 = icmp ne i64 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %9
%15 = load i8*, i8** %3
%16 = call i32 @rmdir(i8* %15)
store i32 %16, i32* %2
br label %20
17: ; preds = %9
%18 = load i8*, i8** %3
%19 = call i32 @unlink(i8* %18)
store i32 %19, i32* %2
br label %20
20: ; preds = %17, %14, %8
%21 = load i32, i32* %2
ret i32 %21
}

LLVM IR (-O2):
%struct.stat = type { i32 }
define i32 @remove(i8* nocapture %0) {
%2 = alloca %struct.stat
%3 = bitcast %struct.stat* %2 to i8*
%4 = call i64 @lstat(i8* %0, %struct.stat* %2)
%5 = icmp slt i64 %4, 0
br i1 %5, label %15, label %6
6: ; preds = %1
%7 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%8 = load i32, i32* %7
%9 = tail call i64 @S_ISDIR(i32 %8)
%10 = icmp eq i64 %9, 0
br i1 %10, label %13, label %11
11: ; preds = %6
%12 = tail call i32 @rmdir(i8* %0)
br label %15
13: ; preds = %6
%14 = tail call i32 @unlink(i8* %0)
br label %15
15: ; preds = %1, %13, %11
%16 = phi i32 [ %12, %11 ], [ %14, %13 ], [ -1, %1 ]
ret i32 %16
}