#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 scalar_t__ uint32_t;

/* Variables and functions */
int __semihost(int, scalar_t__ *);
int semihost_write(int fh, const unsigned char *buffer, unsigned int length,
                   int mode) {
  if (length == 0)
    return 0;
  uint32_t args[3];
  args[0] = (uint32_t)fh;
  args[1] = (uint32_t)buffer;
  args[2] = (uint32_t)length;
  return __semihost((0x5), args);
}


x86 assembly (clang, -O0):
<semihost_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
cmpl   $0x0,-0x14(%rbp)
jne    113b <semihost_write+0x2b>
movl   $0x0,-0x4(%rbp)
jmp    1163 <semihost_write+0x53>
movslq -0x8(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x14(%rbp),%eax
mov    %rax,-0x20(%rbp)
lea    -0x30(%rbp),%rsi
mov    $0x5,%edi
call   1030 <__semihost@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<semihost_write>:
test   %edx,%edx
je     113d <semihost_write+0x2d>
sub    $0x18,%rsp
movslq %edi,%rax
mov    %rax,(%rsp)
mov    %rsi,0x8(%rsp)
mov    %edx,%eax
mov    %rax,0x10(%rsp)
mov    %rsp,%rsi
mov    $0x5,%edi
call   1030 <__semihost@plt>
add    $0x18,%rsp
ret
xor    %eax,%eax
ret

LLVM IR (-O0):
define i32 @semihost_write(i32 %0, i8* %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca [3 x i64]
store i32 %0, i32* %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store i32 %3, i32* %9
%11 = load i32, i32* %8
%12 = icmp eq i32 %11, 0
br i1 %12, label %13, label %14
13: ; preds = %4
store i32 0, i32* %5
br label %26
14: ; preds = %4
%15 = load i32, i32* %6
%16 = sext i32 %15 to i64
%17 = getelementptr inbounds [3 x i64], [3 x i64]* %10, i64 0, i64 0
store i64 %16, i64* %17
%18 = load i8*, i8** %7
%19 = ptrtoint i8* %18 to i64
%20 = getelementptr inbounds [3 x i64], [3 x i64]* %10, i64 0, i64 1
store i64 %19, i64* %20
%21 = load i32, i32* %8
%22 = zext i32 %21 to i64
%23 = getelementptr inbounds [3 x i64], [3 x i64]* %10, i64 0, i64 2
store i64 %22, i64* %23
%24 = getelementptr inbounds [3 x i64], [3 x i64]* %10, i64 0, i64 0
%25 = call i32 @__semihost(i32 5, i64* %24)
store i32 %25, i32* %5
br label %26
26: ; preds = %14, %13
%27 = load i32, i32* %5
ret i32 %27
}

LLVM IR (-O2):
define i32 @semihost_write(i32 %0, i8* %1, i32 %2, i32 %3) {
%5 = alloca [3 x i64]
%6 = icmp eq i32 %2, 0
br i1 %6, label %16, label %7
7: ; preds = %4
%8 = bitcast [3 x i64]* %5 to i8*
%9 = sext i32 %0 to i64
%10 = getelementptr inbounds [3 x i64], [3 x i64]* %5, i64 0, i64 0
store i64 %9, i64* %10
%11 = ptrtoint i8* %1 to i64
%12 = getelementptr inbounds [3 x i64], [3 x i64]* %5, i64 0, i64 1
store i64 %11, i64* %12
%13 = zext i32 %2 to i64
%14 = getelementptr inbounds [3 x i64], [3 x i64]* %5, i64 0, i64 2
store i64 %13, i64* %14
%15 = call i32 @__semihost(i32 5, i64* %10)
br label %16
16: ; preds = %4, %7
%17 = phi i32 [ %15, %7 ], [ 0, %4 ]
ret i32 %17
}