#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 {
  int mode;
};
struct shmid_ds {
  struct ipc_perm shm_perm;
};

/* Variables and functions */
int IPC_STAT;
int SHM_LOCKED;
int SHM_UNLOCK;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int shmctl(int, int, struct shmid_ds *);
void lock_cmd(int shmid, int cmd) {
  int result;
  struct shmid_ds sds;
  struct ipc_perm *ipcp = &sds.shm_perm;
  printf("%s shmid %d...", cmd == SHM_UNLOCK ? "Unlocking" : "Locking", shmid);
  result = shmctl(shmid, cmd, NULL);
  if (result) {
    perror("shmctl");
    exit(3);
  }
  printf(" OK! verifying... ");
  result = shmctl(shmid, IPC_STAT, &sds);
  if (result) {
    perror("shmctl");
    exit(4);
  }
  if (cmd == SHM_UNLOCK) {
    printf(ipcp->mode & SHM_LOCKED ? "FAIL :(\n" : "OK!\n");
  } else {
    printf(ipcp->mode & SHM_LOCKED ? "OK!\n" : "FAIL :(\n");
  }
}


x86 assembly (clang, -O0):
<lock_cmd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
lea    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%ecx
mov    0x2e88(%rip),%rax
mov    (%rax),%edx
lea    0xeb0(%rip),%rsi
lea    0xe9f(%rip),%rax
cmp    %edx,%ecx
cmove  %rax,%rsi
mov    -0x4(%rbp),%edx
lea    0xe80(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x4(%rbp),%edi
mov    -0x8(%rbp),%esi
xor    %eax,%eax
mov    %eax,%edx
call   1040 <shmctl@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
je     11b9 <lock_cmd+0x79>
lea    0xe77(%rip),%rdi
call   1050 <perror@plt>
mov    $0x3,%edi
call   1060 <exit@plt>
lea    0xe68(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x4(%rbp),%edi
mov    0x2dff(%rip),%rax
mov    (%rax),%esi
lea    -0x10(%rbp),%rdx
call   1040 <shmctl@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
je     11ff <lock_cmd+0xbf>
lea    0xe31(%rip),%rdi
call   1050 <perror@plt>
mov    $0x4,%edi
call   1060 <exit@plt>
mov    -0x8(%rbp),%eax
mov    0x2ddf(%rip),%rcx
cmp    (%rcx),%eax
jne    1241 <lock_cmd+0x101>
mov    -0x18(%rbp),%rax
mov    (%rax),%ecx
mov    0x2dba(%rip),%rax
and    (%rax),%ecx
lea    0xe1d(%rip),%rdi
lea    0xe0d(%rip),%rax
cmp    $0x0,%ecx
cmovne %rax,%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    126c <lock_cmd+0x12c>
mov    -0x18(%rbp),%rax
mov    (%rax),%ecx
mov    0x2d8a(%rip),%rax
and    (%rax),%ecx
lea    0xde4(%rip),%rdi
lea    0xde6(%rip),%rax
cmp    $0x0,%ecx
cmovne %rax,%rdi
mov    $0x0,%al
call   1030 <printf@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lock_cmd>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %esi,%ebx
mov    %edi,%ebp
mov    0x2e95(%rip),%r14
cmp    %esi,(%r14)
lea    0xeb2(%rip),%rax
lea    0xeb5(%rip),%rsi
cmove  %rax,%rsi
lea    0xe91(%rip),%rdi
mov    %ebp,%edx
xor    %eax,%eax
call   1030 <printf@plt>
mov    %ebp,%edi
mov    %ebx,%esi
xor    %edx,%edx
call   1040 <shmctl@plt>
test   %eax,%eax
jne    11eb <lock_cmd+0xab>
lea    0xe9a(%rip),%rdi
xor    %eax,%eax
call   1030 <printf@plt>
mov    0x2e34(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebp,%edi
call   1040 <shmctl@plt>
test   %eax,%eax
jne    1201 <lock_cmd+0xc1>
mov    0x2e23(%rip),%rax
mov    (%rax),%eax
and    0x8(%rsp),%eax
lea    0xe79(%rip),%rdi
lea    0xe7b(%rip),%rax
mov    %rax,%rcx
cmove  %rdi,%rcx
cmove  %rax,%rdi
cmp    %ebx,(%r14)
cmovne %rcx,%rdi
xor    %eax,%eax
call   1030 <printf@plt>
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe2f(%rip),%rdi
call   1050 <perror@plt>
mov    $0x3,%edi
call   1060 <exit@plt>
lea    0xe19(%rip),%rdi
call   1050 <perror@plt>
mov    $0x4,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
%struct.shmid_ds = type { %struct.ipc_perm }
%struct.ipc_perm = type { i32 }
@.str = [15 x i8] c"%s shmid %d...\00"
@SHM_UNLOCK = global i32 0
@.str.1 = [10 x i8] c"Unlocking\00"
@.str.2 = [8 x i8] c"Locking\00"
@.str.3 = [7 x i8] c"shmctl\00"
@.str.4 = [19 x i8] c" OK! verifying... \00"
@IPC_STAT = global i32 0
@SHM_LOCKED = global i32 0
@.str.5 = [9 x i8] c"FAIL :(\0A\00"
@.str.6 = [5 x i8] c"OK!\0A\00"
define void @lock_cmd(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.shmid_ds
%7 = alloca %struct.ipc_perm*
store i32 %0, i32* %3
store i32 %1, i32* %4
%8 = getelementptr inbounds %struct.shmid_ds, %struct.shmid_ds* %6, i32 0, i32 0
store %struct.ipc_perm* %8, %struct.ipc_perm** %7
%9 = load i32, i32* %4
%10 = load i32, i32* @SHM_UNLOCK
%11 = icmp eq i32 %9, %10
%12 = zext i1 %11 to i64
%13 = select i1 %11, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0)
%14 = load i32, i32* %3
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %13, i32 %14)
%16 = load i32, i32* %3
%17 = load i32, i32* %4
%18 = call i32 @shmctl(i32 %16, i32 %17, %struct.shmid_ds* null)
store i32 %18, i32* %5
%19 = load i32, i32* %5
%20 = icmp ne i32 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %2
%22 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%23 = call i32 @exit(i32 3)
unreachable
24: ; preds = %2
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0))
%26 = load i32, i32* %3
%27 = load i32, i32* @IPC_STAT
%28 = call i32 @shmctl(i32 %26, i32 %27, %struct.shmid_ds* %6)
store i32 %28, i32* %5
%29 = load i32, i32* %5
%30 = icmp ne i32 %29, 0
br i1 %30, label %31, label %34
31: ; preds = %24
%32 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%33 = call i32 @exit(i32 4)
unreachable
34: ; preds = %24
%35 = load i32, i32* %4
%36 = load i32, i32* @SHM_UNLOCK
%37 = icmp eq i32 %35, %36
br i1 %37, label %38, label %48
38: ; preds = %34
%39 = load %struct.ipc_perm*, %struct.ipc_perm** %7
%40 = getelementptr inbounds %struct.ipc_perm, %struct.ipc_perm* %39, i32 0, i32 0
%41 = load i32, i32* %40
%42 = load i32, i32* @SHM_LOCKED
%43 = and i32 %41, %42
%44 = icmp ne i32 %43, 0
%45 = zext i1 %44 to i64
%46 = select i1 %44, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0)
%47 = call i32 (i8*, ...) @printf(i8* %46)
br label %58
48: ; preds = %34
%49 = load %struct.ipc_perm*, %struct.ipc_perm** %7
%50 = getelementptr inbounds %struct.ipc_perm, %struct.ipc_perm* %49, i32 0, i32 0
%51 = load i32, i32* %50
%52 = load i32, i32* @SHM_LOCKED
%53 = and i32 %51, %52
%54 = icmp ne i32 %53, 0
%55 = zext i1 %54 to i64
%56 = select i1 %54, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0), i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0)
%57 = call i32 (i8*, ...) @printf(i8* %56)
br label %58
58: ; preds = %48, %38
ret void
}

