#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__ ssize_t;
typedef int /*<<< orphan*/ pid_t;

/* Variables and functions */
int /*<<< orphan*/ O_WRONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ free(char *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
char *string(char *, int /*<<< orphan*/);
scalar_t__ strlen(char *);
scalar_t__ write(int, char *, scalar_t__);
void denysetgroups(pid_t pid) {
  char *path, *text = "deny";
  int fd;
  path = string("/proc/%d/setgroups", pid);
  if ((fd = open(path, O_WRONLY)) < 0) {
    fprintf(stderr, "Failed to disable setgroups() in container");
    return;
  } else if (write(fd, text, strlen(text)) != (ssize_t)strlen(text)) {
    fprintf(stderr, "Failed to disable setgroups() in container");
    return;
  }
  close(fd);
  free(path);
}


x86 assembly (clang, -O0):
<denysetgroups>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
lea    0xe7e(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    -0x4(%rbp),%esi
lea    0xe75(%rip),%rdi
call   1060 <string@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e2c(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jge    11d1 <denysetgroups+0x61>
mov    0x2e22(%rip),%rax
mov    (%rax),%edi
lea    0xe51(%rip),%rsi
call   1080 <fprintf@plt>
jmp    1244 <denysetgroups+0xd4>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x34(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    %rax,%rdx
call   1040 <write@plt>
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,%rcx
mov    -0x28(%rbp),%rax
cmp    %rcx,%rax
je     122e <denysetgroups+0xbe>
mov    0x2dc5(%rip),%rax
mov    (%rax),%edi
lea    0xdf4(%rip),%rsi
call   1080 <fprintf@plt>
jmp    1244 <denysetgroups+0xd4>
jmp    1233 <denysetgroups+0xc3>
mov    -0x1c(%rbp),%edi
call   1070 <close@plt>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<denysetgroups>:
push   %rbp
push   %rbx
push   %rax
mov    %edi,%esi
lea    0xe99(%rip),%rdi
call   1050 <string@plt>
mov    %rax,%rbx
mov    0x2e55(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1080 <open@plt>
test   %eax,%eax
js     11b9 <denysetgroups+0x59>
mov    %eax,%ebp
lea    0xe6e(%rip),%rsi
mov    $0x4,%edx
mov    %eax,%edi
call   1040 <write@plt>
cmp    $0x4,%rax
jne    11b9 <denysetgroups+0x59>
mov    %ebp,%edi
call   1060 <close@plt>
mov    %rbx,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1030 <free@plt>
mov    0x2e20(%rip),%rax
mov    (%rax),%edi
lea    0xe4f(%rip),%rsi
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1070 <fprintf@plt>

LLVM IR (-O0):
@.str = [5 x i8] c"deny\00"
@.str.1 = [19 x i8] c"/proc/%d/setgroups\00"
@O_WRONLY = global i32 0
@stderr = global i32 0
@.str.2 = [43 x i8] c"Failed to disable setgroups() in container\00"
define void @denysetgroups(i32 %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
store i32 %0, i32* %2
store i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8** %4
%6 = load i32, i32* %2
%7 = call i8* @string(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i32 %6)
store i8* %7, i8** %3
%8 = load i8*, i8** %3
%9 = load i32, i32* @O_WRONLY
%10 = call i32 @open(i8* %8, i32 %9)
store i32 %10, i32* %5
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %1
%13 = load i32, i32* @stderr
%14 = call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.2, i64 0, i64 0))
br label %33
15: ; preds = %1
%16 = load i32, i32* %5
%17 = load i8*, i8** %4
%18 = load i8*, i8** %4
%19 = call i64 @strlen(i8* %18)
%20 = call i64 @write(i32 %16, i8* %17, i64 %19)
%21 = load i8*, i8** %4
%22 = call i64 @strlen(i8* %21)
%23 = icmp ne i64 %20, %22
br i1 %23, label %24, label %27
24: ; preds = %15
%25 = load i32, i32* @stderr
%26 = call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.2, i64 0, i64 0))
br label %33
27: ; preds = %15
br label %28
28: ; preds = %27
%29 = load i32, i32* %5
%30 = call i32 @close(i32 %29)
%31 = load i8*, i8** %3
%32 = call i32 @free(i8* %31)
br label %33
33: ; preds = %28, %24, %12
ret void
}

LLVM IR (-O2):
@.str = [5 x i8] c"deny\00"
@.str.1 = [19 x i8] c"/proc/%d/setgroups\00"
@O_WRONLY = global i32 0
@stderr = global i32 0
@.str.2 = [43 x i8] c"Failed to disable setgroups() in container\00"
define void @denysetgroups(i32 %0) {
%2 = tail call i8* @string(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i32 %0)
%3 = load i32, i32* @O_WRONLY
%4 = tail call i32 @open(i8* %2, i32 %3)
%5 = icmp slt i32 %4, 0
br i1 %5, label %6, label %9
6: ; preds = %1
%7 = load i32, i32* @stderr
%8 = tail call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.2, i64 0, i64 0))
br label %18
9: ; preds = %1
%10 = tail call i64 @write(i32 %4, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i64 4)
%11 = icmp eq i64 %10, 4
br i1 %11, label %15, label %12
12: ; preds = %9
%13 = load i32, i32* @stderr
%14 = tail call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.2, i64 0, i64 0))
br label %18
15: ; preds = %9
%16 = tail call i32 @close(i32 %4)
%17 = tail call i32 @free(i8* %2)
br label %18
18: ; preds = %15, %12, %6
ret void
}