#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 key = 0x8888;
  int id;
  int error;
  struct shmid_ds buf;
  while ((ch = getopt(argc, argv, "k:")) != -1) {
    switch (ch) {
    case 'k':
      key = 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   $0x8888,-0x18(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
lea    0xe85(%rip),%rdx
call   1050 <getopt@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
je     11b3 <main+0x63>
mov    -0x14(%rbp),%eax
sub    $0x6b,%eax
jne    11ae <main+0x5e>
jmp    119d <main+0x4d>
mov    0x2e54(%rip),%rax
mov    (%rax),%edi
call   1070 <atoi@plt>
mov    %eax,-0x18(%rbp)
jmp    116d <main+0x1d>
mov    -0x18(%rbp),%edi
mov    0x2dfb(%rip),%rax
mov    (%rax),%edx
or     $0x1ff,%edx
mov    $0x800,%esi
call   1060 <shmget@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11e8 <main+0x98>
movl   $0x1,-0x4(%rbp)
jmp    12a1 <main+0x151>
mov    -0x1c(%rbp),%edi
mov    0x2dd6(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1040 <shmctl@plt>
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%esi
lea    0xdfa(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x1c(%rbp),%edi
mov    0x2dc5(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1040 <shmctl@plt>
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%esi
lea    0xdd0(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x1c(%rbp),%edi
mov    0x2d8c(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
mov    %eax,%edx
call   1040 <shmctl@plt>
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%esi
lea    0xda7(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x1c(%rbp),%edi
mov    0x2d53(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
mov    %eax,%edx
call   1040 <shmctl@plt>
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%esi
lea    0xd7e(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xd74(%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
cs nopw 0x0(%rax,%rax,1)
nop
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r15,%rdx
call   1060 <getopt@plt>
cmp    $0xffffffff,%eax
je     11b5 <main+0x55>
cmp    $0x6b,%eax
jne    1190 <main+0x30>
mov    (%r12),%edi
call   1080 <atoi@plt>
mov    %eax,%r14d
jmp    1190 <main+0x30>
mov    0x2dfc(%rip),%rax
mov    $0x1ff,%edx
or     (%rax),%edx
mov    %r14d,%edi
mov    $0x800,%esi
call   1070 <shmget@plt>
cmp    $0xffffffff,%eax
je     1273 <main+0x113>
mov    %eax,%ebp
mov    0x2de6(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%r14
mov    %ebp,%edi
mov    %r14,%rdx
call   1050 <shmctl@plt>
lea    0xe0a(%rip),%rdi
xor    %ebx,%ebx
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x2dd4(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
mov    %r14,%rdx
call   1050 <shmctl@plt>
lea    0xde4(%rip),%r14
mov    %r14,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x2d9e(%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    0x2d70(%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    1278 <main+0x118>
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 = [3 x i8] c"k:\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 %struct.shmid_ds
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 34952, i32* %7
br label %11
11: ; preds = %21, %2
%12 = load i32, i32* %4
%13 = load i8**, i8*** %5
%14 = call i32 @getopt(i32 %12, i8** %13, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32 %14, i32* %6
%15 = icmp ne i32 %14, -1
br i1 %15, label %16, label %22
16: ; preds = %11
%17 = load i32, i32* %6
switch i32 %17, label %21 [
i32 107, label %18
]
18: ; preds = %16
%19 = load i32, i32* @optarg
%20 = call i32 @atoi(i32 %19)
store i32 %20, i32* %7
br label %21
21: ; preds = %16, %18
br label %11
22: ; preds = %11
%23 = load i32, i32* %7
%24 = load i32, i32* @IPC_CREAT
%25 = or i32 %24, 511
%26 = call i32 @shmget(i32 %23, i32 2048, i32 %25)
store i32 %26, i32* %8
%27 = load i32, i32* %8
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %30
29: ; preds = %22
store i32 1, i32* %3
br label %52
30: ; preds = %22
%31 = load i32, i32* %8
%32 = load i32, i32* @IPC_STAT
%33 = call i32 @shmctl(i32 %31, i32 %32, %struct.shmid_ds* %10)
store i32 %33, i32* %9
%34 = load i32, i32* %9
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32 %34)
%36 = load i32, i32* %8
%37 = load i32, i32* @IPC_SET
%38 = call i32 @shmctl(i32 %36, i32 %37, %struct.shmid_ds* %10)
store i32 %38, i32* %9
%39 = load i32, i32* %9
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %39)
%41 = load i32, i32* %8
%42 = load i32, i32* @SHM_LOCK
%43 = call i32 @shmctl(i32 %41, i32 %42, %struct.shmid_ds* null)
store i32 %43, i32* %9
%44 = load i32, i32* %9
%45 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %44)
%46 = load i32, i32* %8
%47 = load i32, i32* @IPC_RMID
%48 = call i32 @shmctl(i32 %46, i32 %47, %struct.shmid_ds* null)
store i32 %48, i32* %9
%49 = load i32, i32* %9
%50 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %49)
%51 = 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 %52
52: ; preds = %30, %29
%53 = load i32, i32* %3
ret i32 %53
}

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