#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 atoi(char *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
char *shmat(int, int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ shmdt(char *);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ system(char *);
int main(int argc, char *argv[]) {
  int shm_id;
  char *shm_buf;
  if (argc != 2) {
    printf("USAGE: atshm <identifier>");
    exit(1);
  }
  shm_id = atoi(argv[1]);
  if ((shm_buf = shmat(shm_id, 0, 0)) < (char *)0) {
    perror("shmat");
    exit(1);
  }
  printf(" segment attached at %p\n", shm_buf);
  system("ipcs -m");
  sleep(3);
  if ((shmdt(shm_buf)) < 0) {
    perror("shmdt");
    exit(1);
  }
  printf("segment detached \n");
  system("ipcs -m ");
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11b8 <main+0x38>
lea    0xe59(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1080 <atoi@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
xor    %edx,%edx
mov    %edx,%esi
call   1070 <shmat@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jae    11f8 <main+0x78>
lea    0xe31(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x20(%rbp),%rsi
lea    0xe1d(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xe28(%rip),%rdi
call   1040 <system@plt>
mov    $0x3,%edi
call   10a0 <sleep@plt>
mov    -0x20(%rbp),%rdi
call   1030 <shmdt@plt>
cmp    $0x0,%rax
jge    1249 <main+0xc9>
lea    0xe07(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
lea    0xdf7(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xdfc(%rip),%rdi
call   1040 <system@plt>
xor    %edi,%edi
call   1090 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbx
cmp    $0x2,%edi
jne    11f7 <main+0x67>
mov    0x8(%rsi),%rdi
call   1090 <atoi@plt>
mov    %eax,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1080 <shmat@plt>
mov    %rax,%rbx
lea    0xe66(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xe6e(%rip),%rdi
call   1050 <system@plt>
mov    $0x3,%edi
call   10b0 <sleep@plt>
mov    %rbx,%rdi
call   1040 <shmdt@plt>
test   %rax,%rax
jns    120f <main+0x7f>
lea    0xe53(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
lea    0xe02(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
lea    0xe34(%rip),%rdi
call   1030 <puts@plt>
lea    0xe1f(%rip),%rdi
call   1050 <system@plt>
xor    %edi,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [26 x i8] c"USAGE: atshm <identifier>\00"
@.str.1 = [6 x i8] c"shmat\00"
@.str.2 = [25 x i8] c" segment attached at %p\0A\00"
@.str.3 = [8 x i8] c"ipcs -m\00"
@.str.4 = [6 x i8] c"shmdt\00"
@.str.5 = [19 x i8] c"segment detached \0A\00"
@.str.6 = [9 x i8] c"ipcs -m \00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = load i32, i32* %4
%9 = icmp ne i32 %8, 2
br i1 %9, label %10, label %13
10: ; preds = %2
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
%12 = call i32 @exit(i32 1)
unreachable
13: ; preds = %2
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 1
%16 = load i8*, i8** %15
%17 = call i32 @atoi(i8* %16)
store i32 %17, i32* %6
%18 = load i32, i32* %6
%19 = call i8* @shmat(i32 %18, i32 0, i32 0)
store i8* %19, i8** %7
%20 = icmp ult i8* %19, null
br i1 %20, label %21, label %24
21: ; preds = %13
%22 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%23 = call i32 @exit(i32 1)
unreachable
24: ; preds = %13
%25 = load i8*, i8** %7
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i8* %25)
%27 = call i32 @system(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
%28 = call i32 @sleep(i32 3)
%29 = load i8*, i8** %7
%30 = call i64 @shmdt(i8* %29)
%31 = icmp slt i64 %30, 0
br i1 %31, label %32, label %35
32: ; preds = %24
%33 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
%34 = call i32 @exit(i32 1)
unreachable
35: ; preds = %24
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.5, i64 0, i64 0))
%37 = call i32 @system(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.6, i64 0, i64 0))
%38 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@.str = [26 x i8] c"USAGE: atshm <identifier>\00"
@.str.2 = [25 x i8] c" segment attached at %p\0A\00"
@.str.3 = [8 x i8] c"ipcs -m\00"
@.str.4 = [6 x i8] c"shmdt\00"
@.str.6 = [9 x i8] c"ipcs -m \00"
@str = [18 x i8] c"segment detached \00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 2
br i1 %3, label %7, label %4
4: ; preds = %2
%5 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
%6 = tail call i32 @exit(i32 1)
unreachable
7: ; preds = %2
%8 = getelementptr inbounds i8*, i8** %1, i64 1
%9 = load i8*, i8** %8
%10 = tail call i32 @atoi(i8* %9)
%11 = tail call i8* @shmat(i32 %10, i32 0, i32 0)
%12 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i8* %11)
%13 = tail call i32 @system(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
%14 = tail call i32 @sleep(i32 3)
%15 = tail call i64 @shmdt(i8* %11)
%16 = icmp slt i64 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %7
%18 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
%19 = tail call i32 @exit(i32 1)
unreachable
20: ; preds = %7
%21 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str, i64 0, i64 0))
%22 = tail call i32 @system(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.6, i64 0, i64 0))
%23 = tail call i32 @exit(i32 0)
unreachable
}