#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 atoi(char *);
int /*<<< orphan*/ copyPartition(int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                                 int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ isdigit(char);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ readPartitionsInfo(int /*<<< orphan*/ *);
int strlen(char *);
scalar_t__ verifyBootSignature(int /*<<< orphan*/ *);
int /*<<< orphan*/ verifyPartition(int);
int main(int argc, char **argv) {
  FILE *in;
  FILE *out;
  if (argc != 4) {
    printf("Usage: fileIn filePartitionOut "
           "numberOfPartition(0,1,2 or 3)\n");
    return 1;
  }
  if (strlen(argv[3]) != 1 || !isdigit(argv[3][0])) {
    printf("Wrong partition number\n");
    return 1;
  }
  int getPartition = atoi(argv[3]);
  if (getPartition < 0 || getPartition > 3) {
    printf("Wrong partition number\n");
    return 1;
  }
  in = fopen(argv[1], "r");
  if (in == NULL) {
    printf("Can't open fileIn\n");
    return 1;
  }
  out = fopen(argv[2], "w");
  if (out == NULL) {
    printf("Can't open filePartitionOut\n");
    return 1;
  }
  if (verifyBootSignature(in))
    printf("Boot signature OK!\n");
  else {
    printf("Wrong boot signature in mbr! Is fileIn valid disk image?\n");
    return 1;
  }
  if (!readPartitionsInfo(in)) {
    printf("Failed to read data about partitions");
    return 1;
  }
  if (!verifyPartition(getPartition)) {
    printf("Selected partition does not exist\n");
    return 1;
  }
  if (!copyPartition(in, out, getPartition)) {
    printf("Error while extracting partition\n");
    return 1;
  }
  printf("Done!\n");
  fclose(in);
  fclose(out);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x4,-0x8(%rbp)
je     11d8 <main+0x38>
lea    0xe39(%rip),%rdi
call   1070 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    13b6 <main+0x216>
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
call   1060 <strlen@plt>
cmp    $0x1,%eax
jne    1207 <main+0x67>
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rax
movsbl (%rax),%edi
call   1030 <isdigit@plt>
cmp    $0x0,%eax
jne    121f <main+0x7f>
lea    0xe30(%rip),%rdi
call   1070 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    13b6 <main+0x216>
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
call   10c0 <atoi@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jl     1243 <main+0xa3>
cmpl   $0x3,-0x24(%rbp)
jle    125b <main+0xbb>
lea    0xdf4(%rip),%rdi
call   1070 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    13b6 <main+0x216>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xdec(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1296 <main+0xf6>
lea    0xdd3(%rip),%rdi
call   1070 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    13b6 <main+0x216>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
lea    0xdc6(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    12d1 <main+0x131>
lea    0xdad(%rip),%rdi
call   1070 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    13b6 <main+0x216>
mov    -0x18(%rbp),%rdi
call   1080 <verifyBootSignature@plt>
cmp    $0x0,%rax
je     12f5 <main+0x155>
lea    0xd9f(%rip),%rdi
call   1070 <printf@plt>
jmp    130d <main+0x16d>
lea    0xda2(%rip),%rdi
call   1070 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    13b6 <main+0x216>
mov    -0x18(%rbp),%rdi
call   1090 <readPartitionsInfo@plt>
cmp    $0x0,%eax
jne    1337 <main+0x197>
lea    0xdb2(%rip),%rdi
call   1070 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    13b6 <main+0x216>
mov    -0x24(%rbp),%edi
call   10b0 <verifyPartition@plt>
cmp    $0x0,%eax
jne    1360 <main+0x1c0>
lea    0xdae(%rip),%rdi
call   1070 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    13b6 <main+0x216>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1040 <copyPartition@plt>
cmp    $0x0,%eax
jne    1391 <main+0x1f1>
lea    0xda0(%rip),%rdi
call   1070 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    13b6 <main+0x216>
lea    0xdaa(%rip),%rdi
call   1070 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
cmp    $0x4,%edi
jne    11f4 <main+0x44>
mov    %rsi,%rbx
mov    0x18(%rsi),%rdi
call   1070 <strlen@plt>
cmp    $0x1,%eax
jne    11eb <main+0x3b>
mov    0x18(%rbx),%rax
movsbl (%rax),%edi
call   1030 <isdigit@plt>
test   %eax,%eax
je     11eb <main+0x3b>
mov    0x18(%rbx),%rdi
call   10d0 <atoi@plt>
cmp    $0x4,%eax
jb     1210 <main+0x60>
lea    0xef8(%rip),%rdi
jmp    11fb <main+0x4b>
lea    0xf06(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %eax,%ebp
mov    0x8(%rbx),%rdi
lea    0xee2(%rip),%rsi
call   10b0 <fopen@plt>
test   %rax,%rax
je     12a6 <main+0xf6>
mov    %rax,%r15
mov    0x10(%rbx),%rdi
lea    0xdcb(%rip),%rsi
call   10b0 <fopen@plt>
test   %rax,%rax
je     12b2 <main+0x102>
mov    %rax,%r14
mov    %r15,%rdi
call   1090 <verifyBootSignature@plt>
test   %rax,%rax
je     12be <main+0x10e>
lea    0xe0a(%rip),%rdi
call   1040 <puts@plt>
mov    %r15,%rdi
call   10a0 <readPartitionsInfo@plt>
test   %eax,%eax
je     12ca <main+0x11a>
mov    %ebp,%edi
call   10c0 <verifyPartition@plt>
test   %eax,%eax
je     12db <main+0x12b>
mov    %r15,%rdi
mov    %r14,%rsi
mov    %ebp,%edx
call   1050 <copyPartition@plt>
test   %eax,%eax
je     12e7 <main+0x137>
lea    0xe2c(%rip),%rdi
call   1040 <puts@plt>
mov    %r15,%rdi
call   1060 <fclose@plt>
mov    %r14,%rdi
call   1060 <fclose@plt>
xor    %eax,%eax
jmp    1205 <main+0x55>
lea    0xe2b(%rip),%rdi
jmp    11fb <main+0x4b>
lea    0xe03(%rip),%rdi
jmp    11fb <main+0x4b>
lea    0xd62(%rip),%rdi
jmp    11fb <main+0x4b>
lea    0xd31(%rip),%rdi
call   1080 <printf@plt>
jmp    1200 <main+0x50>
lea    0xd91(%rip),%rdi
jmp    11fb <main+0x4b>
lea    0xda7(%rip),%rdi
jmp    11fb <main+0x4b>

LLVM IR (-O0):
@.str = [62 x i8] c"Usage: fileIn filePartitionOut numberOfPartition(0,1,2 or 3)\0A\00"
@.str.1 = [24 x i8] c"Wrong partition number\0A\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [19 x i8] c"Can't open fileIn\0A\00"
@.str.4 = [2 x i8] c"w\00"
@.str.5 = [29 x i8] c"Can't open filePartitionOut\0A\00"
@.str.6 = [20 x i8] c"Boot signature OK!\0A\00"
@.str.7 = [58 x i8] c"Wrong boot signature in mbr! Is fileIn valid disk image?\0A\00"
@.str.8 = [37 x i8] c"Failed to read data about partitions\00"
@.str.9 = [35 x i8] c"Selected partition does not exist\0A\00"
@.str.10 = [34 x i8] c"Error while extracting partition\0A\00"
@.str.11 = [7 x i8] c"Done!\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 i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* %4
%10 = icmp ne i32 %9, 4
br i1 %10, label %11, label %13
11: ; preds = %2
%12 = call i32 @printf(i8* getelementptr inbounds ([62 x i8], [62 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %93
13: ; preds = %2
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 3
%16 = load i8*, i8** %15
%17 = call i32 @strlen(i8* %16)
%18 = icmp ne i32 %17, 1
br i1 %18, label %27, label %19
19: ; preds = %13
%20 = load i8**, i8*** %5
%21 = getelementptr inbounds i8*, i8** %20, i64 3
%22 = load i8*, i8** %21
%23 = getelementptr inbounds i8, i8* %22, i64 0
%24 = load i8, i8* %23
%25 = call i32 @isdigit(i8 signext %24)
%26 = icmp ne i32 %25, 0
br i1 %26, label %29, label %27
27: ; preds = %19, %13
%28 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %3
br label %93
29: ; preds = %19
%30 = load i8**, i8*** %5
%31 = getelementptr inbounds i8*, i8** %30, i64 3
%32 = load i8*, i8** %31
%33 = call i32 @atoi(i8* %32)
store i32 %33, i32* %8
%34 = load i32, i32* %8
%35 = icmp slt i32 %34, 0
br i1 %35, label %39, label %36
36: ; preds = %29
%37 = load i32, i32* %8
%38 = icmp sgt i32 %37, 3
br i1 %38, label %39, label %41
39: ; preds = %36, %29
%40 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %3
br label %93
41: ; preds = %36
%42 = load i8**, i8*** %5
%43 = getelementptr inbounds i8*, i8** %42, i64 1
%44 = load i8*, i8** %43
%45 = call i32* @fopen(i8* %44, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %45, i32** %6
%46 = load i32*, i32** %6
%47 = icmp eq i32* %46, null
br i1 %47, label %48, label %50
48: ; preds = %41
%49 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %3
br label %93
50: ; preds = %41
%51 = load i8**, i8*** %5
%52 = getelementptr inbounds i8*, i8** %51, i64 2
%53 = load i8*, i8** %52
%54 = call i32* @fopen(i8* %53, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
store i32* %54, i32** %7
%55 = load i32*, i32** %7
%56 = icmp eq i32* %55, null
br i1 %56, label %57, label %59
57: ; preds = %50
%58 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.5, i64 0, i64 0))
store i32 1, i32* %3
br label %93
59: ; preds = %50
%60 = load i32*, i32** %6
%61 = call i64 @verifyBootSignature(i32* %60)
%62 = icmp ne i64 %61, 0
br i1 %62, label %63, label %65
63: ; preds = %59
%64 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.6, i64 0, i64 0))
br label %67
65: ; preds = %59
%66 = call i32 @printf(i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str.7, i64 0, i64 0))
store i32 1, i32* %3
br label %93
67: ; preds = %63
%68 = load i32*, i32** %6
%69 = call i32 @readPartitionsInfo(i32* %68)
%70 = icmp ne i32 %69, 0
br i1 %70, label %73, label %71
71: ; preds = %67
%72 = call i32 @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.8, i64 0, i64 0))
store i32 1, i32* %3
br label %93
73: ; preds = %67
%74 = load i32, i32* %8
%75 = call i32 @verifyPartition(i32 %74)
%76 = icmp ne i32 %75, 0
br i1 %76, label %79, label %77
77: ; preds = %73
%78 = call i32 @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.9, i64 0, i64 0))
store i32 1, i32* %3
br label %93
79: ; preds = %73
%80 = load i32*, i32** %6
%81 = load i32*, i32** %7
%82 = load i32, i32* %8
%83 = call i32 @copyPartition(i32* %80, i32* %81, i32 %82)
%84 = icmp ne i32 %83, 0
br i1 %84, label %87, label %85
85: ; preds = %79
%86 = call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.10, i64 0, i64 0))
store i32 1, i32* %3
br label %93
87: ; preds = %79
%88 = call i32 @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.11, i64 0, i64 0))
%89 = load i32*, i32** %6
%90 = call i32 @fclose(i32* %89)
%91 = load i32*, i32** %7
%92 = call i32 @fclose(i32* %91)
store i32 0, i32* %3
br label %93
93: ; preds = %87, %85, %77, %71, %65, %57, %48, %39, %27, %11
%94 = load i32, i32* %3
ret i32 %94
}

