#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 dirent {
  char *d_name;
};
typedef int /*<<< orphan*/ netns_name;
typedef int /*<<< orphan*/ etc_netns_path;
typedef int /*<<< orphan*/ etc_name;
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
int MAXPATHLEN;
int /*<<< orphan*/ MS_BIND;
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, char *, char *);
scalar_t__ mount(char *, char *, char *, int /*<<< orphan*/,
                 int /*<<< orphan*/ *);
int /*<<< orphan*/ *opendir(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ snprintf(char *, int, char *, char *, ...);
int /*<<< orphan*/ stderr;
scalar_t__ strcmp(char *, char *);
char *strerror(int /*<<< orphan*/);
void bind_etc(const char *name) {
  char etc_netns_path[MAXPATHLEN];
  char netns_name[MAXPATHLEN];
  char etc_name[MAXPATHLEN];
  struct dirent *entry;
  DIR *dir;
  snprintf(etc_netns_path, sizeof(etc_netns_path), "%s/%s", "/etc/netns", name);
  dir = opendir(etc_netns_path);
  if (!dir)
    return;
  while ((entry = readdir(dir)) != NULL) {
    if (strcmp(entry->d_name, ".") == 0)
      continue;
    if (strcmp(entry->d_name, "..") == 0)
      continue;
    snprintf(netns_name, sizeof(netns_name), "%s/%s", etc_netns_path,
             entry->d_name);
    snprintf(etc_name, sizeof(etc_name), "/etc/%s", entry->d_name);
    if (mount(netns_name, etc_name, "none", MS_BIND, NULL) < 0) {
      fprintf(stderr, "Bind %s -> %s failed: %s\n", netns_name, etc_name,
              strerror(errno));
    }
  }
  closedir(dir);
}


x86 assembly (clang, -O0):
<bind_etc>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2e4d(%rip),%rcx
mov    (%rcx),%eax
mov    %rsp,%rdx
mov    %rdx,-0x10(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rdi
sub    %rdx,%rdi
mov    %rdi,-0x48(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x18(%rbp)
mov    (%rcx),%edx
mov    %rdx,-0x68(%rbp)
mov    %rdx,%r8
add    $0xf,%r8
and    $0xfffffffffffffff0,%r8
mov    %rsp,%rsi
sub    %r8,%rsi
mov    %rsi,-0x60(%rbp)
mov    %rsi,%rsp
mov    %rdx,-0x20(%rbp)
mov    (%rcx),%ecx
mov    %rcx,-0x58(%rbp)
mov    %rcx,%rsi
add    $0xf,%rsi
and    $0xfffffffffffffff0,%rsi
mov    %rsp,%rdx
sub    %rsi,%rdx
mov    %rdx,-0x50(%rbp)
mov    %rdx,%rsp
mov    %rcx,-0x28(%rbp)
mov    %eax,%esi
mov    -0x8(%rbp),%r8
lea    0xdf7(%rip),%rdx
lea    0xdf6(%rip),%rcx
mov    $0x0,%al
call   1050 <snprintf@plt>
mov    -0x48(%rbp),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    123b <bind_etc+0xbb>
movl   $0x1,-0x3c(%rbp)
jmp    1352 <bind_etc+0x1d2>
jmp    1240 <bind_etc+0xc0>
mov    -0x38(%rbp),%rdi
call   1090 <readdir@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
je     1342 <bind_etc+0x1c2>
mov    -0x30(%rbp),%rax
mov    (%rax),%rdi
lea    0xdad(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
jne    1279 <bind_etc+0xf9>
jmp    1240 <bind_etc+0xc0>
mov    -0x30(%rbp),%rax
mov    (%rax),%rdi
lea    0xd8a(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
jne    129b <bind_etc+0x11b>
jmp    1240 <bind_etc+0xc0>
mov    -0x60(%rbp),%rdi
mov    -0x48(%rbp),%rcx
mov    -0x68(%rbp),%rax
mov    %eax,%esi
mov    -0x30(%rbp),%rax
mov    (%rax),%r8
lea    0xd49(%rip),%rdx
mov    $0x0,%al
call   1050 <snprintf@plt>
mov    -0x50(%rbp),%rdi
mov    -0x58(%rbp),%rax
mov    %eax,%esi
mov    -0x30(%rbp),%rax
mov    (%rax),%rcx
lea    0xd3e(%rip),%rdx
mov    $0x0,%al
call   1050 <snprintf@plt>
mov    -0x60(%rbp),%rdi
mov    -0x50(%rbp),%rsi
mov    0x2cfc(%rip),%rax
mov    (%rax),%ecx
lea    0xd27(%rip),%rdx
xor    %eax,%eax
mov    %eax,%r8d
call   1040 <mount@plt>
cmp    $0x0,%rax
jge    133d <bind_etc+0x1bd>
mov    0x2cc0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x6c(%rbp)
mov    0x2cac(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x6c(%rbp),%edi
mov    -0x60(%rbp),%rdx
mov    -0x50(%rbp),%rcx
mov    %rax,%r8
lea    0xce9(%rip),%rsi
call   1080 <fprintf@plt>
jmp    1240 <bind_etc+0xc0>
mov    -0x38(%rbp),%rdi
call   1060 <closedir@plt>
movl   $0x0,-0x3c(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bind_etc>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r8
mov    0x2e48(%rip),%rax
mov    (%rax),%r15d
mov    %rsp,%rbx
mov    %r15,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    %rsp,%r12
sub    %rax,%r12
mov    %r12,%rsp
mov    %rsp,%r13
sub    %rax,%r13
mov    %r13,%rsp
lea    0xe38(%rip),%rdx
lea    0xe37(%rip),%rcx
mov    %rbx,%rdi
mov    %r15d,%esi
xor    %eax,%eax
call   1050 <snprintf@plt>
mov    %rbx,-0x30(%rbp)
mov    %rbx,%rdi
call   1030 <opendir@plt>
test   %rax,%rax
je     12e2 <bind_etc+0x162>
mov    %rax,%rbx
mov    %rax,%rdi
call   1090 <readdir@plt>
test   %rax,%rax
je     12da <bind_etc+0x15a>
mov    %rax,%r14
jmp    1224 <bind_etc+0xa4>
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1090 <readdir@plt>
mov    %rax,%r14
test   %rax,%rax
je     12da <bind_etc+0x15a>
mov    (%r14),%rdi
lea    0xde4(%rip),%rsi
call   1070 <strcmp@plt>
test   %rax,%rax
je     1210 <bind_etc+0x90>
mov    (%r14),%rdi
lea    0xdcf(%rip),%rsi
call   1070 <strcmp@plt>
test   %rax,%rax
je     1210 <bind_etc+0x90>
mov    (%r14),%r8
mov    %r12,%rdi
mov    %r15d,%esi
lea    0xda4(%rip),%rdx
mov    -0x30(%rbp),%rcx
xor    %eax,%eax
call   1050 <snprintf@plt>
mov    (%r14),%rcx
mov    %r13,%rdi
mov    %r15d,%esi
lea    0xd9d(%rip),%rdx
xor    %eax,%eax
call   1050 <snprintf@plt>
mov    0x2d63(%rip),%rax
mov    (%rax),%ecx
mov    %r12,%rdi
mov    %r13,%rsi
lea    0xd88(%rip),%rdx
xor    %r8d,%r8d
call   1040 <mount@plt>
test   %rax,%rax
jns    1210 <bind_etc+0x90>
mov    0x2d24(%rip),%rax
mov    (%rax),%r14d
mov    0x2d12(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    %r14d,%edi
lea    0xd5a(%rip),%rsi
mov    %r12,%rdx
mov    %r13,%rcx
mov    %rax,%r8
call   1080 <fprintf@plt>
jmp    1210 <bind_etc+0x90>
mov    %rbx,%rdi
call   1060 <closedir@plt>
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.dirent = type { i8* }
@MAXPATHLEN = global i32 0
@.str = [6 x i8] c"%s/%s\00"
@.str.1 = [11 x i8] c"/etc/netns\00"
@.str.2 = [2 x i8] c".\00"
@.str.3 = [3 x i8] c"..\00"
@.str.4 = [8 x i8] c"/etc/%s\00"
@.str.5 = [5 x i8] c"none\00"
@MS_BIND = global i32 0
@stderr = global i32 0
@.str.6 = [26 x i8] c"Bind %s -> %s failed: %s\0A\00"
@errno = global i32 0
define void @bind_etc(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i64
%6 = alloca i64
%7 = alloca %struct.dirent*
%8 = alloca i32*
%9 = alloca i32
store i8* %0, i8** %2
%10 = load i32, i32* @MAXPATHLEN
%11 = zext i32 %10 to i64
%12 = call i8* @llvm.stacksave()
store i8* %12, i8** %3
%13 = alloca i8, i64 %11
store i64 %11, i64* %4
%14 = load i32, i32* @MAXPATHLEN
%15 = zext i32 %14 to i64
%16 = alloca i8, i64 %15
store i64 %15, i64* %5
%17 = load i32, i32* @MAXPATHLEN
%18 = zext i32 %17 to i64
%19 = alloca i8, i64 %18
store i64 %18, i64* %6
%20 = trunc i64 %11 to i32
%21 = load i8*, i8** %2
%22 = call i32 (i8*, i32, i8*, i8*, ...) @snprintf(i8* %13, i32 %20, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i8* %21)
%23 = call i32* @opendir(i8* %13)
store i32* %23, i32** %8
%24 = load i32*, i32** %8
%25 = icmp ne i32* %24, null
br i1 %25, label %27, label %26
26: ; preds = %1
store i32 1, i32* %9
br label %69
27: ; preds = %1
br label %28
28: ; preds = %65, %45, %38, %27
%29 = load i32*, i32** %8
%30 = call %struct.dirent* @readdir(i32* %29)
store %struct.dirent* %30, %struct.dirent** %7
%31 = icmp ne %struct.dirent* %30, null
br i1 %31, label %32, label %66
32: ; preds = %28
%33 = load %struct.dirent*, %struct.dirent** %7
%34 = getelementptr inbounds %struct.dirent, %struct.dirent* %33, i32 0, i32 0
%35 = load i8*, i8** %34
%36 = call i64 @strcmp(i8* %35, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%37 = icmp eq i64 %36, 0
br i1 %37, label %38, label %39
38: ; preds = %32
br label %28
39: ; preds = %32
%40 = load %struct.dirent*, %struct.dirent** %7
%41 = getelementptr inbounds %struct.dirent, %struct.dirent* %40, i32 0, i32 0
%42 = load i8*, i8** %41
%43 = call i64 @strcmp(i8* %42, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%44 = icmp eq i64 %43, 0
br i1 %44, label %45, label %46
45: ; preds = %39
br label %28
46: ; preds = %39
%47 = trunc i64 %15 to i32
%48 = load %struct.dirent*, %struct.dirent** %7
%49 = getelementptr inbounds %struct.dirent, %struct.dirent* %48, i32 0, i32 0
%50 = load i8*, i8** %49
%51 = call i32 (i8*, i32, i8*, i8*, ...) @snprintf(i8* %16, i32 %47, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %13, i8* %50)
%52 = trunc i64 %18 to i32
%53 = load %struct.dirent*, %struct.dirent** %7
%54 = getelementptr inbounds %struct.dirent, %struct.dirent* %53, i32 0, i32 0
%55 = load i8*, i8** %54
%56 = call i32 (i8*, i32, i8*, i8*, ...) @snprintf(i8* %19, i32 %52, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), i8* %55)
%57 = load i32, i32* @MS_BIND
%58 = call i64 @mount(i8* %16, i8* %19, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0), i32 %57, i32* null)
%59 = icmp slt i64 %58, 0
br i1 %59, label %60, label %65
60: ; preds = %46
%61 = load i32, i32* @stderr
%62 = load i32, i32* @errno
%63 = call i8* @strerror(i32 %62)
%64 = call i32 @fprintf(i32 %61, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.6, i64 0, i64 0), i8* %16, i8* %19, i8* %63)
br label %65
65: ; preds = %60, %46
br label %28
66: ; preds = %28
%67 = load i32*, i32** %8
%68 = call i32 @closedir(i32* %67)
store i32 0, i32* %9
br label %69
69: ; preds = %66, %26
%70 = load i8*, i8** %3
%71 = load i32, i32* %9
switch i32 %71, label %73 [
i32 0, label %72
i32 1, label %72
]
72: ; preds = %69, %69
ret void
73: ; preds = %69
unreachable
}

LLVM IR (-O2):
%struct.dirent = type { i8* }
@MAXPATHLEN = global i32 0
@.str = [6 x i8] c"%s/%s\00"
@.str.1 = [11 x i8] c"/etc/netns\00"
@.str.2 = [2 x i8] c".\00"
@.str.3 = [3 x i8] c"..\00"
@.str.4 = [8 x i8] c"/etc/%s\00"
@.str.5 = [5 x i8] c"none\00"
@MS_BIND = global i32 0
@stderr = global i32 0
@.str.6 = [26 x i8] c"Bind %s -> %s failed: %s\0A\00"
@errno = global i32 0
define void @bind_etc(i8* %0) {
%2 = load i32, i32* @MAXPATHLEN
%3 = zext i32 %2 to i64
%4 = alloca i8, i64 %3
%5 = alloca i8, i64 %3
%6 = alloca i8, i64 %3
%7 = call i32 (i8*, i32, i8*, i8*, ...) @snprintf(i8* %4, i32 %2, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%8 = call i32* @opendir(i8* %4)
%9 = icmp eq i32* %8, null
br i1 %9, label %41, label %10
10: ; preds = %1
%11 = call %struct.dirent* @readdir(i32* %8)
%12 = icmp eq %struct.dirent* %11, null
br i1 %12, label %39, label %13
13: ; preds = %10, %36
%14 = phi %struct.dirent* [ %37, %36 ], [ %11, %10 ]
%15 = getelementptr inbounds %struct.dirent, %struct.dirent* %14, i64 0, i32 0
%16 = load i8*, i8** %15
%17 = call i64 @strcmp(i8* %16, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%18 = icmp eq i64 %17, 0
br i1 %18, label %36, label %19
19: ; preds = %13
%20 = load i8*, i8** %15
%21 = call i64 @strcmp(i8* %20, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%22 = icmp eq i64 %21, 0
br i1 %22, label %36, label %23
23: ; preds = %19
%24 = load i8*, i8** %15
%25 = call i32 (i8*, i32, i8*, i8*, ...) @snprintf(i8* %5, i32 %2, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8* %4, i8* %24)
%26 = load i8*, i8** %15
%27 = call i32 (i8*, i32, i8*, i8*, ...) @snprintf(i8* %6, i32 %2, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), i8* %26)
%28 = load i32, i32* @MS_BIND
%29 = call i64 @mount(i8* %5, i8* %6, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0), i32 %28, i32* null)
%30 = icmp slt i64 %29, 0
br i1 %30, label %31, label %36
31: ; preds = %23
%32 = load i32, i32* @stderr
%33 = load i32, i32* @errno
%34 = call i8* @strerror(i32 %33)
%35 = call i32 @fprintf(i32 %32, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.6, i64 0, i64 0), i8* %5, i8* %6, i8* %34)
br label %36
36: ; preds = %23, %31, %13, %19
%37 = call %struct.dirent* @readdir(i32* %8)
%38 = icmp eq %struct.dirent* %37, null
br i1 %38, label %39, label %13
39: ; preds = %36, %10
%40 = call i32 @closedir(i32* %8)
br label %41
41: ; preds = %1, %39
ret void
}