#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 EXIT_FAILURE;
int IPC_CREAT;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ memcpy(char *, char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
char *shmat(int, int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ shmdt(char *);
int shmget(int, int, int);
int /*<<< orphan*/ strlen(char *);
int main(int argc, char *argv[]) {
  printf("Program 1: input ---> shm1\n");
  FILE *fp;
  int shmid1;
  int linecountid;
  key_t key = 1234;
  key_t key2 = 5554;
  char *shm1;
  char *linecount;
  char *s;
  linecountid = shmget(key2, 500, 0666);
  if (linecountid < 0) {
    perror("shmget");
    exit(1);
  }
  linecount = shmat(linecountid, NULL, 0);
  if (linecount == (char *)-1) {
    perror("shmat");
    exit(1);
  }
  shmid1 = shmget(key, 500, IPC_CREAT | 0666);
  if (shmid1 < 0) {
    perror("shmget");
    exit(1);
  }
  shm1 = shmat(shmid1, NULL, 0);
  if (shm1 == (char *)-1) {
    perror("shmat");
    exit(1);
  }
  fp = fopen("input.txt", "r");
  if (fp == NULL) {
    perror(" porg 1 : Can not open file !!");
    exit(EXIT_FAILURE);
  }
  int line = 1;
  int found = 0;
  char liner[400];
  int linesearch = *linecount;
  while (fgets(liner, 40, fp)) {
    if (linesearch == line) {
      found = 1;
      memcpy(shm1, liner, strlen(liner));
    }
    line++;
  }
  if (found == 0) {
    printf("Linie nummer %d findes desvaerre ikke!", linesearch);
  }
  line = line - 1;
  fclose(fp);
  shmdt(shm1);
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x200,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe30(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x4d2,-0x24(%rbp)
movl   $0x15b2,-0x28(%rbp)
mov    -0x28(%rbp),%edi
mov    $0x1f4,%esi
mov    $0x1b6,%edx
call   10c0 <shmget@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    121a <main+0x6a>
lea    0xe11(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    -0x20(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   10b0 <shmat@plt>
mov    %rax,-0x38(%rbp)
mov    $0xffffffffffffffff,%rax
cmp    %rax,-0x38(%rbp)
jne    1253 <main+0xa3>
lea    0xddf(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    -0x24(%rbp),%edi
mov    0x2d7b(%rip),%rax
mov    (%rax),%edx
or     $0x1b6,%edx
mov    $0x1f4,%esi
call   10c0 <shmget@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1292 <main+0xe2>
lea    0xd99(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    -0x1c(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   10b0 <shmat@plt>
mov    %rax,-0x30(%rbp)
mov    $0xffffffffffffffff,%rax
cmp    %rax,-0x30(%rbp)
jne    12cb <main+0x11b>
lea    0xd67(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
lea    0xd57(%rip),%rdi
lea    0xd5a(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1307 <main+0x157>
lea    0xd41(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2ce0(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
movl   $0x1,-0x44(%rbp)
movl   $0x0,-0x48(%rbp)
mov    -0x38(%rbp),%rax
movsbl (%rax),%eax
mov    %eax,-0x1e4(%rbp)
lea    -0x1e0(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    $0x28,%esi
call   1070 <fgets@plt>
cmp    $0x0,%rax
je     139f <main+0x1ef>
mov    -0x1e4(%rbp),%eax
cmp    -0x44(%rbp),%eax
jne    1391 <main+0x1e1>
movl   $0x1,-0x48(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x1f8(%rbp)
lea    -0x1e0(%rbp),%rax
mov    %rax,-0x1f0(%rbp)
lea    -0x1e0(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x1f8(%rbp),%rdi
mov    -0x1f0(%rbp),%rsi
mov    %eax,%edx
call   1080 <memcpy@plt>
mov    -0x44(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x44(%rbp)
jmp    1322 <main+0x172>
cmpl   $0x0,-0x48(%rbp)
jne    13bd <main+0x20d>
mov    -0x1e4(%rbp),%esi
lea    0xc9e(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x44(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x44(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x30(%rbp),%rdi
call   1030 <shmdt@plt>
xor    %edi,%edi
call   10d0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x198,%rsp
lea    0xe87(%rip),%rdi
call   1030 <puts@plt>
mov    $0x15b2,%edi
mov    $0x1f4,%esi
mov    $0x1b6,%edx
call   10d0 <shmget@plt>
test   %eax,%eax
js     122a <main+0x6a>
mov    %eax,%edi
xor    %esi,%esi
xor    %edx,%edx
call   10c0 <shmat@plt>
cmp    $0xffffffffffffffff,%rax
je     1244 <main+0x84>
mov    %rax,%r15
mov    0x2dc8(%rip),%rax
mov    $0x1b6,%edx
or     (%rax),%edx
mov    $0x4d2,%edi
mov    $0x1f4,%esi
call   10d0 <shmget@plt>
test   %eax,%eax
jns    1233 <main+0x73>
lea    0xdcf(%rip),%rdi
jmp    124b <main+0x8b>
mov    %eax,%edi
xor    %esi,%esi
xor    %edx,%edx
call   10c0 <shmat@plt>
cmp    $0xffffffffffffffff,%rax
jne    125a <main+0x9a>
lea    0xdbc(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
mov    %rax,%r14
lea    0xda9(%rip),%rdi
lea    0xdac(%rip),%rsi
call   10a0 <fopen@plt>
test   %rax,%rax
jne    128f <main+0xcf>
lea    0xd9d(%rip),%rdi
call   10b0 <perror@plt>
mov    0x2d58(%rip),%rax
mov    (%rax),%edi
call   10e0 <exit@plt>
mov    %rax,%r12
movsbl (%r15),%r15d
mov    %rsp,%rdi
mov    $0x28,%esi
mov    %rax,%rdx
call   1080 <fgets@plt>
test   %rax,%rax
je     12fc <main+0x13c>
lea    -0x1(%r15),%ebp
xor    %r13d,%r13d
mov    %rsp,%rbx
jmp    12d5 <main+0x115>
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    $0x28,%esi
mov    %r12,%rdx
call   1080 <fgets@plt>
test   %rax,%rax
je     12f7 <main+0x137>
sub    $0x1,%ebp
jae    12c0 <main+0x100>
mov    %rbx,%rdi
call   1060 <strlen@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %eax,%edx
call   1090 <memcpy@plt>
mov    $0x1,%r13d
jmp    12c0 <main+0x100>
test   %r13d,%r13d
jne    130d <main+0x14d>
lea    0xd35(%rip),%rdi
mov    %r15d,%esi
xor    %eax,%eax
call   1070 <printf@plt>
mov    %r12,%rdi
call   1050 <fclose@plt>
mov    %r14,%rdi
call   1040 <shmdt@plt>
xor    %edi,%edi
call   10e0 <exit@plt>

LLVM IR (-O0):
@.str = [28 x i8] c"Program 1: input ---> shm1\0A\00"
@.str.1 = [7 x i8] c"shmget\00"
@.str.2 = [6 x i8] c"shmat\00"
@IPC_CREAT = global i32 0
@.str.3 = [10 x i8] c"input.txt\00"
@.str.4 = [2 x i8] c"r\00"
@.str.5 = [31 x i8] c" porg 1 : Can not open file !!\00"
@EXIT_FAILURE = global i32 0
@.str.6 = [39 x i8] c"Linie nummer %d findes desvaerre ikke!\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 i8*
%12 = alloca i8*
%13 = alloca i8*
%14 = alloca i32
%15 = alloca i32
%16 = alloca [400 x i8]
%17 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
store i32 1234, i32* %9
store i32 5554, i32* %10
%19 = load i32, i32* %10
%20 = call i32 @shmget(i32 %19, i32 500, i32 438)
store i32 %20, i32* %8
%21 = load i32, i32* %8
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %2
%24 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%25 = call i32 @exit(i32 1)
unreachable
26: ; preds = %2
%27 = load i32, i32* %8
%28 = call i8* @shmat(i32 %27, i32* null, i32 0)
store i8* %28, i8** %12
%29 = load i8*, i8** %12
%30 = icmp eq i8* %29, inttoptr (i64 -1 to i8*)
br i1 %30, label %31, label %34
31: ; preds = %26
%32 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%33 = call i32 @exit(i32 1)
unreachable
34: ; preds = %26
%35 = load i32, i32* %9
%36 = load i32, i32* @IPC_CREAT
%37 = or i32 %36, 438
%38 = call i32 @shmget(i32 %35, i32 500, i32 %37)
store i32 %38, i32* %7
%39 = load i32, i32* %7
%40 = icmp slt i32 %39, 0
br i1 %40, label %41, label %44
41: ; preds = %34
%42 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%43 = call i32 @exit(i32 1)
unreachable
44: ; preds = %34
%45 = load i32, i32* %7
%46 = call i8* @shmat(i32 %45, i32* null, i32 0)
store i8* %46, i8** %11
%47 = load i8*, i8** %11
%48 = icmp eq i8* %47, inttoptr (i64 -1 to i8*)
br i1 %48, label %49, label %52
49: ; preds = %44
%50 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%51 = call i32 @exit(i32 1)
unreachable
52: ; preds = %44
%53 = call i32* @fopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
store i32* %53, i32** %6
%54 = load i32*, i32** %6
%55 = icmp eq i32* %54, null
br i1 %55, label %56, label %60
56: ; preds = %52
%57 = call i32 @perror(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.5, i64 0, i64 0))
%58 = load i32, i32* @EXIT_FAILURE
%59 = call i32 @exit(i32 %58)
unreachable
60: ; preds = %52
store i32 1, i32* %14
store i32 0, i32* %15
%61 = load i8*, i8** %12
%62 = load i8, i8* %61
%63 = sext i8 %62 to i32
store i32 %63, i32* %17
br label %64
64: ; preds = %79, %60
%65 = getelementptr inbounds [400 x i8], [400 x i8]* %16, i64 0, i64 0
%66 = load i32*, i32** %6
%67 = call i64 @fgets(i8* %65, i32 40, i32* %66)
%68 = icmp ne i64 %67, 0
br i1 %68, label %69, label %82
69: ; preds = %64
%70 = load i32, i32* %17
%71 = load i32, i32* %14
%72 = icmp eq i32 %70, %71
br i1 %72, label %73, label %79
73: ; preds = %69
store i32 1, i32* %15
%74 = load i8*, i8** %11
%75 = getelementptr inbounds [400 x i8], [400 x i8]* %16, i64 0, i64 0
%76 = getelementptr inbounds [400 x i8], [400 x i8]* %16, i64 0, i64 0
%77 = call i32 @strlen(i8* %76)
%78 = call i32 @memcpy(i8* %74, i8* %75, i32 %77)
br label %79
79: ; preds = %73, %69
%80 = load i32, i32* %14
%81 = add nsw i32 %80, 1
store i32 %81, i32* %14
br label %64
82: ; preds = %64
%83 = load i32, i32* %15
%84 = icmp eq i32 %83, 0
br i1 %84, label %85, label %88
85: ; preds = %82
%86 = load i32, i32* %17
%87 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.6, i64 0, i64 0), i32 %86)
br label %88
88: ; preds = %85, %82
%89 = load i32, i32* %14
%90 = sub nsw i32 %89, 1
store i32 %90, i32* %14
%91 = load i32*, i32** %6
%92 = call i32 @fclose(i32* %91)
%93 = load i8*, i8** %11
%94 = call i32 @shmdt(i8* %93)
%95 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@.str.1 = [7 x i8] c"shmget\00"
@.str.2 = [6 x i8] c"shmat\00"
@IPC_CREAT = global i32 0
@.str.3 = [10 x i8] c"input.txt\00"
@.str.4 = [2 x i8] c"r\00"
@.str.5 = [31 x i8] c" porg 1 : Can not open file !!\00"
@EXIT_FAILURE = global i32 0
@.str.6 = [39 x i8] c"Linie nummer %d findes desvaerre ikke!\00"
@str = [27 x i8] c"Program 1: input ---> shm1\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [400 x i8]
%4 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str, i64 0, i64 0))
%5 = tail call i32 @shmget(i32 5554, 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.1, 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.2, i64 0, i64 0))
%15 = tail call i32 @exit(i32 1)
unreachable
16: ; preds = %10
%17 = load i32, i32* @IPC_CREAT
%18 = or i32 %17, 438
%19 = tail call i32 @shmget(i32 1234, i32 500, i32 %18)
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %16
%22 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%23 = tail call i32 @exit(i32 1)
unreachable
24: ; preds = %16
%25 = tail call i8* @shmat(i32 %19, i32* null, i32 0)
%26 = icmp eq i8* %25, inttoptr (i64 -1 to i8*)
br i1 %26, label %27, label %30
27: ; preds = %24
%28 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%29 = tail call i32 @exit(i32 1)
unreachable
30: ; preds = %24
%31 = tail call i32* @fopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%32 = icmp eq i32* %31, null
br i1 %32, label %33, label %37
33: ; preds = %30
%34 = tail call i32 @perror(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.5, i64 0, i64 0))
%35 = load i32, i32* @EXIT_FAILURE
%36 = tail call i32 @exit(i32 %35)
unreachable
37: ; preds = %30
%38 = getelementptr inbounds [400 x i8], [400 x i8]* %3, i64 0, i64 0
%39 = load i8, i8* %11
%40 = sext i8 %39 to i32
%41 = call i64 @fgets(i8* %38, i32 40, i32* %31)
%42 = icmp eq i64 %41, 0
br i1 %42, label %57, label %43
43: ; preds = %37, %50
%44 = phi i32 [ %52, %50 ], [ 1, %37 ]
%45 = phi i32 [ %51, %50 ], [ 0, %37 ]
%46 = icmp eq i32 %44, %40
br i1 %46, label %47, label %50
47: ; preds = %43
%48 = call i32 @strlen(i8* %38)
%49 = call i32 @memcpy(i8* %25, i8* %38, i32 %48)
br label %50
50: ; preds = %47, %43
%51 = phi i32 [ 1, %47 ], [ %45, %43 ]
%52 = add nuw nsw i32 %44, 1
%53 = call i64 @fgets(i8* %38, i32 40, i32* %31)
%54 = icmp eq i64 %53, 0
br i1 %54, label %55, label %43
55: ; preds = %50
%56 = icmp eq i32 %51, 0
br i1 %56, label %57, label %59
57: ; preds = %37, %55
%58 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([39 x i8], [39 x i8]* @.str.6, i64 0, i64 0), i32 %40)
br label %59
59: ; preds = %57, %55
%60 = call i32 @fclose(i32* %31)
%61 = call i32 @shmdt(i8* %25)
%62 = call i32 @exit(i32 0)
unreachable
}