#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*/ uint64_t;
typedef int uint32_t;
struct stat {
  int st_size;
};
struct beri_debug {
  int dummy;
};

/* Variables and functions */
int *MAP_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ O_RDWR;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ free(int *);
int /*<<< orphan*/ hex2addr(char const *, int /*<<< orphan*/ *);
int *malloc(int);
int /*<<< orphan*/ memcpy(int *, int *, int);
int *mmap(int /*<<< orphan*/, int, int, int /*<<< orphan*/, int, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ read(int, int *, int);
char *realpath(char const *, int /*<<< orphan*/ *);
int /*<<< orphan*/ stat(char *, struct stat *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ warn(char *, char const *);
int berictl_loaddram_sockit(struct beri_debug *bdp, const char *addrp,
                            const char *filep) {
  char *realfilep;
  uint32_t filefd;
  struct stat st;
  uint32_t memfd;
  uint32_t *map;
  uint32_t *buf;
  uint32_t addr;
  int filesize;
  if (filep == NULL)
    return ((-1));
  if (addrp == NULL)
    return ((-1));
  if ((realfilep = realpath(filep, NULL)) == NULL) {
    warn("realpath(%s)", filep);
    return ((-1));
  }
  memfd = open("/dev/beri_mem", O_RDWR);
  filefd = open(realfilep, O_RDONLY);
  hex2addr(addrp, (uint64_t *)&addr);
  stat(realfilep, &st);
  filesize = st.st_size;
  fprintf(stdout, "Writing %d bytes at 0x%08x\n", filesize, addr);
  buf = malloc(filesize);
  if (buf == NULL) {
    fprintf(stderr, "Failed to malloc(%d)\n", filesize);
    return (18);
  }
  read(filefd, buf, filesize);
  map = mmap(0, filesize, PROT_READ | PROT_WRITE, MAP_SHARED, memfd, addr);
  if (map == MAP_FAILED) {
    fprintf(stderr, "Failed to mmap()\n");
    return (9);
  };
  memcpy(map, buf, filesize);
  free(buf);
  close(filefd);
  close(memfd);
  return (0);
}


x86 assembly (clang, -O0):
<berictl_loaddram_sockit>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11eb <berictl_loaddram_sockit+0x2b>
movl   $0xffffffff,-0x4(%rbp)
jmp    1387 <berictl_loaddram_sockit+0x1c7>
cmpq   $0x0,-0x18(%rbp)
jne    1202 <berictl_loaddram_sockit+0x42>
movl   $0xffffffff,-0x4(%rbp)
jmp    1387 <berictl_loaddram_sockit+0x1c7>
mov    -0x20(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   10a0 <realpath@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    1239 <berictl_loaddram_sockit+0x79>
mov    -0x20(%rbp),%rsi
lea    0xdd8(%rip),%rdi
call   1040 <warn@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1387 <berictl_loaddram_sockit+0x1c7>
mov    0x2d98(%rip),%rax
mov    (%rax),%esi
lea    0xdc4(%rip),%rdi
call   10d0 <open@plt>
mov    %eax,-0x34(%rbp)
mov    -0x28(%rbp),%rdi
mov    0x2d74(%rip),%rax
mov    (%rax),%esi
call   10d0 <open@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x18(%rbp),%rdi
lea    -0x4c(%rbp),%rsi
call   10e0 <hex2addr@plt>
mov    -0x28(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   1090 <stat@plt>
mov    -0x30(%rbp),%eax
mov    %eax,-0x50(%rbp)
mov    0x2d23(%rip),%rax
mov    (%rax),%edi
mov    -0x50(%rbp),%edx
mov    -0x4c(%rbp),%ecx
lea    0xd7f(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    -0x50(%rbp),%edi
call   10c0 <malloc@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
jne    12e0 <berictl_loaddram_sockit+0x120>
mov    0x2cff(%rip),%rax
mov    (%rax),%edi
mov    -0x50(%rbp),%edx
lea    0xd6a(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
movl   $0x12,-0x4(%rbp)
jmp    1387 <berictl_loaddram_sockit+0x1c7>
mov    -0x2c(%rbp),%edi
mov    -0x48(%rbp),%rsi
mov    -0x50(%rbp),%edx
call   1070 <read@plt>
mov    -0x50(%rbp),%esi
mov    0x2cff(%rip),%rax
mov    (%rax),%edx
mov    0x2cb6(%rip),%rax
or     (%rax),%edx
mov    0x2cdd(%rip),%rax
mov    (%rax),%ecx
mov    -0x34(%rbp),%r8d
mov    -0x4c(%rbp),%r9d
xor    %edi,%edi
call   1050 <mmap@plt>
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rax
mov    0x2c7d(%rip),%rcx
cmp    (%rcx),%rax
jne    1357 <berictl_loaddram_sockit+0x197>
mov    0x2c85(%rip),%rax
mov    (%rax),%edi
lea    0xd09(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
movl   $0x9,-0x4(%rbp)
jmp    1387 <berictl_loaddram_sockit+0x1c7>
mov    -0x40(%rbp),%rdi
mov    -0x48(%rbp),%rsi
mov    -0x50(%rbp),%edx
call   10b0 <memcpy@plt>
mov    -0x48(%rbp),%rdi
call   1030 <free@plt>
mov    -0x2c(%rbp),%edi
call   1060 <close@plt>
mov    -0x34(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<berictl_loaddram_sockit>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    $0xffffffff,%r13d
test   %rsi,%rsi
je     133a <berictl_loaddram_sockit+0x17a>
mov    %rdx,%rbx
test   %rdx,%rdx
je     133a <berictl_loaddram_sockit+0x17a>
mov    %rsi,%rbp
mov    %rbx,%rdi
xor    %esi,%esi
call   10a0 <realpath@plt>
test   %rax,%rax
je     12eb <berictl_loaddram_sockit+0x12b>
mov    %rax,%r12
mov    0x2dcf(%rip),%rax
mov    (%rax),%esi
lea    0xdfb(%rip),%rdi
call   10d0 <open@plt>
mov    %eax,%r14d
mov    0x2daf(%rip),%rax
mov    (%rax),%esi
mov    %r12,%rdi
call   10d0 <open@plt>
mov    %eax,%r15d
lea    0xc(%rsp),%rsi
mov    %rbp,%rdi
call   10e0 <hex2addr@plt>
lea    0x10(%rsp),%rsi
mov    %r12,%rdi
call   1090 <stat@plt>
mov    0x10(%rsp),%ebp
mov    0x2d5d(%rip),%rax
mov    (%rax),%edi
mov    0xc(%rsp),%ecx
lea    0xdbb(%rip),%rsi
mov    %ebp,%edx
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    %ebp,%edi
call   10c0 <malloc@plt>
test   %rax,%rax
je     12fc <berictl_loaddram_sockit+0x13c>
mov    %rax,%r12
mov    %r15d,%edi
mov    %rax,%rsi
mov    %ebp,%edx
call   1070 <read@plt>
mov    0x2d68(%rip),%rax
mov    0x2d21(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2d46(%rip),%rax
mov    (%rax),%ecx
mov    0xc(%rsp),%r9d
xor    %r13d,%r13d
xor    %edi,%edi
mov    %ebp,%esi
mov    %r14d,%r8d
call   1050 <mmap@plt>
mov    0x2ce9(%rip),%rcx
cmp    (%rcx),%rax
je     131d <berictl_loaddram_sockit+0x15d>
mov    %rax,%rdi
mov    %r12,%rsi
mov    %ebp,%edx
call   10b0 <memcpy@plt>
mov    %r12,%rdi
call   1030 <free@plt>
mov    %r15d,%edi
call   1060 <close@plt>
mov    %r14d,%edi
call   1060 <close@plt>
jmp    133a <berictl_loaddram_sockit+0x17a>
lea    0xd0e(%rip),%rdi
mov    %rbx,%rsi
call   1040 <warn@plt>
jmp    133a <berictl_loaddram_sockit+0x17a>
mov    0x2cbd(%rip),%rax
mov    (%rax),%edi
lea    0xd2b(%rip),%rsi
mov    %ebp,%edx
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    $0x12,%r13d
jmp    133a <berictl_loaddram_sockit+0x17a>
mov    0x2c9c(%rip),%rax
mov    (%rax),%edi
lea    0xd20(%rip),%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    $0x9,%r13d
mov    %r13d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.beri_debug = type { i32 }
%struct.stat = type { i32 }
@.str = [13 x i8] c"realpath(%s)\00"
@.str.1 = [14 x i8] c"/dev/beri_mem\00"
@O_RDWR = global i32 0
@O_RDONLY = global i32 0
@stdout = global i32 0
@.str.2 = [28 x i8] c"Writing %d bytes at 0x%08x\0A\00"
@stderr = global i32 0
@.str.3 = [22 x i8] c"Failed to malloc(%d)\0A\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i32* null
@.str.4 = [18 x i8] c"Failed to mmap()\0A\00"
define i32 @berictl_loaddram_sockit(%struct.beri_debug* %0, i8* %1, i8* %2) {
%4 = alloca i32
%5 = alloca %struct.beri_debug*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32
%10 = alloca %struct.stat
%11 = alloca i32
%12 = alloca i32*
%13 = alloca i32*
%14 = alloca i32
%15 = alloca i32
store %struct.beri_debug* %0, %struct.beri_debug** %5
store i8* %1, i8** %6
store i8* %2, i8** %7
%16 = load i8*, i8** %7
%17 = icmp eq i8* %16, null
br i1 %17, label %18, label %19
18: ; preds = %3
store i32 -1, i32* %4
br label %84
19: ; preds = %3
%20 = load i8*, i8** %6
%21 = icmp eq i8* %20, null
br i1 %21, label %22, label %23
22: ; preds = %19
store i32 -1, i32* %4
br label %84
23: ; preds = %19
%24 = load i8*, i8** %7
%25 = call i8* @realpath(i8* %24, i32* null)
store i8* %25, i8** %8
%26 = icmp eq i8* %25, null
br i1 %26, label %27, label %30
27: ; preds = %23
%28 = load i8*, i8** %7
%29 = call i32 @warn(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* %28)
store i32 -1, i32* %4
br label %84
30: ; preds = %23
%31 = load i32, i32* @O_RDWR
%32 = call i32 @open(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 %31)
store i32 %32, i32* %11
%33 = load i8*, i8** %8
%34 = load i32, i32* @O_RDONLY
%35 = call i32 @open(i8* %33, i32 %34)
store i32 %35, i32* %9
%36 = load i8*, i8** %6
%37 = call i32 @hex2addr(i8* %36, i32* %14)
%38 = load i8*, i8** %8
%39 = call i32 @stat(i8* %38, %struct.stat* %10)
%40 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 0
%41 = load i32, i32* %40
store i32 %41, i32* %15
%42 = load i32, i32* @stdout
%43 = load i32, i32* %15
%44 = load i32, i32* %14
%45 = call i32 (i32, i8*, ...) @fprintf(i32 %42, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i32 %43, i32 %44)
%46 = load i32, i32* %15
%47 = call i32* @malloc(i32 %46)
store i32* %47, i32** %13
%48 = load i32*, i32** %13
%49 = icmp eq i32* %48, null
br i1 %49, label %50, label %54
50: ; preds = %30
%51 = load i32, i32* @stderr
%52 = load i32, i32* %15
%53 = call i32 (i32, i8*, ...) @fprintf(i32 %51, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i32 %52)
store i32 18, i32* %4
br label %84
54: ; preds = %30
%55 = load i32, i32* %9
%56 = load i32*, i32** %13
%57 = load i32, i32* %15
%58 = call i32 @read(i32 %55, i32* %56, i32 %57)
%59 = load i32, i32* %15
%60 = load i32, i32* @PROT_READ
%61 = load i32, i32* @PROT_WRITE
%62 = or i32 %60, %61
%63 = load i32, i32* @MAP_SHARED
%64 = load i32, i32* %11
%65 = load i32, i32* %14
%66 = call i32* @mmap(i32 0, i32 %59, i32 %62, i32 %63, i32 %64, i32 %65)
store i32* %66, i32** %12
%67 = load i32*, i32** %12
%68 = load i32*, i32** @MAP_FAILED
%69 = icmp eq i32* %67, %68
br i1 %69, label %70, label %73
70: ; preds = %54
%71 = load i32, i32* @stderr
%72 = call i32 (i32, i8*, ...) @fprintf(i32 %71, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0))
store i32 9, i32* %4
br label %84
73: ; preds = %54
%74 = load i32*, i32** %12
%75 = load i32*, i32** %13
%76 = load i32, i32* %15
%77 = call i32 @memcpy(i32* %74, i32* %75, i32 %76)
%78 = load i32*, i32** %13
%79 = call i32 @free(i32* %78)
%80 = load i32, i32* %9
%81 = call i32 @close(i32 %80)
%82 = load i32, i32* %11
%83 = call i32 @close(i32 %82)
store i32 0, i32* %4
br label %84
84: ; preds = %73, %70, %50, %27, %22, %18
%85 = load i32, i32* %4
ret i32 %85
}

LLVM IR (-O2):
%struct.beri_debug = type { i32 }
%struct.stat = type { i32 }
@.str = [13 x i8] c"realpath(%s)\00"
@.str.1 = [14 x i8] c"/dev/beri_mem\00"
@O_RDWR = global i32 0
@O_RDONLY = global i32 0
@stdout = global i32 0
@.str.2 = [28 x i8] c"Writing %d bytes at 0x%08x\0A\00"
@stderr = global i32 0
@.str.3 = [22 x i8] c"Failed to malloc(%d)\0A\00"
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i32* null
@.str.4 = [18 x i8] c"Failed to mmap()\0A\00"
define i32 @berictl_loaddram_sockit(%struct.beri_debug* nocapture readnone %0, i8* %1, i8* %2) {
%4 = alloca %struct.stat
%5 = alloca i32
%6 = bitcast %struct.stat* %4 to i8*
%7 = bitcast i32* %5 to i8*
%8 = icmp eq i8* %2, null
%9 = icmp eq i8* %1, null
%10 = or i1 %9, %8
br i1 %10, label %51, label %11
11: ; preds = %3
%12 = tail call i8* @realpath(i8* %2, i32* null)
%13 = icmp eq i8* %12, null
br i1 %13, label %14, label %16
14: ; preds = %11
%15 = tail call i32 @warn(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8* %2)
br label %51
16: ; preds = %11
%17 = load i32, i32* @O_RDWR
%18 = tail call i32 @open(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 %17)
%19 = load i32, i32* @O_RDONLY
%20 = tail call i32 @open(i8* %12, i32 %19)
%21 = call i32 @hex2addr(i8* %1, i32* %5)
%22 = call i32 @stat(i8* %12, %struct.stat* %4)
%23 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%24 = load i32, i32* %23
%25 = load i32, i32* @stdout
%26 = load i32, i32* %5
%27 = call i32 (i32, i8*, ...) @fprintf(i32 %25, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i32 %24, i32 %26)
%28 = call i32* @malloc(i32 %24)
%29 = icmp eq i32* %28, null
br i1 %29, label %30, label %33
30: ; preds = %16
%31 = load i32, i32* @stderr
%32 = call i32 (i32, i8*, ...) @fprintf(i32 %31, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0), i32 %24)
br label %51
33: ; preds = %16
%34 = call i32 @read(i32 %20, i32* %28, i32 %24)
%35 = load i32, i32* @PROT_READ
%36 = load i32, i32* @PROT_WRITE
%37 = or i32 %36, %35
%38 = load i32, i32* @MAP_SHARED
%39 = load i32, i32* %5
%40 = call i32* @mmap(i32 0, i32 %24, i32 %37, i32 %38, i32 %18, i32 %39)
%41 = load i32*, i32** @MAP_FAILED
%42 = icmp eq i32* %40, %41
br i1 %42, label %43, label %46
43: ; preds = %33
%44 = load i32, i32* @stderr
%45 = call i32 (i32, i8*, ...) @fprintf(i32 %44, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0))
br label %51
46: ; preds = %33
%47 = call i32 @memcpy(i32* %40, i32* %28, i32 %24)
%48 = call i32 @free(i32* %28)
%49 = call i32 @close(i32 %20)
%50 = call i32 @close(i32 %18)
br label %51
51: ; preds = %3, %46, %43, %30, %14
%52 = phi i32 [ -1, %14 ], [ 18, %30 ], [ 9, %43 ], [ 0, %46 ], [ -1, %3 ]
ret i32 %52
}