#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
struct TYPE_2__ {
  int mode;
  int /*<<< orphan*/ gid;
  int /*<<< orphan*/ uid;
};
struct shmid_ds {
  TYPE_1__ shm_perm;
};

/* Variables and functions */
int /*<<< orphan*/ IPC_SET;
int /*<<< orphan*/ err_sys(char *, int);
int /*<<< orphan*/ getpgid(int /*<<< orphan*/);
int /*<<< orphan*/ getpid();
int /*<<< orphan*/ printf(char *);
int shmctl(int, int /*<<< orphan*/, struct shmid_ds *);
void set_mode(int mid, int mode) {
  struct shmid_ds mbuf;
  mbuf.shm_perm.uid = getpid();
  mbuf.shm_perm.gid = getpgid(getpid());
  mbuf.shm_perm.mode = mode;
  int ret = shmctl(mid, IPC_SET, &mbuf);
  if (ret < 0)
    err_sys("shmctl to set mode for %d failed", mid);
  printf("set shm mode ok\n");
}


x86 assembly (clang, -O0):
<set_mode>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,-0x10(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%edi
call   1070 <getpgid@plt>
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2e5f(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1060 <shmctl@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11b0 <set_mode+0x60>
mov    -0x4(%rbp),%esi
lea    0xe55(%rip),%rdi
call   1030 <err_sys@plt>
lea    0xe6a(%rip),%rdi
call   1050 <printf@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_mode>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    %esi,%ebp
mov    %edi,%ebx
xor    %eax,%eax
call   1050 <getpid@plt>
mov    %eax,0x10(%rsp)
xor    %eax,%eax
call   1050 <getpid@plt>
mov    %eax,%edi
call   1070 <getpgid@plt>
mov    %eax,0xc(%rsp)
mov    %ebp,0x8(%rsp)
mov    0x2e66(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebx,%edi
call   1060 <shmctl@plt>
test   %eax,%eax
jns    11a2 <set_mode+0x52>
lea    0xe65(%rip),%rdi
mov    %ebx,%esi
call   1040 <err_sys@plt>
lea    0xe78(%rip),%rdi
call   1030 <puts@plt>
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.shmid_ds = type { %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32, i32, i32 }
@IPC_SET = global i32 0
@.str = [33 x i8] c"shmctl to set mode for %d failed\00"
@.str.1 = [17 x i8] c"set shm mode ok\0A\00"
define void @set_mode(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.shmid_ds
%6 = alloca i32
store i32 %0, i32* %3
store i32 %1, i32* %4
%7 = call i32 (...) @getpid()
%8 = getelementptr inbounds %struct.shmid_ds, %struct.shmid_ds* %5, i32 0, i32 0
%9 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %8, i32 0, i32 2
store i32 %7, i32* %9
%10 = call i32 (...) @getpid()
%11 = call i32 @getpgid(i32 %10)
%12 = getelementptr inbounds %struct.shmid_ds, %struct.shmid_ds* %5, i32 0, i32 0
%13 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %12, i32 0, i32 1
store i32 %11, i32* %13
%14 = load i32, i32* %4
%15 = getelementptr inbounds %struct.shmid_ds, %struct.shmid_ds* %5, i32 0, i32 0
%16 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %15, i32 0, i32 0
store i32 %14, i32* %16
%17 = load i32, i32* %3
%18 = load i32, i32* @IPC_SET
%19 = call i32 @shmctl(i32 %17, i32 %18, %struct.shmid_ds* %5)
store i32 %19, i32* %6
%20 = load i32, i32* %6
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %2
%23 = load i32, i32* %3
%24 = call i32 @err_sys(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i32 %23)
br label %25
25: ; preds = %22, %2
%26 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
ret void
}

LLVM IR (-O2):
%struct.shmid_ds = type { %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32, i32, i32 }
@IPC_SET = global i32 0
@.str = [33 x i8] c"shmctl to set mode for %d failed\00"
@str = [16 x i8] c"set shm mode ok\00"
define void @set_mode(i32 %0, i32 %1) {
%3 = alloca %struct.shmid_ds
%4 = bitcast %struct.shmid_ds* %3 to i8*
%5 = tail call i32 (...) @getpid()
%6 = getelementptr inbounds %struct.shmid_ds, %struct.shmid_ds* %3, i64 0, i32 0, i32 2
store i32 %5, i32* %6
%7 = tail call i32 (...) @getpid()
%8 = tail call i32 @getpgid(i32 %7)
%9 = getelementptr inbounds %struct.shmid_ds, %struct.shmid_ds* %3, i64 0, i32 0, i32 1
store i32 %8, i32* %9
%10 = getelementptr inbounds %struct.shmid_ds, %struct.shmid_ds* %3, i64 0, i32 0, i32 0
store i32 %1, i32* %10
%11 = load i32, i32* @IPC_SET
%12 = call i32 @shmctl(i32 %0, i32 %11, %struct.shmid_ds* %3)
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %2
%15 = call i32 @err_sys(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0), i32 %0)
br label %16
16: ; preds = %14, %2
%17 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str, i64 0, i64 0))
ret void
}