#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 */

/* Variables and functions */
char *strchr(char *, char);
int /*<<< orphan*/ strcpy(char *, char *);
int getdir(char *fullname, char *path, int loop) {
  char tmpBuf[100], *p, *p1;
  strcpy(tmpBuf, fullname);
  path[0] = '\0';
  p1 = tmpBuf;
  while (1) {
    if ((p = strchr(p1, '/'))) {
      if (--loop == 0) {
        *p = '\0';
        strcpy(path, tmpBuf);
        return 0;
      }
      p1 = ++p;
    } else
      break;
  }
  return -1;
}


x86 assembly (clang, -O0):
<getdir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
lea    -0x80(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
lea    -0x80(%rbp),%rax
mov    %rax,-0x90(%rbp)
mov    -0x90(%rbp),%rdi
mov    $0x2f,%esi
call   1040 <strchr@plt>
mov    %rax,-0x88(%rbp)
cmp    $0x0,%rax
je     11ca <getdir+0xaa>
mov    -0x1c(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jne    11ac <getdir+0x8c>
mov    -0x88(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x18(%rbp),%rdi
lea    -0x80(%rbp),%rsi
call   1030 <strcpy@plt>
movl   $0x0,-0x4(%rbp)
jmp    11db <getdir+0xbb>
mov    -0x88(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x88(%rbp)
mov    %rax,-0x90(%rbp)
jmp    11cf <getdir+0xaf>
jmp    11d4 <getdir+0xb4>
jmp    1155 <getdir+0x35>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getdir>:
push   %rbp
push   %r14
push   %rbx
sub    $0x70,%rsp
mov    %edx,%ebp
mov    %rsi,%r14
mov    %rdi,%rsi
mov    %rsp,%rbx
mov    %rbx,%rdi
call   1030 <strcpy@plt>
movb   $0x0,(%r14)
mov    %rbx,%rdi
mov    $0x2f,%esi
call   1040 <strchr@plt>
mov    $0xffffffff,%ebx
test   %rax,%rax
je     118d <getdir+0x6d>
cs nopw 0x0(%rax,%rax,1)
add    $0xffffffff,%ebp
je     117d <getdir+0x5d>
add    $0x1,%rax
mov    %rax,%rdi
mov    $0x2f,%esi
call   1040 <strchr@plt>
test   %rax,%rax
jne    1160 <getdir+0x40>
jmp    118d <getdir+0x6d>
movb   $0x0,(%rax)
mov    %rsp,%rsi
mov    %r14,%rdi
call   1030 <strcpy@plt>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x70,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define i32 @getdir(i8* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca [100 x i8]
%9 = alloca i8*
%10 = alloca i8*
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%11 = getelementptr inbounds [100 x i8], [100 x i8]* %8, i64 0, i64 0
%12 = load i8*, i8** %5
%13 = call i32 @strcpy(i8* %11, i8* %12)
%14 = load i8*, i8** %6
%15 = getelementptr inbounds i8, i8* %14, i64 0
store i8 0, i8* %15
%16 = getelementptr inbounds [100 x i8], [100 x i8]* %8, i64 0, i64 0
store i8* %16, i8** %10
br label %17
17: ; preds = %3, %34
%18 = load i8*, i8** %10
%19 = call i8* @strchr(i8* %18, i8 signext 47)
store i8* %19, i8** %9
%20 = icmp ne i8* %19, null
br i1 %20, label %21, label %33
21: ; preds = %17
%22 = load i32, i32* %7
%23 = add nsw i32 %22, -1
store i32 %23, i32* %7
%24 = icmp eq i32 %23, 0
br i1 %24, label %25, label %30
25: ; preds = %21
%26 = load i8*, i8** %9
store i8 0, i8* %26
%27 = load i8*, i8** %6
%28 = getelementptr inbounds [100 x i8], [100 x i8]* %8, i64 0, i64 0
%29 = call i32 @strcpy(i8* %27, i8* %28)
store i32 0, i32* %4
br label %36
30: ; preds = %21
%31 = load i8*, i8** %9
%32 = getelementptr inbounds i8, i8* %31, i32 1
store i8* %32, i8** %9
store i8* %32, i8** %10
br label %34
33: ; preds = %17
br label %35
34: ; preds = %30
br label %17
35: ; preds = %33
store i32 -1, i32* %4
br label %36
36: ; preds = %35, %25
%37 = load i32, i32* %4
ret i32 %37
}

LLVM IR (-O2):
define i32 @getdir(i8* %0, i8* %1, i32 %2) {
%4 = alloca [100 x i8]
%5 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%6 = call i32 @strcpy(i8* %5, i8* %0)
store i8 0, i8* %1
%7 = call i8* @strchr(i8* dereferenceable(1) %5, i8 signext 47)
%8 = icmp eq i8* %7, null
br i1 %8, label %20, label %9
9: ; preds = %3, %16
%10 = phi i8* [ %18, %16 ], [ %7, %3 ]
%11 = phi i32 [ %12, %16 ], [ %2, %3 ]
%12 = add nsw i32 %11, -1
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %9
store i8 0, i8* %10
%15 = call i32 @strcpy(i8* %1, i8* %5)
br label %20
16: ; preds = %9
%17 = getelementptr inbounds i8, i8* %10, i64 1
%18 = call i8* @strchr(i8* dereferenceable(1) %17, i8 signext 47)
%19 = icmp eq i8* %18, null
br i1 %19, label %20, label %9
20: ; preds = %16, %3, %14
%21 = phi i32 [ 0, %14 ], [ -1, %3 ], [ -1, %16 ]
ret i32 %21
}