#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 EINVAL;
int /*<<< orphan*/ O_WRONLY;
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ sprintf(char *, char *, unsigned int, char const *);
int strlen(char const *);
int /*<<< orphan*/ write(int, char *, int);
int qemu_add_mapping(unsigned int addr, const char *name) {
  char buf[((4 * 1024) + 20)];
  if (strlen(name) > (4 * 1024))
    return EINVAL;
  int fd = open("/sys/qemu_trace/symbol", O_WRONLY);
  if (fd < 0)
    return fd;
  sprintf(buf, "%x %s\n", addr, name);
  write(fd, buf, strlen(buf));
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<qemu_add_mapping>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1050,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
cmp    $0x1000,%eax
jle    1187 <qemu_add_mapping+0x37>
mov    0x2e63(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1223 <qemu_add_mapping+0xd3>
mov    0x2e42(%rip),%rax
mov    (%rax),%esi
lea    0xe69(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x1034(%rbp)
cmpl   $0x0,-0x1034(%rbp)
jge    11bd <qemu_add_mapping+0x6d>
mov    -0x1034(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1223 <qemu_add_mapping+0xd3>
lea    -0x1030(%rbp),%rdi
mov    -0x8(%rbp),%edx
mov    -0x10(%rbp),%rcx
lea    0xe45(%rip),%rsi
call   1070 <sprintf@plt>
mov    -0x1034(%rbp),%eax
mov    %eax,-0x1044(%rbp)
lea    -0x1030(%rbp),%rax
mov    %rax,-0x1040(%rbp)
lea    -0x1030(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x1044(%rbp),%edi
mov    -0x1040(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    -0x1034(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1050,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<qemu_add_mapping>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x1018,%rsp
mov    %rsi,%r14
mov    %edi,%ebp
mov    %rsi,%rdi
call   1040 <strlen@plt>
cmp    $0x1001,%eax
jl     117c <qemu_add_mapping+0x2c>
mov    0x2e68(%rip),%rax
mov    (%rax),%ebx
jmp    11cb <qemu_add_mapping+0x7b>
mov    0x2e4d(%rip),%rax
mov    (%rax),%esi
lea    0xe74(%rip),%rdi
call   1060 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
js     11cb <qemu_add_mapping+0x7b>
lea    0xe79(%rip),%rsi
mov    %rsp,%r15
mov    %r15,%rdi
mov    %ebp,%edx
mov    %r14,%rcx
call   1070 <sprintf@plt>
mov    %r15,%rdi
call   1040 <strlen@plt>
mov    %ebx,%edi
mov    %r15,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %ebx,%edi
call   1050 <close@plt>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x1018,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@EINVAL = global i32 0
@.str = [23 x i8] c"/sys/qemu_trace/symbol\00"
@O_WRONLY = global i32 0
@.str.1 = [7 x i8] c"%x %s\0A\00"
define i32 @qemu_add_mapping(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca [4116 x i8]
%7 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
%8 = load i8*, i8** %5
%9 = call i32 @strlen(i8* %8)
%10 = icmp sgt i32 %9, 4096
br i1 %10, label %11, label %13
11: ; preds = %2
%12 = load i32, i32* @EINVAL
store i32 %12, i32* %3
br label %32
13: ; preds = %2
%14 = load i32, i32* @O_WRONLY
%15 = call i32 @open(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i32 %14)
store i32 %15, i32* %7
%16 = load i32, i32* %7
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %13
%19 = load i32, i32* %7
store i32 %19, i32* %3
br label %32
20: ; preds = %13
%21 = getelementptr inbounds [4116 x i8], [4116 x i8]* %6, i64 0, i64 0
%22 = load i32, i32* %4
%23 = load i8*, i8** %5
%24 = call i32 @sprintf(i8* %21, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32 %22, i8* %23)
%25 = load i32, i32* %7
%26 = getelementptr inbounds [4116 x i8], [4116 x i8]* %6, i64 0, i64 0
%27 = getelementptr inbounds [4116 x i8], [4116 x i8]* %6, i64 0, i64 0
%28 = call i32 @strlen(i8* %27)
%29 = call i32 @write(i32 %25, i8* %26, i32 %28)
%30 = load i32, i32* %7
%31 = call i32 @close(i32 %30)
store i32 0, i32* %3
br label %32
32: ; preds = %20, %18, %11
%33 = load i32, i32* %3
ret i32 %33
}

LLVM IR (-O2):
@EINVAL = global i32 0
@.str = [23 x i8] c"/sys/qemu_trace/symbol\00"
@O_WRONLY = global i32 0
@.str.1 = [7 x i8] c"%x %s\0A\00"
define i32 @qemu_add_mapping(i32 %0, i8* %1) {
%3 = alloca [4116 x i8]
%4 = getelementptr inbounds [4116 x i8], [4116 x i8]* %3, i64 0, i64 0
%5 = tail call i32 @strlen(i8* %1)
%6 = icmp sgt i32 %5, 4096
br i1 %6, label %7, label %9
7: ; preds = %2
%8 = load i32, i32* @EINVAL
br label %18
9: ; preds = %2
%10 = load i32, i32* @O_WRONLY
%11 = tail call i32 @open(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i32 %10)
%12 = icmp slt i32 %11, 0
br i1 %12, label %18, label %13
13: ; preds = %9
%14 = call i32 @sprintf(i8* %4, i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32 %0, i8* %1)
%15 = call i32 @strlen(i8* %4)
%16 = call i32 @write(i32 %11, i8* %4, i32 %15)
%17 = call i32 @close(i32 %11)
br label %18
18: ; preds = %13, %9, %7
%19 = phi i32 [ %8, %7 ], [ 0, %13 ], [ %11, %9 ]
ret i32 %19
}