#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 */
struct ipc_perm {
  scalar_t__ uid;
};
struct shmid_ds {
  struct ipc_perm shm_perm;
};
struct shm_info {
  int dummy;
};
struct passwd {
  scalar_t__ pw_uid;
};

/* Variables and functions */
int /*<<< orphan*/ SHM_INFO;
int /*<<< orphan*/ SHM_STAT;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
struct passwd *getpwnam(char *);
int /*<<< orphan*/ lock_cmd(int, int);
int shmctl(int, int /*<<< orphan*/, struct shmid_ds *);
int /*<<< orphan*/ stderr;
void lock_user_shm(char *user_name, int cmd) {
  struct shm_info si;
  struct shmid_ds sds;
  struct ipc_perm *ipcp = &sds.shm_perm;
  struct passwd *pw;
  int num = 0, shmid, maxid, id;
  pw = getpwnam(user_name);
  if (!pw) {
    fprintf(stderr, "Invalid user: '%s'\n", user_name);
    exit(5);
  }
  maxid = shmctl(0, SHM_INFO, (struct shmid_ds *)(&si));
  for (id = 0; id <= maxid; id++) {
    shmid = shmctl(id, SHM_STAT, &sds);
    if (shmid < 0 || pw->pw_uid != ipcp->uid) {
      continue;
    }
    lock_cmd(shmid, cmd);
    num++;
  }
  if (!num) {
    fprintf(stderr, "No shared memory segments under the user %s\n", user_name);
    exit(6);
  }
}


