#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*/);
int /*<<< orphan*/ _DIAGASSERT(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;
  _DIAGASSERT(file != NULL);
  if (lstat(file, &sb) < 0)
    return -1;
  if (S_ISDIR(sb.st_mode))
    return rmdir(file);
  else
    return unlink(file);
}


x86 assembly (clang, -O0):
<remove>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <_DIAGASSERT@plt>
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1060 <lstat@plt>
cmp    $0x0,%rax
jge    1191 <remove+0x41>
movl   $0xffffffff,-0x4(%rbp)
jmp    11c0 <remove+0x70>
mov    -0x18(%rbp),%edi
call   1050 <S_ISDIR@plt>
cmp    $0x0,%rax
je     11b4 <remove+0x64>
mov    -0x10(%rbp),%rdi
call   1040 <rmdir@plt>
mov    %eax,-0x4(%rbp)
jmp    11c0 <remove+0x70>
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
xor    %edi,%edi
test   %rbx,%rbx
setne  %dil
call   1070 <_DIAGASSERT@plt>
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
call   1060 <lstat@plt>
test   %rax,%rax
js     1193 <remove+0x43>
mov    0x8(%rsp),%edi
call   1050 <S_ISDIR@plt>
mov    %rbx,%rdi
add    $0x10,%rsp
test   %rax,%rax
je     119e <remove+0x4e>
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 = icmp ne i8* %5, null
%7 = zext i1 %6 to i32
%8 = call i32 @_DIAGASSERT(i32 %7)
%9 = load i8*, i8** %3
%10 = call i64 @lstat(i8* %9, %struct.stat* %4)
%11 = icmp slt i64 %10, 0
br i1 %11, label %12, label %13
12: ; preds = %1
store i32 -1, i32* %2
br label %24
13: ; preds = %1
%14 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%15 = load i32, i32* %14
%16 = call i64 @S_ISDIR(i32 %15)
%17 = icmp ne i64 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %13
%19 = load i8*, i8** %3
%20 = call i32 @rmdir(i8* %19)
store i32 %20, i32* %2
br label %24
21: ; preds = %13
%22 = load i8*, i8** %3
%23 = call i32 @unlink(i8* %22)
store i32 %23, i32* %2
br label %24
24: ; preds = %21, %18, %12
%25 = load i32, i32* %2
ret i32 %25
}

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