#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 EIO;
int ENOENT;
int /*<<< orphan*/ error_printk(char *, char *);
int /*<<< orphan*/ *fopen(char *, char *);
int fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printk(char *, char *, char *);
int main(void) {
  int ret = 0;
  char buff[1024];
  FILE *f;
  f = fopen("/home/raphio/test_semihosting", "rb");
  if (!f) {
    error_printk("failed to open: %s\n", "/home/raphio/test_semihosting");
    ret = -ENOENT;
    goto out;
  }
  memset(buff, 0, 1024);
  ret = fread(buff, 1024, sizeof(char), f);
  if (ret < 0) {
    error_printk("failed to read from: %s\n", "/home/raphio/test_semihosting");
    ret = -EIO;
    goto out;
  }
  printk("content from [%s]: %s\n", "/home/raphio/test_semihosting", buff);
out:
  return ret;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
lea    0xe90(%rip),%rdi
lea    0xea7(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x418(%rbp)
cmpq   $0x0,-0x418(%rbp)
jne    11b7 <main+0x67>
lea    0xe89(%rip),%rdi
lea    0xe61(%rip),%rsi
call   1040 <error_printk@plt>
xor    %eax,%eax
mov    0x2e23(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x8(%rbp)
jmp    1234 <main+0xe4>
lea    -0x410(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1060 <memset@plt>
lea    -0x410(%rbp),%rdi
mov    -0x418(%rbp),%rcx
mov    $0x400,%esi
mov    $0x1,%edx
call   1050 <fread@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    121a <main+0xca>
lea    0xe3a(%rip),%rdi
lea    0xdfe(%rip),%rsi
call   1040 <error_printk@plt>
xor    %eax,%eax
mov    0x2dd0(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x8(%rbp)
jmp    1234 <main+0xe4>
lea    -0x410(%rbp),%rdx
lea    0xe26(%rip),%rdi
lea    0xdd1(%rip),%rsi
call   1030 <printk@plt>
mov    -0x8(%rbp),%eax
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x400,%rsp
lea    0xe9e(%rip),%rdi
lea    0xeb5(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     11bf <main+0x6f>
mov    %rax,%rbx
mov    %rsp,%r14
xor    %ebp,%ebp
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1060 <memset@plt>
mov    %r14,%rdi
mov    $0x400,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1050 <fread@plt>
test   %eax,%eax
js     11dd <main+0x8d>
mov    %eax,%ebx
lea    0xea2(%rip),%rdi
lea    0xe4d(%rip),%rsi
mov    %rsp,%rdx
call   1030 <printk@plt>
mov    %ebx,%ebp
jmp    11f9 <main+0xa9>
lea    0xe5b(%rip),%rdi
lea    0xe33(%rip),%rsi
call   1040 <error_printk@plt>
mov    0x2df7(%rip),%rax
xor    %ebp,%ebp
jmp    11f7 <main+0xa7>
lea    0xe51(%rip),%rdi
lea    0xe15(%rip),%rsi
call   1040 <error_printk@plt>
mov    0x2de9(%rip),%rax
sub    (%rax),%ebp
mov    %ebp,%eax
add    $0x400,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [30 x i8] c"/home/raphio/test_semihosting\00"
@.str.1 = [3 x i8] c"rb\00"
@.str.2 = [20 x i8] c"failed to open: %s\0A\00"
@ENOENT = global i32 0
@.str.3 = [25 x i8] c"failed to read from: %s\0A\00"
@EIO = global i32 0
@.str.4 = [23 x i8] c"content from [%s]: %s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [1024 x i8]
%4 = alloca i32*
store i32 0, i32* %1
store i32 0, i32* %2
%5 = call i32* @fopen(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %5, i32** %4
%6 = load i32*, i32** %4
%7 = icmp ne i32* %6, null
br i1 %7, label %12, label %8
8: ; preds = %0
%9 = call i32 @error_printk(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
%10 = load i32, i32* @ENOENT
%11 = sub nsw i32 0, %10
store i32 %11, i32* %2
br label %27
12: ; preds = %0
%13 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%14 = call i32 @memset(i8* %13, i32 0, i32 1024)
%15 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%16 = load i32*, i32** %4
%17 = call i32 @fread(i8* %15, i32 1024, i32 1, i32* %16)
store i32 %17, i32* %2
%18 = load i32, i32* %2
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %24
20: ; preds = %12
%21 = call i32 @error_printk(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
%22 = load i32, i32* @EIO
%23 = sub nsw i32 0, %22
store i32 %23, i32* %2
br label %27
24: ; preds = %12
%25 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%26 = call i32 @printk(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* %25)
br label %27
27: ; preds = %24, %20, %8
%28 = load i32, i32* %2
ret i32 %28
}

LLVM IR (-O2):
@.str = [30 x i8] c"/home/raphio/test_semihosting\00"
@.str.1 = [3 x i8] c"rb\00"
@.str.2 = [20 x i8] c"failed to open: %s\0A\00"
@ENOENT = global i32 0
@.str.3 = [25 x i8] c"failed to read from: %s\0A\00"
@EIO = global i32 0
@.str.4 = [23 x i8] c"content from [%s]: %s\0A\00"
define i32 @main() {
%1 = alloca [1024 x i8]
%2 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 0
%3 = tail call i32* @fopen(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %5, label %9
5: ; preds = %0
%6 = tail call i32 @error_printk(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
%7 = load i32, i32* @ENOENT
%8 = sub nsw i32 0, %7
br label %19
9: ; preds = %0
%10 = call i32 @memset(i8* %2, i32 0, i32 1024)
%11 = call i32 @fread(i8* %2, i32 1024, i32 1, i32* %3)
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %17
13: ; preds = %9
%14 = call i32 @error_printk(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
%15 = load i32, i32* @EIO
%16 = sub nsw i32 0, %15
br label %19
17: ; preds = %9
%18 = call i32 @printk(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i8* %2)
br label %19
19: ; preds = %17, %13, %5
%20 = phi i32 [ %16, %13 ], [ %11, %17 ], [ %8, %5 ]
ret i32 %20
}