#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 O_CREAT;
int /*<<< orphan*/ O_RDONLY;
int O_TRUNC;
int O_WRONLY;
int S_IRUSR;
int S_IWUSR;
int /*<<< orphan*/ close(int);
int mx28_create_nand_image(int, int);
int mx28_create_sd_image(int, int);
int open(char *, int, ...);
int parse_ops(int, char **);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ strcmp(char *, char *);
int /*<<< orphan*/ usage();
int main(int argc, char **argv) {
  int infd, outfd;
  int ret = 0;
  int offset;
  offset = parse_ops(argc, argv);
  if (offset < 0) {
    usage();
    ret = 1;
    goto err1;
  }
  infd = open(argv[offset + 1], O_RDONLY);
  if (infd < 0) {
    printf("Input BootStream file can not be opened\n");
    ret = 2;
    goto err1;
  }
  outfd =
      open(argv[offset + 2], O_CREAT | O_TRUNC | O_WRONLY, S_IRUSR | S_IWUSR);
  if (outfd < 0) {
    printf("Output file can not be created\n");
    ret = 3;
    goto err2;
  }
  if (!strcmp(argv[offset], "sd"))
    ret = mx28_create_sd_image(infd, outfd);
  else if (!strcmp(argv[offset], "nand"))
    ret = mx28_create_nand_image(infd, outfd);
  close(outfd);
err2:
  close(infd);
err1:
  return ret;
}


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)
movl   $0x0,-0x1c(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
call   10a0 <parse_ops@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    11c9 <main+0x49>
mov    $0x0,%al
call   1080 <usage@plt>
movl   $0x1,-0x1c(%rbp)
jmp    12f1 <main+0x171>
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,8),%rdi
mov    0x2df7(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1090 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    120f <main+0x8f>
lea    0xe02(%rip),%rdi
call   1040 <printf@plt>
movl   $0x2,-0x1c(%rbp)
jmp    12f1 <main+0x171>
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%ecx
add    $0x2,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,8),%rdi
mov    0x2dd1(%rip),%rax
mov    (%rax),%esi
mov    0x2dc0(%rip),%rax
or     (%rax),%esi
mov    0x2d77(%rip),%rax
or     (%rax),%esi
mov    0x2d7e(%rip),%rax
mov    (%rax),%edx
mov    0x2d85(%rip),%rax
or     (%rax),%edx
mov    $0x0,%al
call   1090 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    1279 <main+0xf9>
lea    0xdc1(%rip),%rdi
call   1040 <printf@plt>
movl   $0x3,-0x1c(%rbp)
jmp    12e9 <main+0x169>
mov    -0x10(%rbp),%rax
movslq -0x20(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
lea    0xdbd(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%eax
jne    12ad <main+0x12d>
mov    -0x14(%rbp),%edi
mov    -0x18(%rbp),%esi
call   1050 <mx28_create_sd_image@plt>
mov    %eax,-0x1c(%rbp)
jmp    12e1 <main+0x161>
mov    -0x10(%rbp),%rax
movslq -0x20(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
lea    0xd8c(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%eax
jne    12dc <main+0x15c>
mov    -0x14(%rbp),%edi
mov    -0x18(%rbp),%esi
call   1030 <mx28_create_nand_image@plt>
mov    %eax,-0x1c(%rbp)
jmp    12e1 <main+0x161>
mov    -0x18(%rbp),%edi
call   1060 <close@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
mov    -0x1c(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
call   10a0 <parse_ops@plt>
test   %eax,%eax
js     1232 <main+0xb2>
mov    %eax,%ebp
mov    0x8(%rbx,%rbp,8),%rdi
mov    0x2e33(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
call   1090 <open@plt>
test   %eax,%eax
js     1240 <main+0xc0>
mov    %eax,%r14d
mov    0x10(%rbx,%rbp,8),%rdi
mov    0x2e33(%rip),%rax
mov    0x2e24(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2dd9(%rip),%rax
or     (%rax),%esi
mov    0x2de0(%rip),%rax
mov    0x2de9(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
xor    %eax,%eax
call   1090 <open@plt>
test   %eax,%eax
js     1253 <main+0xd3>
mov    %eax,%r15d
mov    (%rbx,%rbp,8),%rbx
lea    0xdfc(%rip),%rsi
mov    %rbx,%rdi
call   1070 <strcmp@plt>
test   %eax,%eax
je     1266 <main+0xe6>
lea    0xdec(%rip),%rsi
mov    %rbx,%rdi
call   1070 <strcmp@plt>
xor    %ebx,%ebx
test   %eax,%eax
jne    1273 <main+0xf3>
mov    %r14d,%edi
mov    %r15d,%esi
call   1040 <mx28_create_nand_image@plt>
jmp    1271 <main+0xf1>
xor    %eax,%eax
call   1080 <usage@plt>
mov    $0x1,%ebx
jmp    1283 <main+0x103>
lea    0xde0(%rip),%rdi
call   1030 <puts@plt>
mov    $0x2,%ebx
jmp    1283 <main+0x103>
lea    0xdae(%rip),%rdi
call   1030 <puts@plt>
mov    $0x3,%ebx
jmp    127b <main+0xfb>
mov    %r14d,%edi
mov    %r15d,%esi
call   1050 <mx28_create_sd_image@plt>
mov    %eax,%ebx
mov    %r15d,%edi
call   1060 <close@plt>
mov    %r14d,%edi
call   1060 <close@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@O_RDONLY = global i32 0
@.str = [41 x i8] c"Input BootStream file can not be opened\0A\00"
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@O_WRONLY = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str.1 = [32 x i8] c"Output file can not be created\0A\00"
@.str.2 = [3 x i8] c"sd\00"
@.str.3 = [5 x i8] c"nand\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %8
%10 = load i32, i32* %4
%11 = load i8**, i8*** %5
%12 = call i32 @parse_ops(i32 %10, i8** %11)
store i32 %12, i32* %9
%13 = load i32, i32* %9
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %2
%16 = call i32 (...) @usage()
store i32 1, i32* %8
br label %81
17: ; preds = %2
%18 = load i8**, i8*** %5
%19 = load i32, i32* %9
%20 = add nsw i32 %19, 1
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds i8*, i8** %18, i64 %21
%23 = load i8*, i8** %22
%24 = load i32, i32* @O_RDONLY
%25 = call i32 (i8*, i32, ...) @open(i8* %23, i32 %24)
store i32 %25, i32* %6
%26 = load i32, i32* %6
%27 = icmp slt i32 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %17
%29 = call i32 @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0))
store i32 2, i32* %8
br label %81
30: ; preds = %17
%31 = load i8**, i8*** %5
%32 = load i32, i32* %9
%33 = add nsw i32 %32, 2
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds i8*, i8** %31, i64 %34
%36 = load i8*, i8** %35
%37 = load i32, i32* @O_CREAT
%38 = load i32, i32* @O_TRUNC
%39 = or i32 %37, %38
%40 = load i32, i32* @O_WRONLY
%41 = or i32 %39, %40
%42 = load i32, i32* @S_IRUSR
%43 = load i32, i32* @S_IWUSR
%44 = or i32 %42, %43
%45 = call i32 (i8*, i32, ...) @open(i8* %36, i32 %41, i32 %44)
store i32 %45, i32* %7
%46 = load i32, i32* %7
%47 = icmp slt i32 %46, 0
br i1 %47, label %48, label %50
48: ; preds = %30
%49 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0))
store i32 3, i32* %8
br label %78
50: ; preds = %30
%51 = load i8**, i8*** %5
%52 = load i32, i32* %9
%53 = sext i32 %52 to i64
%54 = getelementptr inbounds i8*, i8** %51, i64 %53
%55 = load i8*, i8** %54
%56 = call i32 @strcmp(i8* %55, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%57 = icmp ne i32 %56, 0
br i1 %57, label %62, label %58
58: ; preds = %50
%59 = load i32, i32* %6
%60 = load i32, i32* %7
%61 = call i32 @mx28_create_sd_image(i32 %59, i32 %60)
store i32 %61, i32* %8
br label %75
62: ; preds = %50
%63 = load i8**, i8*** %5
%64 = load i32, i32* %9
%65 = sext i32 %64 to i64
%66 = getelementptr inbounds i8*, i8** %63, i64 %65
%67 = load i8*, i8** %66
%68 = call i32 @strcmp(i8* %67, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%69 = icmp ne i32 %68, 0
br i1 %69, label %74, label %70
70: ; preds = %62
%71 = load i32, i32* %6
%72 = load i32, i32* %7
%73 = call i32 @mx28_create_nand_image(i32 %71, i32 %72)
store i32 %73, i32* %8
br label %74
74: ; preds = %70, %62
br label %75
75: ; preds = %74, %58
%76 = load i32, i32* %7
%77 = call i32 @close(i32 %76)
br label %78
78: ; preds = %75, %48
%79 = load i32, i32* %6
%80 = call i32 @close(i32 %79)
br label %81
81: ; preds = %78, %28, %15
%82 = load i32, i32* %8
ret i32 %82
}

LLVM IR (-O2):
@O_RDONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@O_WRONLY = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str.2 = [3 x i8] c"sd\00"
@.str.3 = [5 x i8] c"nand\00"
@str = [31 x i8] c"Output file can not be created\00"
@str.4 = [40 x i8] c"Input BootStream file can not be opened\00"
define i32 @main(i32 %0, i8** %1) {
%3 = tail call i32 @parse_ops(i32 %0, i8** %1)
%4 = icmp slt i32 %3, 0
br i1 %4, label %5, label %7
5: ; preds = %2
%6 = tail call i32 (...) @usage()
br label %53
7: ; preds = %2
%8 = add nuw nsw i32 %3, 1
%9 = zext i32 %8 to i64
%10 = getelementptr inbounds i8*, i8** %1, i64 %9
%11 = load i8*, i8** %10
%12 = load i32, i32* @O_RDONLY
%13 = tail call i32 (i8*, i32, ...) @open(i8* %11, i32 %12)
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %7
%16 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @str.4, i64 0, i64 0))
br label %53
17: ; preds = %7
%18 = add nuw nsw i32 %3, 2
%19 = zext i32 %18 to i64
%20 = getelementptr inbounds i8*, i8** %1, i64 %19
%21 = load i8*, i8** %20
%22 = load i32, i32* @O_CREAT
%23 = load i32, i32* @O_TRUNC
%24 = or i32 %23, %22
%25 = load i32, i32* @O_WRONLY
%26 = or i32 %24, %25
%27 = load i32, i32* @S_IRUSR
%28 = load i32, i32* @S_IWUSR
%29 = or i32 %28, %27
%30 = tail call i32 (i8*, i32, ...) @open(i8* %21, i32 %26, i32 %29)
%31 = icmp slt i32 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %17
%33 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @str, i64 0, i64 0))
br label %50
34: ; preds = %17
%35 = zext i32 %3 to i64
%36 = getelementptr inbounds i8*, i8** %1, i64 %35
%37 = load i8*, i8** %36
%38 = tail call i32 @strcmp(i8* dereferenceable(1) %37, i8* dereferenceable(3) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%39 = icmp eq i32 %38, 0
br i1 %39, label %40, label %42
40: ; preds = %34
%41 = tail call i32 @mx28_create_sd_image(i32 %13, i32 %30)
br label %47
42: ; preds = %34
%43 = tail call i32 @strcmp(i8* dereferenceable(1) %37, i8* dereferenceable(5) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%44 = icmp eq i32 %43, 0
br i1 %44, label %45, label %47
45: ; preds = %42
%46 = tail call i32 @mx28_create_nand_image(i32 %13, i32 %30)
br label %47
47: ; preds = %42, %45, %40
%48 = phi i32 [ 0, %42 ], [ %46, %45 ], [ %41, %40 ]
%49 = tail call i32 @close(i32 %30)
br label %50
50: ; preds = %47, %32
%51 = phi i32 [ 3, %32 ], [ %48, %47 ]
%52 = tail call i32 @close(i32 %13)
br label %53
53: ; preds = %50, %15, %5
%54 = phi i32 [ 1, %5 ], [ 2, %15 ], [ %51, %50 ]
ret i32 %54
}