#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 PATH_MAX;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
char *malloc(int);
scalar_t__ realpath(char *, char *);
int /*<<< orphan*/ stderr;
int strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, int);
char *canonicalize(char *path) {
  char *canonical = malloc(PATH_MAX + 1);
  if (!canonical) {
    fprintf(stderr, "Error! Not enough memory!\n");
    return NULL;
  }
  if (strlen(path) > PATH_MAX) {
    fprintf(stderr, "Mount point string too long\n");
    return NULL;
  }
  if (path == NULL)
    return NULL;
  if (realpath(path, canonical))
    return canonical;
  strncpy(canonical, path, PATH_MAX);
  canonical[PATH_MAX] = '\0';
  return canonical;
}


x86 assembly (clang, -O0):
<canonicalize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e8d(%rip),%rax
mov    (%rax),%edi
add    $0x1,%edi
call   1070 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    119e <canonicalize+0x4e>
mov    0x2e55(%rip),%rax
mov    (%rax),%edi
lea    0xe74(%rip),%rsi
call   1050 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    1244 <canonicalize+0xf4>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    0x2e42(%rip),%rcx
cmp    (%rcx),%eax
jle    11d8 <canonicalize+0x88>
mov    0x2e1b(%rip),%rax
mov    (%rax),%edi
lea    0xe55(%rip),%rsi
call   1050 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    1244 <canonicalize+0xf4>
cmpq   $0x0,-0x10(%rbp)
jne    11f0 <canonicalize+0xa0>
movq   $0x0,-0x8(%rbp)
jmp    1244 <canonicalize+0xf4>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1060 <realpath@plt>
cmp    $0x0,%rax
je     1214 <canonicalize+0xc4>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1244 <canonicalize+0xf4>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    0x2dcd(%rip),%rax
mov    (%rax),%edx
call   1030 <strncpy@plt>
mov    -0x18(%rbp),%rax
mov    0x2dbb(%rip),%rcx
movslq (%rcx),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<canonicalize>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r14
mov    0x2e91(%rip),%r15
mov    (%r15),%edi
add    $0x1,%edi
call   1070 <malloc@plt>
test   %rax,%rax
je     1191 <canonicalize+0x41>
mov    %rax,%rbx
mov    %r14,%rdi
call   1040 <strlen@plt>
cmp    (%r15),%eax
jle    11b1 <canonicalize+0x61>
mov    0x2e52(%rip),%rax
mov    (%rax),%edi
lea    0xe8c(%rip),%rsi
jmp    11a1 <canonicalize+0x51>
mov    0x2e40(%rip),%rax
mov    (%rax),%edi
lea    0xe5f(%rip),%rsi
call   1050 <fprintf@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret
test   %r14,%r14
je     11a6 <canonicalize+0x56>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1060 <realpath@plt>
test   %rax,%rax
jne    11a8 <canonicalize+0x58>
mov    (%r15),%edx
mov    %rbx,%rdi
mov    %r14,%rsi
call   1030 <strncpy@plt>
movslq (%r15),%rax
movb   $0x0,(%rbx,%rax,1)
jmp    11a8 <canonicalize+0x58>

LLVM IR (-O0):
@PATH_MAX = global i32 0
@stderr = global i32 0
@.str = [27 x i8] c"Error! Not enough memory!\0A\00"
@.str.1 = [29 x i8] c"Mount point string too long\0A\00"
define i8* @canonicalize(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
store i8* %0, i8** %3
%5 = load i32, i32* @PATH_MAX
%6 = add nsw i32 %5, 1
%7 = call i8* @malloc(i32 %6)
store i8* %7, i8** %4
%8 = load i8*, i8** %4
%9 = icmp ne i8* %8, null
br i1 %9, label %13, label %10
10: ; preds = %1
%11 = load i32, i32* @stderr
%12 = call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
store i8* null, i8** %2
br label %42
13: ; preds = %1
%14 = load i8*, i8** %3
%15 = call i32 @strlen(i8* %14)
%16 = load i32, i32* @PATH_MAX
%17 = icmp sgt i32 %15, %16
br i1 %17, label %18, label %21
18: ; preds = %13
%19 = load i32, i32* @stderr
%20 = call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
store i8* null, i8** %2
br label %42
21: ; preds = %13
%22 = load i8*, i8** %3
%23 = icmp eq i8* %22, null
br i1 %23, label %24, label %25
24: ; preds = %21
store i8* null, i8** %2
br label %42
25: ; preds = %21
%26 = load i8*, i8** %3
%27 = load i8*, i8** %4
%28 = call i64 @realpath(i8* %26, i8* %27)
%29 = icmp ne i64 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %25
%31 = load i8*, i8** %4
store i8* %31, i8** %2
br label %42
32: ; preds = %25
%33 = load i8*, i8** %4
%34 = load i8*, i8** %3
%35 = load i32, i32* @PATH_MAX
%36 = call i32 @strncpy(i8* %33, i8* %34, i32 %35)
%37 = load i8*, i8** %4
%38 = load i32, i32* @PATH_MAX
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds i8, i8* %37, i64 %39
store i8 0, i8* %40
%41 = load i8*, i8** %4
store i8* %41, i8** %2
br label %42
42: ; preds = %32, %30, %24, %18, %10
%43 = load i8*, i8** %2
ret i8* %43
}

LLVM IR (-O2):
@PATH_MAX = global i32 0
@stderr = global i32 0
@.str = [27 x i8] c"Error! Not enough memory!\0A\00"
@.str.1 = [29 x i8] c"Mount point string too long\0A\00"
define i8* @canonicalize(i8* %0) {
%2 = load i32, i32* @PATH_MAX
%3 = add nsw i32 %2, 1
%4 = tail call i8* @malloc(i32 %3)
%5 = icmp eq i8* %4, null
br i1 %5, label %6, label %9
6: ; preds = %1
%7 = load i32, i32* @stderr
%8 = tail call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0))
br label %27
9: ; preds = %1
%10 = tail call i32 @strlen(i8* %0)
%11 = load i32, i32* @PATH_MAX
%12 = icmp sgt i32 %10, %11
br i1 %12, label %13, label %16
13: ; preds = %9
%14 = load i32, i32* @stderr
%15 = tail call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0))
br label %27
16: ; preds = %9
%17 = icmp eq i8* %0, null
br i1 %17, label %27, label %18
18: ; preds = %16
%19 = tail call i64 @realpath(i8* %0, i8* %4)
%20 = icmp eq i64 %19, 0
br i1 %20, label %21, label %27
21: ; preds = %18
%22 = load i32, i32* @PATH_MAX
%23 = tail call i32 @strncpy(i8* %4, i8* %0, i32 %22)
%24 = load i32, i32* @PATH_MAX
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds i8, i8* %4, i64 %25
store i8 0, i8* %26
br label %27
27: ; preds = %18, %16, %21, %13, %6
%28 = phi i8* [ null, %13 ], [ %4, %21 ], [ null, %6 ], [ null, %16 ], [ %4, %18 ]
ret i8* %28
}