#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*/ memset(char *, int /*<<< orphan*/, int);
int readlink(char *, char *, int);
int /*<<< orphan*/ strncpy(char *, char *, int);
char *strrchr(char *, char);
char *getrealdir(char *path) {
  char *p;
  char buf[1024];
  int len;
  len = readlink(path, buf, 1023);
  if (len < 0)
    return NULL;
  buf[len] = 0;
  p = strrchr(buf, '/');
  if (!p) {
    return NULL;
  } else if (p == buf) {
    *(++p) = '\0';
  } else
    *p = '\0';
  memset(path, 0, 1024);
  strncpy(path, buf, 1024);
  return path;
}


x86 assembly (clang, -O0):
<getrealdir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x420(%rbp),%rsi
mov    $0x3ff,%edx
call   1040 <readlink@plt>
mov    %eax,-0x424(%rbp)
cmpl   $0x0,-0x424(%rbp)
jge    1184 <getrealdir+0x44>
movq   $0x0,-0x8(%rbp)
jmp    1225 <getrealdir+0xe5>
movslq -0x424(%rbp),%rax
movb   $0x0,-0x420(%rbp,%rax,1)
lea    -0x420(%rbp),%rdi
mov    $0x2f,%esi
call   1050 <strrchr@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11c0 <getrealdir+0x80>
movq   $0x0,-0x8(%rbp)
jmp    1225 <getrealdir+0xe5>
mov    -0x18(%rbp),%rax
lea    -0x420(%rbp),%rcx
cmp    %rcx,%rax
jne    11ec <getrealdir+0xac>
mov    -0x18(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x18(%rbp)
movb   $0x0,0x1(%rax)
jmp    11f3 <getrealdir+0xb3>
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
jmp    11f8 <getrealdir+0xb8>
mov    -0x10(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1060 <memset@plt>
mov    -0x10(%rbp),%rdi
lea    -0x420(%rbp),%rsi
mov    $0x400,%edx
call   1030 <strncpy@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getrealdir>:
push   %r14
push   %rbx
sub    $0x408,%rsp
mov    %rdi,%rbx
mov    %rsp,%rsi
mov    $0x3ff,%edx
call   1040 <readlink@plt>
test   %eax,%eax
js     11a9 <getrealdir+0x69>
mov    %eax,%eax
movb   $0x0,(%rsp,%rax,1)
mov    %rsp,%r14
mov    %r14,%rdi
mov    $0x2f,%esi
call   1050 <strrchr@plt>
test   %rax,%rax
je     11a9 <getrealdir+0x69>
cmp    %r14,%rax
lea    0x1(%rsp),%rcx
cmovne %rax,%rcx
movb   $0x0,(%rcx)
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1060 <memset@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
mov    $0x400,%edx
call   1030 <strncpy@plt>
jmp    11ab <getrealdir+0x6b>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x408,%rsp
pop    %rbx
pop    %r14
ret

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

LLVM IR (-O2):
define i8* @getrealdir(i8* %0) {
%2 = alloca [1024 x i8]
%3 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%4 = call i32 @readlink(i8* %0, i8* %3, i32 1023)
%5 = icmp slt i32 %4, 0
br i1 %5, label %17, label %6
6: ; preds = %1
%7 = zext i32 %4 to i64
%8 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 %7
store i8 0, i8* %8
%9 = call i8* @strrchr(i8* %3, i8 signext 47)
%10 = icmp eq i8* %9, null
br i1 %10, label %17, label %11
11: ; preds = %6
%12 = icmp eq i8* %9, %3
%13 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 1
%14 = select i1 %12, i8* %13, i8* %9
store i8 0, i8* %14
%15 = call i32 @memset(i8* %0, i32 0, i32 1024)
%16 = call i32 @strncpy(i8* %0, i8* %3, i32 1024)
br label %17
17: ; preds = %6, %1, %11
%18 = phi i8* [ %0, %11 ], [ null, %1 ], [ null, %6 ]
ret i8* %18
}