#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 */

/* Variables and functions */
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ read(int, char *, int);
int /*<<< orphan*/ scanf(char *, char *);
int /*<<< orphan*/ system(char *);
int /*<<< orphan*/ write(int, char *, int);
int main(void) {
  int fd;
  char *num = "0";
  char buf1[10] = {0, 1};
  char buf2[1024];
  fd = open("/dev/chardev", O_RDWR);
  if (fd < 0) {
    printf("Open /dev/mychar error\n");
    return -1;
  }
  num = buf2;
  read(fd, num, sizeof(int));
  printf("The myChar is %s\n", num);
  printf("Please input a number written to myChar: ");
  scanf("%s", num);
  write(fd, num, sizeof(int));
  read(fd, num, sizeof(int));
  printf("The myChar you input is %s\n", num);
  system("dmesg |tail -n 30 ");
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe77(%rip),%rax
mov    %rax,-0x10(%rbp)
mov    0xf01(%rip),%rax
mov    %rax,-0x1a(%rbp)
mov    0xefe(%rip),%ax
mov    %ax,-0x12(%rbp)
mov    0x2e3e(%rip),%rax
mov    (%rax),%esi
lea    0xe4f(%rip),%rdi
call   1090 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    11df <main+0x6f>
lea    0xe43(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    127a <main+0x10a>
lea    -0x420(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x4,%edx
call   1070 <read@plt>
mov    -0x10(%rbp),%rsi
lea    0xe21(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xe25(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x10(%rbp),%rsi
lea    0xe3d(%rip),%rdi
call   1080 <scanf@plt>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x4,%edx
call   1030 <write@plt>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x4,%edx
call   1070 <read@plt>
mov    -0x10(%rbp),%rsi
lea    0xe0e(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xe1c(%rip),%rdi
call   1040 <system@plt>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x400,%rsp
mov    0x2e56(%rip),%rax
mov    (%rax),%esi
lea    0xe65(%rip),%rdi
call   10a0 <open@plt>
test   %eax,%eax
js     1231 <main+0xb1>
mov    %eax,%ebx
mov    %rsp,%rbp
mov    %eax,%edi
mov    %rbp,%rsi
mov    $0x4,%edx
call   1080 <read@plt>
lea    0xe4a(%rip),%rdi
xor    %r14d,%r14d
mov    %rbp,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xe48(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xe64(%rip),%rdi
mov    %rbp,%rsi
call   1090 <scanf@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0x4,%edx
call   1040 <write@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0x4,%edx
call   1080 <read@plt>
lea    0xe3a(%rip),%rdi
mov    %rbp,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xe45(%rip),%rdi
call   1050 <system@plt>
mov    %ebx,%edi
call   1070 <close@plt>
jmp    1243 <main+0xc3>
lea    0xe43(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
add    $0x400,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"0\00"
@__const.main.buf1 = <{ i8, i8, [8 x i8] }> <{ i8 0, i8 1, [8 x i8] zeroinitializer }>
@.str.1 = [13 x i8] c"/dev/chardev\00"
@O_RDWR = global i32 0
@.str.2 = [24 x i8] c"Open /dev/mychar error\0A\00"
@.str.3 = [18 x i8] c"The myChar is %s\0A\00"
@.str.4 = [42 x i8] c"Please input a number written to myChar: \00"
@.str.5 = [3 x i8] c"%s\00"
@.str.6 = [28 x i8] c"The myChar you input is %s\0A\00"
@.str.7 = [19 x i8] c"dmesg |tail -n 30 \00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i8*
%4 = alloca [10 x i8]
%5 = alloca [1024 x i8]
store i32 0, i32* %1
store i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8** %3
%6 = bitcast [10 x i8]* %4 to i8*
%7 = load i32, i32* @O_RDWR
%8 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i32 %7)
store i32 %8, i32* %2
%9 = load i32, i32* %2
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %0
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %1
br label %34
13: ; preds = %0
%14 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
store i8* %14, i8** %3
%15 = load i32, i32* %2
%16 = load i8*, i8** %3
%17 = call i32 @read(i32 %15, i8* %16, i32 4)
%18 = load i8*, i8** %3
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i8* %18)
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.4, i64 0, i64 0))
%21 = load i8*, i8** %3
%22 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i8* %21)
%23 = load i32, i32* %2
%24 = load i8*, i8** %3
%25 = call i32 @write(i32 %23, i8* %24, i32 4)
%26 = load i32, i32* %2
%27 = load i8*, i8** %3
%28 = call i32 @read(i32 %26, i8* %27, i32 4)
%29 = load i8*, i8** %3
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.6, i64 0, i64 0), i8* %29)
%31 = call i32 @system(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.7, i64 0, i64 0))
%32 = load i32, i32* %2
%33 = call i32 @close(i32 %32)
store i32 0, i32* %1
br label %34
34: ; preds = %13, %11
%35 = load i32, i32* %1
ret i32 %35
}

LLVM IR (-O2):
@.str.1 = [13 x i8] c"/dev/chardev\00"
@O_RDWR = global i32 0
@.str.3 = [18 x i8] c"The myChar is %s\0A\00"
@.str.4 = [42 x i8] c"Please input a number written to myChar: \00"
@.str.5 = [3 x i8] c"%s\00"
@.str.6 = [28 x i8] c"The myChar you input is %s\0A\00"
@.str.7 = [19 x i8] c"dmesg |tail -n 30 \00"
@str = [23 x i8] c"Open /dev/mychar error\00"
define i32 @main() {
%1 = alloca [1024 x i8]
%2 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 0
%3 = load i32, i32* @O_RDWR
%4 = tail call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i32 %3)
%5 = icmp slt i32 %4, 0
br i1 %5, label %6, label %8
6: ; preds = %0
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str, i64 0, i64 0))
br label %18
8: ; preds = %0
%9 = call i32 @read(i32 %4, i8* %2, i32 4)
%10 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i8* %2)
%11 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([42 x i8], [42 x i8]* @.str.4, i64 0, i64 0))
%12 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i8* %2)
%13 = call i32 @write(i32 %4, i8* %2, i32 4)
%14 = call i32 @read(i32 %4, i8* %2, i32 4)
%15 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.6, i64 0, i64 0), i8* %2)
%16 = call i32 @system(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.7, i64 0, i64 0))
%17 = call i32 @close(i32 %4)
br label %18
18: ; preds = %8, %6
%19 = phi i32 [ -1, %6 ], [ 0, %8 ]
ret i32 %19
}