#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 */

/* Variables and functions */
int SHM_LOCK;
int SHM_UNLOCK;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int getopt(int, char **, char *);
int /*<<< orphan*/ help(char *);
int /*<<< orphan*/ lock_cmd(int, int);
int /*<<< orphan*/ lock_user_shm(char *, int);
char *optarg;
int /*<<< orphan*/ stderr;
int strtol(char *, char **, int);
int main(int argc, char **argv) {
  int c;
  char *endptr;
  int shmid = 0;
  char *user = NULL;
  int cmd = SHM_LOCK;
  while ((c = getopt(argc, argv, "hlui:U:")) != -1) {
    switch (c) {
    case 'l':
      cmd = SHM_LOCK;
      break;
    case 'u':
      cmd = SHM_UNLOCK;
      break;
    case 'i':
      shmid = strtol(optarg, &endptr, 10);
      if (endptr == optarg || *endptr != '\0' || shmid == 0) {
        fprintf(stderr, "Invalid shmid\n");
        exit(2);
      }
      break;
    case 'U':
      user = optarg;
      break;
    case 'h':
      help(argv[0]);
      exit(0);
    default:
      exit(1);
    }
  }
  if (user) {
    lock_user_shm(user, cmd);
  }
  if (shmid) {
    lock_cmd(shmid, cmd);
  }
  if (!user && !shmid) {
    help(argv[0]);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x24(%rbp)
movq   $0x0,-0x30(%rbp)
mov    0x2e2c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
lea    0xed5(%rip),%rdx
call   1060 <getopt@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
je     12ad <main+0x13d>
mov    -0x14(%rbp),%eax
add    $0xffffffab,%eax
mov    %eax,%ecx
mov    %rcx,-0x40(%rbp)
sub    $0x20,%eax
ja     129e <main+0x12e>
mov    -0x40(%rbp),%rax
lea    0xe20(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
mov    0x2dd8(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
jmp    12a8 <main+0x138>
mov    0x2ddf(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
jmp    12a8 <main+0x138>
mov    0x2de6(%rip),%rax
mov    (%rax),%rdi
lea    -0x20(%rbp),%rsi
mov    $0xa,%edx
call   1050 <strtol@plt>
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%rax
mov    0x2dc7(%rip),%rcx
cmp    (%rcx),%rax
je     1254 <main+0xe4>
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    1254 <main+0xe4>
cmpl   $0x0,-0x24(%rbp)
jne    1273 <main+0x103>
mov    0x2d75(%rip),%rax
mov    (%rax),%edi
lea    0xe28(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x2,%edi
call   1090 <exit@plt>
jmp    12a8 <main+0x138>
mov    0x2d79(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x30(%rbp)
jmp    12a8 <main+0x138>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1080 <help@plt>
xor    %edi,%edi
call   1090 <exit@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
jmp    11a1 <main+0x31>
cmpq   $0x0,-0x30(%rbp)
je     12c4 <main+0x154>
mov    -0x30(%rbp),%rdi
mov    -0x34(%rbp),%esi
call   1070 <lock_user_shm@plt>
cmpl   $0x0,-0x24(%rbp)
je     12d9 <main+0x169>
mov    -0x24(%rbp),%edi
mov    -0x34(%rbp),%esi
call   1030 <lock_cmd@plt>
cmpq   $0x0,-0x30(%rbp)
jne    12fa <main+0x18a>
cmpl   $0x0,-0x24(%rbp)
jne    12fa <main+0x18a>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1080 <help@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%rbx
mov    %edi,%ebp
mov    0x2e3e(%rip),%r12
mov    (%r12),%r15d
xor    %eax,%eax
lea    0xeed(%rip),%r13
lea    0xe62(%rip),%r14
xor    %ecx,%ecx
mov    %rcx,0x8(%rsp)
mov    %rax,(%rsp)
jmp    11ba <main+0x4a>
nopl   0x0(%rax,%rax,1)
mov    0x2e29(%rip),%rax
mov    (%rax),%r15d
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r13,%rdx
call   1060 <getopt@plt>
lea    -0x55(%rax),%ecx
cmp    $0x20,%ecx
ja     1242 <main+0xd2>
movslq (%r14,%rcx,4),%rcx
add    %r14,%rcx
mov    %r12,%rax
jmp    *%rcx
mov    0x2e16(%rip),%rax
mov    (%rax),%rax
mov    %rax,0x8(%rsp)
jmp    11ba <main+0x4a>
nopl   0x0(%rax)
mov    0x2e01(%rip),%rax
mov    (%rax),%rdi
lea    0x10(%rsp),%rsi
mov    $0xa,%edx
call   1050 <strtol@plt>
mov    0x10(%rsp),%rcx
mov    0x2de3(%rip),%rdx
cmp    (%rdx),%rcx
je     1223 <main+0xb3>
cmpb   $0x0,(%rcx)
jne    1223 <main+0xb3>
test   %eax,%eax
jne    11a5 <main+0x35>
mov    0x2da6(%rip),%rax
mov    (%rax),%edi
lea    0xe59(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x2,%edi
call   1090 <exit@plt>
cmp    $0xffffffff,%eax
jne    128e <main+0x11e>
mov    0x8(%rsp),%rdi
test   %rdi,%rdi
je     1263 <main+0xf3>
mov    %r15d,%esi
call   1070 <lock_user_shm@plt>
mov    (%rsp),%rdi
test   %edi,%edi
jne    126b <main+0xfb>
jmp    1273 <main+0x103>
mov    (%rsp),%rdi
test   %edi,%edi
je     1284 <main+0x114>
mov    %r15d,%esi
call   1030 <lock_cmd@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    (%rbx),%rdi
call   1080 <help@plt>
jmp    1273 <main+0x103>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    (%rbx),%rdi
call   1080 <help@plt>
xor    %edi,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@SHM_LOCK = global i32 0
@.str = [8 x i8] c"hlui:U:\00"
@SHM_UNLOCK = global i32 0
@optarg = global i8* null
@stderr = global i32 0
@.str.1 = [15 x i8] c"Invalid shmid\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %8
store i8* null, i8** %9
%11 = load i32, i32* @SHM_LOCK
store i32 %11, i32* %10
br label %12
12: ; preds = %52, %2
%13 = load i32, i32* %4
%14 = load i8**, i8*** %5
%15 = call i32 @getopt(i32 %13, i8** %14, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
store i32 %15, i32* %6
%16 = icmp ne i32 %15, -1
br i1 %16, label %17, label %53
17: ; preds = %12
%18 = load i32, i32* %6
switch i32 %18, label %50 [
i32 108, label %19
i32 117, label %21
i32 105, label %23
i32 85, label %42
i32 104, label %44
]
19: ; preds = %17
%20 = load i32, i32* @SHM_LOCK
store i32 %20, i32* %10
br label %52
21: ; preds = %17
%22 = load i32, i32* @SHM_UNLOCK
store i32 %22, i32* %10
br label %52
23: ; preds = %17
%24 = load i8*, i8** @optarg
%25 = call i32 @strtol(i8* %24, i8** %7, i32 10)
store i32 %25, i32* %8
%26 = load i8*, i8** %7
%27 = load i8*, i8** @optarg
%28 = icmp eq i8* %26, %27
br i1 %28, label %37, label %29
29: ; preds = %23
%30 = load i8*, i8** %7
%31 = load i8, i8* %30
%32 = sext i8 %31 to i32
%33 = icmp ne i32 %32, 0
br i1 %33, label %37, label %34
34: ; preds = %29
%35 = load i32, i32* %8
%36 = icmp eq i32 %35, 0
br i1 %36, label %37, label %41
37: ; preds = %34, %29, %23
%38 = load i32, i32* @stderr
%39 = call i32 @fprintf(i32 %38, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%40 = call i32 @exit(i32 2)
unreachable
41: ; preds = %34
br label %52
42: ; preds = %17
%43 = load i8*, i8** @optarg
store i8* %43, i8** %9
br label %52
44: ; preds = %17
%45 = load i8**, i8*** %5
%46 = getelementptr inbounds i8*, i8** %45, i64 0
%47 = load i8*, i8** %46
%48 = call i32 @help(i8* %47)
%49 = call i32 @exit(i32 0)
unreachable
50: ; preds = %17
%51 = call i32 @exit(i32 1)
unreachable
52: ; preds = %42, %41, %21, %19
br label %12
53: ; preds = %12
%54 = load i8*, i8** %9
%55 = icmp ne i8* %54, null
br i1 %55, label %56, label %60
56: ; preds = %53
%57 = load i8*, i8** %9
%58 = load i32, i32* %10
%59 = call i32 @lock_user_shm(i8* %57, i32 %58)
br label %60
60: ; preds = %56, %53
%61 = load i32, i32* %8
%62 = icmp ne i32 %61, 0
br i1 %62, label %63, label %67
63: ; preds = %60
%64 = load i32, i32* %8
%65 = load i32, i32* %10
%66 = call i32 @lock_cmd(i32 %64, i32 %65)
br label %67
67: ; preds = %63, %60
%68 = load i8*, i8** %9
%69 = icmp ne i8* %68, null
br i1 %69, label %78, label %70
70: ; preds = %67
%71 = load i32, i32* %8
%72 = icmp ne i32 %71, 0
br i1 %72, label %78, label %73
73: ; preds = %70
%74 = load i8**, i8*** %5
%75 = getelementptr inbounds i8*, i8** %74, i64 0
%76 = load i8*, i8** %75
%77 = call i32 @help(i8* %76)
br label %78
78: ; preds = %73, %70, %67
ret i32 0
}

LLVM IR (-O2):
@SHM_LOCK = global i32 0
@.str = [8 x i8] c"hlui:U:\00"
@SHM_UNLOCK = global i32 0
@optarg = global i8* null
@stderr = global i32 0
@.str.1 = [15 x i8] c"Invalid shmid\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i8*
%4 = bitcast i8** %3 to i8*
%5 = load i32, i32* @SHM_LOCK
br label %6
6: ; preds = %26, %2
%7 = phi i32 [ %22, %26 ], [ 0, %2 ]
%8 = phi i8* [ %11, %26 ], [ null, %2 ]
%9 = phi i32 [ %14, %26 ], [ %5, %2 ]
br label %10
10: ; preds = %6, %35
%11 = phi i8* [ %8, %6 ], [ %36, %35 ]
%12 = phi i32 [ %9, %6 ], [ %14, %35 ]
br label %13
13: ; preds = %17, %10
%14 = phi i32 [ %12, %10 ], [ %19, %17 ]
%15 = tail call i32 @getopt(i32 %0, i8** %1, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
switch i32 %15, label %41 [
i32 -1, label %43
i32 108, label %17
i32 117, label %16
i32 105, label %20
i32 85, label %35
i32 104, label %37
]
16: ; preds = %13
br label %17
17: ; preds = %13, %16
%18 = phi i32* [ @SHM_UNLOCK, %16 ], [ @SHM_LOCK, %13 ]
%19 = load i32, i32* %18
br label %13
20: ; preds = %13
%21 = load i8*, i8** @optarg
%22 = call i32 @strtol(i8* %21, i8** %3, i32 10)
%23 = load i8*, i8** %3
%24 = load i8*, i8** @optarg
%25 = icmp eq i8* %23, %24
br i1 %25, label %31, label %26
26: ; preds = %20
%27 = load i8, i8* %23
%28 = icmp ne i8 %27, 0
%29 = icmp eq i32 %22, 0
%30 = select i1 %28, i1 true, i1 %29
br i1 %30, label %31, label %6
31: ; preds = %26, %20
%32 = load i32, i32* @stderr
%33 = tail call i32 @fprintf(i32 %32, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%34 = tail call i32 @exit(i32 2)
unreachable
35: ; preds = %13
%36 = load i8*, i8** @optarg
br label %10
37: ; preds = %13
%38 = load i8*, i8** %1
%39 = tail call i32 @help(i8* %38)
%40 = tail call i32 @exit(i32 0)
unreachable
41: ; preds = %13
%42 = tail call i32 @exit(i32 1)
unreachable
43: ; preds = %13
%44 = icmp eq i8* %11, null
br i1 %44, label %45, label %47
45: ; preds = %43
%46 = icmp eq i32 %7, 0
br i1 %46, label %52, label %50
47: ; preds = %43
%48 = tail call i32 @lock_user_shm(i8* %11, i32 %14)
%49 = icmp eq i32 %7, 0
br i1 %49, label %55, label %50
50: ; preds = %47, %45
%51 = tail call i32 @lock_cmd(i32 %7, i32 %14)
br label %55
52: ; preds = %45
%53 = load i8*, i8** %1
%54 = tail call i32 @help(i8* %53)
br label %55
55: ; preds = %47, %50, %52
ret i32 0
}