#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 listdir(char const *, int);
int listfile(int /*<<< orphan*/ *, char const *, int);
int lstat(char const *, struct stat *);
int /*<<< orphan*/ perror(char const *);
int /*<<< orphan*/ printf(char *, char const *);
int stat(char const *, struct stat *);
int strlen(char const *);
int listpath(const char *name, int flags) {
  struct stat s;
  int err;
  if (name[strlen(name) - 1] == '/')
    err = stat(name, &s);
  else
    err = lstat(name, &s);
  if (err < 0) {
    perror(name);
    return -1;
  }
  if ((flags & (1 << 3)) == 0 && S_ISDIR(s.st_mode)) {
    if (flags & (1 << 2))
      printf("\n%s:\n", name);
    return listdir(name, flags);
  } else {
    return listfile(NULL, name, flags);
  }
}


x86 assembly (clang, -O0):
<listpath>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x2f,%eax
jne    11ce <listpath+0x4e>
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1080 <stat@plt>
mov    %eax,-0x1c(%rbp)
jmp    11de <listpath+0x5e>
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1070 <lstat@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11fd <listpath+0x7d>
mov    -0x10(%rbp),%rdi
call   10a0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1264 <listpath+0xe4>
mov    -0x14(%rbp),%eax
and    $0x8,%eax
cmp    $0x0,%eax
jne    1251 <listpath+0xd1>
mov    -0x18(%rbp),%edi
call   1060 <S_ISDIR@plt>
cmp    $0x0,%rax
je     1251 <listpath+0xd1>
mov    -0x14(%rbp),%eax
and    $0x4,%eax
cmp    $0x0,%eax
je     123d <listpath+0xbd>
mov    -0x10(%rbp),%rsi
lea    0xdc8(%rip),%rdi
call   1050 <printf@plt>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1040 <listdir@plt>
mov    %eax,-0x4(%rbp)
jmp    1264 <listpath+0xe4>
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
xor    %eax,%eax
mov    %eax,%edi
call   1090 <listfile@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<listpath>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %rdi,%rbx
call   1030 <strlen@plt>
cltq
cmpb   $0x2f,-0x1(%rax,%rbx,1)
jne    11ef <listpath+0x6f>
mov    %rsp,%rsi
mov    %rbx,%rdi
call   1080 <stat@plt>
test   %eax,%eax
js     11fe <listpath+0x7e>
test   $0x8,%bpl
jne    11dd <listpath+0x5d>
mov    (%rsp),%edi
call   1060 <S_ISDIR@plt>
test   %rax,%rax
je     11dd <listpath+0x5d>
test   $0x4,%bpl
je     11cd <listpath+0x4d>
lea    0xe3b(%rip),%rdi
mov    %rbx,%rsi
call   1050 <printf@plt>
mov    %rbx,%rdi
mov    %ebp,%esi
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1040 <listdir@plt>
xor    %edi,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1090 <listfile@plt>
mov    %rsp,%rsi
mov    %rbx,%rdi
call   1070 <lstat@plt>
test   %eax,%eax
jns    11a5 <listpath+0x25>
mov    %rbx,%rdi
call   10a0 <perror@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [6 x i8] c"\0A%s:\0A\00"
define i32 @listpath(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca %struct.stat
%7 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
%8 = load i8*, i8** %4
%9 = load i8*, i8** %4
%10 = call i32 @strlen(i8* %9)
%11 = sub nsw i32 %10, 1
%12 = sext i32 %11 to i64
%13 = getelementptr inbounds i8, i8* %8, i64 %12
%14 = load i8, i8* %13
%15 = sext i8 %14 to i32
%16 = icmp eq i32 %15, 47
br i1 %16, label %17, label %20
17: ; preds = %2
%18 = load i8*, i8** %4
%19 = call i32 @stat(i8* %18, %struct.stat* %6)
store i32 %19, i32* %7
br label %23
20: ; preds = %2
%21 = load i8*, i8** %4
%22 = call i32 @lstat(i8* %21, %struct.stat* %6)
store i32 %22, i32* %7
br label %23
23: ; preds = %20, %17
%24 = load i32, i32* %7
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %29
26: ; preds = %23
%27 = load i8*, i8** %4
%28 = call i32 @perror(i8* %27)
store i32 -1, i32* %3
br label %53
29: ; preds = %23
%30 = load i32, i32* %5
%31 = and i32 %30, 8
%32 = icmp eq i32 %31, 0
br i1 %32, label %33, label %49
33: ; preds = %29
%34 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%35 = load i32, i32* %34
%36 = call i64 @S_ISDIR(i32 %35)
%37 = icmp ne i64 %36, 0
br i1 %37, label %38, label %49
38: ; preds = %33
%39 = load i32, i32* %5
%40 = and i32 %39, 4
%41 = icmp ne i32 %40, 0
br i1 %41, label %42, label %45
42: ; preds = %38
%43 = load i8*, i8** %4
%44 = call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %43)
br label %45
45: ; preds = %42, %38
%46 = load i8*, i8** %4
%47 = load i32, i32* %5
%48 = call i32 @listdir(i8* %46, i32 %47)
store i32 %48, i32* %3
br label %53
49: ; preds = %33, %29
%50 = load i8*, i8** %4
%51 = load i32, i32* %5
%52 = call i32 @listfile(i32* null, i8* %50, i32 %51)
store i32 %52, i32* %3
br label %53
53: ; preds = %49, %45, %26
%54 = load i32, i32* %3
ret i32 %54
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [6 x i8] c"\0A%s:\0A\00"
define i32 @listpath(i8* %0, i32 %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = tail call i32 @strlen(i8* %0)
%6 = add nsw i32 %5, -1
%7 = sext i32 %6 to i64
%8 = getelementptr inbounds i8, i8* %0, i64 %7
%9 = load i8, i8* %8
%10 = icmp eq i8 %9, 47
br i1 %10, label %11, label %13
11: ; preds = %2
%12 = call i32 @stat(i8* %0, %struct.stat* %3)
br label %15
13: ; preds = %2
%14 = call i32 @lstat(i8* %0, %struct.stat* %3)
br label %15
15: ; preds = %13, %11
%16 = phi i32 [ %12, %11 ], [ %14, %13 ]
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %15
%19 = tail call i32 @perror(i8* %0)
br label %37
20: ; preds = %15
%21 = and i32 %1, 8
%22 = icmp eq i32 %21, 0
br i1 %22, label %23, label %35
23: ; preds = %20
%24 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%25 = load i32, i32* %24
%26 = tail call i64 @S_ISDIR(i32 %25)
%27 = icmp eq i64 %26, 0
br i1 %27, label %35, label %28
28: ; preds = %23
%29 = and i32 %1, 4
%30 = icmp eq i32 %29, 0
br i1 %30, label %33, label %31
31: ; preds = %28
%32 = tail call i32 @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %33
33: ; preds = %31, %28
%34 = tail call i32 @listdir(i8* %0, i32 %1)
br label %37
35: ; preds = %23, %20
%36 = tail call i32 @listfile(i32* null, i8* %0, i32 %1)
br label %37
37: ; preds = %35, %33, %18
%38 = phi i32 [ -1, %18 ], [ %34, %33 ], [ %36, %35 ]
ret i32 %38
}