#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 (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     11e4 <main+0x94>
mov    -0x14(%rbp),%eax
mov    %eax,-0x2c(%rbp)
sub    $0x6b,%eax
je     11b8 <main+0x68>
jmp    11a7 <main+0x57>
mov    -0x2c(%rbp),%eax
sub    $0x6e,%eax
je     11ce <main+0x7e>
jmp    11df <main+0x8f>
mov    0x2e39(%rip),%rax
mov    (%rax),%edi
call   1070 <atoi@plt>
mov    %eax,-0x1c(%rbp)
jmp    11df <main+0x8f>
mov    0x2e23(%rip),%rax
mov    (%rax),%edi
call   1070 <atoi@plt>
mov    %eax,-0x18(%rbp)
jmp    1174 <main+0x24>
mov    -0x1c(%rbp),%edi
mov    0x2dca(%rip),%rax
mov    (%rax),%edx
or     $0x1ff,%edx
mov    $0x800,%esi
call   1060 <shmget@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffffff,-0x20(%rbp)
jne    1219 <main+0xc9>
movl   $0x1,-0x4(%rbp)
jmp    12d2 <main+0x182>
mov    -0x20(%rbp),%edi
mov    0x2da5(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1040 <shmctl@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%esi
lea    0xdcc(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x20(%rbp),%edi
mov    0x2d94(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1040 <shmctl@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%esi
lea    0xda2(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x20(%rbp),%edi
mov    0x2d5b(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
mov    %eax,%edx
call   1040 <shmctl@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%esi
lea    0xd79(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x20(%rbp),%edi
mov    0x2d22(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
mov    %eax,%edx
call   1040 <shmctl@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%esi
lea    0xd50(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xd46(%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
mov    $0x8888,%r14d
lea    0xe82(%rip),%r15
mov    0x2e73(%rip),%r12
jmp    1199 <main+0x39>
nopw   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    $0x6e,%eax
je     1190 <main+0x30>
cmp    $0xffffffff,%eax
je     11c3 <main+0x63>
cmp    $0x6b,%eax
jne    1199 <main+0x39>
mov    (%r12),%edi
call   1080 <atoi@plt>
mov    %eax,%r14d
jmp    1199 <main+0x39>
mov    0x2dee(%rip),%rax
mov    $0x1ff,%edx
or     (%rax),%edx
mov    %r14d,%edi
mov    $0x800,%esi
call   1070 <shmget@plt>
cmp    $0xffffffff,%eax
je     1281 <main+0x121>
mov    %eax,%ebp
mov    0x2dd8(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%r14
mov    %ebp,%edi
mov    %r14,%rdx
call   1050 <shmctl@plt>
lea    0xdff(%rip),%rdi
xor    %ebx,%ebx
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x2dc6(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
mov    %r14,%rdx
call   1050 <shmctl@plt>
lea    0xdd9(%rip),%r14
mov    %r14,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x2d90(%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    0x2d62(%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    1286 <main+0x126>
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 = %25, %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 %26
17: ; preds = %12
%18 = load i32, i32* %6
switch i32 %18, label %25 [
i32 107, label %19
i32 110, label %22
]
19: ; preds = %17
%20 = load i32, i32* @optarg
%21 = call i32 @atoi(i32 %20)
store i32 %21, i32* %8
br label %25
22: ; preds = %17
%23 = load i32, i32* @optarg
%24 = call i32 @atoi(i32 %23)
store i32 %24, i32* %7
br label %25
25: ; preds = %17, %22, %19
br label %12
26: ; preds = %12
%27 = load i32, i32* %8
%28 = load i32, i32* @IPC_CREAT
%29 = or i32 %28, 511
%30 = call i32 @shmget(i32 %27, i32 2048, i32 %29)
store i32 %30, i32* %9
%31 = load i32, i32* %9
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %34
33: ; preds = %26
store i32 1, i32* %3
br label %56
34: ; preds = %26
%35 = load i32, i32* %9
%36 = load i32, i32* @IPC_STAT
%37 = call i32 @shmctl(i32 %35, i32 %36, %struct.shmid_ds* %11)
store i32 %37, i32* %10
%38 = load i32, i32* %10
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 %38)
%40 = load i32, i32* %9
%41 = load i32, i32* @IPC_SET
%42 = call i32 @shmctl(i32 %40, i32 %41, %struct.shmid_ds* %11)
store i32 %42, i32* %10
%43 = load i32, i32* %10
%44 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %43)
%45 = load i32, i32* %9
%46 = load i32, i32* @SHM_LOCK
%47 = call i32 @shmctl(i32 %45, i32 %46, %struct.shmid_ds* null)
store i32 %47, i32* %10
%48 = load i32, i32* %10
%49 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %48)
%50 = load i32, i32* %9
%51 = load i32, i32* @IPC_RMID
%52 = call i32 @shmctl(i32 %50, i32 %51, %struct.shmid_ds* null)
store i32 %52, i32* %10
%53 = load i32, i32* %10
%54 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %53)
%55 = 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 %56
56: ; preds = %34, %33
%57 = load i32, i32* %3
ret i32 %57
}

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*
br label %5
5: ; preds = %10, %2
%6 = phi i32 [ %12, %10 ], [ 34952, %2 ]
br label %7
7: ; preds = %9, %5
%8 = tail call i32 @getopt(i32 %0, i8** %1, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
switch i32 %8, label %9 [
i32 -1, label %16
i32 107, label %10
i32 110, label %13
]
9: ; preds = %7, %13
br label %7
10: ; preds = %7
%11 = load i32, i32* @optarg
%12 = tail call i32 @atoi(i32 %11)
br label %5
13: ; preds = %7
%14 = load i32, i32* @optarg
%15 = tail call i32 @atoi(i32 %14)
br label %9
16: ; preds = %7
%17 = load i32, i32* @IPC_CREAT
%18 = or i32 %17, 511
%19 = tail call i32 @shmget(i32 %6, i32 2048, i32 %18)
%20 = icmp eq i32 %19, -1
br i1 %20, label %35, label %21
21: ; preds = %16
%22 = load i32, i32* @IPC_STAT
%23 = call i32 @shmctl(i32 %19, i32 %22, %struct.shmid_ds* %3)
%24 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 %23)
%25 = load i32, i32* @IPC_SET
%26 = call i32 @shmctl(i32 %19, i32 %25, %struct.shmid_ds* %3)
%27 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %26)
%28 = load i32, i32* @SHM_LOCK
%29 = call i32 @shmctl(i32 %19, i32 %28, %struct.shmid_ds* null)
%30 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %29)
%31 = load i32, i32* @IPC_RMID
%32 = call i32 @shmctl(i32 %19, i32 %31, %struct.shmid_ds* null)
%33 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %32)
%34 = call i32 @putchar(i32 10)
br label %35
35: ; preds = %16, %21
%36 = phi i32 [ 0, %21 ], [ 1, %16 ]
ret i32 %36
}