#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;
};
typedef int /*<<< orphan*/ buffer;

/* Variables and functions */
scalar_t__ ENOENT;
int /*<<< orphan*/ PATH_MAX;
int /*<<< orphan*/ assert(int);
scalar_t__ errno;
scalar_t__ stat(char *, struct stat *);
int strlen(char const *);
int check_path_components(const char *path) {
  char buffer[PATH_MAX + 1], *bufferpos;
  struct stat statbuf;
  assert(strlen(path) < sizeof(buffer));
  bufferpos = buffer;
  while (*path) {
    do {
      *(bufferpos++) = *(path++);
    } while (*path && *path != '/');
    *bufferpos = 0;
    if (stat(buffer, &statbuf) < 0 && (*path || errno != ENOENT))
      return errno;
  }
  return 0;
}


x86 assembly (clang, -O0):
<check_path_components>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2ead(%rip),%rax
mov    (%rax),%eax
add    $0x1,%eax
mov    %rax,-0x48(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x18(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x40(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x48(%rbp),%rcx
cltq
cmp    %rcx,%rax
setb   %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
mov    -0x40(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
cmpb   $0x0,(%rax)
je     1260 <check_path_components+0x130>
jmp    11a8 <check_path_components+0x78>
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x10(%rbp)
mov    (%rax),%cl
mov    -0x28(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x28(%rbp)
mov    %cl,(%rax)
mov    -0x10(%rbp),%rax
movsbl (%rax),%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x49(%rbp)
je     11ef <check_path_components+0xbf>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
setne  %al
mov    %al,-0x49(%rbp)
mov    -0x49(%rbp),%al
test   $0x1,%al
jne    11a8 <check_path_components+0x78>
mov    -0x40(%rbp),%rdi
mov    -0x28(%rbp),%rax
movb   $0x0,(%rax)
lea    -0x30(%rbp),%rsi
call   1050 <stat@plt>
cmp    $0x0,%rax
jge    125b <check_path_components+0x12b>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    1242 <check_path_components+0x112>
mov    0x2da9(%rip),%rax
mov    (%rax),%rax
mov    0x2d8f(%rip),%rcx
cmp    (%rcx),%rax
je     125b <check_path_components+0x12b>
mov    0x2d8f(%rip),%rax
mov    (%rax),%rax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x34(%rbp)
jmp    126e <check_path_components+0x13e>
jmp    1196 <check_path_components+0x66>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x34(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_path_components>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
mov    0x2ea8(%rip),%rax
mov    (%rax),%ebx
add    $0x1,%ebx
mov    %rsp,%r13
lea    0xf(%rbx),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r13
mov    %r13,%rsp
call   1030 <strlen@plt>
cltq
xor    %edi,%edi
cmp    %rbx,%rax
setb   %dil
call   1040 <assert@plt>
mov    (%r14),%cl
xor    %r15d,%r15d
test   %cl,%cl
je     11e9 <check_path_components+0xb9>
mov    $0x1,%ebx
lea    -0x30(%rbp),%r12
jmp    1194 <check_path_components+0x64>
nopl   0x0(%rax,%rax,1)
add    $0x1,%rbx
mov    %cl,-0x1(%r13,%rbx,1)
movzbl (%r14,%rbx,1),%ecx
test   %cl,%cl
je     11a7 <check_path_components+0x77>
cmp    $0x2f,%cl
jne    1190 <check_path_components+0x60>
movb   $0x0,0x0(%r13,%rbx,1)
mov    %r13,%rdi
mov    %r12,%rsi
call   1050 <stat@plt>
movzbl (%r14,%rbx,1),%ecx
test   %rax,%rax
js     11c8 <check_path_components+0x98>
test   %cl,%cl
jne    1190 <check_path_components+0x60>
jmp    11e9 <check_path_components+0xb9>
mov    0x2e09(%rip),%rax
mov    (%rax),%rax
mov    0x2def(%rip),%rdx
xor    %r15d,%r15d
cmp    (%rdx),%rax
cmovne %eax,%r15d
test   %cl,%cl
cmovne %eax,%r15d
mov    %r15d,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@PATH_MAX = global i32 0
@errno = global i64 0
@ENOENT = global i64 0
define i32 @check_path_components(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i8*
%7 = alloca %struct.stat
%8 = alloca i32
store i8* %0, i8** %3
%9 = load i32, i32* @PATH_MAX
%10 = add nsw i32 %9, 1
%11 = zext i32 %10 to i64
%12 = call i8* @llvm.stacksave()
store i8* %12, i8** %4
%13 = alloca i8, i64 %11
store i64 %11, i64* %5
%14 = load i8*, i8** %3
%15 = call i32 @strlen(i8* %14)
%16 = sext i32 %15 to i64
%17 = icmp ult i64 %16, %11
%18 = zext i1 %17 to i32
%19 = call i32 @assert(i32 %18)
store i8* %13, i8** %6
br label %20
20: ; preds = %59, %1
%21 = load i8*, i8** %3
%22 = load i8, i8* %21
%23 = icmp ne i8 %22, 0
br i1 %23, label %24, label %60
24: ; preds = %20
br label %25
25: ; preds = %41, %24
%26 = load i8*, i8** %3
%27 = getelementptr inbounds i8, i8* %26, i32 1
store i8* %27, i8** %3
%28 = load i8, i8* %26
%29 = load i8*, i8** %6
%30 = getelementptr inbounds i8, i8* %29, i32 1
store i8* %30, i8** %6
store i8 %28, i8* %29
br label %31
31: ; preds = %25
%32 = load i8*, i8** %3
%33 = load i8, i8* %32
%34 = sext i8 %33 to i32
%35 = icmp ne i32 %34, 0
br i1 %35, label %36, label %41
36: ; preds = %31
%37 = load i8*, i8** %3
%38 = load i8, i8* %37
%39 = sext i8 %38 to i32
%40 = icmp ne i32 %39, 47
br label %41
41: ; preds = %36, %31
%42 = phi i1 [ false, %31 ], [ %40, %36 ]
br i1 %42, label %25, label %43
43: ; preds = %41
%44 = load i8*, i8** %6
store i8 0, i8* %44
%45 = call i64 @stat(i8* %13, %struct.stat* %7)
%46 = icmp slt i64 %45, 0
br i1 %46, label %47, label %59
47: ; preds = %43
%48 = load i8*, i8** %3
%49 = load i8, i8* %48
%50 = sext i8 %49 to i32
%51 = icmp ne i32 %50, 0
br i1 %51, label %56, label %52
52: ; preds = %47
%53 = load i64, i64* @errno
%54 = load i64, i64* @ENOENT
%55 = icmp ne i64 %53, %54
br i1 %55, label %56, label %59
56: ; preds = %52, %47
%57 = load i64, i64* @errno
%58 = trunc i64 %57 to i32
store i32 %58, i32* %2
store i32 1, i32* %8
br label %61
59: ; preds = %52, %43
br label %20
60: ; preds = %20
store i32 0, i32* %2
store i32 1, i32* %8
br label %61
61: ; preds = %60, %56
%62 = load i8*, i8** %4
%63 = load i32, i32* %2
ret i32 %63
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@PATH_MAX = global i32 0
@errno = global i64 0
@ENOENT = global i64 0
define i32 @check_path_components(i8* %0) {
%2 = alloca %struct.stat
%3 = load i32, i32* @PATH_MAX
%4 = add nsw i32 %3, 1
%5 = zext i32 %4 to i64
%6 = alloca i8, i64 %5
%7 = bitcast %struct.stat* %2 to i8*
%8 = tail call i32 @strlen(i8* %0)
%9 = sext i32 %8 to i64
%10 = icmp ult i64 %9, %5
%11 = zext i1 %10 to i32
%12 = tail call i32 @assert(i32 %11)
%13 = load i8, i8* %0
%14 = icmp eq i8 %13, 0
br i1 %14, label %37, label %15
15: ; preds = %1, %22
%16 = phi i8 [ %23, %22 ], [ %13, %1 ]
%17 = phi i8* [ %19, %22 ], [ %0, %1 ]
%18 = phi i8* [ %20, %22 ], [ %6, %1 ]
%19 = getelementptr inbounds i8, i8* %17, i64 1
%20 = getelementptr inbounds i8, i8* %18, i64 1
store i8 %16, i8* %18
%21 = load i8, i8* %19
switch i8 %21, label %22 [
i8 0, label %24
i8 47, label %24
]
22: ; preds = %15, %36
%23 = phi i8 [ %21, %15 ], [ %27, %36 ]
br label %15
24: ; preds = %15, %15
store i8 0, i8* %20
%25 = call i64 @stat(i8* %6, %struct.stat* %2)
%26 = icmp slt i64 %25, 0
%27 = load i8, i8* %19
%28 = icmp eq i8 %27, 0
br i1 %26, label %29, label %36
29: ; preds = %24
%30 = load i64, i64* @errno
%31 = load i64, i64* @ENOENT
%32 = icmp eq i64 %30, %31
%33 = select i1 %28, i1 %32, i1 false
%34 = trunc i64 %30 to i32
%35 = select i1 %33, i32 0, i32 %34
br label %37
36: ; preds = %24
br i1 %28, label %37, label %22
37: ; preds = %36, %29, %1
%38 = phi i32 [ 0, %1 ], [ %35, %29 ], [ 0, %36 ]
ret i32 %38
}