#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 */
struct stat {
  int st_mode;
};
typedef int /*<<< orphan*/ path_buf2;
typedef int /*<<< orphan*/ path_buf;

/* Variables and functions */
int MS_BIND;
int MS_REC;
int S_IFMT;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, char *, ...);
int /*<<< orphan*/ mkdir(char *, int);
scalar_t__ mount(char *, char *, char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ snprintf(char *, int, char *, char const *, ...);
scalar_t__ stat(char *, struct stat *);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
void add_path(const char *name, const char *rootdir) {
  char path_buf[4096];
  snprintf(path_buf, sizeof(path_buf), "/%s", name);
  struct stat statbuf;
  if (stat(path_buf, &statbuf) < 0) {
    fprintf(stderr, "Cannot stat %s: %s\n", path_buf, strerror(errno));
    return;
  }
  char path_buf2[4096];
  snprintf(path_buf2, sizeof(path_buf2), "%s/%s", rootdir, name);
  mkdir(path_buf2, statbuf.st_mode & ~S_IFMT);
  if (mount(path_buf, path_buf2, "none", MS_BIND | MS_REC, NULL) < 0) {
    fprintf(stderr, "Cannot bind mount %s to %s: %s\n", path_buf, path_buf2,
            strerror(errno));
  }
}


x86 assembly (clang, -O0):
<add_path>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2050,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x1010(%rbp),%rdi
mov    -0x8(%rbp),%rcx
mov    $0x1000,%esi
lea    0xe8c(%rip),%rdx
mov    $0x0,%al
call   1050 <snprintf@plt>
lea    -0x1010(%rbp),%rdi
lea    -0x1018(%rbp),%rsi
call   1070 <stat@plt>
cmp    $0x0,%rax
jge    11fc <add_path+0x9c>
mov    0x2e13(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x202c(%rbp)
lea    -0x1010(%rbp),%rax
mov    %rax,-0x2028(%rbp)
mov    0x2dee(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x202c(%rbp),%edi
mov    -0x2028(%rbp),%rdx
mov    %rax,%rcx
lea    0xe10(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
jmp    12d7 <add_path+0x177>
lea    -0x2020(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    -0x8(%rbp),%r8
mov    $0x1000,%esi
lea    0xdfd(%rip),%rdx
mov    $0x0,%al
call   1050 <snprintf@plt>
lea    -0x2020(%rbp),%rdi
mov    -0x1018(%rbp),%esi
mov    0x2d9e(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    %eax,%esi
call   1030 <mkdir@plt>
lea    -0x1010(%rbp),%rdi
lea    -0x2020(%rbp),%rsi
mov    0x2d95(%rip),%rax
mov    (%rax),%ecx
mov    0x2d84(%rip),%rax
or     (%rax),%ecx
lea    0xdb5(%rip),%rdx
xor    %eax,%eax
mov    %eax,%r8d
call   1040 <mount@plt>
cmp    $0x0,%rax
jge    12d7 <add_path+0x177>
mov    0x2d48(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2044(%rbp)
lea    -0x1010(%rbp),%rax
mov    %rax,-0x2040(%rbp)
lea    -0x2020(%rbp),%rax
mov    %rax,-0x2038(%rbp)
mov    0x2d15(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x2044(%rbp),%edi
mov    -0x2040(%rbp),%rdx
mov    -0x2038(%rbp),%rcx
mov    %rax,%r8
lea    0xd4f(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
add    $0x2050,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<add_path>:
push   %r15
push   %r14
push   %rbx
sub    $0x2010,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
lea    0xe9d(%rip),%rdx
lea    0x10(%rsp),%r15
mov    %r15,%rdi
mov    $0x1000,%esi
mov    %rbx,%rcx
xor    %eax,%eax
call   1050 <snprintf@plt>
lea    0x8(%rsp),%rsi
mov    %r15,%rdi
call   1070 <stat@plt>
test   %rax,%rax
js     1248 <add_path+0xe8>
lea    0xe67(%rip),%rdx
lea    0x1010(%rsp),%r15
mov    %r15,%rdi
mov    $0x1000,%esi
mov    %r14,%rcx
mov    %rbx,%r8
xor    %eax,%eax
call   1050 <snprintf@plt>
mov    0x2dff(%rip),%rax
mov    (%rax),%esi
not    %esi
and    0x8(%rsp),%esi
mov    %r15,%rdi
call   1030 <mkdir@plt>
mov    0x2e00(%rip),%rax
mov    0x2df1(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
lea    0xe20(%rip),%rdx
lea    0x10(%rsp),%rdi
mov    %r15,%rsi
xor    %r8d,%r8d
call   1040 <mount@plt>
test   %rax,%rax
jns    1277 <add_path+0x117>
mov    0x2db2(%rip),%rax
mov    (%rax),%ebx
mov    0x2da1(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xdf2(%rip),%rsi
lea    0x10(%rsp),%rdx
lea    0x1010(%rsp),%rcx
mov    %ebx,%edi
mov    %rax,%r8
xor    %eax,%eax
call   1060 <fprintf@plt>
jmp    1277 <add_path+0x117>
mov    0x2d79(%rip),%rax
mov    (%rax),%ebx
mov    0x2d68(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xd9a(%rip),%rsi
lea    0x10(%rsp),%rdx
mov    %ebx,%edi
mov    %rax,%rcx
xor    %eax,%eax
call   1060 <fprintf@plt>
add    $0x2010,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [4 x i8] c"/%s\00"
@stderr = global i32 0
@.str.1 = [20 x i8] c"Cannot stat %s: %s\0A\00"
@errno = global i32 0
@.str.2 = [6 x i8] c"%s/%s\00"
@S_IFMT = global i32 0
@.str.3 = [5 x i8] c"none\00"
@MS_BIND = global i32 0
@MS_REC = global i32 0
@.str.4 = [32 x i8] c"Cannot bind mount %s to %s: %s\0A\00"
define void @add_path(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca [4096 x i8]
%6 = alloca %struct.stat
%7 = alloca [4096 x i8]
store i8* %0, i8** %3
store i8* %1, i8** %4
%8 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
%9 = load i8*, i8** %3
%10 = call i32 (i8*, i32, i8*, i8*, ...) @snprintf(i8* %8, i32 4096, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* %9)
%11 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
%12 = call i64 @stat(i8* %11, %struct.stat* %6)
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %20
14: ; preds = %2
%15 = load i32, i32* @stderr
%16 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
%17 = load i32, i32* @errno
%18 = call i8* @strerror(i32 %17)
%19 = call i32 (i32, i8*, i8*, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8* %16, i8* %18)
br label %46
20: ; preds = %2
%21 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 0
%22 = load i8*, i8** %4
%23 = load i8*, i8** %3
%24 = call i32 (i8*, i32, i8*, i8*, ...) @snprintf(i8* %21, i32 4096, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %22, i8* %23)
%25 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 0
%26 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%27 = load i32, i32* %26
%28 = load i32, i32* @S_IFMT
%29 = xor i32 %28, -1
%30 = and i32 %27, %29
%31 = call i32 @mkdir(i8* %25, i32 %30)
%32 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
%33 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 0
%34 = load i32, i32* @MS_BIND
%35 = load i32, i32* @MS_REC
%36 = or i32 %34, %35
%37 = call i64 @mount(i8* %32, i8* %33, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i32 %36, i32* null)
%38 = icmp slt i64 %37, 0
br i1 %38, label %39, label %46
39: ; preds = %20
%40 = load i32, i32* @stderr
%41 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
%42 = getelementptr inbounds [4096 x i8], [4096 x i8]* %7, i64 0, i64 0
%43 = load i32, i32* @errno
%44 = call i8* @strerror(i32 %43)
%45 = call i32 (i32, i8*, i8*, i8*, ...) @fprintf(i32 %40, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.4, i64 0, i64 0), i8* %41, i8* %42, i8* %44)
br label %46
46: ; preds = %14, %39, %20
ret void
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [4 x i8] c"/%s\00"
@stderr = global i32 0
@.str.1 = [20 x i8] c"Cannot stat %s: %s\0A\00"
@errno = global i32 0
@.str.2 = [6 x i8] c"%s/%s\00"
@S_IFMT = global i32 0
@.str.3 = [5 x i8] c"none\00"
@MS_BIND = global i32 0
@MS_REC = global i32 0
@.str.4 = [32 x i8] c"Cannot bind mount %s to %s: %s\0A\00"
define void @add_path(i8* %0, i8* %1) {
%3 = alloca [4096 x i8]
%4 = alloca %struct.stat
%5 = alloca [4096 x i8]
%6 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%7 = call i32 (i8*, i32, i8*, i8*, ...) @snprintf(i8* %6, i32 4096, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* %0)
%8 = bitcast %struct.stat* %4 to i8*
%9 = call i64 @stat(i8* %6, %struct.stat* %4)
%10 = icmp slt i64 %9, 0
br i1 %10, label %11, label %16
11: ; preds = %2
%12 = load i32, i32* @stderr
%13 = load i32, i32* @errno
%14 = call i8* @strerror(i32 %13)
%15 = call i32 (i32, i8*, i8*, i8*, ...) @fprintf(i32 %12, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8* %6, i8* %14)
br label %36
16: ; preds = %2
%17 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
%18 = call i32 (i8*, i32, i8*, i8*, ...) @snprintf(i8* %17, i32 4096, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %1, i8* %0)
%19 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%20 = load i32, i32* %19
%21 = load i32, i32* @S_IFMT
%22 = xor i32 %21, -1
%23 = and i32 %20, %22
%24 = call i32 @mkdir(i8* %17, i32 %23)
%25 = load i32, i32* @MS_BIND
%26 = load i32, i32* @MS_REC
%27 = or i32 %26, %25
%28 = call i64 @mount(i8* %6, i8* %17, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i32 %27, i32* null)
%29 = icmp slt i64 %28, 0
br i1 %29, label %30, label %35
30: ; preds = %16
%31 = load i32, i32* @stderr
%32 = load i32, i32* @errno
%33 = call i8* @strerror(i32 %32)
%34 = call i32 (i32, i8*, i8*, i8*, ...) @fprintf(i32 %31, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.4, i64 0, i64 0), i8* %6, i8* %17, i8* %33)
br label %35
35: ; preds = %30, %16
br label %36
36: ; preds = %35, %11
ret void
}