#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 scalar_t__ pid_t;

/* Variables and functions */
scalar_t__ ENOENT;
int /*<<< orphan*/ O_RDWR;
int PATH_MAX;
int /*<<< orphan*/ close(int);
scalar_t__ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, char *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ snprintf(char *, int, char *, long);
int /*<<< orphan*/ stderr;
char *strerror(scalar_t__);
int /*<<< orphan*/ strlen(char *);
int write(int, char *, int /*<<< orphan*/);
void proc_setgroups_write(pid_t child_pid, char *str) {
  char setgroups_path[PATH_MAX];
  int fd;
  snprintf(setgroups_path, PATH_MAX, "/proc/%ld/setgroups", (long)child_pid);
  fd = open(setgroups_path, O_RDWR);
  if (fd == -1) {
    if (errno != ENOENT)
      fprintf(stderr,
              "nsenter: "
              "failed to open %s: %s\n"
              ": %m\n",
              setgroups_path, strerror(errno));
    return;
  }
  if (write(fd, str, strlen(str)) == -1)
    fprintf(stderr,
            "nsenter: "
            "failed to write %s: %s\n"
            ": %m\n",
            setgroups_path, strerror(errno));
  close(fd);
}


x86 assembly (clang, -O0):
<proc_setgroups_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e69(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x18(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x30(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x20(%rbp)
mov    0x2e3d(%rip),%rax
mov    (%rax),%esi
mov    -0x8(%rbp),%rcx
lea    0xe40(%rip),%rdx
call   1050 <snprintf@plt>
mov    -0x30(%rbp),%rdi
mov    0x2e10(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    123b <proc_setgroups_write+0xcb>
mov    0x2ddd(%rip),%rax
mov    (%rax),%rax
mov    0x2dc3(%rip),%rcx
cmp    (%rcx),%rax
je     122f <proc_setgroups_write+0xbf>
mov    0x2dcb(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    0x2db7(%rip),%rax
mov    (%rax),%rdi
call   1090 <strerror@plt>
mov    -0x34(%rbp),%edi
mov    -0x30(%rbp),%rdx
mov    %rax,%rcx
lea    0xdea(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0x1,-0x28(%rbp)
jmp    12a9 <proc_setgroups_write+0x139>
mov    -0x24(%rbp),%eax
mov    %eax,-0x44(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x44(%rbp),%edi
mov    -0x40(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    129a <proc_setgroups_write+0x12a>
mov    0x2d60(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x48(%rbp)
mov    0x2d4c(%rip),%rax
mov    (%rax),%rdi
call   1090 <strerror@plt>
mov    -0x48(%rbp),%edi
mov    -0x30(%rbp),%rdx
mov    %rax,%rcx
lea    0xda4(%rip),%rsi
call   1070 <fprintf@plt>
mov    -0x24(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x28(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<proc_setgroups_write>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rcx
mov    0x2e69(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rbx
mov    %rsi,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
lea    0xe5c(%rip),%rdx
mov    %rbx,%rdi
call   1050 <snprintf@plt>
mov    0x2e2d(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     1217 <proc_setgroups_write+0xa7>
mov    %eax,%r15d
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %r15d,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    120d <proc_setgroups_write+0x9d>
mov    0x2dea(%rip),%rax
mov    (%rax),%r14d
mov    0x2dd8(%rip),%rax
mov    (%rax),%rdi
call   1090 <strerror@plt>
lea    0xe3a(%rip),%rsi
mov    %r14d,%edi
mov    %rbx,%rdx
mov    %rax,%rcx
call   1070 <fprintf@plt>
mov    %r15d,%edi
call   1060 <close@plt>
jmp    1251 <proc_setgroups_write+0xe1>
mov    0x2daa(%rip),%rax
mov    (%rax),%rdi
mov    0x2d90(%rip),%rax
cmp    (%rax),%rdi
je     1251 <proc_setgroups_write+0xe1>
mov    0x2d9c(%rip),%rax
mov    (%rax),%r14d
call   1090 <strerror@plt>
lea    0xdd1(%rip),%rsi
mov    %r14d,%edi
mov    %rbx,%rdx
mov    %rax,%rcx
call   1070 <fprintf@plt>
lea    -0x18(%rbp),%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@PATH_MAX = global i32 0
@.str = [20 x i8] c"/proc/%ld/setgroups\00"
@O_RDWR = global i32 0
@errno = global i64 0
@ENOENT = global i64 0
@stderr = global i32 0
@.str.1 = [37 x i8] c"nsenter: failed to open %s: %s\0A: %m\0A\00"
@.str.2 = [38 x i8] c"nsenter: failed to write %s: %s\0A: %m\0A\00"
define void @proc_setgroups_write(i64 %0, i8* %1) {
%3 = alloca i64
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i32
%8 = alloca i32
store i64 %0, i64* %3
store i8* %1, i8** %4
%9 = load i32, i32* @PATH_MAX
%10 = zext i32 %9 to i64
%11 = call i8* @llvm.stacksave()
store i8* %11, i8** %5
%12 = alloca i8, i64 %10
store i64 %10, i64* %6
%13 = load i32, i32* @PATH_MAX
%14 = load i64, i64* %3
%15 = call i32 @snprintf(i8* %12, i32 %13, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i64 %14)
%16 = load i32, i32* @O_RDWR
%17 = call i32 @open(i8* %12, i32 %16)
store i32 %17, i32* %7
%18 = load i32, i32* %7
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %30
20: ; preds = %2
%21 = load i64, i64* @errno
%22 = load i64, i64* @ENOENT
%23 = icmp ne i64 %21, %22
br i1 %23, label %24, label %29
24: ; preds = %20
%25 = load i32, i32* @stderr
%26 = load i64, i64* @errno
%27 = call i8* @strerror(i64 %26)
%28 = call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i8* %12, i8* %27)
br label %29
29: ; preds = %24, %20
store i32 1, i32* %8
br label %45
30: ; preds = %2
%31 = load i32, i32* %7
%32 = load i8*, i8** %4
%33 = load i8*, i8** %4
%34 = call i32 @strlen(i8* %33)
%35 = call i32 @write(i32 %31, i8* %32, i32 %34)
%36 = icmp eq i32 %35, -1
br i1 %36, label %37, label %42
37: ; preds = %30
%38 = load i32, i32* @stderr
%39 = load i64, i64* @errno
%40 = call i8* @strerror(i64 %39)
%41 = call i32 @fprintf(i32 %38, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0), i8* %12, i8* %40)
br label %42
42: ; preds = %37, %30
%43 = load i32, i32* %7
%44 = call i32 @close(i32 %43)
store i32 0, i32* %8
br label %45
45: ; preds = %42, %29
%46 = load i8*, i8** %5
%47 = load i32, i32* %8
switch i32 %47, label %49 [
i32 0, label %48
i32 1, label %48
]
48: ; preds = %45, %45
ret void
49: ; preds = %45
unreachable
}

LLVM IR (-O2):
@PATH_MAX = global i32 0
@.str = [20 x i8] c"/proc/%ld/setgroups\00"
@O_RDWR = global i32 0
@errno = global i64 0
@ENOENT = global i64 0
@stderr = global i32 0
@.str.1 = [37 x i8] c"nsenter: failed to open %s: %s\0A: %m\0A\00"
@.str.2 = [38 x i8] c"nsenter: failed to write %s: %s\0A: %m\0A\00"
define void @proc_setgroups_write(i64 %0, i8* %1) {
%3 = load i32, i32* @PATH_MAX
%4 = zext i32 %3 to i64
%5 = alloca i8, i64 %4
%6 = call i32 @snprintf(i8* %5, i32 %3, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i64 %0)
%7 = load i32, i32* @O_RDWR
%8 = call i32 @open(i8* %5, i32 %7)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %18
10: ; preds = %2
%11 = load i64, i64* @errno
%12 = load i64, i64* @ENOENT
%13 = icmp eq i64 %11, %12
br i1 %13, label %29, label %14
14: ; preds = %10
%15 = load i32, i32* @stderr
%16 = call i8* @strerror(i64 %11)
%17 = call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i8* %5, i8* %16)
br label %29
18: ; preds = %2
%19 = call i32 @strlen(i8* %1)
%20 = call i32 @write(i32 %8, i8* %1, i32 %19)
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %27
22: ; preds = %18
%23 = load i32, i32* @stderr
%24 = load i64, i64* @errno
%25 = call i8* @strerror(i64 %24)
%26 = call i32 @fprintf(i32 %23, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0), i8* %5, i8* %25)
br label %27
27: ; preds = %22, %18
%28 = call i32 @close(i32 %8)
br label %29
29: ; preds = %10, %14, %27
ret void
}