#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 /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int getopt(int, char **, char *);
int getprog(int);
scalar_t__ initmc(int);
int open_port(char *);
char *optarg;
int /*<<< orphan*/ printf(char *, ...);
int main(int argc, char **argv) {
  int mainfd = 0;
  int opt;
  char *filename;
  char *devname;
  if (argc < 2) {
    printf("Usage: %s -f <file.dat> -d <tty device>\n", argv[0]);
    return 1;
  }
  while (1) {
    opt = getopt(argc, argv, "d:");
    if (opt == -1)
      break;
    switch (opt) {
    case 'd':
      devname = optarg;
      break;
    default:
      printf("Usage: %s -f <file.dat> -d <tty device>\n", argv[0]);
      exit(1);
      break;
    }
  }
  mainfd = open_port(devname);
  if (mainfd < 0) {
    printf("failed opening %s\n", devname);
    return 1;
  }
  printf("opened device successfully\n");
  if (initmc(mainfd) < 0) {
    printf("failed mc init\n");
    return 1;
  }
  printf("intialzied micro controller successfully\n");
  if (getprog(mainfd) == 1) {
    printf("faied getting program\n");
    return 1;
  }
  printf("got program successfully\n");
  close(mainfd);
}


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)
movl   $0x0,-0x14(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11b8 <main+0x48>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe5b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12e7 <main+0x177>
jmp    11bd <main+0x4d>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
lea    0xe5e(%rip),%rdx
call   1070 <getopt@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    11e2 <main+0x72>
jmp    122a <main+0xba>
mov    -0x18(%rbp),%eax
sub    $0x64,%eax
jne    1206 <main+0x96>
jmp    11f3 <main+0x83>
mov    0x2dfe(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
jmp    1225 <main+0xb5>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xdec(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
jmp    11bd <main+0x4d>
mov    -0x28(%rbp),%rdi
call   1090 <open_port@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    125e <main+0xee>
mov    -0x28(%rbp),%rsi
lea    0xde1(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12e7 <main+0x177>
lea    0xdda(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x14(%rbp),%edi
call   1030 <initmc@plt>
cmp    $0x0,%rax
jge    1298 <main+0x128>
lea    0xdd6(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12e7 <main+0x177>
lea    0xdcc(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x14(%rbp),%edi
call   1060 <getprog@plt>
cmp    $0x1,%eax
jne    12d1 <main+0x161>
lea    0xdd7(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12e7 <main+0x177>
lea    0xdd4(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%rbp
cmp    $0x2,%edi
jge    11a0 <main+0x20>
mov    0x0(%rbp),%rsi
lea    0xe65(%rip),%rdi
jmp    1222 <main+0xa2>
mov    %edi,%ebx
lea    0xe80(%rip),%r15
mov    0x2e48(%rip),%r12
mov    %ebx,%edi
mov    %rbp,%rsi
mov    %r15,%rdx
call   1080 <getopt@plt>
cmp    $0x64,%eax
jne    11c8 <main+0x48>
mov    (%r12),%r14
jmp    11b0 <main+0x30>
cmp    $0xffffffff,%eax
jne    1261 <main+0xe1>
mov    %r14,%rdi
call   10a0 <open_port@plt>
test   %eax,%eax
js     1218 <main+0x98>
mov    %eax,%ebp
lea    0xe59(%rip),%rdi
call   1040 <puts@plt>
mov    %ebp,%edi
call   1030 <initmc@plt>
test   %rax,%rax
js     1230 <main+0xb0>
lea    0xe5c(%rip),%rdi
call   1040 <puts@plt>
mov    %ebp,%edi
call   1070 <getprog@plt>
cmp    $0x1,%eax
jne    1243 <main+0xc3>
lea    0xe86(%rip),%rdi
jmp    1237 <main+0xb7>
lea    0xe0d(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%eax
jmp    1258 <main+0xd8>
lea    0xe7b(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%eax
jmp    1258 <main+0xd8>
lea    0xe39(%rip),%rdi
call   1040 <puts@plt>
mov    %ebp,%edi
call   1060 <close@plt>
xor    %eax,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x0(%rbp),%rsi
lea    0xd94(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [41 x i8] c"Usage: %s -f <file.dat> -d <tty device>\0A\00"
@.str.1 = [3 x i8] c"d:\00"
@optarg = global i8* null
@.str.2 = [19 x i8] c"failed opening %s\0A\00"
@.str.3 = [28 x i8] c"opened device successfully\0A\00"
@.str.4 = [16 x i8] c"failed mc init\0A\00"
@.str.5 = [42 x i8] c"intialzied micro controller successfully\0A\00"
@.str.6 = [23 x i8] c"faied getting program\0A\00"
@.str.7 = [26 x i8] c"got program successfully\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*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %6
%10 = load i32, i32* %4
%11 = icmp slt i32 %10, 2
br i1 %11, label %12, label %17
12: ; preds = %2
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 0
%15 = load i8*, i8** %14
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0), i8* %15)
store i32 1, i32* %3
br label %62
17: ; preds = %2
br label %18
18: ; preds = %17, %35
%19 = load i32, i32* %4
%20 = load i8**, i8*** %5
%21 = call i32 @getopt(i32 %19, i8** %20, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32 %21, i32* %7
%22 = load i32, i32* %7
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %25
24: ; preds = %18
br label %36
25: ; preds = %18
%26 = load i32, i32* %7
switch i32 %26, label %29 [
i32 100, label %27
]
27: ; preds = %25
%28 = load i8*, i8** @optarg
store i8* %28, i8** %9
br label %35
29: ; preds = %25
%30 = load i8**, i8*** %5
%31 = getelementptr inbounds i8*, i8** %30, i64 0
%32 = load i8*, i8** %31
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0), i8* %32)
%34 = call i32 @exit(i32 1)
unreachable
35: ; preds = %27
br label %18
36: ; preds = %24
%37 = load i8*, i8** %9
%38 = call i32 @open_port(i8* %37)
store i32 %38, i32* %6
%39 = load i32, i32* %6
%40 = icmp slt i32 %39, 0
br i1 %40, label %41, label %44
41: ; preds = %36
%42 = load i8*, i8** %9
%43 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i8* %42)
store i32 1, i32* %3
br label %62
44: ; preds = %36
%45 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0))
%46 = load i32, i32* %6
%47 = call i64 @initmc(i32 %46)
%48 = icmp slt i64 %47, 0
br i1 %48, label %49, label %51
49: ; preds = %44
%50 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0))
store i32 1, i32* %3
br label %62
51: ; preds = %44
%52 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.5, i64 0, i64 0))
%53 = load i32, i32* %6
%54 = call i32 @getprog(i32 %53)
%55 = icmp eq i32 %54, 1
br i1 %55, label %56, label %58
56: ; preds = %51
%57 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.6, i64 0, i64 0))
store i32 1, i32* %3
br label %62
58: ; preds = %51
%59 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.7, i64 0, i64 0))
%60 = load i32, i32* %6
%61 = call i32 @close(i32 %60)
br label %62
62: ; preds = %58, %56, %49, %41, %12
%63 = load i32, i32* %3
ret i32 %63
}

