#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;
};

/* Variables and functions */
int lstat(char *, struct stat *);
size_t strlen(char *);
int llstat(char *path, struct stat *buf) {
  int ret;
  char *c = NULL;
  size_t len = strlen(path);
  while (len > 1 && path[len - 1] == '/') {
    --len;
    c = path + len;
  }
  if (c) {
    *c = '\0';
    ret = lstat(path, buf);
    *c = '/';
  } else {
    ret = lstat(path, buf);
  }
  return ret;
}


x86 assembly (clang, -O0):
<llstat>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x28(%rbp)
xor    %eax,%eax
cmpq   $0x1,-0x28(%rbp)
mov    %al,-0x29(%rbp)
jbe    116e <llstat+0x4e>
mov    -0x8(%rbp),%rax
mov    -0x28(%rbp),%rcx
sub    $0x1,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x2f,%eax
sete   %al
mov    %al,-0x29(%rbp)
mov    -0x29(%rbp),%al
test   $0x1,%al
jne    117e <llstat+0x5e>
jmp    119b <llstat+0x7b>
mov    -0x28(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%rax
add    -0x28(%rbp),%rax
mov    %rax,-0x20(%rbp)
jmp    1145 <llstat+0x25>
cmpq   $0x0,-0x20(%rbp)
je     11c9 <llstat+0xa9>
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <lstat@plt>
mov    %eax,-0x14(%rbp)
mov    -0x20(%rbp),%rax
movb   $0x2f,(%rax)
jmp    11d9 <llstat+0xb9>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <lstat@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<llstat>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %rdi,%r15
call   1030 <strlen@plt>
cmp    $0x2,%rax
jb     118f <llstat+0x6f>
cmpb   $0x2f,-0x1(%r15,%rax,1)
jne    1169 <llstat+0x49>
lea    0x1(%r15),%rcx
lea    (%r15,%rax,1),%rdx
add    $0xffffffffffffffff,%rax
nopw   0x0(%rax,%rax,1)
cmp    $0x2,%rax
jb     1187 <llstat+0x67>
add    $0xffffffffffffffff,%rax
lea    -0x1(%rdx),%rbx
cmpb   $0x2f,-0x2(%rdx)
mov    %rbx,%rdx
je     1150 <llstat+0x30>
jmp    116b <llstat+0x4b>
xor    %ebx,%ebx
test   %rbx,%rbx
je     118f <llstat+0x6f>
movb   $0x0,(%rbx)
mov    %r15,%rdi
mov    %r14,%rsi
call   1040 <lstat@plt>
movb   $0x2f,(%rbx)
pop    %rbx
pop    %r14
pop    %r15
ret
mov    %rcx,%rbx
test   %rbx,%rbx
jne    1170 <llstat+0x50>
mov    %r15,%rdi
mov    %r14,%rsi
pop    %rbx
pop    %r14
pop    %r15
jmp    1040 <lstat@plt>

LLVM IR (-O0):
%struct.stat = type { i32 }
define i32 @llstat(i8* %0, %struct.stat* %1) {
%3 = alloca i8*
%4 = alloca %struct.stat*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
store i8* %0, i8** %3
store %struct.stat* %1, %struct.stat** %4
store i8* null, i8** %6
%8 = load i8*, i8** %3
%9 = call i64 @strlen(i8* %8)
store i64 %9, i64* %7
br label %10
10: ; preds = %23, %2
%11 = load i64, i64* %7
%12 = icmp ugt i64 %11, 1
br i1 %12, label %13, label %21
13: ; preds = %10
%14 = load i8*, i8** %3
%15 = load i64, i64* %7
%16 = sub i64 %15, 1
%17 = getelementptr inbounds i8, i8* %14, i64 %16
%18 = load i8, i8* %17
%19 = sext i8 %18 to i32
%20 = icmp eq i32 %19, 47
br label %21
21: ; preds = %13, %10
%22 = phi i1 [ false, %10 ], [ %20, %13 ]
br i1 %22, label %23, label %29
23: ; preds = %21
%24 = load i64, i64* %7
%25 = add i64 %24, -1
store i64 %25, i64* %7
%26 = load i8*, i8** %3
%27 = load i64, i64* %7
%28 = getelementptr inbounds i8, i8* %26, i64 %27
store i8* %28, i8** %6
br label %10
29: ; preds = %21
%30 = load i8*, i8** %6
%31 = icmp ne i8* %30, null
br i1 %31, label %32, label %38
32: ; preds = %29
%33 = load i8*, i8** %6
store i8 0, i8* %33
%34 = load i8*, i8** %3
%35 = load %struct.stat*, %struct.stat** %4
%36 = call i32 @lstat(i8* %34, %struct.stat* %35)
store i32 %36, i32* %5
%37 = load i8*, i8** %6
store i8 47, i8* %37
br label %42
38: ; preds = %29
%39 = load i8*, i8** %3
%40 = load %struct.stat*, %struct.stat** %4
%41 = call i32 @lstat(i8* %39, %struct.stat* %40)
store i32 %41, i32* %5
br label %42
42: ; preds = %38, %32
%43 = load i32, i32* %5
ret i32 %43
}

LLVM IR (-O2):
%struct.stat = type { i32 }
define i32 @llstat(i8* %0, %struct.stat* nocapture %1) {
%3 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%4 = icmp ugt i64 %3, 1
br i1 %4, label %5, label %25
5: ; preds = %2
%6 = getelementptr i8, i8* %0, i64 1
%7 = add i64 %3, -1
%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 %20
11: ; preds = %5, %15
%12 = phi i8* [ %17, %15 ], [ %8, %5 ]
%13 = phi i64 [ %16, %15 ], [ %7, %5 ]
%14 = icmp ugt i64 %13, 1
br i1 %14, label %15, label %20
15: ; preds = %11
%16 = add i64 %13, -1
%17 = getelementptr inbounds i8, i8* %0, i64 %16
%18 = load i8, i8* %17
%19 = icmp eq i8 %18, 47
br i1 %19, label %11, label %20
20: ; preds = %11, %15, %5
%21 = phi i8* [ null, %5 ], [ %6, %11 ], [ %12, %15 ]
%22 = icmp eq i8* %21, null
br i1 %22, label %25, label %23
23: ; preds = %20
store i8 0, i8* %21
%24 = tail call i32 @lstat(i8* %0, %struct.stat* %1)
store i8 47, i8* %21
br label %27
25: ; preds = %2, %20
%26 = tail call i32 @lstat(i8* %0, %struct.stat* %1)
br label %27
27: ; preds = %25, %23
%28 = phi i32 [ %24, %23 ], [ %26, %25 ]
ret i32 %28
}