LLVM IR (-O2):
@.str.2 = [2 x i8] c"r\00"
@.str.4 = [2 x i8] c"w\00"
@.str.8 = [37 x i8] c"Failed to read data about partitions\00"
@str.12 = [57 x i8] c"Wrong boot signature in mbr! Is fileIn valid disk image?\00"
@str.13 = [19 x i8] c"Boot signature OK!\00"
@str.14 = [34 x i8] c"Selected partition does not exist\00"
@str.15 = [33 x i8] c"Error while extracting partition\00"
@str.16 = [6 x i8] c"Done!\00"
@str.17 = [28 x i8] c"Can't open filePartitionOut\00"
@str.18 = [18 x i8] c"Can't open fileIn\00"
@str.19 = [23 x i8] c"Wrong partition number\00"
@str.20 = [61 x i8] c"Usage: fileIn filePartitionOut numberOfPartition(0,1,2 or 3)\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 4
br i1 %3, label %6, label %4
4: ; preds = %2
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([61 x i8], [61 x i8]* @str.20, i64 0, i64 0))
br label %63
6: ; preds = %2
%7 = getelementptr inbounds i8*, i8** %1, i64 3
%8 = load i8*, i8** %7
%9 = tail call i32 @strlen(i8* %8)
%10 = icmp eq i32 %9, 1
br i1 %10, label %11, label %16
11: ; preds = %6
%12 = load i8*, i8** %7
%13 = load i8, i8* %12
%14 = tail call i32 @isdigit(i8 signext %13)
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %11, %6
%17 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str.19, i64 0, i64 0))
br label %63
18: ; preds = %11
%19 = load i8*, i8** %7
%20 = tail call i32 @atoi(i8* %19)
%21 = icmp ugt i32 %20, 3
br i1 %21, label %22, label %24
22: ; preds = %18
%23 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str.19, i64 0, i64 0))
br label %63
24: ; preds = %18
%25 = getelementptr inbounds i8*, i8** %1, i64 1
%26 = load i8*, i8** %25
%27 = tail call i32* @fopen(i8* %26, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%28 = icmp eq i32* %27, null
br i1 %28, label %29, label %31
29: ; preds = %24
%30 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str.18, i64 0, i64 0))
br label %63
31: ; preds = %24
%32 = getelementptr inbounds i8*, i8** %1, i64 2
%33 = load i8*, i8** %32
%34 = tail call i32* @fopen(i8* %33, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%35 = icmp eq i32* %34, null
br i1 %35, label %36, label %38
36: ; preds = %31
%37 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @str.17, i64 0, i64 0))
br label %63
38: ; preds = %31
%39 = tail call i64 @verifyBootSignature(i32* %27)
%40 = icmp eq i64 %39, 0
br i1 %40, label %45, label %41
41: ; preds = %38
%42 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str.13, i64 0, i64 0))
%43 = tail call i32 @readPartitionsInfo(i32* %27)
%44 = icmp eq i32 %43, 0
br i1 %44, label %47, label %49
45: ; preds = %38
%46 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([57 x i8], [57 x i8]* @str.12, i64 0, i64 0))
br label %63
47: ; preds = %41
%48 = tail call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([37 x i8], [37 x i8]* @.str.8, i64 0, i64 0))
br label %63
49: ; preds = %41
%50 = tail call i32 @verifyPartition(i32 %20)
%51 = icmp eq i32 %50, 0
br i1 %51, label %52, label %54
52: ; preds = %49
%53 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @str.14, i64 0, i64 0))
br label %63
54: ; preds = %49
%55 = tail call i32 @copyPartition(i32* %27, i32* %34, i32 %20)
%56 = icmp eq i32 %55, 0
br i1 %56, label %57, label %59
57: ; preds = %54
%58 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @str.15, i64 0, i64 0))
br label %63
59: ; preds = %54
%60 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @str.16, i64 0, i64 0))
%61 = tail call i32 @fclose(i32* %27)
%62 = tail call i32 @fclose(i32* %34)
br label %63
63: ; preds = %22, %29, %36, %45, %47, %52, %57, %59, %16, %4
%64 = phi i32 [ 1, %4 ], [ 1, %16 ], [ 1, %22 ], [ 1, %29 ], [ 1, %36 ], [ 0, %59 ], [ 1, %57 ], [ 1, %52 ], [ 1, %47 ], [ 1, %45 ]
ret i32 %64
}