LLVM IR (-O2):
@.str = [41 x i8] c"Usage: %s -f <file.dat> -d <tty device>\0A\00"
@.str.1 = [3 x i8] c"d:\00"
@optarg = global i8* null
@.str.2 = [19 x i8] c"failed opening %s\0A\00"
@str = [27 x i8] c"opened device successfully\00"
@str.8 = [41 x i8] c"intialzied micro controller successfully\00"
@str.9 = [25 x i8] c"got program successfully\00"
@str.10 = [22 x i8] c"faied getting program\00"
@str.11 = [15 x i8] c"failed mc init\00"
define i32 @main(i32 %0, i8** %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*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0), i8* %5)
br label %36
7: ; preds = %2, %10
%8 = phi i8* [ %11, %10 ], [ undef, %2 ]
%9 = tail call i32 @getopt(i32 %0, i8** %1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
switch i32 %9, label %12 [
i32 -1, label %16
i32 100, label %10
]
10: ; preds = %7
%11 = load i8*, i8** @optarg
br label %7
12: ; preds = %7
%13 = load i8*, i8** %1
%14 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0), i8* %13)
%15 = tail call i32 @exit(i32 1)
unreachable
16: ; preds = %7
%17 = tail call i32 @open_port(i8* %8)
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %16
%20 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i8* %8)
br label %36
21: ; preds = %16
%22 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str, i64 0, i64 0))
%23 = tail call i64 @initmc(i32 %17)
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %21
%26 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str.11, i64 0, i64 0))
br label %36
27: ; preds = %21
%28 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([41 x i8], [41 x i8]* @str.8, i64 0, i64 0))
%29 = tail call i32 @getprog(i32 %17)
%30 = icmp eq i32 %29, 1
br i1 %30, label %31, label %33
31: ; preds = %27
%32 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str.10, i64 0, i64 0))
br label %36
33: ; preds = %27
%34 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str.9, i64 0, i64 0))
%35 = tail call i32 @close(i32 %17)
br label %36
36: ; preds = %33, %31, %25, %19, %4
%37 = phi i32 [ 1, %4 ], [ 1, %19 ], [ 1, %25 ], [ 1, %31 ], [ 0, %33 ]
ret i32 %37
}