LLVM IR (-O2):
%struct.shmid_ds = type { %struct.ipc_perm }
%struct.ipc_perm = type { i32 }
@.str = [15 x i8] c"%s shmid %d...\00"
@SHM_UNLOCK = global i32 0
@.str.1 = [10 x i8] c"Unlocking\00"
@.str.2 = [8 x i8] c"Locking\00"
@.str.3 = [7 x i8] c"shmctl\00"
@.str.4 = [19 x i8] c" OK! verifying... \00"
@IPC_STAT = global i32 0
@SHM_LOCKED = global i32 0
@.str.5 = [9 x i8] c"FAIL :(\0A\00"
@.str.6 = [5 x i8] c"OK!\0A\00"
define void @lock_cmd(i32 %0, i32 %1) {
%3 = alloca %struct.shmid_ds
%4 = bitcast %struct.shmid_ds* %3 to i8*
%5 = load i32, i32* @SHM_UNLOCK
%6 = icmp eq i32 %5, %1
%7 = select i1 %6, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0)
%8 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i8* %7, i32 %0)
%9 = tail call i32 @shmctl(i32 %0, i32 %1, %struct.shmid_ds* null)
%10 = icmp eq i32 %9, 0
br i1 %10, label %14, label %11
11: ; preds = %2
%12 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%13 = tail call i32 @exit(i32 3)
unreachable
14: ; preds = %2
%15 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0))
%16 = load i32, i32* @IPC_STAT
%17 = call i32 @shmctl(i32 %0, i32 %16, %struct.shmid_ds* %3)
%18 = icmp eq i32 %17, 0
br i1 %18, label %22, label %19
19: ; preds = %14
%20 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%21 = call i32 @exit(i32 4)
unreachable
22: ; preds = %14
%23 = load i32, i32* @SHM_UNLOCK
%24 = icmp eq i32 %23, %1
%25 = getelementptr inbounds %struct.shmid_ds, %struct.shmid_ds* %3, i64 0, i32 0, i32 0
%26 = load i32, i32* %25
%27 = load i32, i32* @SHM_LOCKED
%28 = and i32 %27, %26
%29 = icmp eq i32 %28, 0
%30 = select i1 %29, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0)
%31 = select i1 %29, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.6, i64 0, i64 0), i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0)
%32 = select i1 %24, i8* %31, i8* %30
%33 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) %32)
ret void
}