#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 shmid_ds {
  int dummy;
};

/* Variables and functions */
int IPC_CREAT;
int /*<<< orphan*/ IPC_RMID;
int /*<<< orphan*/ IPC_SET;
int /*<<< orphan*/ IPC_STAT;
int /*<<< orphan*/ SHM_LOCK;
int atoi(int /*<<< orphan*/);
int getopt(int, char **, char *);
int /*<<< orphan*/ optarg;
int /*<<< orphan*/ printf(char *, ...);
int shmctl(int, int /*<<< orphan*/, struct shmid_ds *);
int shmget(int, int, int);
int main(int argc, char **argv) {
  int ch;
  int num = 1;
  int key = 0x8888;
  int id;
  int error;
  struct shmid_ds buf;
  while ((ch = getopt(argc, argv, "k:-n:")) != -1) {
    switch ((char)ch) {
    case 'k':
      key = atoi(optarg);
      break;
    case 'n':
      num = atoi(optarg);
      break;
    }
  }
  id = shmget(key, 2048, IPC_CREAT | 0777);
  if (id == -1)
    return 1;
  error = shmctl(id, IPC_STAT, &buf);
  printf("%d", error);
  error = shmctl(id, IPC_SET, &buf);
  printf(" %d", error);
  error = shmctl(id, SHM_LOCK, NULL);
  printf(" %d", error);
  error = shmctl(id, IPC_RMID, NULL);
  printf(" %d", error);
  printf("\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x1,-0x18(%rbp)
movl   $0x8888,-0x1c(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
lea    0xe7e(%rip),%rdx
call   1050 <getopt@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
je     11e5 <main+0x95>
movsbl -0x14(%rbp),%eax
mov    %eax,-0x2c(%rbp)
sub    $0x6b,%eax
je     11b9 <main+0x69>
jmp    11a8 <main+0x58>
mov    -0x2c(%rbp),%eax
sub    $0x6e,%eax
je     11cf <main+0x7f>
jmp    11e0 <main+0x90>
mov    0x2e38(%rip),%rax
mov    (%rax),%edi
call   1070 <atoi@plt>
mov    %eax,-0x1c(%rbp)
jmp    11e0 <main+0x90>
mov    0x2e22(%rip),%rax
mov    (%rax),%edi
call   1070 <atoi@plt>
mov    %eax,-0x18(%rbp)
jmp    1174 <main+0x24>
mov    -0x1c(%rbp),%edi
mov    0x2dc9(%rip),%rax
mov    (%rax),%edx
or     $0x1ff,%edx
mov    $0x800,%esi
call   1060 <shmget@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
jne    121a <main+0xca>
movl   $0x1,-0x4(%rbp)
jmp    12d3 <main+0x183>
mov    -0x20(%rbp),%edi
mov    0x2da4(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1040 <shmctl@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%esi
lea    0xdcb(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x20(%rbp),%edi
mov    0x2d93(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1040 <shmctl@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%esi
lea    0xda1(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x20(%rbp),%edi
mov    0x2d5a(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
mov    %eax,%edx
call   1040 <shmctl@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%esi
lea    0xd78(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x20(%rbp),%edi
mov    0x2d21(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
mov    %eax,%edx
call   1040 <shmctl@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%esi
lea    0xd4f(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xd45(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbx
mov    %edi,%ebp
lea    0xe88(%rip),%rdx
call   1060 <getopt@plt>
mov    $0x8888,%r14d
cmp    $0xffffffff,%eax
je     11d5 <main+0x75>
mov    0x2e69(%rip),%r12
lea    0xe6a(%rip),%r15
jmp    11bb <main+0x5b>
nopl   0x0(%rax,%rax,1)
mov    (%r12),%edi
call   1080 <atoi@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r15,%rdx
call   1060 <getopt@plt>
cmp    $0xffffffff,%eax
je     11d5 <main+0x75>
cmp    $0x6e,%al
je     11a0 <main+0x40>
movsbl %al,%eax
cmp    $0x6b,%eax
jne    11a9 <main+0x49>
mov    (%r12),%edi
call   1080 <atoi@plt>
mov    %eax,%r14d
jmp    11a9 <main+0x49>
mov    0x2ddc(%rip),%rax
mov    $0x1ff,%edx
or     (%rax),%edx
mov    %r14d,%edi
mov    $0x800,%esi
call   1070 <shmget@plt>
cmp    $0xffffffff,%eax
je     1293 <main+0x133>
mov    %eax,%ebp
mov    0x2dc6(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%r14
mov    %ebp,%edi
mov    %r14,%rdx
call   1050 <shmctl@plt>
lea    0xded(%rip),%rdi
xor    %ebx,%ebx
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x2db4(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
mov    %r14,%rdx
call   1050 <shmctl@plt>
lea    0xdc7(%rip),%r14
mov    %r14,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x2d7e(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
xor    %edx,%edx
call   1050 <shmctl@plt>
mov    %r14,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x2d50(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
xor    %edx,%edx
call   1050 <shmctl@plt>
mov    %r14,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
jmp    1298 <main+0x138>
mov    $0x1,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.shmid_ds = type { i32 }
@.str = [6 x i8] c"k:-n:\00"
@optarg = global i32 0
@IPC_CREAT = global i32 0
@IPC_STAT = global i32 0
@.str.1 = [3 x i8] c"%d\00"
@IPC_SET = global i32 0
@.str.2 = [4 x i8] c" %d\00"
@SHM_LOCK = global i32 0
@IPC_RMID = global i32 0
@.str.3 = [2 x i8] c"\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca %struct.shmid_ds
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 1, i32* %7
store i32 34952, i32* %8
br label %12
12: ; preds = %27, %2
%13 = load i32, i32* %4
%14 = load i8**, i8*** %5
%15 = call i32 @getopt(i32 %13, i8** %14, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
store i32 %15, i32* %6
%16 = icmp ne i32 %15, -1
br i1 %16, label %17, label %28
17: ; preds = %12
%18 = load i32, i32* %6
%19 = trunc i32 %18 to i8
%20 = sext i8 %19 to i32
switch i32 %20, label %27 [
i32 107, label %21
i32 110, label %24
]
21: ; preds = %17
%22 = load i32, i32* @optarg
%23 = call i32 @atoi(i32 %22)
store i32 %23, i32* %8
br label %27
24: ; preds = %17
%25 = load i32, i32* @optarg
%26 = call i32 @atoi(i32 %25)
store i32 %26, i32* %7
br label %27
27: ; preds = %17, %24, %21
br label %12
28: ; preds = %12
%29 = load i32, i32* %8
%30 = load i32, i32* @IPC_CREAT
%31 = or i32 %30, 511
%32 = call i32 @shmget(i32 %29, i32 2048, i32 %31)
store i32 %32, i32* %9
%33 = load i32, i32* %9
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %36
35: ; preds = %28
store i32 1, i32* %3
br label %58
36: ; preds = %28
%37 = load i32, i32* %9
%38 = load i32, i32* @IPC_STAT
%39 = call i32 @shmctl(i32 %37, i32 %38, %struct.shmid_ds* %11)
store i32 %39, i32* %10
%40 = load i32, i32* %10
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 %40)
%42 = load i32, i32* %9
%43 = load i32, i32* @IPC_SET
%44 = call i32 @shmctl(i32 %42, i32 %43, %struct.shmid_ds* %11)
store i32 %44, i32* %10
%45 = load i32, i32* %10
%46 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %45)
%47 = load i32, i32* %9
%48 = load i32, i32* @SHM_LOCK
%49 = call i32 @shmctl(i32 %47, i32 %48, %struct.shmid_ds* null)
store i32 %49, i32* %10
%50 = load i32, i32* %10
%51 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %50)
%52 = load i32, i32* %9
%53 = load i32, i32* @IPC_RMID
%54 = call i32 @shmctl(i32 %52, i32 %53, %struct.shmid_ds* null)
store i32 %54, i32* %10
%55 = load i32, i32* %10
%56 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %55)
%57 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32 0, i32* %3
br label %58
58: ; preds = %36, %35
%59 = load i32, i32* %3
ret i32 %59
}

LLVM IR (-O2):
%struct.shmid_ds = type { i32 }
@.str = [6 x i8] c"k:-n:\00"
@optarg = global i32 0
@IPC_CREAT = global i32 0
@IPC_STAT = global i32 0
@.str.1 = [3 x i8] c"%d\00"
@IPC_SET = global i32 0
@.str.2 = [4 x i8] c" %d\00"
@SHM_LOCK = global i32 0
@IPC_RMID = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca %struct.shmid_ds
%4 = bitcast %struct.shmid_ds* %3 to i8*
%5 = tail call i32 @getopt(i32 %0, i8** %1, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i32 %5, -1
br i1 %6, label %22, label %7
7: ; preds = %2, %18
%8 = phi i32 [ %20, %18 ], [ %5, %2 ]
%9 = phi i32 [ %19, %18 ], [ 34952, %2 ]
%10 = shl i32 %8, 24
%11 = ashr exact i32 %10, 24
switch i32 %11, label %18 [
i32 107, label %12
i32 110, label %15
]
12: ; preds = %7
%13 = load i32, i32* @optarg
%14 = tail call i32 @atoi(i32 %13)
br label %18
15: ; preds = %7
%16 = load i32, i32* @optarg
%17 = tail call i32 @atoi(i32 %16)
br label %18
18: ; preds = %7, %15, %12
%19 = phi i32 [ %9, %7 ], [ %9, %15 ], [ %14, %12 ]
%20 = tail call i32 @getopt(i32 %0, i8** %1, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %7
22: ; preds = %18, %2
%23 = phi i32 [ 34952, %2 ], [ %19, %18 ]
%24 = load i32, i32* @IPC_CREAT
%25 = or i32 %24, 511
%26 = tail call i32 @shmget(i32 %23, i32 2048, i32 %25)
%27 = icmp eq i32 %26, -1
br i1 %27, label %42, label %28
28: ; preds = %22
%29 = load i32, i32* @IPC_STAT
%30 = call i32 @shmctl(i32 %26, i32 %29, %struct.shmid_ds* %3)
%31 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 %30)
%32 = load i32, i32* @IPC_SET
%33 = call i32 @shmctl(i32 %26, i32 %32, %struct.shmid_ds* %3)
%34 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %33)
%35 = load i32, i32* @SHM_LOCK
%36 = call i32 @shmctl(i32 %26, i32 %35, %struct.shmid_ds* null)
%37 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %36)
%38 = load i32, i32* @IPC_RMID
%39 = call i32 @shmctl(i32 %26, i32 %38, %struct.shmid_ds* null)
%40 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %39)
%41 = call i32 @putchar(i32 10)
br label %42
42: ; preds = %22, %28
%43 = phi i32 [ 0, %28 ], [ 1, %22 ]
ret i32 %43
}