#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 /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ close(int /*<<< orphan*/ *);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ *open(char *, int /*<<< orphan*/);
scalar_t__ read(int /*<<< orphan*/ *, char *, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
char *strerror(int /*<<< orphan*/);
scalar_t__ write(int /*<<< orphan*/ *, char *, int);
int main() {
  char ch_data = 0;
  FILE *fd;
  if ((fd = open("/dev/radmonc", O_RDWR)) < 0) {
    fprintf(stderr, "unable to open /dev/radmonc: %s\n", strerror(errno));
    return 0;
  }
  if (read(fd, &ch_data, 1) < 0)
    fprintf(stderr, "oh no, could not read: %s\n", strerror(errno));
  else {
    fprintf(stdout, "read from input: 0x%x\n", ch_data);
  }
  ch_data = (char)(0x10 | 0x08);
  if (write(fd, &ch_data, 1) < 0)
    fprintf(stderr, "help! could not write: %s\n", strerror(errno));
  else
    fprintf(stdout, "wrote %x to output?\n", ch_data);
  while (1) {
    ch_data = (char)(0x04 | 0x10);
    write(fd, &ch_data, 1);
    ch_data = (char)(0x08);
    write(fd, &ch_data, 1);
  }
  if (fd >= 0)
    close(fd);
  fprintf(stdout, "finished\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movb   $0x0,-0x5(%rbp)
mov    0x2e7e(%rip),%rax
mov    (%rax),%esi
lea    0xe8d(%rip),%rdi
call   1060 <open@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jae    11bc <main+0x6c>
mov    0x2e4b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    0x2e37(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x14(%rbp),%edi
mov    %rax,%rdx
lea    0xe60(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret
mov    -0x10(%rbp),%rdi
lea    -0x5(%rbp),%rsi
mov    $0x1,%edx
call   1040 <read@plt>
cmp    $0x0,%rax
jge    120b <main+0xbb>
mov    0x2df9(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    0x2de5(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x18(%rbp),%edi
mov    %rax,%rdx
lea    0xe2f(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
jmp    1226 <main+0xd6>
mov    0x2db6(%rip),%rax
mov    (%rax),%edi
movsbl -0x5(%rbp),%edx
lea    0xe2a(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movb   $0x18,-0x5(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x5(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
cmp    $0x0,%rax
jge    1279 <main+0x129>
mov    0x2d8b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2d77(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x1c(%rbp),%edi
mov    %rax,%rdx
lea    0xdf3(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
jmp    1294 <main+0x144>
mov    0x2d48(%rip),%rax
mov    (%rax),%edi
movsbl -0x5(%rbp),%edx
lea    0xdee(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
jmp    1299 <main+0x149>
movb   $0x14,-0x5(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x5(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
movb   $0x8,-0x5(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x5(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
jmp    1299 <main+0x149>

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
movb   $0x0,0x7(%rsp)
mov    0x2e88(%rip),%rax
mov    (%rax),%esi
lea    0xe97(%rip),%rdi
call   1060 <open@plt>
mov    %rax,%r14
lea    0x7(%rsp),%rsi
mov    %rax,%rdi
mov    $0x1,%edx
call   1040 <read@plt>
test   %rax,%rax
js     11a6 <main+0x56>
mov    0x2e39(%rip),%rax
mov    (%rax),%edi
movsbl 0x7(%rsp),%edx
lea    0xe8b(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    11d0 <main+0x80>
mov    0x2e2b(%rip),%rax
mov    (%rax),%ebx
mov    0x2e1a(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xe49(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
movb   $0x18,0x7(%rsp)
lea    0x7(%rsp),%rsi
mov    %r14,%rdi
mov    $0x1,%edx
call   1030 <write@plt>
test   %rax,%rax
js     120a <main+0xba>
mov    0x2dd5(%rip),%rax
mov    (%rax),%edi
lea    0xe5e(%rip),%rsi
mov    $0x18,%edx
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    1234 <main+0xe4>
mov    0x2dc7(%rip),%rax
mov    (%rax),%ebx
mov    0x2db6(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xe17(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
lea    0x7(%rsp),%rbx
nopl   0x0(%rax)
movb   $0x14,0x7(%rsp)
mov    %r14,%rdi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1030 <write@plt>
movb   $0x8,0x7(%rsp)
mov    %r14,%rdi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1030 <write@plt>
jmp    1240 <main+0xf0>

LLVM IR (-O0):
@.str = [13 x i8] c"/dev/radmonc\00"
@O_RDWR = global i32 0
@stderr = global i32 0
@.str.1 = [33 x i8] c"unable to open /dev/radmonc: %s\0A\00"
@errno = global i32 0
@.str.2 = [27 x i8] c"oh no, could not read: %s\0A\00"
@stdout = global i32 0
@.str.3 = [23 x i8] c"read from input: 0x%x\0A\00"
@.str.4 = [27 x i8] c"help! could not write: %s\0A\00"
@.str.5 = [21 x i8] c"wrote %x to output?\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i8
%3 = alloca i32*
store i32 0, i32* %1
store i8 0, i8* %2
%4 = load i32, i32* @O_RDWR
%5 = call i32* @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %4)
store i32* %5, i32** %3
%6 = icmp ult i32* %5, null
br i1 %6, label %7, label %12
7: ; preds = %0
%8 = load i32, i32* @stderr
%9 = load i32, i32* @errno
%10 = call i8* @strerror(i32 %9)
%11 = call i32 (i32, i8*, ...) @fprintf(i32 %8, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i8* %10)
ret i32 0
12: ; preds = %0
%13 = load i32*, i32** %3
%14 = call i64 @read(i32* %13, i8* %2, i32 1)
%15 = icmp slt i64 %14, 0
br i1 %15, label %16, label %21
16: ; preds = %12
%17 = load i32, i32* @stderr
%18 = load i32, i32* @errno
%19 = call i8* @strerror(i32 %18)
%20 = call i32 (i32, i8*, ...) @fprintf(i32 %17, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i8* %19)
br label %26
21: ; preds = %12
%22 = load i32, i32* @stdout
%23 = load i8, i8* %2
%24 = sext i8 %23 to i32
%25 = call i32 (i32, i8*, ...) @fprintf(i32 %22, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i32 %24)
br label %26
26: ; preds = %21, %16
store i8 24, i8* %2
%27 = load i32*, i32** %3
%28 = call i64 @write(i32* %27, i8* %2, i32 1)
%29 = icmp slt i64 %28, 0
br i1 %29, label %30, label %35
30: ; preds = %26
%31 = load i32, i32* @stderr
%32 = load i32, i32* @errno
%33 = call i8* @strerror(i32 %32)
%34 = call i32 (i32, i8*, ...) @fprintf(i32 %31, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.4, i64 0, i64 0), i8* %33)
br label %40
35: ; preds = %26
%36 = load i32, i32* @stdout
%37 = load i8, i8* %2
%38 = sext i8 %37 to i32
%39 = call i32 (i32, i8*, ...) @fprintf(i32 %36, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0), i32 %38)
br label %40
40: ; preds = %35, %30
br label %41
41: ; preds = %40, %41
store i8 20, i8* %2
%42 = load i32*, i32** %3
%43 = call i64 @write(i32* %42, i8* %2, i32 1)
store i8 8, i8* %2
%44 = load i32*, i32** %3
%45 = call i64 @write(i32* %44, i8* %2, i32 1)
br label %41
}

LLVM IR (-O2):
@.str = [13 x i8] c"/dev/radmonc\00"
@O_RDWR = global i32 0
@stderr = global i32 0
@errno = global i32 0
@.str.2 = [27 x i8] c"oh no, could not read: %s\0A\00"
@stdout = global i32 0
@.str.3 = [23 x i8] c"read from input: 0x%x\0A\00"
@.str.4 = [27 x i8] c"help! could not write: %s\0A\00"
@.str.5 = [21 x i8] c"wrote %x to output?\0A\00"
define i32 @main() {
%1 = alloca i8
store i8 0, i8* %1
%2 = load i32, i32* @O_RDWR
%3 = tail call i32* @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %2)
%4 = call i64 @read(i32* %3, i8* %1, i32 1)
%5 = icmp slt i64 %4, 0
br i1 %5, label %6, label %11
6: ; preds = %0
%7 = load i32, i32* @stderr
%8 = load i32, i32* @errno
%9 = tail call i8* @strerror(i32 %8)
%10 = tail call i32 (i32, i8*, ...) @fprintf(i32 %7, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i8* %9)
br label %16
11: ; preds = %0
%12 = load i32, i32* @stdout
%13 = load i8, i8* %1
%14 = sext i8 %13 to i32
%15 = tail call i32 (i32, i8*, ...) @fprintf(i32 %12, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0), i32 %14)
br label %16
16: ; preds = %11, %6
store i8 24, i8* %1
%17 = call i64 @write(i32* %3, i8* %1, i32 1)
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %24
19: ; preds = %16
%20 = load i32, i32* @stderr
%21 = load i32, i32* @errno
%22 = tail call i8* @strerror(i32 %21)
%23 = tail call i32 (i32, i8*, ...) @fprintf(i32 %20, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.4, i64 0, i64 0), i8* %22)
br label %27
24: ; preds = %16
%25 = load i32, i32* @stdout
%26 = tail call i32 (i32, i8*, ...) @fprintf(i32 %25, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.5, i64 0, i64 0), i32 24)
br label %27
27: ; preds = %24, %19
br label %28
28: ; preds = %27, %28
store i8 20, i8* %1
%29 = call i64 @write(i32* %3, i8* %1, i32 1)
store i8 8, i8* %1
%30 = call i64 @write(i32* %3, i8* %1, i32 1)
br label %28
}