#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*/ STDOUT_FILENO;
int /*<<< orphan*/ _IONBF;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ setvbuf(int /*<<< orphan*/, int /*<<< orphan*/ *,
                           int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stdout;
int app_retarget_output(void) {
  int fd = -1;
  fd = open("/dev/ttySAC0", O_RDWR);
  if (fd < 0) {
    printf("can not open console\r\n");
  }
  dup2(fd, STDOUT_FILENO);
  close(fd);
  setvbuf(stdout, NULL, _IONBF, 0);
  return 0;
}


x86 assembly (clang, -O0):
<app_retarget_output>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0xffffffff,-0x4(%rbp)
mov    0x2e7a(%rip),%rax
mov    (%rax),%esi
lea    0xe91(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
jge    118d <app_retarget_output+0x3d>
lea    0xe85(%rip),%rdi
call   1040 <printf@plt>
mov    -0x4(%rbp),%edi
mov    0x2e59(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
mov    -0x4(%rbp),%edi
call   1050 <close@plt>
mov    0x2e1b(%rip),%rax
mov    (%rax),%edi
mov    0x2e22(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
xor    %ecx,%ecx
call   1060 <setvbuf@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<app_retarget_output>:
push   %rbx
mov    0x2e88(%rip),%rax
mov    (%rax),%esi
lea    0xe9f(%rip),%rdi
call   1070 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
jns    1178 <app_retarget_output+0x28>
lea    0xe9a(%rip),%rdi
call   1030 <puts@plt>
mov    0x2e71(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1040 <dup2@plt>
mov    %ebx,%edi
call   1050 <close@plt>
mov    0x2e32(%rip),%rax
mov    (%rax),%edi
mov    0x2e39(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
xor    %ecx,%ecx
call   1060 <setvbuf@plt>
xor    %eax,%eax
pop    %rbx
ret

LLVM IR (-O0):
@.str = [13 x i8] c"/dev/ttySAC0\00"
@O_RDWR = global i32 0
@.str.1 = [23 x i8] c"can not open console\0D\0A\00"
@STDOUT_FILENO = global i32 0
@stdout = global i32 0
@_IONBF = global i32 0
define i32 @app_retarget_output() {
%1 = alloca i32
store i32 -1, i32* %1
%2 = load i32, i32* @O_RDWR
%3 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %2)
store i32 %3, i32* %1
%4 = load i32, i32* %1
%5 = icmp slt i32 %4, 0
br i1 %5, label %6, label %8
6: ; preds = %0
%7 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
br label %8
8: ; preds = %6, %0
%9 = load i32, i32* %1
%10 = load i32, i32* @STDOUT_FILENO
%11 = call i32 @dup2(i32 %9, i32 %10)
%12 = load i32, i32* %1
%13 = call i32 @close(i32 %12)
%14 = load i32, i32* @stdout
%15 = load i32, i32* @_IONBF
%16 = call i32 @setvbuf(i32 %14, i32* null, i32 %15, i32 0)
ret i32 0
}

LLVM IR (-O2):
@.str = [13 x i8] c"/dev/ttySAC0\00"
@O_RDWR = global i32 0
@STDOUT_FILENO = global i32 0
@stdout = global i32 0
@_IONBF = global i32 0
@str = [22 x i8] c"can not open console\0D\00"
define i32 @app_retarget_output() {
%1 = load i32, i32* @O_RDWR
%2 = tail call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %1)
%3 = icmp slt i32 %2, 0
br i1 %3, label %4, label %6
4: ; preds = %0
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str, i64 0, i64 0))
br label %6
6: ; preds = %4, %0
%7 = load i32, i32* @STDOUT_FILENO
%8 = tail call i32 @dup2(i32 %2, i32 %7)
%9 = tail call i32 @close(i32 %2)
%10 = load i32, i32* @stdout
%11 = load i32, i32* @_IONBF
%12 = tail call i32 @setvbuf(i32 %10, i32* null, i32 %11, i32 0)
ret i32 0
}