#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 */
typedef int /*<<< orphan*/ key_t;

/* Variables and functions */
int /*<<< orphan*/ EXIT_SUCCESS;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fgets(void *, int, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int semget(int /*<<< orphan*/, int, int /*<<< orphan*/);
int /*<<< orphan*/ set_sem(int, int);
void *shmat(int, void *, int /*<<< orphan*/);
int shmdt(void *);
int shmget(int /*<<< orphan*/, int, int /*<<< orphan*/);
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ usleep(int);
void send_it(void) {
  int shmid, semid, i;
  void *shm_area;
  if ((shmid = shmget((key_t)26, 8196, 0)) == -1) {
    perror("shmget");
    exit(errno);
  };
  if ((shm_area = shmat(shmid, (void *)0, 0)) == (void *)-1) {
    perror("shmat");
    exit(errno);
  };
  printf("SEND: Memory attached at %lX\n", (unsigned long)shm_area);
  if ((semid = semget((key_t)26, 1, 0)) == -1) {
    perror("semget");
    exit(errno);
  };
  for (i = 0; i < 4; i++) {
    set_sem(semid, -1);
    printf("Please type a message:\n");
    fgets(shm_area, 8196, stdin);
    set_sem(semid, 1);
    usleep(1000);
  }
  if (shmdt(shm_area) == -1) {
    perror("shmdt");
    exit(errno);
  };
  printf("SENDND has successfully completed\n");
  exit(EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<send_it>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    $0x1a,%edi
mov    $0x2004,%esi
xor    %edx,%edx
call   10a0 <shmget@plt>
mov    %eax,-0x4(%rbp)
cmp    $0xffffffff,%eax
jne    11df <send_it+0x3f>
lea    0xe34(%rip),%rdi
call   1080 <perror@plt>
mov    0x2e00(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    -0x4(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1090 <shmat@plt>
mov    %rax,-0x18(%rbp)
mov    $0xffffffffffffffff,%rcx
cmp    %rcx,%rax
jne    121b <send_it+0x7b>
lea    0xdff(%rip),%rdi
call   1080 <perror@plt>
mov    0x2dc4(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    -0x18(%rbp),%rsi
lea    0xde7(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0x1a,%edi
mov    $0x1,%esi
xor    %edx,%edx
call   1030 <semget@plt>
mov    %eax,-0x8(%rbp)
cmp    $0xffffffff,%eax
jne    1264 <send_it+0xc4>
lea    0xdda(%rip),%rdi
call   1080 <perror@plt>
mov    0x2d7b(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
movl   $0x0,-0xc(%rbp)
cmpl   $0x4,-0xc(%rbp)
jge    12cc <send_it+0x12c>
mov    -0x8(%rbp),%edi
mov    $0xffffffff,%esi
call   1050 <set_sem@plt>
lea    0xda9(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x18(%rbp),%rdi
mov    0x2d4d(%rip),%rax
mov    (%rax),%edx
mov    $0x2004,%esi
call   1070 <fgets@plt>
mov    -0x8(%rbp),%edi
mov    $0x1,%esi
call   1050 <set_sem@plt>
mov    $0x3e8,%edi
call   10c0 <usleep@plt>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    126b <send_it+0xcb>
mov    -0x18(%rbp),%rdi
call   1040 <shmdt@plt>
cmp    $0xffffffff,%eax
jne    12f8 <send_it+0x158>
lea    0xd65(%rip),%rdi
call   1080 <perror@plt>
mov    0x2ce7(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
lea    0xd51(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    0x2cc3(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>

x86 assembly (clang, -O2):
<send_it>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    $0x1a,%edi
mov    $0x2004,%esi
xor    %edx,%edx
call   10b0 <shmget@plt>
cmp    $0xffffffff,%eax
jne    11d9 <send_it+0x29>
lea    0xe2c(%rip),%rdi
jmp    1338 <send_it+0x188>
mov    %eax,%edi
xor    %esi,%esi
xor    %edx,%edx
call   10a0 <shmat@plt>
cmp    $0xffffffffffffffff,%rax
jne    11f6 <send_it+0x46>
lea    0xe16(%rip),%rdi
jmp    1338 <send_it+0x188>
mov    %rax,%rbx
lea    0xe0d(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
mov    $0x1a,%edi
mov    $0x1,%esi
xor    %edx,%edx
call   1030 <semget@plt>
cmp    $0xffffffff,%eax
jne    122c <send_it+0x7c>
lea    0xe04(%rip),%rdi
jmp    1338 <send_it+0x188>
mov    %eax,%ebp
mov    %eax,%edi
mov    $0xffffffff,%esi
call   1060 <set_sem@plt>
lea    0xe19(%rip),%r14
mov    %r14,%rdi
call   1040 <puts@plt>
mov    0x2d98(%rip),%r15
mov    (%r15),%edx
mov    %rbx,%rdi
mov    $0x2004,%esi
call   1080 <fgets@plt>
mov    %ebp,%edi
mov    $0x1,%esi
call   1060 <set_sem@plt>
mov    $0x3e8,%edi
call   10d0 <usleep@plt>
mov    %ebp,%edi
mov    $0xffffffff,%esi
call   1060 <set_sem@plt>
mov    %r14,%rdi
call   1040 <puts@plt>
mov    (%r15),%edx
mov    %rbx,%rdi
mov    $0x2004,%esi
call   1080 <fgets@plt>
mov    %ebp,%edi
mov    $0x1,%esi
call   1060 <set_sem@plt>
mov    $0x3e8,%edi
call   10d0 <usleep@plt>
mov    %ebp,%edi
mov    $0xffffffff,%esi
call   1060 <set_sem@plt>
mov    %r14,%rdi
call   1040 <puts@plt>
mov    (%r15),%edx
mov    %rbx,%rdi
mov    $0x2004,%esi
call   1080 <fgets@plt>
mov    %ebp,%edi
mov    $0x1,%esi
call   1060 <set_sem@plt>
mov    $0x3e8,%edi
call   10d0 <usleep@plt>
mov    %ebp,%edi
mov    $0xffffffff,%esi
call   1060 <set_sem@plt>
mov    %r14,%rdi
call   1040 <puts@plt>
mov    (%r15),%edx
mov    %rbx,%rdi
mov    $0x2004,%esi
call   1080 <fgets@plt>
mov    %ebp,%edi
mov    $0x1,%esi
call   1060 <set_sem@plt>
mov    $0x3e8,%edi
call   10d0 <usleep@plt>
mov    %rbx,%rdi
call   1050 <shmdt@plt>
cmp    $0xffffffff,%eax
jne    134b <send_it+0x19b>
lea    0xcfa(%rip),%rdi
call   1090 <perror@plt>
mov    0x2c94(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
lea    0xce6(%rip),%rdi
call   1040 <puts@plt>
mov    0x2c72(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
@.str = [7 x i8] c"shmget\00"
@errno = global i32 0
@.str.1 = [6 x i8] c"shmat\00"
@.str.2 = [30 x i8] c"SEND: Memory attached at %lX\0A\00"
@.str.3 = [7 x i8] c"semget\00"
@.str.4 = [24 x i8] c"Please type a message:\0A\00"
@stdin = global i32 0
@.str.5 = [6 x i8] c"shmdt\00"
@.str.6 = [35 x i8] c"SENDND has successfully completed\0A\00"
@EXIT_SUCCESS = global i32 0
define void @send_it() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i8*
%5 = call i32 @shmget(i32 26, i32 8196, i32 0)
store i32 %5, i32* %1
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %11
7: ; preds = %0
%8 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%9 = load i32, i32* @errno
%10 = call i32 @exit(i32 %9)
unreachable
11: ; preds = %0
%12 = load i32, i32* %1
%13 = call i8* @shmat(i32 %12, i8* null, i32 0)
store i8* %13, i8** %4
%14 = icmp eq i8* %13, inttoptr (i64 -1 to i8*)
br i1 %14, label %15, label %19
15: ; preds = %11
%16 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%17 = load i32, i32* @errno
%18 = call i32 @exit(i32 %17)
unreachable
19: ; preds = %11
%20 = load i8*, i8** %4
%21 = ptrtoint i8* %20 to i64
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), i64 %21)
%23 = call i32 @semget(i32 26, i32 1, i32 0)
store i32 %23, i32* %2
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %29
25: ; preds = %19
%26 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%27 = load i32, i32* @errno
%28 = call i32 @exit(i32 %27)
unreachable
29: ; preds = %19
store i32 0, i32* %3
br label %30
30: ; preds = %43, %29
%31 = load i32, i32* %3
%32 = icmp slt i32 %31, 4
br i1 %32, label %33, label %46
33: ; preds = %30
%34 = load i32, i32* %2
%35 = call i32 @set_sem(i32 %34, i32 -1)
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.4, i64 0, i64 0))
%37 = load i8*, i8** %4
%38 = load i32, i32* @stdin
%39 = call i32 @fgets(i8* %37, i32 8196, i32 %38)
%40 = load i32, i32* %2
%41 = call i32 @set_sem(i32 %40, i32 1)
%42 = call i32 @usleep(i32 1000)
br label %43
43: ; preds = %33
%44 = load i32, i32* %3
%45 = add nsw i32 %44, 1
store i32 %45, i32* %3
br label %30
46: ; preds = %30
%47 = load i8*, i8** %4
%48 = call i32 @shmdt(i8* %47)
%49 = icmp eq i32 %48, -1
br i1 %49, label %50, label %54
50: ; preds = %46
%51 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
%52 = load i32, i32* @errno
%53 = call i32 @exit(i32 %52)
unreachable
54: ; preds = %46
%55 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.6, i64 0, i64 0))
%56 = load i32, i32* @EXIT_SUCCESS
%57 = call i32 @exit(i32 %56)
unreachable
}

LLVM IR (-O2):
@.str = [7 x i8] c"shmget\00"
@errno = global i32 0
@.str.1 = [6 x i8] c"shmat\00"
@.str.2 = [30 x i8] c"SEND: Memory attached at %lX\0A\00"
@.str.3 = [7 x i8] c"semget\00"
@stdin = global i32 0
@.str.5 = [6 x i8] c"shmdt\00"
@EXIT_SUCCESS = global i32 0
@str = [34 x i8] c"SENDND has successfully completed\00"
@str.7 = [23 x i8] c"Please type a message:\00"
define void @send_it() {
%1 = tail call i32 @shmget(i32 26, i32 8196, i32 0)
%2 = icmp eq i32 %1, -1
br i1 %2, label %3, label %7
3: ; preds = %0
%4 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%5 = load i32, i32* @errno
%6 = tail call i32 @exit(i32 %5)
unreachable
7: ; preds = %0
%8 = tail call i8* @shmat(i32 %1, i8* null, i32 0)
%9 = icmp eq i8* %8, inttoptr (i64 -1 to i8*)
br i1 %9, label %10, label %14
10: ; preds = %7
%11 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%12 = load i32, i32* @errno
%13 = tail call i32 @exit(i32 %12)
unreachable
14: ; preds = %7
%15 = ptrtoint i8* %8 to i64
%16 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), i64 %15)
%17 = tail call i32 @semget(i32 26, i32 1, i32 0)
%18 = icmp eq i32 %17, -1
br i1 %18, label %46, label %19
19: ; preds = %14
%20 = tail call i32 @set_sem(i32 %17, i32 -1)
%21 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str.7, i64 0, i64 0))
%22 = load i32, i32* @stdin
%23 = tail call i32 @fgets(i8* %8, i32 8196, i32 %22)
%24 = tail call i32 @set_sem(i32 %17, i32 1)
%25 = tail call i32 @usleep(i32 1000)
%26 = tail call i32 @set_sem(i32 %17, i32 -1)
%27 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str.7, i64 0, i64 0))
%28 = load i32, i32* @stdin
%29 = tail call i32 @fgets(i8* %8, i32 8196, i32 %28)
%30 = tail call i32 @set_sem(i32 %17, i32 1)
%31 = tail call i32 @usleep(i32 1000)
%32 = tail call i32 @set_sem(i32 %17, i32 -1)
%33 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str.7, i64 0, i64 0))
%34 = load i32, i32* @stdin
%35 = tail call i32 @fgets(i8* %8, i32 8196, i32 %34)
%36 = tail call i32 @set_sem(i32 %17, i32 1)
%37 = tail call i32 @usleep(i32 1000)
%38 = tail call i32 @set_sem(i32 %17, i32 -1)
%39 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str.7, i64 0, i64 0))
%40 = load i32, i32* @stdin
%41 = tail call i32 @fgets(i8* %8, i32 8196, i32 %40)
%42 = tail call i32 @set_sem(i32 %17, i32 1)
%43 = tail call i32 @usleep(i32 1000)
%44 = tail call i32 @shmdt(i8* %8)
%45 = icmp eq i32 %44, -1
br i1 %45, label %50, label %54
46: ; preds = %14
%47 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%48 = load i32, i32* @errno
%49 = tail call i32 @exit(i32 %48)
unreachable
50: ; preds = %19
%51 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
%52 = load i32, i32* @errno
%53 = tail call i32 @exit(i32 %52)
unreachable
54: ; preds = %19
%55 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @str, i64 0, i64 0))
%56 = load i32, i32* @EXIT_SUCCESS
%57 = tail call i32 @exit(i32 %56)
unreachable
}