#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 */
scalar_t__ ENOENT;
int /*<<< orphan*/ O_RDWR;
int PATH_MAX;
int /*<<< orphan*/ close(int);
scalar_t__ errno;
int /*<<< orphan*/ error(int, scalar_t__, char *, char *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
size_t strlen(char *);
size_t write(int, char *, size_t);
void update_setgroups(pid_t kid, char *str) {
  char path[PATH_MAX];
  int fd;
  snprintf(path, sizeof path, "/proc/%d/setgroups", kid);
  fd = open(path, O_RDWR);
  if (fd < 0) {
    if (errno == ENOENT)
      return;
    error(1, errno, "can't open %s", path);
  }
  size_t n = strlen(str);
  if (n != write(fd, str, n))
    error(1, errno, "i/o error while writing %s", path);
  close(fd);
}


x86 assembly (clang, -O0):
<update_setgroups>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e7a(%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,-0x38(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x20(%rbp)
mov    %eax,%esi
mov    -0x4(%rbp),%ecx
lea    0xe59(%rip),%rdx
call   1050 <snprintf@plt>
mov    -0x38(%rbp),%rdi
mov    0x2e29(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    1210 <update_setgroups+0xb0>
mov    0x2dfe(%rip),%rax
mov    (%rax),%rax
mov    0x2de4(%rip),%rcx
cmp    (%rcx),%rax
jne    11f1 <update_setgroups+0x91>
movl   $0x1,-0x28(%rbp)
jmp    1273 <update_setgroups+0x113>
mov    -0x38(%rbp),%rcx
mov    0x2dd4(%rip),%rax
mov    (%rax),%rsi
mov    $0x1,%edi
lea    0xe08(%rip),%rdx
call   1070 <error@plt>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x24(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x30(%rbp),%rdx
call   1030 <write@plt>
mov    %rax,%rcx
mov    -0x40(%rbp),%rax
cmp    %rcx,%rax
je     1264 <update_setgroups+0x104>
mov    -0x38(%rbp),%rcx
mov    0x2d80(%rip),%rax
mov    (%rax),%rsi
mov    $0x1,%edi
lea    0xdc2(%rip),%rdx
call   1070 <error@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):
<update_setgroups>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%r14
mov    %edi,%ecx
mov    0x2e79(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%r12
mov    %rsi,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r12
mov    %r12,%rsp
lea    0xe6c(%rip),%rdx
mov    %r12,%rdi
call   1050 <snprintf@plt>
mov    0x2e3d(%rip),%rax
mov    (%rax),%esi
mov    %r12,%rdi
call   1080 <open@plt>
mov    %eax,%r15d
test   %eax,%eax
jns    11de <update_setgroups+0x7e>
mov    0x2e15(%rip),%rax
mov    (%rax),%rsi
mov    0x2dfb(%rip),%rax
cmp    (%rax),%rsi
je     1222 <update_setgroups+0xc2>
lea    0xe42(%rip),%rdx
mov    $0x1,%edi
mov    %r12,%rcx
call   1070 <error@plt>
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %rax,%rbx
mov    %r15d,%edi
mov    %r14,%rsi
mov    %rax,%rdx
call   1030 <write@plt>
cmp    %rax,%rbx
je     121a <update_setgroups+0xba>
mov    0x2dcd(%rip),%rax
mov    (%rax),%rsi
lea    0xe14(%rip),%rdx
mov    $0x1,%edi
mov    %r12,%rcx
call   1070 <error@plt>
mov    %r15d,%edi
call   1060 <close@plt>
lea    -0x20(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@PATH_MAX = global i32 0
@.str = [19 x i8] c"/proc/%d/setgroups\00"
@O_RDWR = global i32 0
@errno = global i64 0
@ENOENT = global i64 0
@.str.1 = [14 x i8] c"can't open %s\00"
@.str.2 = [27 x i8] c"i/o error while writing %s\00"
define void @update_setgroups(i32 %0, i8* nocapture %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 ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %0)
%7 = load i32, i32* @O_RDWR
%8 = call i32 @open(i8* %5, i32 %7)
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %16
10: ; preds = %2
%11 = load i64, i64* @errno
%12 = load i64, i64* @ENOENT
%13 = icmp eq i64 %11, %12
br i1 %13, label %25, label %14
14: ; preds = %10
%15 = call i32 @error(i32 1, i64 %11, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i8* %5)
br label %16
16: ; preds = %14, %2
%17 = call i64 @strlen(i8* dereferenceable(1) %1)
%18 = call i64 @write(i32 %8, i8* %1, i64 %17)
%19 = icmp eq i64 %17, %18
br i1 %19, label %23, label %20
20: ; preds = %16
%21 = load i64, i64* @errno
%22 = call i32 @error(i32 1, i64 %21, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i8* %5)
br label %23
23: ; preds = %20, %16
%24 = call i32 @close(i32 %8)
br label %25
25: ; preds = %10, %23
ret void
}