#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 dirent {
  char *d_name;
};

/* Variables and functions */
int /*<<< orphan*/ isdir(char *);
scalar_t__ strcmp(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
char *append_if_directory(char *buffer, char *oldend, struct dirent *dir) {
  char *newend;
  int oldlength, dirlength;
  if (strcmp(dir->d_name, ".") == 0 || strcmp(dir->d_name, "..") == 0)
    return NULL;
  oldlength = strlen(buffer);
  dirlength = strlen(dir->d_name);
  if (256 < oldlength + dirlength + 2)
    return NULL;
  strcpy(oldend, dir->d_name);
  if (!isdir(buffer)) {
    *oldend = 0;
    return NULL;
  }
  newend = oldend + dirlength;
  strcpy(newend, "/");
  newend++;
  return newend;
}


x86 assembly (clang, -O0):
<append_if_directory>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
lea    0xe9f(%rip),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%rax
je     118e <append_if_directory+0x4e>
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
lea    0xe81(%rip),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%rax
jne    119b <append_if_directory+0x5b>
movq   $0x0,-0x8(%rbp)
jmp    1242 <append_if_directory+0x102>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x30(%rbp)
mov    -0x2c(%rbp),%ecx
add    -0x30(%rbp),%ecx
add    $0x2,%ecx
mov    $0x100,%eax
cmp    %ecx,%eax
jge    11d9 <append_if_directory+0x99>
movq   $0x0,-0x8(%rbp)
jmp    1242 <append_if_directory+0x102>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rax
mov    (%rax),%rsi
call   1030 <strcpy@plt>
mov    -0x10(%rbp),%rdi
call   1060 <isdir@plt>
cmp    $0x0,%eax
jne    120f <append_if_directory+0xcf>
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
movq   $0x0,-0x8(%rbp)
jmp    1242 <append_if_directory+0x102>
mov    -0x18(%rbp),%rax
movslq -0x30(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
lea    0xdda(%rip),%rsi
call   1030 <strcpy@plt>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<append_if_directory>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdx,%rbx
mov    %rsi,%r14
mov    %rdi,%r15
mov    (%rdx),%rdi
lea    0xea6(%rip),%rsi
call   1050 <strcmp@plt>
test   %rax,%rax
je     11d0 <append_if_directory+0x90>
mov    (%rbx),%rdi
lea    0xe91(%rip),%rsi
call   1050 <strcmp@plt>
test   %rax,%rax
je     11d0 <append_if_directory+0x90>
mov    %r15,%rdi
call   1040 <strlen@plt>
mov    %eax,%ebp
mov    (%rbx),%rdi
call   1040 <strlen@plt>
add    %eax,%ebp
cmp    $0xfe,%ebp
jg     11d0 <append_if_directory+0x90>
mov    %eax,%r12d
mov    (%rbx),%rsi
mov    %r14,%rdi
call   1030 <strcpy@plt>
mov    %r15,%rdi
call   1060 <isdir@plt>
test   %eax,%eax
je     11cc <append_if_directory+0x8c>
movslq %r12d,%rbx
lea    (%r14,%rbx,1),%rdi
lea    0xe46(%rip),%rsi
call   1030 <strcpy@plt>
lea    (%r14,%rbx,1),%rax
add    $0x1,%rax
jmp    11d2 <append_if_directory+0x92>
movb   $0x0,(%r14)
xor    %eax,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.dirent = type { i8* }
@.str = [2 x i8] c".\00"
@.str.1 = [3 x i8] c"..\00"
@.str.2 = [2 x i8] c"/\00"
define i8* @append_if_directory(i8* %0, i8* %1, %struct.dirent* nocapture %2) {
%4 = getelementptr inbounds %struct.dirent, %struct.dirent* %2, i64 0, i32 0
%5 = load i8*, i8** %4
%6 = tail call i64 @strcmp(i8* %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%7 = icmp eq i64 %6, 0
br i1 %7, label %29, label %8
8: ; preds = %3
%9 = load i8*, i8** %4
%10 = tail call i64 @strcmp(i8* %9, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%11 = icmp eq i64 %10, 0
br i1 %11, label %29, label %12
12: ; preds = %8
%13 = tail call i32 @strlen(i8* %0)
%14 = load i8*, i8** %4
%15 = tail call i32 @strlen(i8* %14)
%16 = add nsw i32 %15, %13
%17 = icmp sgt i32 %16, 254
br i1 %17, label %29, label %18
18: ; preds = %12
%19 = load i8*, i8** %4
%20 = tail call i32 @strcpy(i8* %1, i8* %19)
%21 = tail call i32 @isdir(i8* %0)
%22 = icmp eq i32 %21, 0
br i1 %22, label %23, label %24
23: ; preds = %18
store i8 0, i8* %1
br label %29
24: ; preds = %18
%25 = sext i32 %15 to i64
%26 = getelementptr inbounds i8, i8* %1, i64 %25
%27 = tail call i32 @strcpy(i8* %26, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%28 = getelementptr inbounds i8, i8* %26, i64 1
br label %29
29: ; preds = %12, %3, %8, %24, %23
%30 = phi i8* [ %28, %24 ], [ null, %23 ], [ null, %8 ], [ null, %3 ], [ null, %12 ]
ret i8* %30
}