#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*/ EINVAL;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ realpath(char const *, char *);
int strlcpy(char *, char *, int);
int path_canonicalize(const char *src, char *dst, int dstlen) {
  char buf[4096];
  int n = 0;
  if (!src || !*src) {
    errno = EINVAL;
    return (-1);
  }
  if (!realpath(src, buf)) {
    return (-1);
  }
  if (buf[0] != '/') {
    errno = EINVAL;
    return (-1);
  }
  if ((dst != NULL) && (dstlen > 0)) {
    n = strlcpy(dst, buf, dstlen);
  }
  return (n);
}


x86 assembly (clang, -O0):
<path_canonicalize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1030,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
movl   $0x0,-0x1024(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1158 <path_canonicalize+0x38>
mov    -0x10(%rbp),%rax
cmpb   $0x0,(%rax)
jne    1176 <path_canonicalize+0x56>
mov    0x2e79(%rip),%rax
mov    (%rax),%ecx
mov    0x2e78(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1200 <path_canonicalize+0xe0>
mov    -0x10(%rbp),%rdi
lea    -0x1020(%rbp),%rsi
call   1030 <realpath@plt>
cmp    $0x0,%eax
jne    119b <path_canonicalize+0x7b>
movl   $0xffffffff,-0x4(%rbp)
jmp    1200 <path_canonicalize+0xe0>
movsbl -0x1020(%rbp),%eax
cmp    $0x2f,%eax
je     11c9 <path_canonicalize+0xa9>
mov    0x2e26(%rip),%rax
mov    (%rax),%ecx
mov    0x2e25(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1200 <path_canonicalize+0xe0>
cmpq   $0x0,-0x18(%rbp)
je     11f7 <path_canonicalize+0xd7>
cmpl   $0x0,-0x1c(%rbp)
jle    11f7 <path_canonicalize+0xd7>
mov    -0x18(%rbp),%rdi
lea    -0x1020(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1040 <strlcpy@plt>
mov    %eax,-0x1024(%rbp)
mov    -0x1024(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<path_canonicalize>:
push   %rbp
push   %rbx
sub    $0x1008,%rsp
test   %rdi,%rdi
je     116b <path_canonicalize+0x4b>
cmpb   $0x0,(%rdi)
je     116b <path_canonicalize+0x4b>
mov    %edx,%ebp
mov    %rsi,%rbx
mov    %rsp,%rsi
call   1030 <realpath@plt>
mov    %eax,%ecx
mov    $0xffffffff,%eax
test   %ecx,%ecx
je     1182 <path_canonicalize+0x62>
cmpb   $0x2f,(%rsp)
jne    118c <path_canonicalize+0x6c>
xor    %eax,%eax
test   %rbx,%rbx
je     1182 <path_canonicalize+0x62>
test   %ebp,%ebp
jle    1182 <path_canonicalize+0x62>
mov    %rsp,%rsi
mov    %rbx,%rdi
mov    %ebp,%edx
call   1040 <strlcpy@plt>
jmp    1182 <path_canonicalize+0x62>
mov    0x2e66(%rip),%rax
mov    (%rax),%eax
mov    0x2e65(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%eax
add    $0x1008,%rsp
pop    %rbx
pop    %rbp
ret
mov    0x2e45(%rip),%rcx
mov    (%rcx),%ecx
mov    0x2e44(%rip),%rdx
mov    %ecx,(%rdx)
jmp    1182 <path_canonicalize+0x62>

LLVM IR (-O0):
@EINVAL = global i32 0
@errno = global i32 0
define i32 @path_canonicalize(i8* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca [4096 x i8]
%9 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i32 0, i32* %9
%10 = load i8*, i8** %5
%11 = icmp ne i8* %10, null
br i1 %11, label %12, label %16
12: ; preds = %3
%13 = load i8*, i8** %5
%14 = load i8, i8* %13
%15 = icmp ne i8 %14, 0
br i1 %15, label %18, label %16
16: ; preds = %12, %3
%17 = load i32, i32* @EINVAL
store i32 %17, i32* @errno
store i32 -1, i32* %4
br label %44
18: ; preds = %12
%19 = load i8*, i8** %5
%20 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 0
%21 = call i32 @realpath(i8* %19, i8* %20)
%22 = icmp ne i32 %21, 0
br i1 %22, label %24, label %23
23: ; preds = %18
store i32 -1, i32* %4
br label %44
24: ; preds = %18
%25 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 0
%26 = load i8, i8* %25
%27 = sext i8 %26 to i32
%28 = icmp ne i32 %27, 47
br i1 %28, label %29, label %31
29: ; preds = %24
%30 = load i32, i32* @EINVAL
store i32 %30, i32* @errno
store i32 -1, i32* %4
br label %44
31: ; preds = %24
%32 = load i8*, i8** %6
%33 = icmp ne i8* %32, null
br i1 %33, label %34, label %42
34: ; preds = %31
%35 = load i32, i32* %7
%36 = icmp sgt i32 %35, 0
br i1 %36, label %37, label %42
37: ; preds = %34
%38 = load i8*, i8** %6
%39 = getelementptr inbounds [4096 x i8], [4096 x i8]* %8, i64 0, i64 0
%40 = load i32, i32* %7
%41 = call i32 @strlcpy(i8* %38, i8* %39, i32 %40)
store i32 %41, i32* %9
br label %42
42: ; preds = %37, %34, %31
%43 = load i32, i32* %9
store i32 %43, i32* %4
br label %44
44: ; preds = %42, %29, %23, %16
%45 = load i32, i32* %4
ret i32 %45
}

LLVM IR (-O2):
@EINVAL = global i32 0
@errno = global i32 0
define i32 @path_canonicalize(i8* %0, i8* %1, i32 %2) {
%4 = alloca [4096 x i8]
%5 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 0
%6 = icmp eq i8* %0, null
br i1 %6, label %10, label %7
7: ; preds = %3
%8 = load i8, i8* %0
%9 = icmp eq i8 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %7, %3
%11 = load i32, i32* @EINVAL
store i32 %11, i32* @errno
br label %26
12: ; preds = %7
%13 = call i32 @realpath(i8* %0, i8* %5)
%14 = icmp eq i32 %13, 0
br i1 %14, label %26, label %15
15: ; preds = %12
%16 = load i8, i8* %5
%17 = icmp eq i8 %16, 47
br i1 %17, label %20, label %18
18: ; preds = %15
%19 = load i32, i32* @EINVAL
store i32 %19, i32* @errno
br label %26
20: ; preds = %15
%21 = icmp ne i8* %1, null
%22 = icmp sgt i32 %2, 0
%23 = and i1 %21, %22
br i1 %23, label %24, label %26
24: ; preds = %20
%25 = call i32 @strlcpy(i8* %1, i8* %5, i32 %2)
br label %26
26: ; preds = %20, %24, %12, %18, %10
%27 = phi i32 [ -1, %18 ], [ -1, %10 ], [ -1, %12 ], [ %25, %24 ], [ 0, %20 ]
ret i32 %27
}