#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 asa0 opened\n");
  while (1) {
    nbytes = read(fd, buf, 100);
    *(buf + 84) = '1';
    printf("Read %d bytes from asa0\n", nbytes);
    while (*(buf + 84) != 2)
      ;
    nbytes = write(fd, buf, 100);
    *(buf + 84) = 1;
  }
  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>
mov    -0x14(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    $0x64,%edx
call   1050 <read@plt>
mov    %eax,-0x18(%rbp)
mov    -0x20(%rbp),%rax
movb   $0x31,0x54(%rax)
mov    -0x18(%rbp),%esi
lea    0xdd0(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x20(%rbp),%rax
movsbl 0x54(%rax),%eax
cmp    $0x2,%eax
je     1273 <main+0xf3>
jmp    125d <main+0xdd>
mov    -0x14(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    $0x64,%edx
call   1030 <write@plt>
mov    %eax,-0x18(%rbp)
mov    -0x20(%rbp),%rax
movb   $0x1,0x54(%rax)
jmp    1230 <main+0xb0>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
mov    0x2e45(%rip),%rax
mov    $0x1b6,%edx
or     (%rax),%edx
mov    $0x162e,%edi
mov    $0x64,%esi
call   10a0 <shmget@plt>
test   %eax,%eax
js     1262 <main+0xd2>
mov    %eax,%edi
xor    %esi,%esi
xor    %edx,%edx
call   1090 <shmat@plt>
cmp    $0xffffffffffffffff,%rax
je     126b <main+0xdb>
mov    %rax,%rbx
lea    0xe35(%rip),%rdi
call   1070 <tun_open@plt>
mov    %eax,%ebp
lea    0xe45(%rip),%rdi
call   1030 <puts@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x64,%edx
call   1060 <read@plt>
movb   $0x31,0x54(%rbx)
lea    0xe0d(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
cmpb   $0x2,0x54(%rbx)
jne    1260 <main+0xd0>
lea    0xdf7(%rip),%r14
nopl   0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x64,%edx
call   1040 <write@plt>
movb   $0x1,0x54(%rbx)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x64,%edx
call   1060 <read@plt>
movb   $0x31,0x54(%rbx)
mov    %r14,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
cmpb   $0x2,0x54(%rbx)
je     1220 <main+0x90>
nopl   0x0(%rax,%rax,1)
jmp    1260 <main+0xd0>
lea    0xd97(%rip),%rdi
jmp    1272 <main+0xe2>
lea    0xd95(%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 asa0 opened\0A\00"
@.str.4 = [25 x i8] c"Read %d bytes from asa0\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, %45
%31 = load i32, i32* %6
%32 = load i8*, i8** %8
%33 = call i32 @read(i32 %31, i8* %32, i32 100)
store i32 %33, i32* %7
%34 = load i8*, i8** %8
%35 = getelementptr inbounds i8, i8* %34, i64 84
store i8 49, i8* %35
%36 = load i32, i32* %7
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0), i32 %36)
br label %38
38: ; preds = %44, %30
%39 = load i8*, i8** %8
%40 = getelementptr inbounds i8, i8* %39, i64 84
%41 = load i8, i8* %40
%42 = sext i8 %41 to i32
%43 = icmp ne i32 %42, 2
br i1 %43, label %44, label %45
44: ; preds = %38
br label %38
45: ; preds = %38
%46 = load i32, i32* %6
%47 = load i8*, i8** %8
%48 = call i32 @write(i32 %46, i8* %47, i32 100)
store i32 %48, i32* %7
%49 = load i8*, i8** %8
%50 = getelementptr inbounds i8, i8* %49, i64 84
store i8 1, i8* %50
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 asa0\0A\00"
@str = [19 x i8] c"Device asa0 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 = tail call i32 @read(i32 %17, i8* %11, i32 100)
%20 = getelementptr inbounds i8, i8* %11, i64 84
store i8 49, i8* %20
%21 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0), i32 %19)
%22 = load i8, i8* %20
%23 = icmp eq i8 %22, 2
br i1 %23, label %26, label %24
24: ; preds = %26, %16
br label %25
25: ; preds = %24, %25
br label %25
26: ; preds = %16, %26
%27 = tail call i32 @write(i32 %17, i8* %11, i32 100)
store i8 1, i8* %20
%28 = tail call i32 @read(i32 %17, i8* %11, i32 100)
store i8 49, i8* %20
%29 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0), i32 %28)
%30 = load i8, i8* %20
%31 = icmp eq i8 %30, 2
br i1 %31, label %26, label %24
}