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

/* Variables and functions */
int /*<<< orphan*/ O_WRONLY;
int /*<<< orphan*/ assert_perror(int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int write(int, char *, int);
void disallow_setgroups(pid_t pid) {
  char file[100];
  snprintf(file, sizeof file, "/proc/%d/setgroups", pid);
  int fd = open(file, O_WRONLY);
  if (fd < 0)
    assert_perror(errno);
  int err = write(fd, "deny", 5);
  if (err < 0)
    assert_perror(errno);
  close(fd);
}


x86 assembly (clang, -O0):
<disallow_setgroups>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %edi,-0x4(%rbp)
lea    -0x70(%rbp),%rdi
mov    -0x4(%rbp),%ecx
mov    $0x64,%esi
lea    0xe8f(%rip),%rdx
call   1050 <snprintf@plt>
lea    -0x70(%rbp),%rdi
mov    0x2e4f(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x74(%rbp)
cmpl   $0x0,-0x74(%rbp)
jge    11a3 <disallow_setgroups+0x53>
mov    0x2e44(%rip),%rax
mov    (%rax),%edi
call   1040 <assert_perror@plt>
mov    -0x74(%rbp),%edi
lea    0xe66(%rip),%rsi
mov    $0x5,%edx
call   1030 <write@plt>
mov    %eax,-0x78(%rbp)
cmpl   $0x0,-0x78(%rbp)
jge    11d2 <disallow_setgroups+0x82>
mov    0x2e15(%rip),%rax
mov    (%rax),%edi
call   1040 <assert_perror@plt>
mov    -0x74(%rbp),%edi
call   1060 <close@plt>
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<disallow_setgroups>:
push   %rbx
sub    $0x70,%rsp
mov    %edi,%ecx
lea    0xea2(%rip),%rdx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x64,%esi
call   1050 <snprintf@plt>
mov    0x2e5b(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1070 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    1193 <disallow_setgroups+0x43>
mov    0x2e54(%rip),%rax
mov    (%rax),%edi
call   1040 <assert_perror@plt>
lea    0xe79(%rip),%rsi
mov    %ebx,%edi
mov    $0x5,%edx
call   1030 <write@plt>
test   %eax,%eax
jns    11b8 <disallow_setgroups+0x68>
mov    0x2e2f(%rip),%rax
mov    (%rax),%edi
call   1040 <assert_perror@plt>
mov    %ebx,%edi
call   1060 <close@plt>
add    $0x70,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [19 x i8] c"/proc/%d/setgroups\00"
@O_WRONLY = global i32 0
@errno = global i32 0
@.str.1 = [5 x i8] c"deny\00"
define void @disallow_setgroups(i32 %0) {
%2 = alloca i32
%3 = alloca [100 x i8]
%4 = alloca i32
%5 = alloca i32
store i32 %0, i32* %2
%6 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%7 = load i32, i32* %2
%8 = call i32 @snprintf(i8* %6, i32 100, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %7)
%9 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%10 = load i32, i32* @O_WRONLY
%11 = call i32 @open(i8* %9, i32 %10)
store i32 %11, i32* %4
%12 = load i32, i32* %4
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %1
%15 = load i32, i32* @errno
%16 = call i32 @assert_perror(i32 %15)
br label %17
17: ; preds = %14, %1
%18 = load i32, i32* %4
%19 = call i32 @write(i32 %18, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 5)
store i32 %19, i32* %5
%20 = load i32, i32* %5
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %17
%23 = load i32, i32* @errno
%24 = call i32 @assert_perror(i32 %23)
br label %25
25: ; preds = %22, %17
%26 = load i32, i32* %4
%27 = call i32 @close(i32 %26)
ret void
}

LLVM IR (-O2):
@.str = [19 x i8] c"/proc/%d/setgroups\00"
@O_WRONLY = global i32 0
@errno = global i32 0
@.str.1 = [5 x i8] c"deny\00"
define void @disallow_setgroups(i32 %0) {
%2 = alloca [100 x i8]
%3 = getelementptr inbounds [100 x i8], [100 x i8]* %2, i64 0, i64 0
%4 = call i32 @snprintf(i8* %3, i32 100, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %0)
%5 = load i32, i32* @O_WRONLY
%6 = call i32 @open(i8* %3, i32 %5)
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %11
8: ; preds = %1
%9 = load i32, i32* @errno
%10 = call i32 @assert_perror(i32 %9)
br label %11
11: ; preds = %8, %1
%12 = call i32 @write(i32 %6, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 5)
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %11
%15 = load i32, i32* @errno
%16 = call i32 @assert_perror(i32 %15)
br label %17
17: ; preds = %14, %11
%18 = call i32 @close(i32 %6)
ret void
}