#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 */
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int /*<<< orphan*/ O_RDWR;
int PATH_MAX;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, ...);
int open(char *, int /*<<< orphan*/);
int snprintf(char *, int, char *, int);
int /*<<< orphan*/ stderr;
int write(int, char *, int);
void update_process_idmap(char *pathfmt, int pid, char *map, int map_len) {
  char buf[PATH_MAX];
  int len;
  int fd;
  len = snprintf(buf, sizeof(buf), pathfmt, pid);
  if (len < 0) {
    fprintf(stderr,
            "nsenter: "
            "failed to construct '%s' for %d"
            ": %m\n",
            pathfmt, pid);
    exit(1);
  }
  fd = open(buf, O_RDWR);
  if (fd == -1) {
    fprintf(stderr,
            "nsenter: "
            "failed to open %s"
            ": %m\n",
            buf);
    exit(1);
  }
  len = write(fd, map, map_len);
  if (len == -1) {
    fprintf(stderr,
            "nsenter: "
            "failed to write to %s"
            ": %m\n",
            buf);
    exit(1);
  } else if (len != map_len) {
    fprintf(stderr, "Failed to write data to %s (%d/%d)", buf, len, map_len);
    exit(1);
  }
  close(fd);
}


x86 assembly (clang, -O0):
<update_process_idmap>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    0x2e73(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x28(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x40(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x30(%rbp)
mov    %eax,%esi
mov    -0x8(%rbp),%rdx
mov    -0xc(%rbp),%ecx
call   1040 <snprintf@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jge    11e5 <update_process_idmap+0x85>
mov    0x2e0c(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
mov    -0xc(%rbp),%ecx
lea    0xe2c(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x40(%rbp),%rdi
mov    0x2df0(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x38(%rbp)
cmpl   $0xffffffff,-0x38(%rbp)
jne    1229 <update_process_idmap+0xc9>
mov    -0x40(%rbp),%rdx
mov    0x2dc1(%rip),%rax
mov    (%rax),%edi
lea    0xe16(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x38(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0xffffffff,-0x34(%rbp)
jne    126a <update_process_idmap+0x10a>
mov    -0x40(%rbp),%rdx
mov    0x2d80(%rip),%rax
mov    (%rax),%edi
lea    0xdf5(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x34(%rbp),%eax
cmp    -0x1c(%rbp),%eax
je     12a2 <update_process_idmap+0x142>
mov    -0x40(%rbp),%rdx
mov    0x2d4f(%rip),%rax
mov    (%rax),%edi
mov    -0x34(%rbp),%ecx
mov    -0x1c(%rbp),%r8d
lea    0xde1(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
jmp    12a7 <update_process_idmap+0x147>
mov    -0x38(%rbp),%edi
call   1050 <close@plt>
mov    -0x28(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<update_process_idmap>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %ecx,%r14d
mov    %rdx,-0x30(%rbp)
mov    %esi,%r12d
mov    %rdi,%r13
mov    0x2e6e(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%r15
mov    %rsi,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r15
mov    %r15,%rsp
mov    %r15,%rdi
mov    %r13,%rdx
mov    %r12d,%ecx
call   1040 <snprintf@plt>
test   %eax,%eax
js     11f0 <update_process_idmap+0x90>
mov    0x2e2f(%rip),%rax
mov    (%rax),%esi
mov    %r15,%rdi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     1217 <update_process_idmap+0xb7>
mov    %eax,%ebx
mov    %eax,%edi
mov    -0x30(%rbp),%rsi
mov    %r14d,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
je     1229 <update_process_idmap+0xc9>
cmp    %r14d,%eax
jne    124d <update_process_idmap+0xed>
mov    %ebx,%edi
call   1050 <close@plt>
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2dd9(%rip),%rax
mov    (%rax),%edi
lea    0xe00(%rip),%rsi
mov    %r13,%rdx
mov    %r12d,%ecx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    0x2db2(%rip),%rax
mov    (%rax),%edi
lea    0xe07(%rip),%rsi
jmp    1239 <update_process_idmap+0xd9>
mov    0x2da0(%rip),%rax
mov    (%rax),%edi
lea    0xe15(%rip),%rsi
mov    %r15,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    0x2d7c(%rip),%rcx
mov    (%rcx),%edi
lea    0xe15(%rip),%rsi
mov    %r15,%rdx
mov    %eax,%ecx
mov    %r14d,%r8d
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@PATH_MAX = global i32 0
@stderr = global i32 0
@.str = [46 x i8] c"nsenter: failed to construct '%s' for %d: %m\0A\00"
@O_RDWR = global i32 0
@.str.1 = [32 x i8] c"nsenter: failed to open %s: %m\0A\00"
@.str.2 = [36 x i8] c"nsenter: failed to write to %s: %m\0A\00"
@.str.3 = [35 x i8] c"Failed to write data to %s (%d/%d)\00"
define void @update_process_idmap(i8* %0, i32 %1, i8* %2, i32 %3) {
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i64
%11 = alloca i32
%12 = alloca i32
store i8* %0, i8** %5
store i32 %1, i32* %6
store i8* %2, i8** %7
store i32 %3, i32* %8
%13 = load i32, i32* @PATH_MAX
%14 = zext i32 %13 to i64
%15 = call i8* @llvm.stacksave()
store i8* %15, i8** %9
%16 = alloca i8, i64 %14
store i64 %14, i64* %10
%17 = trunc i64 %14 to i32
%18 = load i8*, i8** %5
%19 = load i32, i32* %6
%20 = call i32 @snprintf(i8* %16, i32 %17, i8* %18, i32 %19)
store i32 %20, i32* %11
%21 = load i32, i32* %11
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %29
23: ; preds = %4
%24 = load i32, i32* @stderr
%25 = load i8*, i8** %5
%26 = load i32, i32* %6
%27 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %24, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str, i64 0, i64 0), i8* %25, i32 %26)
%28 = call i32 @exit(i32 1)
unreachable
29: ; preds = %4
%30 = load i32, i32* @O_RDWR
%31 = call i32 @open(i8* %16, i32 %30)
store i32 %31, i32* %12
%32 = load i32, i32* %12
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %38
34: ; preds = %29
%35 = load i32, i32* @stderr
%36 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %35, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i8* %16)
%37 = call i32 @exit(i32 1)
unreachable
38: ; preds = %29
%39 = load i32, i32* %12
%40 = load i8*, i8** %7
%41 = load i32, i32* %8
%42 = call i32 @write(i32 %39, i8* %40, i32 %41)
store i32 %42, i32* %11
%43 = load i32, i32* %11
%44 = icmp eq i32 %43, -1
br i1 %44, label %45, label %49
45: ; preds = %38
%46 = load i32, i32* @stderr
%47 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %46, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0), i8* %16)
%48 = call i32 @exit(i32 1)
unreachable
49: ; preds = %38
%50 = load i32, i32* %11
%51 = load i32, i32* %8
%52 = icmp ne i32 %50, %51
br i1 %52, label %53, label %59
53: ; preds = %49
%54 = load i32, i32* @stderr
%55 = load i32, i32* %11
%56 = load i32, i32* %8
%57 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %54, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.3, i64 0, i64 0), i8* %16, i32 %55, i32 %56)
%58 = call i32 @exit(i32 1)
unreachable
59: ; preds = %49
br label %60
60: ; preds = %59
%61 = load i32, i32* %12
%62 = call i32 @close(i32 %61)
%63 = load i8*, i8** %9
ret void
}

LLVM IR (-O2):
@PATH_MAX = global i32 0
@stderr = global i32 0
@.str = [46 x i8] c"nsenter: failed to construct '%s' for %d: %m\0A\00"
@O_RDWR = global i32 0
@.str.1 = [32 x i8] c"nsenter: failed to open %s: %m\0A\00"
@.str.2 = [36 x i8] c"nsenter: failed to write to %s: %m\0A\00"
@.str.3 = [35 x i8] c"Failed to write data to %s (%d/%d)\00"
define void @update_process_idmap(i8* %0, i32 %1, i8* nocapture %2, i32 %3) {
%5 = load i32, i32* @PATH_MAX
%6 = zext i32 %5 to i64
%7 = alloca i8, i64 %6
%8 = call i32 @snprintf(i8* %7, i32 %5, i8* %0, i32 %1)
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %14
10: ; preds = %4
%11 = load i32, i32* @stderr
%12 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %11, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str, i64 0, i64 0), i8* %0, i32 %1)
%13 = call i32 @exit(i32 1)
unreachable
14: ; preds = %4
%15 = load i32, i32* @O_RDWR
%16 = call i32 @open(i8* %7, i32 %15)
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %22
18: ; preds = %14
%19 = load i32, i32* @stderr
%20 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i8* %7)
%21 = call i32 @exit(i32 1)
unreachable
22: ; preds = %14
%23 = call i32 @write(i32 %16, i8* %2, i32 %3)
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %29
25: ; preds = %22
%26 = load i32, i32* @stderr
%27 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %26, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0), i8* %7)
%28 = call i32 @exit(i32 1)
unreachable
29: ; preds = %22
%30 = icmp eq i32 %23, %3
br i1 %30, label %35, label %31
31: ; preds = %29
%32 = load i32, i32* @stderr
%33 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %32, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.3, i64 0, i64 0), i8* %7, i32 %23, i32 %3)
%34 = call i32 @exit(i32 1)
unreachable
35: ; preds = %29
%36 = call i32 @close(i32 %16)
ret void
}