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

/* Variables and functions */
int /*<<< orphan*/ IPC_RMID;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
char *shmat(int, int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ shmctl(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ shmdt(char *);
int shmget(int, int, int);
int main(int argc, char *argv[]) {
  printf("Program 3: shm2 ---> sink ---> output fil\n");
  printf("\n");
  FILE *fp;
  int shmid2;
  key_t key2;
  char *shm2;
  char *s;
  key2 = 4321;
  shmid2 = shmget(key2, 500, 0666);
  if (shmid2 < 0) {
    perror("shmget");
    exit(1);
  }
  shm2 = shmat(shmid2, NULL, 0);
  if (shm2 == (char *)-1) {
    perror("shmat");
    exit(1);
  }
  fp = fopen("output.txt", "a+");
  fputs(shm2, fp);
  fclose(fp);
  shmctl(shmid2, IPC_RMID, 0);
  shmdt(shm2);
  exit(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)
lea    0xe43(%rip),%rdi
call   1050 <printf@plt>
lea    0xe60(%rip),%rdi
call   1050 <printf@plt>
movl   $0x10e1,-0x20(%rbp)
mov    -0x20(%rbp),%edi
mov    $0x1f4,%esi
mov    $0x1b6,%edx
call   10b0 <shmget@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    120a <main+0x6a>
lea    0xe30(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x1c(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   10a0 <shmat@plt>
mov    %rax,-0x28(%rbp)
mov    $0xffffffffffffffff,%rax
cmp    %rax,-0x28(%rbp)
jne    1243 <main+0xa3>
lea    0xdfe(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
lea    0xdee(%rip),%rdi
lea    0xdf2(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x18(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1060 <fputs@plt>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x1c(%rbp),%edi
mov    0x2d66(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <shmctl@plt>
mov    -0x28(%rbp),%rdi
call   1030 <shmdt@plt>
xor    %edi,%edi
call   10c0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
lea    0xe5f(%rip),%rdi
call   1040 <puts@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    $0x10e1,%edi
mov    $0x1f4,%esi
mov    $0x1b6,%edx
call   10c0 <shmget@plt>
test   %eax,%eax
jns    11ec <main+0x3c>
lea    0xe16(%rip),%rdi
jmp    1206 <main+0x56>
mov    %eax,%ebx
mov    %eax,%edi
xor    %esi,%esi
xor    %edx,%edx
call   10b0 <shmat@plt>
cmp    $0xffffffffffffffff,%rax
jne    1215 <main+0x65>
lea    0xe01(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    %rax,%r14
lea    0xdee(%rip),%rdi
lea    0xdf2(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,%r15
mov    %r14,%rdi
mov    %rax,%rsi
call   1070 <fputs@plt>
mov    %r15,%rdi
call   1060 <fclose@plt>
mov    0x2d98(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
xor    %edx,%edx
call   1080 <shmctl@plt>
mov    %r14,%rdi
call   1050 <shmdt@plt>
xor    %edi,%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
@.str = [43 x i8] c"Program 3: shm2 ---> sink ---> output fil\0A\00"
@.str.1 = [2 x i8] c"\0A\00"
@.str.2 = [7 x i8] c"shmget\00"
@.str.3 = [6 x i8] c"shmat\00"
@.str.4 = [11 x i8] c"output.txt\00"
@.str.5 = [3 x i8] c"a+\00"
@IPC_RMID = global i32 0
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 i8*
%10 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = call i32 @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0))
%12 = call i32 @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32 4321, i32* %8
%13 = load i32, i32* %8
%14 = call i32 @shmget(i32 %13, i32 500, i32 438)
store i32 %14, i32* %7
%15 = load i32, i32* %7
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %2
%18 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%19 = call i32 @exit(i32 1)
unreachable
20: ; preds = %2
%21 = load i32, i32* %7
%22 = call i8* @shmat(i32 %21, i32* null, i32 0)
store i8* %22, i8** %9
%23 = load i8*, i8** %9
%24 = icmp eq i8* %23, inttoptr (i64 -1 to i8*)
br i1 %24, label %25, label %28
25: ; preds = %20
%26 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%27 = call i32 @exit(i32 1)
unreachable
28: ; preds = %20
%29 = call i32* @fopen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0))
store i32* %29, i32** %6
%30 = load i8*, i8** %9
%31 = load i32*, i32** %6
%32 = call i32 @fputs(i8* %30, i32* %31)
%33 = load i32*, i32** %6
%34 = call i32 @fclose(i32* %33)
%35 = load i32, i32* %7
%36 = load i32, i32* @IPC_RMID
%37 = call i32 @shmctl(i32 %35, i32 %36, i32 0)
%38 = load i8*, i8** %9
%39 = call i32 @shmdt(i8* %38)
%40 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@.str.2 = [7 x i8] c"shmget\00"
@.str.3 = [6 x i8] c"shmat\00"
@.str.4 = [11 x i8] c"output.txt\00"
@.str.5 = [3 x i8] c"a+\00"
@IPC_RMID = global i32 0
@str = [42 x i8] c"Program 3: shm2 ---> sink ---> output fil\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([42 x i8], [42 x i8]* @str, i64 0, i64 0))
%4 = tail call i32 @putchar(i32 10)
%5 = tail call i32 @shmget(i32 4321, i32 500, i32 438)
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %10
7: ; preds = %2
%8 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%9 = tail call i32 @exit(i32 1)
unreachable
10: ; preds = %2
%11 = tail call i8* @shmat(i32 %5, i32* null, i32 0)
%12 = icmp eq i8* %11, inttoptr (i64 -1 to i8*)
br i1 %12, label %13, label %16
13: ; preds = %10
%14 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%15 = tail call i32 @exit(i32 1)
unreachable
16: ; preds = %10
%17 = tail call i32* @fopen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0))
%18 = tail call i32 @fputs(i8* %11, i32* %17)
%19 = tail call i32 @fclose(i32* %17)
%20 = load i32, i32* @IPC_RMID
%21 = tail call i32 @shmctl(i32 %5, i32 %20, i32 0)
%22 = tail call i32 @shmdt(i8* %11)
%23 = tail call i32 @exit(i32 0)
unreachable
}