#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 */
struct flashinfo {
  int dummy;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int O_RDWR;
int O_SYNC;
int /*<<< orphan*/ check_file(int /*<<< orphan*/ *, struct flashinfo *,
                              char const *);
int /*<<< orphan*/ check_mounts();
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ erase_write_verify(int /*<<< orphan*/ *, struct flashinfo *,
                                      int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
char *get_device_id(int);
struct flashinfo *get_flash_info(int);
int open(char const *, int);
int /*<<< orphan*/ panic(char *);
int /*<<< orphan*/ printf(char *, char const *);
int /*<<< orphan*/ showhelp();
int main(int argc, char *argv[]) {
  FILE *src;
  int dest;
  const char *file;
  const char *dev;
  struct flashinfo *fi;
  const char *id;
  if (argc != 3)
    showhelp();
  file = argv[1];
  dev = argv[2];
  check_mounts();
  dest = open(dev, O_SYNC | O_RDWR);
  if (dest < 0)
    panic("error opening device\n");
  fi = get_flash_info(dest);
  id = get_device_id(dest);
  if (id == NULL)
    panic("cannot detemine device ID\n");
  printf("device ID is: %s\n", id);
  src = fopen(file, "rb");
  if (src == NULL)
    panic("error open file\n");
  check_file(src, fi, id);
  erase_write_verify(src, fi, dest);
  fclose(src);
  close(dest);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
je     11e7 <main+0x27>
mov    $0x0,%al
call   10c0 <showhelp@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x30(%rbp)
mov    $0x0,%al
call   10e0 <check_mounts@plt>
mov    -0x30(%rbp),%rdi
mov    0x2dcf(%rip),%rax
mov    (%rax),%esi
mov    0x2dce(%rip),%rax
or     (%rax),%esi
call   10a0 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    123a <main+0x7a>
lea    0xdcb(%rip),%rdi
call   1060 <panic@plt>
mov    -0x1c(%rbp),%edi
call   10d0 <get_flash_info@plt>
mov    %rax,-0x38(%rbp)
mov    -0x1c(%rbp),%edi
call   1030 <get_device_id@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    1269 <main+0xa9>
lea    0xdb2(%rip),%rdi
call   1060 <panic@plt>
mov    -0x40(%rbp),%rsi
lea    0xdbd(%rip),%rdi
call   1050 <printf@plt>
mov    -0x28(%rbp),%rdi
lea    0xdbf(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    12a4 <main+0xe4>
lea    0xda7(%rip),%rdi
call   1060 <panic@plt>
mov    -0x18(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    -0x40(%rbp),%rdx
call   1090 <check_file@plt>
mov    -0x18(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1070 <erase_write_verify@plt>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x1c(%rbp),%edi
call   1080 <close@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%rbx
cmp    $0x3,%edi
je     11d7 <main+0x17>
xor    %eax,%eax
call   10c0 <showhelp@plt>
mov    0x8(%rbx),%r14
mov    0x10(%rbx),%rbx
xor    %eax,%eax
call   10e0 <check_mounts@plt>
mov    0x2df3(%rip),%rax
mov    0x2df4(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    %rbx,%rdi
call   10a0 <open@plt>
mov    %eax,%r12d
test   %eax,%eax
jns    1213 <main+0x53>
lea    0xdf2(%rip),%rdi
call   1060 <panic@plt>
mov    %r12d,%edi
call   10d0 <get_flash_info@plt>
mov    %rax,%r15
mov    %r12d,%edi
call   1030 <get_device_id@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    123a <main+0x7a>
lea    0xde1(%rip),%rdi
call   1060 <panic@plt>
lea    0xdf0(%rip),%rdi
mov    %rbx,%rsi
call   1050 <printf@plt>
lea    0xdf3(%rip),%rsi
mov    %r14,%rdi
call   10b0 <fopen@plt>
mov    %rax,%rbp
test   %rax,%rax
jne    126c <main+0xac>
lea    0xddf(%rip),%rdi
call   1060 <panic@plt>
mov    %rbp,%rdi
mov    %r15,%rsi
mov    %rbx,%rdx
call   1090 <check_file@plt>
mov    %rbp,%rdi
mov    %r15,%rsi
mov    %r12d,%edx
call   1070 <erase_write_verify@plt>
mov    %rbp,%rdi
call   1040 <fclose@plt>
mov    %r12d,%edi
call   1080 <close@plt>
xor    %eax,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.flashinfo = type { i32 }
@O_SYNC = global i32 0
@O_RDWR = global i32 0
@.str = [22 x i8] c"error opening device\0A\00"
@.str.1 = [27 x i8] c"cannot detemine device ID\0A\00"
@.str.2 = [18 x i8] c"device ID is: %s\0A\00"
@.str.3 = [3 x i8] c"rb\00"
@.str.4 = [17 x i8] c"error open file\0A\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 i8*
%9 = alloca i8*
%10 = alloca %struct.flashinfo*
%11 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = load i32, i32* %4
%13 = icmp ne i32 %12, 3
br i1 %13, label %14, label %16
14: ; preds = %2
%15 = call i32 (...) @showhelp()
br label %16
16: ; preds = %14, %2
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 1
%19 = load i8*, i8** %18
store i8* %19, i8** %8
%20 = load i8**, i8*** %5
%21 = getelementptr inbounds i8*, i8** %20, i64 2
%22 = load i8*, i8** %21
store i8* %22, i8** %9
%23 = call i32 (...) @check_mounts()
%24 = load i8*, i8** %9
%25 = load i32, i32* @O_SYNC
%26 = load i32, i32* @O_RDWR
%27 = or i32 %25, %26
%28 = call i32 @open(i8* %24, i32 %27)
store i32 %28, i32* %7
%29 = load i32, i32* %7
%30 = icmp slt i32 %29, 0
br i1 %30, label %31, label %33
31: ; preds = %16
%32 = call i32 @panic(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
br label %33
33: ; preds = %31, %16
%34 = load i32, i32* %7
%35 = call %struct.flashinfo* @get_flash_info(i32 %34)
store %struct.flashinfo* %35, %struct.flashinfo** %10
%36 = load i32, i32* %7
%37 = call i8* @get_device_id(i32 %36)
store i8* %37, i8** %11
%38 = load i8*, i8** %11
%39 = icmp eq i8* %38, null
br i1 %39, label %40, label %42
40: ; preds = %33
%41 = call i32 @panic(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
br label %42
42: ; preds = %40, %33
%43 = load i8*, i8** %11
%44 = call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i8* %43)
%45 = load i8*, i8** %8
%46 = call i32* @fopen(i8* %45, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
store i32* %46, i32** %6
%47 = load i32*, i32** %6
%48 = icmp eq i32* %47, null
br i1 %48, label %49, label %51
49: ; preds = %42
%50 = call i32 @panic(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0))
br label %51
51: ; preds = %49, %42
%52 = load i32*, i32** %6
%53 = load %struct.flashinfo*, %struct.flashinfo** %10
%54 = load i8*, i8** %11
%55 = call i32 @check_file(i32* %52, %struct.flashinfo* %53, i8* %54)
%56 = load i32*, i32** %6
%57 = load %struct.flashinfo*, %struct.flashinfo** %10
%58 = load i32, i32* %7
%59 = call i32 @erase_write_verify(i32* %56, %struct.flashinfo* %57, i32 %58)
%60 = load i32*, i32** %6
%61 = call i32 @fclose(i32* %60)
%62 = load i32, i32* %7
%63 = call i32 @close(i32 %62)
ret i32 0
}

LLVM IR (-O2):
%struct.flashinfo = type { i32 }
@O_SYNC = global i32 0
@O_RDWR = global i32 0
@.str = [22 x i8] c"error opening device\0A\00"
@.str.1 = [27 x i8] c"cannot detemine device ID\0A\00"
@.str.2 = [18 x i8] c"device ID is: %s\0A\00"
@.str.3 = [3 x i8] c"rb\00"
@.str.4 = [17 x i8] c"error open file\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 3
br i1 %3, label %6, label %4
4: ; preds = %2
%5 = tail call i32 (...) @showhelp()
br label %6
6: ; preds = %4, %2
%7 = getelementptr inbounds i8*, i8** %1, i64 1
%8 = load i8*, i8** %7
%9 = getelementptr inbounds i8*, i8** %1, i64 2
%10 = load i8*, i8** %9
%11 = tail call i32 (...) @check_mounts()
%12 = load i32, i32* @O_SYNC
%13 = load i32, i32* @O_RDWR
%14 = or i32 %13, %12
%15 = tail call i32 @open(i8* %10, i32 %14)
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %6
%18 = tail call i32 @panic(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
br label %19
19: ; preds = %17, %6
%20 = tail call %struct.flashinfo* @get_flash_info(i32 %15)
%21 = tail call i8* @get_device_id(i32 %15)
%22 = icmp eq i8* %21, null
br i1 %22, label %23, label %25
23: ; preds = %19
%24 = tail call i32 @panic(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
br label %25
25: ; preds = %23, %19
%26 = tail call i32 @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i8* %21)
%27 = tail call i32* @fopen(i8* %8, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
%28 = icmp eq i32* %27, null
br i1 %28, label %29, label %31
29: ; preds = %25
%30 = tail call i32 @panic(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0))
br label %31
31: ; preds = %29, %25
%32 = tail call i32 @check_file(i32* %27, %struct.flashinfo* %20, i8* %21)
%33 = tail call i32 @erase_write_verify(i32* %27, %struct.flashinfo* %20, i32 %15)
%34 = tail call i32 @fclose(i32* %27)
%35 = tail call i32 @close(i32 %15)
ret i32 0
}