#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;

/* Variables and functions */
int IPC_CREAT;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
char *shmat(int, int /*<<< orphan*/ *, int /*<<< orphan*/);
int shmget(int, int, int);
int tun_open(char *);
int write(int, char *, int);
int main(int argc, char *argv[]) {
  int fd, nbytes;
  char *buf;
  char c;
  int shmid;
  key_t key = 5678;
  if ((shmid = shmget(key, 100, IPC_CREAT | 0666)) < 0) {
    perror("shmget");
    exit(1);
  }
  if ((buf = shmat(shmid, NULL, 0)) == (char *)-1) {
    perror("shmat");
    exit(1);
  }
  fd = tun_open("asa0");
  printf("Device asa1 opened\n");
  while (1) {
    while (*(buf + 84) != 1)
      ;
    nbytes = write(fd, buf, 100);
    nbytes = 0;
    nbytes = read(fd, buf, 100);
    printf("Read %d bytes from asa1\n", nbytes);
    *(buf + 84) = '2';
  }
  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   $0x162e,-0x2c(%rbp)
mov    -0x2c(%rbp),%edi
mov    0x2e39(%rip),%rax
mov    (%rax),%edx
or     $0x1b6,%edx
mov    $0x64,%esi
call   1090 <shmget@plt>
mov    %eax,-0x28(%rbp)
cmp    $0x0,%eax
jge    11db <main+0x5b>
lea    0xe34(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x28(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1080 <shmat@plt>
mov    %rax,-0x20(%rbp)
mov    $0xffffffffffffffff,%rcx
cmp    %rcx,%rax
jne    1213 <main+0x93>
lea    0xe03(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
lea    0xdf3(%rip),%rdi
call   1060 <tun_open@plt>
mov    %eax,-0x14(%rbp)
lea    0xde9(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1235 <main+0xb5>
mov    -0x20(%rbp),%rax
movsbl 0x54(%rax),%eax
cmp    $0x1,%eax
je     124b <main+0xcb>
jmp    1235 <main+0xb5>
mov    -0x14(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    $0x64,%edx
call   1030 <write@plt>
mov    %eax,-0x18(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x14(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    $0x64,%edx
call   1050 <read@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%esi
lea    0xda2(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x20(%rbp),%rax
movb   $0x32,0x54(%rax)
jmp    1230 <main+0xb0>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
mov    0x2e46(%rip),%rax
mov    $0x1b6,%edx
or     (%rax),%edx
mov    $0x162e,%edi
mov    $0x64,%esi
call   10a0 <shmget@plt>
test   %eax,%eax
js     1222 <main+0x92>
mov    %eax,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1090 <shmat@plt>
cmp    $0xffffffffffffffff,%rax
je     122b <main+0x9b>
mov    %rax,%rbx
lea    0xe3e(%rip),%rdi
call   1070 <tun_open@plt>
mov    %eax,%ebp
lea    0xe4e(%rip),%rdi
call   1030 <puts@plt>
cmpb   $0x1,0x54(%rbx)
jne    1220 <main+0x90>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x64,%edx
call   1040 <write@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x64,%edx
call   1060 <read@plt>
lea    0xe05(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
movb   $0x32,0x54(%rbx)
nopw   0x0(%rax,%rax,1)
jmp    1220 <main+0x90>
lea    0xdd7(%rip),%rdi
jmp    1232 <main+0xa2>
lea    0xdd5(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@IPC_CREAT = global i32 0
@.str = [7 x i8] c"shmget\00"
@.str.1 = [6 x i8] c"shmat\00"
@.str.2 = [5 x i8] c"asa0\00"
@.str.3 = [20 x i8] c"Device asa1 opened\0A\00"
@.str.4 = [25 x i8] c"Read %d bytes from asa1\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 i8*
%9 = alloca i8
%10 = alloca i32
%11 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 5678, i32* %11
%12 = load i32, i32* %11
%13 = load i32, i32* @IPC_CREAT
%14 = or i32 %13, 438
%15 = call i32 @shmget(i32 %12, i32 100, i32 %14)
store i32 %15, i32* %10
%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, i64 0, i64 0))
%19 = call i32 @exit(i32 1)
unreachable
20: ; preds = %2
%21 = load i32, i32* %10
%22 = call i8* @shmat(i32 %21, i32* null, i32 0)
store i8* %22, i8** %8
%23 = icmp eq i8* %22, inttoptr (i64 -1 to i8*)
br i1 %23, label %24, label %27
24: ; preds = %20
%25 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%26 = call i32 @exit(i32 1)
unreachable
27: ; preds = %20
%28 = call i32 @tun_open(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
store i32 %28, i32* %6
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0))
br label %30
30: ; preds = %27, %38
br label %31
31: ; preds = %37, %30
%32 = load i8*, i8** %8
%33 = getelementptr inbounds i8, i8* %32, i64 84
%34 = load i8, i8* %33
%35 = sext i8 %34 to i32
%36 = icmp ne i32 %35, 1
br i1 %36, label %37, label %38
37: ; preds = %31
br label %31
38: ; preds = %31
%39 = load i32, i32* %6
%40 = load i8*, i8** %8
%41 = call i32 @write(i32 %39, i8* %40, i32 100)
store i32 %41, i32* %7
store i32 0, i32* %7
%42 = load i32, i32* %6
%43 = load i8*, i8** %8
%44 = call i32 @read(i32 %42, i8* %43, i32 100)
store i32 %44, i32* %7
%45 = load i32, i32* %7
%46 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0), i32 %45)
%47 = load i8*, i8** %8
%48 = getelementptr inbounds i8, i8* %47, i64 84
store i8 50, i8* %48
br label %30
}

LLVM IR (-O2):
@IPC_CREAT = global i32 0
@.str = [7 x i8] c"shmget\00"
@.str.1 = [6 x i8] c"shmat\00"
@.str.2 = [5 x i8] c"asa0\00"
@.str.4 = [25 x i8] c"Read %d bytes from asa1\0A\00"
@str = [19 x i8] c"Device asa1 opened\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = load i32, i32* @IPC_CREAT
%4 = or i32 %3, 438
%5 = tail call i32 @shmget(i32 5678, i32 100, i32 %4)
%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, 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.1, i64 0, i64 0))
%15 = tail call i32 @exit(i32 1)
unreachable
16: ; preds = %10
%17 = tail call i32 @tun_open(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%18 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str, i64 0, i64 0))
%19 = getelementptr inbounds i8, i8* %11, i64 84
%20 = load i8, i8* %19
%21 = icmp eq i8 %20, 1
br i1 %21, label %22, label %26
22: ; preds = %16
%23 = tail call i32 @write(i32 %17, i8* %11, i32 100)
%24 = tail call i32 @read(i32 %17, i8* %11, i32 100)
%25 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0), i32 %24)
store i8 50, i8* %19
br label %26
26: ; preds = %22, %16
br label %27
27: ; preds = %26, %27
br label %27
}