#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 */
typedef int uid_t;
struct TYPE_2__ {
  int uid;
  int gid;
  int mode;
};
struct shmid_ds {
  TYPE_1__ shm_perm;
};
typedef int gid_t;

/* Variables and functions */
int /*<<< orphan*/ IPC_SET;
int /*<<< orphan*/ IPC_STAT;
int /*<<< orphan*/ perror(char *);
int shmctl(int, int /*<<< orphan*/, struct shmid_ds *);
int chshm(int shmid, uid_t uid, gid_t gid, int mode) {
  int ret;
  struct shmid_ds buf;
  ret = shmctl(shmid, IPC_STAT, &buf);
  if (ret == -1) {
    perror("error get shared memory info");
    return -1;
  }
  if (uid != -1) {
    buf.shm_perm.uid = uid;
  }
  if (gid != -1) {
    buf.shm_perm.gid = gid;
  }
  if (mode != -1) {
    buf.shm_perm.mode = mode;
  }
  ret = shmctl(shmid, IPC_SET, &buf);
  if (ret == -1) {
    perror("error: unable to change owner");
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<chshm>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %ecx,-0x14(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e9a(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1030 <shmctl@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    116e <chshm+0x4e>
lea    0xea3(%rip),%rdi
call   1040 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11df <chshm+0xbf>
cmpl   $0xffffffff,-0xc(%rbp)
je     117e <chshm+0x5e>
mov    -0xc(%rbp),%eax
mov    %eax,-0x28(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
je     118e <chshm+0x6e>
mov    -0x10(%rbp),%eax
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
je     119e <chshm+0x7e>
mov    -0x14(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e40(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1030 <shmctl@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    11d8 <chshm+0xb8>
lea    0xe56(%rip),%rdi
call   1040 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11df <chshm+0xbf>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<chshm>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %ecx,%r14d
mov    %edx,%ebp
mov    %esi,%ebx
mov    %edi,%r15d
mov    0x2e9d(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
call   1030 <shmctl@plt>
cmp    $0xffffffff,%eax
je     1193 <chshm+0x73>
cmp    $0xffffffff,%ebx
je     1155 <chshm+0x35>
mov    %ebx,0x8(%rsp)
cmp    $0xffffffff,%ebp
je     115e <chshm+0x3e>
mov    %ebp,0xc(%rsp)
cmp    $0xffffffff,%r14d
je     1169 <chshm+0x49>
mov    %r14d,0x10(%rsp)
mov    0x2e78(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %r15d,%edi
call   1030 <shmctl@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0xffffffff,%ecx
je     119c <chshm+0x7c>
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe66(%rip),%rdi
jmp    11a3 <chshm+0x83>
lea    0xe7a(%rip),%rdi
call   1040 <perror@plt>
mov    $0xffffffff,%eax
jmp    1188 <chshm+0x68>

LLVM IR (-O0):
%struct.shmid_ds = type { %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32, i32, i32 }
@IPC_STAT = global i32 0
@.str = [29 x i8] c"error get shared memory info\00"
@IPC_SET = global i32 0
@.str.1 = [30 x i8] c"error: unable to change owner\00"
define i32 @chshm(i32 %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca %struct.shmid_ds
store i32 %0, i32* %6
store i32 %1, i32* %7
store i32 %2, i32* %8
store i32 %3, i32* %9
%12 = load i32, i32* %6
%13 = load i32, i32* @IPC_STAT
%14 = call i32 @shmctl(i32 %12, i32 %13, %struct.shmid_ds* %11)
store i32 %14, i32* %10
%15 = load i32, i32* %10
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %19
17: ; preds = %4
%18 = call i32 @perror(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %5
br label %49
19: ; preds = %4
%20 = load i32, i32* %7
%21 = icmp ne i32 %20, -1
br i1 %21, label %22, label %26
22: ; preds = %19
%23 = load i32, i32* %7
%24 = getelementptr inbounds %struct.shmid_ds, %struct.shmid_ds* %11, i32 0, i32 0
%25 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %24, i32 0, i32 0
store i32 %23, i32* %25
br label %26
26: ; preds = %22, %19
%27 = load i32, i32* %8
%28 = icmp ne i32 %27, -1
br i1 %28, label %29, label %33
29: ; preds = %26
%30 = load i32, i32* %8
%31 = getelementptr inbounds %struct.shmid_ds, %struct.shmid_ds* %11, i32 0, i32 0
%32 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %31, i32 0, i32 1
store i32 %30, i32* %32
br label %33
33: ; preds = %29, %26
%34 = load i32, i32* %9
%35 = icmp ne i32 %34, -1
br i1 %35, label %36, label %40
36: ; preds = %33
%37 = load i32, i32* %9
%38 = getelementptr inbounds %struct.shmid_ds, %struct.shmid_ds* %11, i32 0, i32 0
%39 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %38, i32 0, i32 2
store i32 %37, i32* %39
br label %40
40: ; preds = %36, %33
%41 = load i32, i32* %6
%42 = load i32, i32* @IPC_SET
%43 = call i32 @shmctl(i32 %41, i32 %42, %struct.shmid_ds* %11)
store i32 %43, i32* %10
%44 = load i32, i32* %10
%45 = icmp eq i32 %44, -1
br i1 %45, label %46, label %48
46: ; preds = %40
%47 = call i32 @perror(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %5
br label %49
48: ; preds = %40
store i32 0, i32* %5
br label %49
49: ; preds = %48, %46, %17
%50 = load i32, i32* %5
ret i32 %50
}

LLVM IR (-O2):
%struct.shmid_ds = type { %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32, i32, i32 }
@IPC_STAT = global i32 0
@.str = [29 x i8] c"error get shared memory info\00"
@IPC_SET = global i32 0
@.str.1 = [30 x i8] c"error: unable to change owner\00"
define i32 @chshm(i32 %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca %struct.shmid_ds
%6 = bitcast %struct.shmid_ds* %5 to i8*
%7 = load i32, i32* @IPC_STAT
%8 = call i32 @shmctl(i32 %0, i32 %7, %struct.shmid_ds* %5)
%9 = icmp eq i32 %8, -1
br i1 %9, label %26, label %10
10: ; preds = %4
%11 = icmp eq i32 %1, -1
br i1 %11, label %14, label %12
12: ; preds = %10
%13 = getelementptr inbounds %struct.shmid_ds, %struct.shmid_ds* %5, i64 0, i32 0, i32 0
store i32 %1, i32* %13
br label %14
14: ; preds = %12, %10
%15 = icmp eq i32 %2, -1
br i1 %15, label %18, label %16
16: ; preds = %14
%17 = getelementptr inbounds %struct.shmid_ds, %struct.shmid_ds* %5, i64 0, i32 0, i32 1
store i32 %2, i32* %17
br label %18
18: ; preds = %16, %14
%19 = icmp eq i32 %3, -1
br i1 %19, label %22, label %20
20: ; preds = %18
%21 = getelementptr inbounds %struct.shmid_ds, %struct.shmid_ds* %5, i64 0, i32 0, i32 2
store i32 %3, i32* %21
br label %22
22: ; preds = %20, %18
%23 = load i32, i32* @IPC_SET
%24 = call i32 @shmctl(i32 %0, i32 %23, %struct.shmid_ds* %5)
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %29
26: ; preds = %22, %4
%27 = phi i8* [ getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), %4 ], [ getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), %22 ]
%28 = call i32 @perror(i8* %27)
br label %29
29: ; preds = %26, %22
%30 = phi i32 [ 0, %22 ], [ -1, %26 ]
ret i32 %30
}