#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 */
struct timespec {
  int /*<<< orphan*/ member_0;
};

/* Variables and functions */
int /*<<< orphan*/ O_RDWR;
int atoi(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ memset(char *, char, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ read(int, char *, int);
int /*<<< orphan*/ sprintf(char *, char *, char *);
int strlen(char *);
int /*<<< orphan*/ write(int, char *, int);
int main(int argc, char *argv[]) {
  int fd, index, val;
  char file[80];
  char msg_buff[512];
  struct timespec t = {0};
  sprintf(file, "/dev/rpmsg_pru%s", argv[1]);
  fd = open(file, O_RDWR);
  if (fd < 0) {
    perror(argv[1]);
    printf("Invalid RPMsg file\nExiting...\n");
    return 1;
  }
  memset(msg_buff, '\0', 512);
  for (index = 0; index < strlen(argv[2]); index++)
    msg_buff[0] ^= argv[2][index];
  if (argc == 4) {
    val = atoi(argv[3]);
    for (index = 0; index < 4; index++) {
      msg_buff[1] = val & 0x000000FF;
      msg_buff[2] = (val & 0x0000FF00) >> 0x08;
      msg_buff[3] = (val & 0x00FF0000) >> 0x10;
      msg_buff[4] = val >> 0x18;
    }
    write(fd, msg_buff, 5);
  } else {
    write(fd, msg_buff, 1);
  }
  read(fd, msg_buff, 4);
  val = (msg_buff[0] | msg_buff[1] << 0x08 | msg_buff[2] << 0x10 |
         msg_buff[3] << 0x18);
  if (val >> 0x1B) {
    val &= (0x7FFFFFF);
    val -= 0x7FFFFFF;
  }
  printf("%d", val);
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x280,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x278(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1060 <memset@plt>
lea    -0x70(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xe21(%rip),%rsi
call   10c0 <sprintf@plt>
lea    -0x70(%rbp),%rdi
mov    0x2df9(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    122a <main+0x8a>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10a0 <perror@plt>
lea    0xdfa(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    13c0 <main+0x220>
lea    -0x270(%rbp),%rdi
xor    %esi,%esi
mov    $0x200,%edx
call   1060 <memset@plt>
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x27c(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x27c(%rbp),%eax
cmp    %ecx,%eax
jge    1297 <main+0xf7>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
movslq -0x18(%rbp),%rcx
movsbl (%rax,%rcx,1),%ecx
movsbl -0x270(%rbp),%eax
xor    %ecx,%eax
mov    %al,-0x270(%rbp)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1244 <main+0xa4>
cmpl   $0x4,-0x8(%rbp)
jne    1325 <main+0x185>
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
call   10b0 <atoi@plt>
mov    %eax,-0x1c(%rbp)
movl   $0x0,-0x18(%rbp)
cmpl   $0x4,-0x18(%rbp)
jge    130c <main+0x16c>
mov    -0x1c(%rbp),%eax
and    $0xff,%eax
mov    %al,-0x26f(%rbp)
mov    -0x1c(%rbp),%eax
and    $0xff00,%eax
sar    $0x8,%eax
mov    %al,-0x26e(%rbp)
mov    -0x1c(%rbp),%eax
and    $0xff0000,%eax
sar    $0x10,%eax
mov    %al,-0x26d(%rbp)
mov    -0x1c(%rbp),%eax
sar    $0x18,%eax
mov    %al,-0x26c(%rbp)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    12b8 <main+0x118>
mov    -0x14(%rbp),%edi
lea    -0x270(%rbp),%rsi
mov    $0x5,%edx
call   1030 <write@plt>
jmp    1339 <main+0x199>
mov    -0x14(%rbp),%edi
lea    -0x270(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    -0x14(%rbp),%edi
lea    -0x270(%rbp),%rsi
mov    $0x4,%edx
call   1080 <read@plt>
movsbl -0x270(%rbp),%eax
movsbl -0x26f(%rbp),%ecx
shl    $0x8,%ecx
or     %ecx,%eax
movsbl -0x26e(%rbp),%ecx
shl    $0x10,%ecx
or     %ecx,%eax
movsbl -0x26d(%rbp),%ecx
shl    $0x18,%ecx
or     %ecx,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
sar    $0x1b,%eax
cmp    $0x0,%eax
je     13a0 <main+0x200>
mov    -0x1c(%rbp),%eax
and    $0x7ffffff,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
sub    $0x7ffffff,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%esi
lea    0xc86(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x280,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x258,%rsp
mov    %rsi,%rbx
mov    %edi,%r14d
mov    0x8(%rsi),%rdx
lea    0xe32(%rip),%rsi
lea    0x200(%rsp),%rbp
mov    %rbp,%rdi
call   10d0 <sprintf@plt>
mov    0x2e03(%rip),%rax
mov    (%rax),%esi
mov    %rbp,%rdi
call   10a0 <open@plt>
test   %eax,%eax
js     12d7 <main+0x127>
mov    %eax,%r15d
mov    %rsp,%rdi
xor    %esi,%esi
mov    $0x200,%edx
call   1070 <memset@plt>
mov    0x10(%rbx),%rdi
call   1050 <strlen@plt>
test   %eax,%eax
jle    123b <main+0x8b>
xor    %ebp,%ebp
nopl   0x0(%rax,%rax,1)
mov    0x10(%rbx),%rdi
movzbl (%rdi,%rbp,1),%eax
xor    %al,(%rsp)
add    $0x1,%rbp
call   1050 <strlen@plt>
cltq
cmp    %rax,%rbp
jl     1220 <main+0x70>
mov    $0x1,%edx
cmp    $0x4,%r14d
jne    1258 <main+0xa8>
mov    0x18(%rbx),%rdi
call   10c0 <atoi@plt>
mov    %eax,0x1(%rsp)
mov    $0x5,%edx
mov    %rsp,%rbx
mov    %r15d,%edi
mov    %rbx,%rsi
call   1040 <write@plt>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0x4,%edx
call   1090 <read@plt>
movsbl (%rsp),%eax
movsbl 0x1(%rsp),%ecx
shl    $0x8,%ecx
or     %eax,%ecx
movsbl 0x2(%rsp),%eax
shl    $0x10,%eax
movzbl 0x3(%rsp),%edx
shl    $0x18,%edx
or     %eax,%edx
or     %ecx,%edx
mov    %edx,%esi
and    $0x7ffffff,%esi
add    $0xf8000001,%esi
cmp    $0x8000000,%edx
cmovb  %edx,%esi
lea    0xd5b(%rip),%rdi
xor    %ebx,%ebx
xor    %eax,%eax
call   1060 <printf@plt>
mov    %r15d,%edi
call   1080 <close@plt>
mov    %ebx,%eax
add    $0x258,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x8(%rbx),%rdi
call   10b0 <perror@plt>
lea    0xd2d(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%ebx
jmp    12c7 <main+0x117>

LLVM IR (-O0):
%struct.timespec = type { i32 }
@.str = [17 x i8] c"/dev/rpmsg_pru%s\00"
@O_RDWR = global i32 0
@.str.1 = [31 x i8] c"Invalid RPMsg file\0AExiting...\0A\00"
@.str.2 = [3 x i8] c"%d\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 [80 x i8]
%10 = alloca [512 x i8]
%11 = alloca %struct.timespec
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = bitcast %struct.timespec* %11 to i8*
%13 = getelementptr inbounds [80 x i8], [80 x i8]* %9, i64 0, i64 0
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 1
%16 = load i8*, i8** %15
%17 = call i32 @sprintf(i8* %13, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* %16)
%18 = getelementptr inbounds [80 x i8], [80 x i8]* %9, i64 0, i64 0
%19 = load i32, i32* @O_RDWR
%20 = call i32 @open(i8* %18, i32 %19)
store i32 %20, i32* %6
%21 = load i32, i32* %6
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %29
23: ; preds = %2
%24 = load i8**, i8*** %5
%25 = getelementptr inbounds i8*, i8** %24, i64 1
%26 = load i8*, i8** %25
%27 = call i32 @perror(i8* %26)
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %3
br label %132
29: ; preds = %2
%30 = getelementptr inbounds [512 x i8], [512 x i8]* %10, i64 0, i64 0
%31 = call i32 @memset(i8* %30, i8 signext 0, i32 512)
store i32 0, i32* %7
br label %32
32: ; preds = %53, %29
%33 = load i32, i32* %7
%34 = load i8**, i8*** %5
%35 = getelementptr inbounds i8*, i8** %34, i64 2
%36 = load i8*, i8** %35
%37 = call i32 @strlen(i8* %36)
%38 = icmp slt i32 %33, %37
br i1 %38, label %39, label %56
39: ; preds = %32
%40 = load i8**, i8*** %5
%41 = getelementptr inbounds i8*, i8** %40, i64 2
%42 = load i8*, i8** %41
%43 = load i32, i32* %7
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds i8, i8* %42, i64 %44
%46 = load i8, i8* %45
%47 = sext i8 %46 to i32
%48 = getelementptr inbounds [512 x i8], [512 x i8]* %10, i64 0, i64 0
%49 = load i8, i8* %48
%50 = sext i8 %49 to i32
%51 = xor i32 %50, %47
%52 = trunc i32 %51 to i8
store i8 %52, i8* %48
br label %53
53: ; preds = %39
%54 = load i32, i32* %7
%55 = add nsw i32 %54, 1
store i32 %55, i32* %7
br label %32
56: ; preds = %32
%57 = load i32, i32* %4
%58 = icmp eq i32 %57, 4
br i1 %58, label %59, label %93
59: ; preds = %56
%60 = load i8**, i8*** %5
%61 = getelementptr inbounds i8*, i8** %60, i64 3
%62 = load i8*, i8** %61
%63 = call i32 @atoi(i8* %62)
store i32 %63, i32* %8
store i32 0, i32* %7
br label %64
64: ; preds = %86, %59
%65 = load i32, i32* %7
%66 = icmp slt i32 %65, 4
br i1 %66, label %67, label %89
67: ; preds = %64
%68 = load i32, i32* %8
%69 = and i32 %68, 255
%70 = trunc i32 %69 to i8
%71 = getelementptr inbounds [512 x i8], [512 x i8]* %10, i64 0, i64 1
store i8 %70, i8* %71
%72 = load i32, i32* %8
%73 = and i32 %72, 65280
%74 = ashr i32 %73, 8
%75 = trunc i32 %74 to i8
%76 = getelementptr inbounds [512 x i8], [512 x i8]* %10, i64 0, i64 2
store i8 %75, i8* %76
%77 = load i32, i32* %8
%78 = and i32 %77, 16711680
%79 = ashr i32 %78, 16
%80 = trunc i32 %79 to i8
%81 = getelementptr inbounds [512 x i8], [512 x i8]* %10, i64 0, i64 3
store i8 %80, i8* %81
%82 = load i32, i32* %8
%83 = ashr i32 %82, 24
%84 = trunc i32 %83 to i8
%85 = getelementptr inbounds [512 x i8], [512 x i8]* %10, i64 0, i64 4
store i8 %84, i8* %85
br label %86
86: ; preds = %67
%87 = load i32, i32* %7
%88 = add nsw i32 %87, 1
store i32 %88, i32* %7
br label %64
89: ; preds = %64
%90 = load i32, i32* %6
%91 = getelementptr inbounds [512 x i8], [512 x i8]* %10, i64 0, i64 0
%92 = call i32 @write(i32 %90, i8* %91, i32 5)
br label %97
93: ; preds = %56
%94 = load i32, i32* %6
%95 = getelementptr inbounds [512 x i8], [512 x i8]* %10, i64 0, i64 0
%96 = call i32 @write(i32 %94, i8* %95, i32 1)
br label %97
97: ; preds = %93, %89
%98 = load i32, i32* %6
%99 = getelementptr inbounds [512 x i8], [512 x i8]* %10, i64 0, i64 0
%100 = call i32 @read(i32 %98, i8* %99, i32 4)
%101 = getelementptr inbounds [512 x i8], [512 x i8]* %10, i64 0, i64 0
%102 = load i8, i8* %101
%103 = sext i8 %102 to i32
%104 = getelementptr inbounds [512 x i8], [512 x i8]* %10, i64 0, i64 1
%105 = load i8, i8* %104
%106 = sext i8 %105 to i32
%107 = shl i32 %106, 8
%108 = or i32 %103, %107
%109 = getelementptr inbounds [512 x i8], [512 x i8]* %10, i64 0, i64 2
%110 = load i8, i8* %109
%111 = sext i8 %110 to i32
%112 = shl i32 %111, 16
%113 = or i32 %108, %112
%114 = getelementptr inbounds [512 x i8], [512 x i8]* %10, i64 0, i64 3
%115 = load i8, i8* %114
%116 = sext i8 %115 to i32
%117 = shl i32 %116, 24
%118 = or i32 %113, %117
store i32 %118, i32* %8
%119 = load i32, i32* %8
%120 = ashr i32 %119, 27
%121 = icmp ne i32 %120, 0
br i1 %121, label %122, label %127
122: ; preds = %97
%123 = load i32, i32* %8
%124 = and i32 %123, 134217727
store i32 %124, i32* %8
%125 = load i32, i32* %8
%126 = sub nsw i32 %125, 134217727
store i32 %126, i32* %8
br label %127
127: ; preds = %122, %97
%128 = load i32, i32* %8
%129 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 %128)
%130 = load i32, i32* %6
%131 = call i32 @close(i32 %130)
store i32 0, i32* %3
br label %132
132: ; preds = %127, %23
%133 = load i32, i32* %3
ret i32 %133
}

LLVM IR (-O2):
@.str = [17 x i8] c"/dev/rpmsg_pru%s\00"
@O_RDWR = global i32 0
@.str.2 = [3 x i8] c"%d\00"
@str = [30 x i8] c"Invalid RPMsg file\0AExiting...\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [80 x i8]
%4 = alloca [512 x i8]
%5 = getelementptr inbounds [80 x i8], [80 x i8]* %3, i64 0, i64 0
%6 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%7 = getelementptr inbounds i8*, i8** %1, i64 1
%8 = load i8*, i8** %7
%9 = call i32 @sprintf(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* %8)
%10 = load i32, i32* @O_RDWR
%11 = call i32 @open(i8* %5, i32 %10)
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %17
13: ; preds = %2
%14 = load i8*, i8** %7
%15 = tail call i32 @perror(i8* %14)
%16 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @str, i64 0, i64 0))
br label %78
17: ; preds = %2
%18 = call i32 @memset(i8* %6, i8 signext 0, i32 512)
%19 = getelementptr inbounds i8*, i8** %1, i64 2
%20 = load i8*, i8** %19
%21 = call i32 @strlen(i8* %20)
%22 = icmp sgt i32 %21, 0
br i1 %22, label %23, label %34
23: ; preds = %17, %23
%24 = phi i64 [ %30, %23 ], [ 0, %17 ]
%25 = load i8*, i8** %19
%26 = getelementptr inbounds i8, i8* %25, i64 %24
%27 = load i8, i8* %26
%28 = load i8, i8* %6
%29 = xor i8 %28, %27
store i8 %29, i8* %6
%30 = add nuw nsw i64 %24, 1
%31 = call i32 @strlen(i8* %25)
%32 = sext i32 %31 to i64
%33 = icmp slt i64 %30, %32
br i1 %33, label %23, label %34
34: ; preds = %23, %17
%35 = icmp eq i32 %0, 4
br i1 %35, label %36, label %51
36: ; preds = %34
%37 = getelementptr inbounds i8*, i8** %1, i64 3
%38 = load i8*, i8** %37
%39 = call i32 @atoi(i8* %38)
%40 = trunc i32 %39 to i8
%41 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 1
store i8 %40, i8* %41
%42 = lshr i32 %39, 8
%43 = trunc i32 %42 to i8
%44 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 2
store i8 %43, i8* %44
%45 = lshr i32 %39, 16
%46 = trunc i32 %45 to i8
%47 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 3
store i8 %46, i8* %47
%48 = lshr i32 %39, 24
%49 = trunc i32 %48 to i8
%50 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 4
store i8 %49, i8* %50
br label %51
51: ; preds = %34, %36
%52 = phi i32 [ 5, %36 ], [ 1, %34 ]
%53 = call i32 @write(i32 %11, i8* %6, i32 %52)
%54 = call i32 @read(i32 %11, i8* %6, i32 4)
%55 = load i8, i8* %6
%56 = sext i8 %55 to i32
%57 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 1
%58 = load i8, i8* %57
%59 = sext i8 %58 to i32
%60 = shl nsw i32 %59, 8
%61 = or i32 %60, %56
%62 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 2
%63 = load i8, i8* %62
%64 = sext i8 %63 to i32
%65 = shl nsw i32 %64, 16
%66 = or i32 %61, %65
%67 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 3
%68 = load i8, i8* %67
%69 = zext i8 %68 to i32
%70 = shl nuw i32 %69, 24
%71 = or i32 %66, %70
%72 = icmp ult i32 %71, 134217728
%73 = and i32 %71, 134217727
%74 = add nsw i32 %73, -134217727
%75 = select i1 %72, i32 %71, i32 %74
%76 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 %75)
%77 = call i32 @close(i32 %11)
br label %78
78: ; preds = %51, %13
%79 = phi i32 [ 1, %13 ], [ 0, %51 ]
ret i32 %79
}