#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 */
size_t PATH_MAX;
int /*<<< orphan*/ free(char *);
char *malloc(size_t);
int snprintf(char *, size_t, char *, char const *, char const *);
int strlen(char const *);
__attribute__((__malloc__)) char *generic_make_pathname(const char *base,
                                                        const char *leaf) {
  size_t size;
  char *path;
  int len;
  size = strlen(base) + strlen(leaf) + 2;
  if (size > PATH_MAX)
    return NULL;
  path = malloc(size);
  if (path == NULL)
    return NULL;
  len = snprintf(path, size, "%s/%s", base, leaf);
  if ((len < 0) || ((size_t)len >= size)) {
    free(path);
    return NULL;
  }
  return path;
}


x86 assembly (clang, -O0):
<generic_make_pathname>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x30(%rbp),%eax
add    %ecx,%eax
add    $0x2,%eax
cltq
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    0x2e70(%rip),%rcx
cmp    (%rcx),%rax
jbe    1196 <generic_make_pathname+0x56>
movq   $0x0,-0x8(%rbp)
jmp    1210 <generic_make_pathname+0xd0>
mov    -0x20(%rbp),%rdi
call   1060 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11bb <generic_make_pathname+0x7b>
movq   $0x0,-0x8(%rbp)
jmp    1210 <generic_make_pathname+0xd0>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x10(%rbp),%rcx
mov    -0x18(%rbp),%r8
lea    0xe2e(%rip),%rdx
call   1050 <snprintf@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jl     11f2 <generic_make_pathname+0xb2>
movslq -0x2c(%rbp),%rax
cmp    -0x20(%rbp),%rax
jb     1208 <generic_make_pathname+0xc8>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    1210 <generic_make_pathname+0xd0>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<generic_make_pathname>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%r14
mov    %rdi,%r15
call   1040 <strlen@plt>
mov    %eax,%ebx
mov    %r14,%rdi
call   1040 <strlen@plt>
lea    (%rbx,%rax,1),%ebp
add    $0x2,%ebp
movslq %ebp,%rbx
mov    0x2e83(%rip),%rax
cmp    %rbx,(%rax)
jb     11aa <generic_make_pathname+0x6a>
mov    %rbx,%rdi
call   1060 <malloc@plt>
test   %rax,%rax
je     11aa <generic_make_pathname+0x6a>
mov    %rax,%r12
lea    0xe77(%rip),%rdx
mov    %rax,%rdi
mov    %rbx,%rsi
mov    %r15,%rcx
mov    %r14,%r8
call   1050 <snprintf@plt>
test   %eax,%eax
js     11a2 <generic_make_pathname+0x62>
cmp    %ebp,%eax
jb     11ad <generic_make_pathname+0x6d>
mov    %r12,%rdi
call   1030 <free@plt>
xor    %r12d,%r12d
mov    %r12,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@PATH_MAX = global i64 0
@.str = [6 x i8] c"%s/%s\00"
define noalias i8* @generic_make_pathname(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
%9 = load i8*, i8** %4
%10 = call i32 @strlen(i8* %9)
%11 = load i8*, i8** %5
%12 = call i32 @strlen(i8* %11)
%13 = add nsw i32 %10, %12
%14 = add nsw i32 %13, 2
%15 = sext i32 %14 to i64
store i64 %15, i64* %6
%16 = load i64, i64* %6
%17 = load i64, i64* @PATH_MAX
%18 = icmp ugt i64 %16, %17
br i1 %18, label %19, label %20
19: ; preds = %2
store i8* null, i8** %3
br label %44
20: ; preds = %2
%21 = load i64, i64* %6
%22 = call i8* @malloc(i64 %21)
store i8* %22, i8** %7
%23 = load i8*, i8** %7
%24 = icmp eq i8* %23, null
br i1 %24, label %25, label %26
25: ; preds = %20
store i8* null, i8** %3
br label %44
26: ; preds = %20
%27 = load i8*, i8** %7
%28 = load i64, i64* %6
%29 = load i8*, i8** %4
%30 = load i8*, i8** %5
%31 = call i32 @snprintf(i8* %27, i64 %28, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %29, i8* %30)
store i32 %31, i32* %8
%32 = load i32, i32* %8
%33 = icmp slt i32 %32, 0
br i1 %33, label %39, label %34
34: ; preds = %26
%35 = load i32, i32* %8
%36 = sext i32 %35 to i64
%37 = load i64, i64* %6
%38 = icmp uge i64 %36, %37
br i1 %38, label %39, label %42
39: ; preds = %34, %26
%40 = load i8*, i8** %7
%41 = call i32 @free(i8* %40)
store i8* null, i8** %3
br label %44
42: ; preds = %34
%43 = load i8*, i8** %7
store i8* %43, i8** %3
br label %44
44: ; preds = %42, %39, %25, %19
%45 = load i8*, i8** %3
ret i8* %45
}

LLVM IR (-O2):
@PATH_MAX = global i64 0
@.str = [6 x i8] c"%s/%s\00"
define noalias i8* @generic_make_pathname(i8* %0, i8* %1) {
%3 = tail call i32 @strlen(i8* %0)
%4 = tail call i32 @strlen(i8* %1)
%5 = add i32 %3, 2
%6 = add i32 %5, %4
%7 = sext i32 %6 to i64
%8 = load i64, i64* @PATH_MAX
%9 = icmp ult i64 %8, %7
br i1 %9, label %20, label %10
10: ; preds = %2
%11 = tail call i8* @malloc(i64 %7)
%12 = icmp eq i8* %11, null
br i1 %12, label %20, label %13
13: ; preds = %10
%14 = tail call i32 @snprintf(i8* %11, i64 %7, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %1)
%15 = icmp sgt i32 %14, -1
%16 = icmp ult i32 %14, %6
%17 = select i1 %15, i1 %16, i1 false
br i1 %17, label %20, label %18
18: ; preds = %13
%19 = tail call i32 @free(i8* %11)
br label %20
20: ; preds = %13, %10, %2, %18
%21 = phi i8* [ null, %18 ], [ null, %2 ], [ null, %10 ], [ %11, %13 ]
ret i8* %21
}