#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__ int64_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fatal(char *, ...);
scalar_t__ find_offset(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ split_file(int /*<<< orphan*/ *, char *, scalar_t__);
int main(int argc, char *argv[]) {
  if (argc < 2)
    fatal("Usage: %s <image file>\n", argv[0]);
  FILE *in_file = fopen(argv[1], "rb");
  if (!in_file)
    fatal("Could not open input image for reading.\n");
  int64_t offset = find_offset(in_file);
  if (offset < 0)
    fatal(
        "Could not find image split offset. Is this a JunOS combined image?\n");
  split_file(in_file, argv[1], offset);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    1175 <main+0x35>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe92(%rip),%rdi
mov    $0x0,%al
call   1060 <fatal@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe94(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11a6 <main+0x66>
lea    0xe7c(%rip),%rdi
mov    $0x0,%al
call   1060 <fatal@plt>
mov    -0x18(%rbp),%rdi
call   1040 <find_offset@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jge    11cc <main+0x8c>
lea    0xe7f(%rip),%rdi
mov    $0x0,%al
call   1060 <fatal@plt>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
mov    -0x20(%rbp),%rdx
call   1030 <split_file@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r14
cmp    $0x1,%edi
jg     115e <main+0x1e>
mov    (%r14),%rsi
lea    0xea9(%rip),%rdi
xor    %eax,%eax
call   1060 <fatal@plt>
mov    0x8(%r14),%rdi
lea    0xeaf(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,%r15
test   %rax,%rax
jne    1184 <main+0x44>
lea    0xe9e(%rip),%rdi
xor    %eax,%eax
call   1060 <fatal@plt>
mov    %r15,%rdi
call   1040 <find_offset@plt>
mov    %rax,%rbx
test   %rax,%rax
jns    11a2 <main+0x62>
lea    0xea9(%rip),%rdi
xor    %eax,%eax
call   1060 <fatal@plt>
mov    0x8(%r14),%rsi
mov    %r15,%rdi
mov    %rbx,%rdx
call   1030 <split_file@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [24 x i8] c"Usage: %s <image file>\0A\00"
@.str.1 = [3 x i8] c"rb\00"
@.str.2 = [41 x i8] c"Could not open input image for reading.\0A\00"
@.str.3 = [68 x i8] c"Could not find image split offset. Is this a JunOS combined image?\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = load i32, i32* %4
%9 = icmp slt i32 %8, 2
br i1 %9, label %10, label %15
10: ; preds = %2
%11 = load i8**, i8*** %5
%12 = getelementptr inbounds i8*, i8** %11, i64 0
%13 = load i8*, i8** %12
%14 = call i32 (i8*, ...) @fatal(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %13)
br label %15
15: ; preds = %10, %2
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 1
%18 = load i8*, i8** %17
%19 = call i32* @fopen(i8* %18, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %19, i32** %6
%20 = load i32*, i32** %6
%21 = icmp ne i32* %20, null
br i1 %21, label %24, label %22
22: ; preds = %15
%23 = call i32 (i8*, ...) @fatal(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0))
br label %24
24: ; preds = %22, %15
%25 = load i32*, i32** %6
%26 = call i64 @find_offset(i32* %25)
store i64 %26, i64* %7
%27 = load i64, i64* %7
%28 = icmp slt i64 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %24
%30 = call i32 (i8*, ...) @fatal(i8* getelementptr inbounds ([68 x i8], [68 x i8]* @.str.3, i64 0, i64 0))
br label %31
31: ; preds = %29, %24
%32 = load i32*, i32** %6
%33 = load i8**, i8*** %5
%34 = getelementptr inbounds i8*, i8** %33, i64 1
%35 = load i8*, i8** %34
%36 = load i64, i64* %7
%37 = call i32 @split_file(i32* %32, i8* %35, i64 %36)
ret i32 0
}

LLVM IR (-O2):
@.str = [24 x i8] c"Usage: %s <image file>\0A\00"
@.str.1 = [3 x i8] c"rb\00"
@.str.2 = [41 x i8] c"Could not open input image for reading.\0A\00"
@.str.3 = [68 x i8] c"Could not find image split offset. Is this a JunOS combined image?\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp slt i32 %0, 2
br i1 %3, label %4, label %7
4: ; preds = %2
%5 = load i8*, i8** %1
%6 = tail call i32 (i8*, ...) @fatal(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %5)
br label %7
7: ; preds = %4, %2
%8 = getelementptr inbounds i8*, i8** %1, i64 1
%9 = load i8*, i8** %8
%10 = tail call i32* @fopen(i8* %9, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%11 = icmp eq i32* %10, null
br i1 %11, label %12, label %14
12: ; preds = %7
%13 = tail call i32 (i8*, ...) @fatal(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0))
br label %14
14: ; preds = %12, %7
%15 = tail call i64 @find_offset(i32* %10)
%16 = icmp slt i64 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %14
%18 = tail call i32 (i8*, ...) @fatal(i8* getelementptr inbounds ([68 x i8], [68 x i8]* @.str.3, i64 0, i64 0))
br label %19
19: ; preds = %17, %14
%20 = load i8*, i8** %8
%21 = tail call i32 @split_file(i32* %10, i8* %20, i64 %15)
ret i32 0
}