#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 O_NDELAY;
int O_RDWR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int open(char *, int);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strlen(char const *);
int write(int, char const *, int /*<<< orphan*/);
int write_chr(const char *string) {
  int fd = open("/dev/char_dev", O_RDWR | O_NDELAY);
  if (fd < 0) {
    fprintf(stderr, "Failed to open device %s!\n", "/dev/char_dev");
    return -1;
  }
  if (write(fd, string, strlen(string)) == -1) {
    fprintf(stderr, "Failed to write into device %s!\n", "/dev/char_dev");
    return -1;
  }
  printf("write into device %s !\n", "/dev/char_dev");
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<write_chr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e75(%rip),%rax
mov    (%rax),%esi
mov    0x2e54(%rip),%rax
or     (%rax),%esi
lea    0xe7b(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11bf <write_chr+0x5f>
mov    0x2e3a(%rip),%rax
mov    (%rax),%edi
lea    0xe67(%rip),%rsi
lea    0xe52(%rip),%rdx
call   1070 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1237 <write_chr+0xd7>
mov    -0x14(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x24(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    1215 <write_chr+0xb5>
mov    0x2de4(%rip),%rax
mov    (%rax),%edi
lea    0xe2c(%rip),%rsi
lea    0xdfc(%rip),%rdx
call   1070 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1237 <write_chr+0xd7>
lea    0xe2e(%rip),%rdi
lea    0xddd(%rip),%rsi
call   1050 <printf@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_chr>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
mov    0x2e7a(%rip),%rax
mov    0x2e5b(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe80(%rip),%rdi
call   1080 <open@plt>
test   %eax,%eax
js     11c2 <write_chr+0x62>
mov    %eax,%ebx
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
je     11d4 <write_chr+0x74>
lea    0xe9f(%rip),%rdi
lea    0xe4e(%rip),%rsi
call   1050 <printf@plt>
mov    %ebx,%edi
call   1060 <close@plt>
xor    %eax,%eax
jmp    11f5 <write_chr+0x95>
mov    0x2e0f(%rip),%rax
mov    (%rax),%edi
lea    0xe3c(%rip),%rsi
jmp    11e4 <write_chr+0x84>
mov    0x2dfd(%rip),%rax
mov    (%rax),%edi
lea    0xe45(%rip),%rsi
lea    0xe15(%rip),%rdx
call   1070 <fprintf@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [14 x i8] c"/dev/char_dev\00"
@O_RDWR = global i32 0
@O_NDELAY = global i32 0
@stderr = global i32 0
@.str.1 = [27 x i8] c"Failed to open device %s!\0A\00"
@.str.2 = [33 x i8] c"Failed to write into device %s!\0A\00"
@.str.3 = [24 x i8] c"write into device %s !\0A\00"
define i32 @write_chr(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
store i8* %0, i8** %3
%5 = load i32, i32* @O_RDWR
%6 = load i32, i32* @O_NDELAY
%7 = or i32 %5, %6
%8 = call i32 @open(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %7)
store i32 %8, i32* %4
%9 = load i32, i32* %4
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %1
%12 = load i32, i32* @stderr
%13 = call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %2
br label %28
14: ; preds = %1
%15 = load i32, i32* %4
%16 = load i8*, i8** %3
%17 = load i8*, i8** %3
%18 = call i32 @strlen(i8* %17)
%19 = call i32 @write(i32 %15, i8* %16, i32 %18)
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %24
21: ; preds = %14
%22 = load i32, i32* @stderr
%23 = call i32 @fprintf(i32 %22, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %2
br label %28
24: ; preds = %14
%25 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%26 = load i32, i32* %4
%27 = call i32 @close(i32 %26)
store i32 0, i32* %2
br label %28
28: ; preds = %24, %21, %11
%29 = load i32, i32* %2
ret i32 %29
}

LLVM IR (-O2):
@.str = [14 x i8] c"/dev/char_dev\00"
@O_RDWR = global i32 0
@O_NDELAY = global i32 0
@stderr = global i32 0
@.str.1 = [27 x i8] c"Failed to open device %s!\0A\00"
@.str.2 = [33 x i8] c"Failed to write into device %s!\0A\00"
@.str.3 = [24 x i8] c"write into device %s !\0A\00"
define i32 @write_chr(i8* %0) {
%2 = load i32, i32* @O_RDWR
%3 = load i32, i32* @O_NDELAY
%4 = or i32 %3, %2
%5 = tail call i32 @open(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %4)
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %10
7: ; preds = %1
%8 = load i32, i32* @stderr
%9 = tail call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %20
10: ; preds = %1
%11 = tail call i32 @strlen(i8* %0)
%12 = tail call i32 @write(i32 %5, i8* %0, i32 %11)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %17
14: ; preds = %10
%15 = load i32, i32* @stderr
%16 = tail call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %20
17: ; preds = %10
%18 = tail call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%19 = tail call i32 @close(i32 %5)
br label %20
20: ; preds = %17, %14, %7
%21 = phi i32 [ -1, %7 ], [ -1, %14 ], [ 0, %17 ]
ret i32 %21
}