#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*/ ENOMEM;
size_t PATH_MAX;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ free(char *);
char *malloc(size_t);
int /*<<< orphan*/ memcpy(char *, char const *, size_t);
size_t strlen(char const *);
char *path_parts(const char **parts) {
  char *path, *cur;
  size_t avail = PATH_MAX, len;
  size_t prev;
  if ((path = malloc(avail + 1)) == NULL) {
    errno = ENOMEM;
    return NULL;
  }
  cur = path;
  prev = 0;
  while (*parts != NULL) {
    len = strlen(*parts);
    if (len + prev > avail) {
      free(path);
      errno = ENAMETOOLONG;
      return NULL;
    }
    if (prev) {
      *cur = '/';
      cur++;
    } else {
      prev = 1;
    }
    memcpy(cur, *parts, len);
    cur += len;
    parts++;
  }
  *cur = '\0';
  return path;
}


x86 assembly (clang, -O0):
<path_parts>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e9d(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
add    $0x1,%rdi
call   1060 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    1194 <path_parts+0x54>
mov    0x2e5c(%rip),%rax
mov    (%rax),%ecx
mov    0x2e4b(%rip),%rax
mov    %ecx,(%rax)
movq   $0x0,-0x8(%rbp)
jmp    1267 <path_parts+0x127>
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x10(%rbp),%rax
cmpq   $0x0,(%rax)
je     1258 <path_parts+0x118>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
add    -0x38(%rbp),%rax
cmp    -0x28(%rbp),%rax
jbe    11fc <path_parts+0xbc>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    0x2de4(%rip),%rax
mov    (%rax),%ecx
mov    0x2de3(%rip),%rax
mov    %ecx,(%rax)
movq   $0x0,-0x8(%rbp)
jmp    1267 <path_parts+0x127>
cmpq   $0x0,-0x38(%rbp)
je     121f <path_parts+0xdf>
mov    -0x20(%rbp),%rax
movb   $0x2f,(%rax)
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    1227 <path_parts+0xe7>
movq   $0x1,-0x38(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
mov    -0x30(%rbp),%rdx
call   1050 <memcpy@plt>
mov    -0x30(%rbp),%rax
add    -0x20(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x10(%rbp)
jmp    11a4 <path_parts+0x64>
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<path_parts>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%r15
mov    0x2e98(%rip),%rax
mov    (%rax),%rax
mov    %rax,0x10(%rsp)
lea    0x1(%rax),%rdi
call   1060 <malloc@plt>
test   %rax,%rax
je     11eb <path_parts+0xab>
mov    (%r15),%r12
mov    %rax,0x8(%rsp)
mov    %rax,%r13
test   %r12,%r12
je     11df <path_parts+0x9f>
add    $0x8,%r15
mov    $0x1,%r14b
xor    %ebp,%ebp
mov    0x8(%rsp),%r13
jmp    11b5 <path_parts+0x75>
xchg   %ax,%ax
mov    %r13,%rdi
mov    %r12,%rsi
mov    %rbx,%rdx
call   1050 <memcpy@plt>
add    %rbx,%r13
mov    (%r15),%r12
add    $0x8,%r15
xor    %r14d,%r14d
mov    $0x1,%ebp
test   %r12,%r12
je     11df <path_parts+0x9f>
mov    %r12,%rdi
call   1040 <strlen@plt>
add    %rax,%rbp
cmp    0x10(%rsp),%rbp
ja     11f4 <path_parts+0xb4>
mov    %rax,%rbx
test   $0x1,%r14b
jne    1190 <path_parts+0x50>
movb   $0x2f,0x0(%r13)
add    $0x1,%r13
mov    -0x8(%r15),%r12
jmp    1190 <path_parts+0x50>
movb   $0x0,0x0(%r13)
mov    0x8(%rsp),%rax
jmp    1212 <path_parts+0xd2>
mov    0x2de6(%rip),%rax
jmp    1205 <path_parts+0xc5>
mov    0x8(%rsp),%rdi
call   1030 <free@plt>
mov    0x2dc3(%rip),%rax
mov    (%rax),%eax
mov    0x2dc2(%rip),%rcx
mov    %eax,(%rcx)
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@PATH_MAX = global i64 0
@ENOMEM = global i32 0
@errno = global i32 0
@ENAMETOOLONG = global i32 0
define i8* @path_parts(i8** %0) {
%2 = alloca i8*
%3 = alloca i8**
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64
%8 = alloca i64
store i8** %0, i8*** %3
%9 = load i64, i64* @PATH_MAX
store i64 %9, i64* %6
%10 = load i64, i64* %6
%11 = add i64 %10, 1
%12 = call i8* @malloc(i64 %11)
store i8* %12, i8** %4
%13 = icmp eq i8* %12, null
br i1 %13, label %14, label %16
14: ; preds = %1
%15 = load i32, i32* @ENOMEM
store i32 %15, i32* @errno
store i8* null, i8** %2
br label %57
16: ; preds = %1
%17 = load i8*, i8** %4
store i8* %17, i8** %5
store i64 0, i64* %8
br label %18
18: ; preds = %43, %16
%19 = load i8**, i8*** %3
%20 = load i8*, i8** %19
%21 = icmp ne i8* %20, null
br i1 %21, label %22, label %54
22: ; preds = %18
%23 = load i8**, i8*** %3
%24 = load i8*, i8** %23
%25 = call i64 @strlen(i8* %24)
store i64 %25, i64* %7
%26 = load i64, i64* %7
%27 = load i64, i64* %8
%28 = add i64 %26, %27
%29 = load i64, i64* %6
%30 = icmp ugt i64 %28, %29
br i1 %30, label %31, label %35
31: ; preds = %22
%32 = load i8*, i8** %4
%33 = call i32 @free(i8* %32)
%34 = load i32, i32* @ENAMETOOLONG
store i32 %34, i32* @errno
store i8* null, i8** %2
br label %57
35: ; preds = %22
%36 = load i64, i64* %8
%37 = icmp ne i64 %36, 0
br i1 %37, label %38, label %42
38: ; preds = %35
%39 = load i8*, i8** %5
store i8 47, i8* %39
%40 = load i8*, i8** %5
%41 = getelementptr inbounds i8, i8* %40, i32 1
store i8* %41, i8** %5
br label %43
42: ; preds = %35
store i64 1, i64* %8
br label %43
43: ; preds = %42, %38
%44 = load i8*, i8** %5
%45 = load i8**, i8*** %3
%46 = load i8*, i8** %45
%47 = load i64, i64* %7
%48 = call i32 @memcpy(i8* %44, i8* %46, i64 %47)
%49 = load i64, i64* %7
%50 = load i8*, i8** %5
%51 = getelementptr inbounds i8, i8* %50, i64 %49
store i8* %51, i8** %5
%52 = load i8**, i8*** %3
%53 = getelementptr inbounds i8*, i8** %52, i32 1
store i8** %53, i8*** %3
br label %18
54: ; preds = %18
%55 = load i8*, i8** %5
store i8 0, i8* %55
%56 = load i8*, i8** %4
store i8* %56, i8** %2
br label %57
57: ; preds = %54, %31, %14
%58 = load i8*, i8** %2
ret i8* %58
}

LLVM IR (-O2):
@PATH_MAX = global i64 0
@ENOMEM = global i32 0
@errno = global i32 0
@ENAMETOOLONG = global i32 0
define i8* @path_parts(i8** nocapture %0) {
%2 = load i64, i64* @PATH_MAX
%3 = add i64 %2, 1
%4 = tail call i8* @malloc(i64 %3)
%5 = icmp eq i8* %4, null
br i1 %5, label %9, label %6
6: ; preds = %1
%7 = load i8*, i8** %0
%8 = icmp eq i8* %7, null
br i1 %8, label %35, label %11
9: ; preds = %1
%10 = load i32, i32* @ENOMEM
store i32 %10, i32* @errno
br label %37
11: ; preds = %6, %27
%12 = phi i8* [ %33, %27 ], [ %7, %6 ]
%13 = phi i64 [ 1, %27 ], [ 0, %6 ]
%14 = phi i1 [ false, %27 ], [ true, %6 ]
%15 = phi i8** [ %32, %27 ], [ %0, %6 ]
%16 = phi i8* [ %31, %27 ], [ %4, %6 ]
%17 = tail call i64 @strlen(i8* %12)
%18 = add i64 %17, %13
%19 = icmp ugt i64 %18, %2
br i1 %19, label %20, label %23
20: ; preds = %11
%21 = tail call i32 @free(i8* %4)
%22 = load i32, i32* @ENAMETOOLONG
store i32 %22, i32* @errno
br label %37
23: ; preds = %11
br i1 %14, label %27, label %24
24: ; preds = %23
store i8 47, i8* %16
%25 = getelementptr inbounds i8, i8* %16, i64 1
%26 = load i8*, i8** %15
br label %27
27: ; preds = %23, %24
%28 = phi i8* [ %26, %24 ], [ %12, %23 ]
%29 = phi i8* [ %25, %24 ], [ %16, %23 ]
%30 = tail call i32 @memcpy(i8* %29, i8* %28, i64 %17)
%31 = getelementptr inbounds i8, i8* %29, i64 %17
%32 = getelementptr inbounds i8*, i8** %15, i64 1
%33 = load i8*, i8** %32
%34 = icmp eq i8* %33, null
br i1 %34, label %35, label %11
35: ; preds = %27, %6
%36 = phi i8* [ %4, %6 ], [ %31, %27 ]
store i8 0, i8* %36
br label %37
37: ; preds = %35, %20, %9
%38 = phi i8* [ null, %9 ], [ null, %20 ], [ %4, %35 ]
ret i8* %38
}