#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 */
int /*<<< orphan*/ ENAMETOOLONG;
int /*<<< orphan*/ errno;
int strlen(char const *);
int /*<<< orphan*/ strncpy(char *, char const *, int);
char *dirname(const char *path) {
  int l = 0;
  static char dir[1024];
  dir[0] = 0;
  for (l = strlen(path); path[l] != '/' && l > 0; l--)
    ;
  if (l >= 1024) {
    errno = ENAMETOOLONG;
    return NULL;
  }
  if (l == 0)
    return ".";
  strncpy(dir, path, l);
  return dir;
}


x86 assembly (clang, -O0):
<dirname>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movb   $0x0,0x2f06(%rip)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
movsbl (%rax,%rcx,1),%ecx
xor    %eax,%eax
cmp    $0x2f,%ecx
mov    %al,-0x15(%rbp)
je     116a <dirname+0x4a>
cmpl   $0x0,-0x14(%rbp)
setg   %al
mov    %al,-0x15(%rbp)
mov    -0x15(%rbp),%al
test   $0x1,%al
jne    117a <dirname+0x5a>
jmp    118d <dirname+0x6d>
jmp    117f <dirname+0x5f>
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
jmp    1146 <dirname+0x26>
cmpl   $0x400,-0x14(%rbp)
jl     11b9 <dirname+0x99>
mov    0x2e37(%rip),%rax
mov    (%rax),%ecx
mov    0x2e36(%rip),%rax
mov    %ecx,(%rax)
movq   $0x0,-0x8(%rbp)
jmp    11f1 <dirname+0xd1>
cmpl   $0x0,-0x14(%rbp)
jne    11d3 <dirname+0xb3>
lea    0xe36(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    11f1 <dirname+0xd1>
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
lea    0x2e5f(%rip),%rdi
call   1030 <strncpy@plt>
lea    0x2e53(%rip),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dirname>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
movb   $0x0,0x2f12(%rip)
call   1040 <strlen@plt>
cltq
lea    0x1(%rax),%edx
nopl   0x0(%rax,%rax,1)
add    $0xffffffff,%edx
cmpb   $0x2f,(%rbx,%rax,1)
je     1155 <dirname+0x35>
lea    -0x1(%rax),%rcx
test   %rax,%rax
mov    %rcx,%rax
jg     1140 <dirname+0x20>
cmp    $0x400,%edx
jl     1174 <dirname+0x54>
mov    0x2e74(%rip),%rax
mov    (%rax),%eax
mov    0x2e73(%rip),%rcx
mov    %eax,(%rcx)
xor    %r14d,%r14d
jmp    1193 <dirname+0x73>
test   %edx,%edx
je     118c <dirname+0x6c>
lea    0x2ec1(%rip),%r14
mov    %r14,%rdi
mov    %rbx,%rsi
call   1030 <strncpy@plt>
jmp    1193 <dirname+0x73>
lea    0xe6d(%rip),%r14
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@dirname.dir = internal global [1024 x i8] zeroinitializer
@ENAMETOOLONG = global i32 0
@errno = global i32 0
@.str = [2 x i8] c".\00"
define i8* @dirname(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i32
store i8* %0, i8** %3
store i32 0, i32* %4
store i8 0, i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @dirname.dir, i64 0, i64 0)
%5 = load i8*, i8** %3
%6 = call i32 @strlen(i8* %5)
store i32 %6, i32* %4
br label %7
7: ; preds = %21, %1
%8 = load i8*, i8** %3
%9 = load i32, i32* %4
%10 = sext i32 %9 to i64
%11 = getelementptr inbounds i8, i8* %8, i64 %10
%12 = load i8, i8* %11
%13 = sext i8 %12 to i32
%14 = icmp ne i32 %13, 47
br i1 %14, label %15, label %18
15: ; preds = %7
%16 = load i32, i32* %4
%17 = icmp sgt i32 %16, 0
br label %18
18: ; preds = %15, %7
%19 = phi i1 [ false, %7 ], [ %17, %15 ]
br i1 %19, label %20, label %24
20: ; preds = %18
br label %21
21: ; preds = %20
%22 = load i32, i32* %4
%23 = add nsw i32 %22, -1
store i32 %23, i32* %4
br label %7
24: ; preds = %18
%25 = load i32, i32* %4
%26 = icmp sge i32 %25, 1024
br i1 %26, label %27, label %29
27: ; preds = %24
%28 = load i32, i32* @ENAMETOOLONG
store i32 %28, i32* @errno
store i8* null, i8** %2
br label %37
29: ; preds = %24
%30 = load i32, i32* %4
%31 = icmp eq i32 %30, 0
br i1 %31, label %32, label %33
32: ; preds = %29
store i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8** %2
br label %37
33: ; preds = %29
%34 = load i8*, i8** %3
%35 = load i32, i32* %4
%36 = call i32 @strncpy(i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @dirname.dir, i64 0, i64 0), i8* %34, i32 %35)
store i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @dirname.dir, i64 0, i64 0), i8** %2
br label %37
37: ; preds = %33, %32, %27
%38 = load i8*, i8** %2
ret i8* %38
}

LLVM IR (-O2):
@dirname.dir = internal global [1024 x i8] zeroinitializer
@ENAMETOOLONG = global i32 0
@errno = global i32 0
@.str = [2 x i8] c".\00"
define i8* @dirname(i8* %0) {
store i8 0, i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @dirname.dir, i64 0, i64 0)
%2 = tail call i32 @strlen(i8* %0)
%3 = sext i32 %2 to i64
br label %4
4: ; preds = %4, %1
%5 = phi i64 [ %11, %4 ], [ %3, %1 ]
%6 = getelementptr inbounds i8, i8* %0, i64 %5
%7 = load i8, i8* %6
%8 = icmp ne i8 %7, 47
%9 = icmp sgt i64 %5, 0
%10 = select i1 %8, i1 %9, i1 false
%11 = add nsw i64 %5, -1
br i1 %10, label %4, label %12
12: ; preds = %4
%13 = trunc i64 %5 to i32
%14 = icmp sgt i32 %13, 1023
br i1 %14, label %15, label %17
15: ; preds = %12
%16 = load i32, i32* @ENAMETOOLONG
store i32 %16, i32* @errno
br label %21
17: ; preds = %12
%18 = icmp eq i32 %13, 0
br i1 %18, label %21, label %19
19: ; preds = %17
%20 = tail call i32 @strncpy(i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @dirname.dir, i64 0, i64 0), i8* %0, i32 %13)
br label %21
21: ; preds = %17, %19, %15
%22 = phi i8* [ null, %15 ], [ getelementptr inbounds ([1024 x i8], [1024 x i8]* @dirname.dir, i64 0, i64 0), %19 ], [ getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), %17 ]
ret i8* %22
}