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

/* Variables and functions */
scalar_t__ ENOENT;
int /*<<< orphan*/ O_RDWR;
int PATH_MAX;
int /*<<< orphan*/ close(int);
scalar_t__ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int open(char *, int /*<<< orphan*/);
int snprintf(char *, int, char *, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ update_process_idmap(char *, int, char *, int);
int write(int, char *, int);
void update_process_gidmap(int pid, uint8_t is_setgroup, char *map,
                           int map_len) {
  if ((map == NULL) || (map_len <= 0)) {
    return;
  }
  if (is_setgroup == 1) {
    int fd;
    int len;
    char buf[PATH_MAX];
    len = snprintf(buf, sizeof(buf), "/proc/%d/setgroups", pid);
    if (len < 0) {
      fprintf(stderr,
              "nsenter: "
              "failed to get setgroups path for %d"
              ": %m\n",
              pid);
      exit(1);
    }
    fd = open(buf, O_RDWR);
    if (fd == -1) {
      fprintf(stderr,
              "nsenter: "
              "failed to open %s"
              ": %m\n",
              buf);
      exit(1);
    }
    if (write(fd, "allow", 5) != 5) {
      if (errno != ENOENT) {
        fprintf(stderr,
                "nsenter: "
                "failed to write allow to %s"
                ": %m\n",
                buf);
        exit(1);
      }
    }
    close(fd);
  }
  update_process_idmap("/proc/%d/gid_map", pid, map, map_len);
}


x86 assembly (clang, -O0):
<update_process_gidmap>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %rdx,-0x10(%rbp)
mov    %ecx,-0x14(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     119a <update_process_gidmap+0x2a>
cmpl   $0x0,-0x14(%rbp)
jg     119f <update_process_gidmap+0x2f>
jmp    12e1 <update_process_gidmap+0x171>
cmpl   $0x1,-0x8(%rbp)
jne    12cb <update_process_gidmap+0x15b>
mov    0x2e40(%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,-0x38(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x30(%rbp)
mov    %eax,%esi
mov    -0x4(%rbp),%ecx
lea    0xe1f(%rip),%rdx
call   1040 <snprintf@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1217 <update_process_gidmap+0xa7>
mov    0x2dd6(%rip),%rax
mov    (%rax),%edi
mov    -0x4(%rbp),%edx
lea    0xe0d(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x38(%rbp),%rdi
mov    0x2dbe(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    125b <update_process_gidmap+0xeb>
mov    -0x38(%rbp),%rdx
mov    0x2d8f(%rip),%rax
mov    (%rax),%edi
lea    0xdfb(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x18(%rbp),%edi
lea    0xe00(%rip),%rsi
mov    $0x5,%edx
call   1030 <write@plt>
cmp    $0x5,%eax
je     12bc <update_process_gidmap+0x14c>
mov    0x2d49(%rip),%rax
mov    (%rax),%rax
mov    0x2d2f(%rip),%rcx
cmp    (%rcx),%rax
je     12b7 <update_process_gidmap+0x147>
mov    -0x38(%rbp),%rdx
mov    0x2d33(%rip),%rax
mov    (%rax),%edi
lea    0xdc5(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
jmp    12bc <update_process_gidmap+0x14c>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
mov    -0x28(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%esi
mov    -0x10(%rbp),%rdx
mov    -0x14(%rbp),%ecx
lea    0xdb9(%rip),%rdi
call   1060 <update_process_idmap@plt>
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<update_process_gidmap>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %rdx,%rdx
je     1244 <update_process_gidmap+0xd4>
mov    %ecx,%r15d
test   %ecx,%ecx
jle    1244 <update_process_gidmap+0xd4>
mov    %rdx,%rbx
mov    %edi,%r14d
cmp    $0x1,%esi
jne    122f <update_process_gidmap+0xbf>
mov    %rsp,-0x30(%rbp)
mov    0x2e44(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%r12
mov    %rsi,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r12
mov    %r12,%rsp
lea    0xe37(%rip),%rdx
mov    %r12,%rdi
mov    %r14d,%ecx
call   1040 <snprintf@plt>
test   %eax,%eax
js     1253 <update_process_gidmap+0xe3>
mov    0x2e01(%rip),%rax
mov    (%rax),%esi
mov    %r12,%rdi
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     1277 <update_process_gidmap+0x107>
mov    %eax,%r13d
lea    0xe69(%rip),%rsi
mov    %eax,%edi
mov    $0x5,%edx
call   1030 <write@plt>
cmp    $0x5,%eax
je     1223 <update_process_gidmap+0xb3>
mov    0x2db4(%rip),%rax
mov    (%rax),%rax
mov    0x2d9a(%rip),%rcx
cmp    (%rcx),%rax
jne    1289 <update_process_gidmap+0x119>
mov    %r13d,%edi
call   1050 <close@plt>
mov    -0x30(%rbp),%rsp
lea    0xe5f(%rip),%rdi
mov    %r14d,%esi
mov    %rbx,%rdx
mov    %r15d,%ecx
call   1060 <update_process_idmap@plt>
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d76(%rip),%rax
mov    (%rax),%edi
lea    0xdb0(%rip),%rsi
mov    %r14d,%edx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    0x2d52(%rip),%rax
mov    (%rax),%edi
lea    0xdbe(%rip),%rsi
jmp    1299 <update_process_gidmap+0x129>
mov    0x2d40(%rip),%rax
mov    (%rax),%edi
lea    0xdd2(%rip),%rsi
mov    %r12,%rdx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

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

LLVM IR (-O2):
@PATH_MAX = global i32 0
@.str = [19 x i8] c"/proc/%d/setgroups\00"
@stderr = global i32 0
@.str.1 = [50 x i8] c"nsenter: failed to get setgroups path for %d: %m\0A\00"
@O_RDWR = global i32 0
@.str.2 = [32 x i8] c"nsenter: failed to open %s: %m\0A\00"
@.str.3 = [6 x i8] c"allow\00"
@errno = global i64 0
@ENOENT = global i64 0
@.str.4 = [42 x i8] c"nsenter: failed to write allow to %s: %m\0A\00"
@.str.5 = [17 x i8] c"/proc/%d/gid_map\00"
define void @update_process_gidmap(i32 %0, i32 %1, i8* %2, i32 %3) {
%5 = icmp eq i8* %2, null
%6 = icmp slt i32 %3, 1
%7 = or i1 %5, %6
br i1 %7, label %44, label %8
8: ; preds = %4
%9 = icmp eq i32 %1, 1
br i1 %9, label %10, label %42
10: ; preds = %8
%11 = load i32, i32* @PATH_MAX
%12 = zext i32 %11 to i64
%13 = tail call i8* @llvm.stacksave()
%14 = alloca i8, i64 %12
%15 = call i32 @snprintf(i8* %14, i32 %11, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %0)
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %21
17: ; preds = %10
%18 = load i32, i32* @stderr
%19 = call i32 (i32, i8*, ...) @fprintf(i32 %18, i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.1, i64 0, i64 0), i32 %0)
%20 = call i32 @exit(i32 1)
unreachable
21: ; preds = %10
%22 = load i32, i32* @O_RDWR
%23 = call i32 @open(i8* %14, i32 %22)
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %29
25: ; preds = %21
%26 = load i32, i32* @stderr
%27 = call i32 (i32, i8*, ...) @fprintf(i32 %26, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), i8* %14)
%28 = call i32 @exit(i32 1)
unreachable
29: ; preds = %21
%30 = call i32 @write(i32 %23, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i32 5)
%31 = icmp eq i32 %30, 5
br i1 %31, label %40, label %32
32: ; preds = %29
%33 = load i64, i64* @errno
%34 = load i64, i64* @ENOENT
%35 = icmp eq i64 %33, %34
br i1 %35, label %40, label %36
36: ; preds = %32
%37 = load i32, i32* @stderr
%38 = call i32 (i32, i8*, ...) @fprintf(i32 %37, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.4, i64 0, i64 0), i8* %14)
%39 = call i32 @exit(i32 1)
unreachable
40: ; preds = %32, %29
%41 = call i32 @close(i32 %23)
br label %42
42: ; preds = %40, %8
%43 = call i32 @update_process_idmap(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.5, i64 0, i64 0), i32 %0, i8* %2, i32 %3)
br label %44
44: ; preds = %4, %42
ret void
}