x86 assembly (clang, -O0):
<lock_user_shm>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
lea    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <getpwnam@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11a9 <lock_user_shm+0x59>
mov    0x2e4b(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xe66(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x5,%edi
call   1070 <exit@plt>
mov    0x2e38(%rip),%rax
mov    (%rax),%esi
lea    -0x10(%rbp),%rdx
xor    %edi,%edi
call   1060 <shmctl@plt>
mov    %eax,-0x34(%rbp)
movl   $0x0,-0x38(%rbp)
mov    -0x38(%rbp),%eax
cmp    -0x34(%rbp),%eax
jg     1230 <lock_user_shm+0xe0>
mov    -0x38(%rbp),%edi
mov    0x2df3(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1060 <shmctl@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0x0,-0x30(%rbp)
jl     1209 <lock_user_shm+0xb9>
mov    -0x28(%rbp),%rax
mov    (%rax),%rax
mov    -0x20(%rbp),%rcx
cmp    (%rcx),%rax
je     120e <lock_user_shm+0xbe>
jmp    1222 <lock_user_shm+0xd2>
mov    -0x30(%rbp),%edi
mov    -0xc(%rbp),%esi
call   1030 <lock_cmd@plt>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x38(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x38(%rbp)
jmp    11c7 <lock_user_shm+0x77>
cmpl   $0x0,-0x2c(%rbp)
jne    125d <lock_user_shm+0x10d>
mov    0x2d97(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xdc6(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x6,%edi
call   1070 <exit@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lock_user_shm>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %esi,0xc(%rsp)
mov    %rdi,0x10(%rsp)
call   1040 <getpwnam@plt>
test   %rax,%rax
je     121c <lock_user_shm+0xcc>
mov    %rax,%r12
mov    0x2e69(%rip),%rax
mov    (%rax),%esi
xor    %ebp,%ebp
lea    0x20(%rsp),%rdx
xor    %edi,%edi
call   1060 <shmctl@plt>
test   %eax,%eax
js     11f8 <lock_user_shm+0xa8>
mov    %eax,%ebx
add    $0x1,%ebx
mov    0x2e31(%rip),%r14
lea    0x18(%rsp),%r13
xor    %r15d,%r15d
jmp    11b7 <lock_user_shm+0x67>
nopl   0x0(%rax)
add    $0x1,%ebp
cmp    %ebp,%ebx
je     11e4 <lock_user_shm+0x94>
mov    (%r14),%esi
mov    %ebp,%edi
mov    %r13,%rdx
call   1060 <shmctl@plt>
test   %eax,%eax
js     11b0 <lock_user_shm+0x60>
mov    (%r12),%rcx
cmp    0x18(%rsp),%rcx
jne    11b0 <lock_user_shm+0x60>
mov    %eax,%edi
mov    0xc(%rsp),%esi
call   1030 <lock_cmd@plt>
add    $0x1,%r15d
jmp    11b0 <lock_user_shm+0x60>
test   %r15d,%r15d
je     11f8 <lock_user_shm+0xa8>
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2dd9(%rip),%rax
mov    (%rax),%edi
lea    0xe0c(%rip),%rsi
mov    0x10(%rsp),%rdx
call   1050 <fprintf@plt>
mov    $0x6,%edi
call   1070 <exit@plt>
mov    0x2db5(%rip),%rax
mov    (%rax),%edi
lea    0xdd4(%rip),%rsi
mov    0x10(%rsp),%rdx
call   1050 <fprintf@plt>
mov    $0x5,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
%struct.shm_info = type { i32 }
%struct.shmid_ds = type { %struct.ipc_perm }
%struct.ipc_perm = type { i64 }
%struct.passwd = type { i64 }
@stderr = global i32 0
@.str = [20 x i8] c"Invalid user: '%s'\0A\00"
@SHM_INFO = global i32 0
@SHM_STAT = global i32 0
@.str.1 = [45 x i8] c"No shared memory segments under the user %s\0A\00"
define void @lock_user_shm(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca %struct.shm_info
%6 = alloca %struct.shmid_ds
%7 = alloca %struct.ipc_perm*
%8 = alloca %struct.passwd*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
%13 = getelementptr inbounds %struct.shmid_ds, %struct.shmid_ds* %6, i32 0, i32 0
store %struct.ipc_perm* %13, %struct.ipc_perm** %7
store i32 0, i32* %9
%14 = load i8*, i8** %3
%15 = call %struct.passwd* @getpwnam(i8* %14)
store %struct.passwd* %15, %struct.passwd** %8
%16 = load %struct.passwd*, %struct.passwd** %8
%17 = icmp ne %struct.passwd* %16, null
br i1 %17, label %23, label %18
18: ; preds = %2
%19 = load i32, i32* @stderr
%20 = load i8*, i8** %3
%21 = call i32 @fprintf(i32 %19, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %20)
%22 = call i32 @exit(i32 5)
unreachable
23: ; preds = %2
%24 = load i32, i32* @SHM_INFO
%25 = bitcast %struct.shm_info* %5 to %struct.shmid_ds*
%26 = call i32 @shmctl(i32 0, i32 %24, %struct.shmid_ds* %25)
store i32 %26, i32* %11
store i32 0, i32* %12
br label %27
27: ; preds = %52, %23
%28 = load i32, i32* %12
%29 = load i32, i32* %11
%30 = icmp sle i32 %28, %29
br i1 %30, label %31, label %55
31: ; preds = %27
%32 = load i32, i32* %12
%33 = load i32, i32* @SHM_STAT
%34 = call i32 @shmctl(i32 %32, i32 %33, %struct.shmid_ds* %6)
store i32 %34, i32* %10
%35 = load i32, i32* %10
%36 = icmp slt i32 %35, 0
br i1 %36, label %45, label %37
37: ; preds = %31
%38 = load %struct.passwd*, %struct.passwd** %8
%39 = getelementptr inbounds %struct.passwd, %struct.passwd* %38, i32 0, i32 0
%40 = load i64, i64* %39
%41 = load %struct.ipc_perm*, %struct.ipc_perm** %7
%42 = getelementptr inbounds %struct.ipc_perm, %struct.ipc_perm* %41, i32 0, i32 0
%43 = load i64, i64* %42
%44 = icmp ne i64 %40, %43
br i1 %44, label %45, label %46
45: ; preds = %37, %31
br label %52
46: ; preds = %37
%47 = load i32, i32* %10
%48 = load i32, i32* %4
%49 = call i32 @lock_cmd(i32 %47, i32 %48)
%50 = load i32, i32* %9
%51 = add nsw i32 %50, 1
store i32 %51, i32* %9
br label %52
52: ; preds = %46, %45
%53 = load i32, i32* %12
%54 = add nsw i32 %53, 1
store i32 %54, i32* %12
br label %27
55: ; preds = %27
%56 = load i32, i32* %9
%57 = icmp ne i32 %56, 0
br i1 %57, label %63, label %58
58: ; preds = %55
%59 = load i32, i32* @stderr
%60 = load i8*, i8** %3
%61 = call i32 @fprintf(i32 %59, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0), i8* %60)
%62 = call i32 @exit(i32 6)
unreachable
63: ; preds = %55
ret void
}

LLVM IR (-O2):
%struct.shm_info = type { i32 }
%struct.shmid_ds = type { %struct.ipc_perm }
%struct.ipc_perm = type { i64 }
%struct.passwd = type { i64 }
@stderr = global i32 0
@.str = [20 x i8] c"Invalid user: '%s'\0A\00"
@SHM_INFO = global i32 0
@SHM_STAT = global i32 0
@.str.1 = [45 x i8] c"No shared memory segments under the user %s\0A\00"
define void @lock_user_shm(i8* %0, i32 %1) {
%3 = alloca %struct.shm_info
%4 = alloca %struct.shmid_ds
%5 = bitcast %struct.shm_info* %3 to i8*
%6 = bitcast %struct.shmid_ds* %4 to i8*
%7 = tail call %struct.passwd* @getpwnam(i8* %0)
%8 = icmp eq %struct.passwd* %7, null
br i1 %8, label %9, label %13
9: ; preds = %2
%10 = load i32, i32* @stderr
%11 = tail call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i8* %0)
%12 = tail call i32 @exit(i32 5)
unreachable
13: ; preds = %2
%14 = load i32, i32* @SHM_INFO
%15 = bitcast %struct.shm_info* %3 to %struct.shmid_ds*
%16 = call i32 @shmctl(i32 0, i32 %14, %struct.shmid_ds* %15)
%17 = icmp slt i32 %16, 0
br i1 %17, label %40, label %18
18: ; preds = %13
%19 = getelementptr inbounds %struct.passwd, %struct.passwd* %7, i64 0, i32 0
%20 = getelementptr inbounds %struct.shmid_ds, %struct.shmid_ds* %4, i64 0, i32 0, i32 0
br label %21
21: ; preds = %18, %34
%22 = phi i32 [ 0, %18 ], [ %36, %34 ]
%23 = phi i32 [ 0, %18 ], [ %35, %34 ]
%24 = load i32, i32* @SHM_STAT
%25 = call i32 @shmctl(i32 %22, i32 %24, %struct.shmid_ds* %4)
%26 = icmp slt i32 %25, 0
br i1 %26, label %34, label %27
27: ; preds = %21
%28 = load i64, i64* %19
%29 = load i64, i64* %20
%30 = icmp eq i64 %28, %29
br i1 %30, label %31, label %34
31: ; preds = %27
%32 = call i32 @lock_cmd(i32 %25, i32 %1)
%33 = add nsw i32 %23, 1
br label %34
34: ; preds = %21, %27, %31
%35 = phi i32 [ %23, %21 ], [ %23, %27 ], [ %33, %31 ]
%36 = add nuw i32 %22, 1
%37 = icmp eq i32 %22, %16
br i1 %37, label %38, label %21
38: ; preds = %34
%39 = icmp eq i32 %35, 0
br i1 %39, label %40, label %44
40: ; preds = %13, %38
%41 = load i32, i32* @stderr
%42 = call i32 @fprintf(i32 %41, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%43 = call i32 @exit(i32 6)
unreachable
44: ; preds = %38
ret void
}