#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 /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ O_WRONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int load_ramdisk_compressed(char const *, int /*<<< orphan*/ *,
                            int /*<<< orphan*/);
int open(char const *, int /*<<< orphan*/);
int rd_copy_uncompressed(int, int);
int xpread(int, unsigned char *, int, int /*<<< orphan*/);
int rd_copy_image(const char *path) {
  int ffd = open(path, O_RDONLY);
  int rv = -1;
  unsigned char gzip_magic[2];
  if (ffd < 0)
    goto barf;
  if (xpread(ffd, gzip_magic, 2, 0) == 2 && gzip_magic[0] == 037 &&
      gzip_magic[1] == 0213) {
    FILE *wfd = fopen("/dev/ram0", "w");
    if (!wfd)
      goto barf;
    rv = load_ramdisk_compressed(path, wfd, 0);
    fclose(wfd);
  } else {
    int dfd = open("/dev/ram0", O_WRONLY);
    if (dfd < 0)
      goto barf;
    rv = rd_copy_uncompressed(ffd, dfd);
    close(dfd);
  }
barf:
  if (ffd >= 0)
    close(ffd);
  return rv;
}


x86 assembly (clang, -O0):
<rd_copy_image>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e61(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0xc(%rbp)
movl   $0xffffffff,-0x10(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    11a7 <rd_copy_image+0x37>
jmp    1268 <rd_copy_image+0xf8>
mov    -0xc(%rbp),%edi
lea    -0x12(%rbp),%rsi
mov    $0x2,%edx
xor    %ecx,%ecx
call   1060 <xpread@plt>
cmp    $0x2,%eax
jne    1226 <rd_copy_image+0xb6>
movzbl -0x12(%rbp),%eax
cmp    $0x1f,%eax
jne    1226 <rd_copy_image+0xb6>
movzbl -0x11(%rbp),%eax
cmp    $0x8b,%eax
jne    1226 <rd_copy_image+0xb6>
lea    0xe1a(%rip),%rdi
lea    0xe1d(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1206 <rd_copy_image+0x96>
jmp    1268 <rd_copy_image+0xf8>
mov    -0x8(%rbp),%rdi
mov    -0x20(%rbp),%rsi
xor    %edx,%edx
call   1030 <load_ramdisk_compressed@plt>
mov    %eax,-0x10(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
jmp    1263 <rd_copy_image+0xf3>
mov    0x2da3(%rip),%rax
mov    (%rax),%esi
lea    0xdca(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    124d <rd_copy_image+0xdd>
jmp    1268 <rd_copy_image+0xf8>
mov    -0xc(%rbp),%edi
mov    -0x24(%rbp),%esi
call   1070 <rd_copy_uncompressed@plt>
mov    %eax,-0x10(%rbp)
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
jmp    1268 <rd_copy_image+0xf8>
cmpl   $0x0,-0xc(%rbp)
jl     127a <rd_copy_image+0x10a>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
mov    -0x10(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rd_copy_image>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
mov    0x2e67(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
test   %eax,%eax
js     11e8 <rd_copy_image+0x78>
mov    %eax,%ebx
lea    0x6(%rsp),%rsi
mov    %eax,%edi
mov    $0x2,%edx
xor    %ecx,%ecx
call   1060 <xpread@plt>
cmp    $0x2,%eax
jne    11ef <rd_copy_image+0x7f>
cmpb   $0x1f,0x6(%rsp)
jne    11ef <rd_copy_image+0x7f>
cmpb   $0x8b,0x7(%rsp)
jne    11ef <rd_copy_image+0x7f>
lea    0xe45(%rip),%rdi
lea    0xe48(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     1220 <rd_copy_image+0xb0>
mov    %rax,%r15
mov    %r14,%rdi
mov    %rax,%rsi
xor    %edx,%edx
call   1030 <load_ramdisk_compressed@plt>
mov    %eax,%ebp
mov    %r15,%rdi
call   1040 <fclose@plt>
jmp    1225 <rd_copy_image+0xb5>
mov    $0xffffffff,%ebp
jmp    122c <rd_copy_image+0xbc>
mov    0x2dda(%rip),%rax
mov    (%rax),%esi
lea    0xe01(%rip),%rdi
call   1080 <open@plt>
test   %eax,%eax
js     1220 <rd_copy_image+0xb0>
mov    %eax,%r14d
mov    %ebx,%edi
mov    %eax,%esi
call   1070 <rd_copy_uncompressed@plt>
mov    %eax,%ebp
mov    %r14d,%edi
call   1050 <close@plt>
jmp    1225 <rd_copy_image+0xb5>
mov    $0xffffffff,%ebp
mov    %ebx,%edi
call   1050 <close@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_RDONLY = global i32 0
@.str = [10 x i8] c"/dev/ram0\00"
@.str.1 = [2 x i8] c"w\00"
@O_WRONLY = global i32 0
define i32 @rd_copy_image(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca [2 x i8]
%6 = alloca i32*
%7 = alloca i32
store i8* %0, i8** %2
%8 = load i8*, i8** %2
%9 = load i32, i32* @O_RDONLY
%10 = call i32 @open(i8* %8, i32 %9)
store i32 %10, i32* %3
store i32 -1, i32* %4
%11 = load i32, i32* %3
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %14
13: ; preds = %1
br label %53
14: ; preds = %1
%15 = load i32, i32* %3
%16 = getelementptr inbounds [2 x i8], [2 x i8]* %5, i64 0, i64 0
%17 = call i32 @xpread(i32 %15, i8* %16, i32 2, i32 0)
%18 = icmp eq i32 %17, 2
br i1 %18, label %19, label %40
19: ; preds = %14
%20 = getelementptr inbounds [2 x i8], [2 x i8]* %5, i64 0, i64 0
%21 = load i8, i8* %20
%22 = zext i8 %21 to i32
%23 = icmp eq i32 %22, 31
br i1 %23, label %24, label %40
24: ; preds = %19
%25 = getelementptr inbounds [2 x i8], [2 x i8]* %5, i64 0, i64 1
%26 = load i8, i8* %25
%27 = zext i8 %26 to i32
%28 = icmp eq i32 %27, 139
br i1 %28, label %29, label %40
29: ; preds = %24
%30 = call i32* @fopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %30, i32** %6
%31 = load i32*, i32** %6
%32 = icmp ne i32* %31, null
br i1 %32, label %34, label %33
33: ; preds = %29
br label %53
34: ; preds = %29
%35 = load i8*, i8** %2
%36 = load i32*, i32** %6
%37 = call i32 @load_ramdisk_compressed(i8* %35, i32* %36, i32 0)
store i32 %37, i32* %4
%38 = load i32*, i32** %6
%39 = call i32 @fclose(i32* %38)
br label %52
40: ; preds = %24, %19, %14
%41 = load i32, i32* @O_WRONLY
%42 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %41)
store i32 %42, i32* %7
%43 = load i32, i32* %7
%44 = icmp slt i32 %43, 0
br i1 %44, label %45, label %46
45: ; preds = %40
br label %53
46: ; preds = %40
%47 = load i32, i32* %3
%48 = load i32, i32* %7
%49 = call i32 @rd_copy_uncompressed(i32 %47, i32 %48)
store i32 %49, i32* %4
%50 = load i32, i32* %7
%51 = call i32 @close(i32 %50)
br label %52
52: ; preds = %46, %34
br label %53
53: ; preds = %52, %45, %33, %13
%54 = load i32, i32* %3
%55 = icmp sge i32 %54, 0
br i1 %55, label %56, label %59
56: ; preds = %53
%57 = load i32, i32* %3
%58 = call i32 @close(i32 %57)
br label %59
59: ; preds = %56, %53
%60 = load i32, i32* %4
ret i32 %60
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@.str = [10 x i8] c"/dev/ram0\00"
@.str.1 = [2 x i8] c"w\00"
@O_WRONLY = global i32 0
define i32 @rd_copy_image(i8* %0) {
%2 = alloca [2 x i8]
%3 = load i32, i32* @O_RDONLY
%4 = tail call i32 @open(i8* %0, i32 %3)
%5 = getelementptr inbounds [2 x i8], [2 x i8]* %2, i64 0, i64 0
%6 = icmp slt i32 %4, 0
br i1 %6, label %33, label %7
7: ; preds = %1
%8 = call i32 @xpread(i32 %4, i8* %5, i32 2, i32 0)
%9 = icmp eq i32 %8, 2
%10 = load i8, i8* %5
%11 = icmp eq i8 %10, 31
%12 = select i1 %9, i1 %11, i1 false
%13 = getelementptr inbounds [2 x i8], [2 x i8]* %2, i64 0, i64 1
%14 = load i8, i8* %13
%15 = icmp eq i8 %14, -117
%16 = select i1 %12, i1 %15, i1 false
br i1 %16, label %17, label %23
17: ; preds = %7
%18 = call i32* @fopen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%19 = icmp eq i32* %18, null
br i1 %19, label %30, label %20
20: ; preds = %17
%21 = call i32 @load_ramdisk_compressed(i8* %0, i32* %18, i32 0)
%22 = call i32 @fclose(i32* %18)
br label %30
23: ; preds = %7
%24 = load i32, i32* @O_WRONLY
%25 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %24)
%26 = icmp slt i32 %25, 0
br i1 %26, label %30, label %27
27: ; preds = %23
%28 = call i32 @rd_copy_uncompressed(i32 %4, i32 %25)
%29 = call i32 @close(i32 %25)
br label %30
30: ; preds = %27, %23, %20, %17
%31 = phi i32 [ -1, %23 ], [ %28, %27 ], [ -1, %17 ], [ %21, %20 ]
%32 = call i32 @close(i32 %4)
br label %33
33: ; preds = %1, %30
%34 = phi i32 [ %31, %30 ], [ -1, %1 ]
ret i32 %34
}