#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);
scalar_t__ dos2unix(char *, char *);
int /*<<< orphan*/ exit(int);
int getopt(int, char **, char *);
scalar_t__ initmc(int);
int open_port(char *);
char *optarg;
int /*<<< orphan*/ printf(char *, ...);
scalar_t__ sendprog(char *, int);
int strlen(char *);
int /*<<< orphan*/ unlink(char *);
int main(int argc, char **argv) {
  int mainfd = 0;
  int opt;
  char *filename;
  char *devname;
  if (argc < 3) {
    printf("Usage: %s -f <file.dat> -d <tty device>\n", argv[0]);
    return 1;
  }
  while (1) {
    opt = getopt(argc, argv, "d:f:");
    if (opt == -1)
      break;
    switch (opt) {
    case 'd':
      devname = optarg;
      break;
    case 'f':
      filename = optarg;
      break;
    default:
      printf("Usage: %s -f <file.dat> -d <tty device>\n", argv[0]);
      exit(1);
      break;
    }
  }
  if (strlen(filename) < 1 || strlen(devname) < 1) {
    printf("Usage: %s -f <file.dat> -d <tty device>\n", argv[0]);
    exit(1);
  }
  if (dos2unix(filename, "123123123temp.dat") < 0) {
    printf("dos2unix conversion failed, probably failed opening %s or creating "
           "123123123temp.dat\n",
           filename);
    return 1;
  }
  mainfd = open_port(devname);
  if (mainfd < 0) {
    printf("failed opening %s\n", devname);
    return 1;
  } else
    printf("opened device successfully\n");
  if (initmc(mainfd) < 0) {
    printf("failed mc init\n");
    return 1;
  } else
    printf("intialzied micro controller successfully\n");
  if (sendprog("123123123temp.dat", mainfd) < 0) {
    printf("failed programming\n");
    return 1;
  } else
    printf("programmed eeprom successfully\n");
  unlink("123123123temp.dat");
  close(mainfd);
  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)
movl   $0x0,-0x14(%rbp)
cmpl   $0x3,-0x8(%rbp)
jge    11e8 <main+0x48>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe2b(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    13d4 <main+0x234>
jmp    11ed <main+0x4d>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
lea    0xe2e(%rip),%rdx
call   10a0 <getopt@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    1212 <main+0x72>
jmp    1281 <main+0xe1>
mov    -0x18(%rbp),%eax
mov    %eax,-0x2c(%rbp)
sub    $0x64,%eax
je     1237 <main+0x97>
jmp    1226 <main+0x86>
mov    -0x2c(%rbp),%eax
sub    $0x66,%eax
je     124a <main+0xaa>
jmp    125d <main+0xbd>
mov    0x2dba(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
jmp    127c <main+0xdc>
mov    0x2da7(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x20(%rbp)
jmp    127c <main+0xdc>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xd95(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
jmp    11ed <main+0x4d>
mov    -0x20(%rbp),%rdi
call   1050 <strlen@plt>
cmp    $0x1,%eax
jl     12a5 <main+0x105>
mov    -0x28(%rbp),%rdi
call   1050 <strlen@plt>
cmp    $0x1,%eax
jge    12c4 <main+0x124>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xd4d(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x20(%rbp),%rdi
lea    0xd5f(%rip),%rsi
call   1080 <dos2unix@plt>
cmp    $0x0,%rax
jge    12fc <main+0x15c>
mov    -0x20(%rbp),%rsi
lea    0xd57(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    13d4 <main+0x234>
mov    -0x28(%rbp),%rdi
call   10c0 <open_port@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1330 <main+0x190>
mov    -0x28(%rbp),%rsi
lea    0xd79(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    13d4 <main+0x234>
lea    0xd72(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x14(%rbp),%edi
call   1030 <initmc@plt>
cmp    $0x0,%rax
jge    136a <main+0x1ca>
lea    0xd6e(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    13d4 <main+0x234>
lea    0xd64(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
mov    -0x14(%rbp),%esi
lea    0xcac(%rip),%rdi
call   1060 <sendprog@plt>
cmp    $0x0,%rax
jge    13ab <main+0x20b>
lea    0xd67(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    13d4 <main+0x234>
lea    0xd61(%rip),%rdi
mov    $0x0,%al
call   1070 <printf@plt>
lea    0xc6e(%rip),%rdi
call   1040 <unlink@plt>
mov    -0x14(%rbp),%edi
call   1090 <close@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   %r13
push   %r12
push   %rbx
push   %rax
mov    %rsi,%rbx
cmp    $0x3,%edi
jge    11d2 <main+0x22>
mov    (%rbx),%rsi
lea    0xe33(%rip),%rdi
jmp    12cc <main+0x11c>
mov    %edi,%ebp
lea    0xe4e(%rip),%r12
mov    0x2e16(%rip),%r13
jmp    11f4 <main+0x44>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    0x0(%r13),%r15
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r12,%rdx
call   10b0 <getopt@plt>
cmp    $0x64,%eax
je     11f0 <main+0x40>
cmp    $0xffffffff,%eax
je     121a <main+0x6a>
cmp    $0x66,%eax
jne    12fe <main+0x14e>
mov    0x0(%r13),%r14
jmp    11f4 <main+0x44>
mov    %r14,%rdi
call   1060 <strlen@plt>
test   %eax,%eax
jle    12fe <main+0x14e>
mov    %r15,%rdi
call   1060 <strlen@plt>
test   %eax,%eax
jle    12fe <main+0x14e>
lea    0xded(%rip),%rsi
mov    %r14,%rdi
call   1090 <dos2unix@plt>
test   %rax,%rax
js     12b6 <main+0x106>
mov    %r15,%rdi
call   10d0 <open_port@plt>
test   %eax,%eax
js     12c2 <main+0x112>
mov    %eax,%ebx
lea    0xe46(%rip),%rdi
call   1050 <puts@plt>
mov    %ebx,%edi
call   1030 <initmc@plt>
test   %rax,%rax
js     12e7 <main+0x137>
lea    0xe49(%rip),%rdi
call   1050 <puts@plt>
lea    0xda7(%rip),%rdi
mov    %ebx,%esi
call   1070 <sendprog@plt>
test   %rax,%rax
js     12f0 <main+0x140>
lea    0xe53(%rip),%rdi
call   1050 <puts@plt>
lea    0xd88(%rip),%rdi
call   1040 <unlink@plt>
mov    %ebx,%edi
call   10a0 <close@plt>
xor    %eax,%eax
jmp    12d8 <main+0x128>
lea    0xd83(%rip),%rdi
mov    %r14,%rsi
jmp    12cc <main+0x11c>
lea    0xdcd(%rip),%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   1080 <printf@plt>
mov    $0x1,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe31(%rip),%rdi
jmp    12f7 <main+0x147>
lea    0xe15(%rip),%rdi
call   1050 <puts@plt>
jmp    12d3 <main+0x123>
mov    (%rbx),%rsi
lea    0xcf8(%rip),%rdi
xor    %eax,%eax
call   1080 <printf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
@.str = [41 x i8] c"Usage: %s -f <file.dat> -d <tty device>\0A\00"
@.str.1 = [5 x i8] c"d:f:\00"
@optarg = global i8* null
@.str.2 = [18 x i8] c"123123123temp.dat\00"
@.str.3 = [86 x i8] c"dos2unix conversion failed, probably failed opening %s or creating 123123123temp.dat\0A\00"
@.str.4 = [19 x i8] c"failed opening %s\0A\00"
@.str.5 = [28 x i8] c"opened device successfully\0A\00"
@.str.6 = [16 x i8] c"failed mc init\0A\00"
@.str.7 = [42 x i8] c"intialzied micro controller successfully\0A\00"
@.str.8 = [20 x i8] c"failed programming\0A\00"
@.str.9 = [32 x i8] c"programmed eeprom 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, 3
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 %89
17: ; preds = %2
br label %18
18: ; preds = %17, %37
%19 = load i32, i32* %4
%20 = load i8**, i8*** %5
%21 = call i32 @getopt(i32 %19, i8** %20, i8* getelementptr inbounds ([5 x i8], [5 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 %38
25: ; preds = %18
%26 = load i32, i32* %7
switch i32 %26, label %31 [
i32 100, label %27
i32 102, label %29
]
27: ; preds = %25
%28 = load i8*, i8** @optarg
store i8* %28, i8** %9
br label %37
29: ; preds = %25
%30 = load i8*, i8** @optarg
store i8* %30, i8** %8
br label %37
31: ; preds = %25
%32 = load i8**, i8*** %5
%33 = getelementptr inbounds i8*, i8** %32, i64 0
%34 = load i8*, i8** %33
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0), i8* %34)
%36 = call i32 @exit(i32 1)
unreachable
37: ; preds = %29, %27
br label %18
38: ; preds = %24
%39 = load i8*, i8** %8
%40 = call i32 @strlen(i8* %39)
%41 = icmp slt i32 %40, 1
br i1 %41, label %46, label %42
42: ; preds = %38
%43 = load i8*, i8** %9
%44 = call i32 @strlen(i8* %43)
%45 = icmp slt i32 %44, 1
br i1 %45, label %46, label %52
46: ; preds = %42, %38
%47 = load i8**, i8*** %5
%48 = getelementptr inbounds i8*, i8** %47, i64 0
%49 = load i8*, i8** %48
%50 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0), i8* %49)
%51 = call i32 @exit(i32 1)
unreachable
52: ; preds = %42
%53 = load i8*, i8** %8
%54 = call i64 @dos2unix(i8* %53, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
%55 = icmp slt i64 %54, 0
br i1 %55, label %56, label %59
56: ; preds = %52
%57 = load i8*, i8** %8
%58 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([86 x i8], [86 x i8]* @.str.3, i64 0, i64 0), i8* %57)
store i32 1, i32* %3
br label %89
59: ; preds = %52
%60 = load i8*, i8** %9
%61 = call i32 @open_port(i8* %60)
store i32 %61, i32* %6
%62 = load i32, i32* %6
%63 = icmp slt i32 %62, 0
br i1 %63, label %64, label %67
64: ; preds = %59
%65 = load i8*, i8** %9
%66 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), i8* %65)
store i32 1, i32* %3
br label %89
67: ; preds = %59
%68 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.5, i64 0, i64 0))
br label %69
69: ; preds = %67
%70 = load i32, i32* %6
%71 = call i64 @initmc(i32 %70)
%72 = icmp slt i64 %71, 0
br i1 %72, label %73, label %75
73: ; preds = %69
%74 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.6, i64 0, i64 0))
store i32 1, i32* %3
br label %89
75: ; preds = %69
%76 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.7, i64 0, i64 0))
br label %77
77: ; preds = %75
%78 = load i32, i32* %6
%79 = call i64 @sendprog(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i32 %78)
%80 = icmp slt i64 %79, 0
br i1 %80, label %81, label %83
81: ; preds = %77
%82 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.8, i64 0, i64 0))
store i32 1, i32* %3
br label %89
83: ; preds = %77
%84 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.9, i64 0, i64 0))
br label %85
85: ; preds = %83
%86 = call i32 @unlink(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
%87 = load i32, i32* %6
%88 = call i32 @close(i32 %87)
store i32 0, i32* %3
br label %89
89: ; preds = %85, %81, %73, %64, %56, %12
%90 = load i32, i32* %3
ret i32 %90
}

LLVM IR (-O2):
@.str = [41 x i8] c"Usage: %s -f <file.dat> -d <tty device>\0A\00"
@.str.1 = [5 x i8] c"d:f:\00"
@optarg = global i8* null
@.str.2 = [18 x i8] c"123123123temp.dat\00"
@.str.3 = [86 x i8] c"dos2unix conversion failed, probably failed opening %s or creating 123123123temp.dat\0A\00"
@.str.4 = [19 x i8] c"failed opening %s\0A\00"
@str = [27 x i8] c"opened device successfully\00"
@str.10 = [41 x i8] c"intialzied micro controller successfully\00"
@str.11 = [31 x i8] c"programmed eeprom successfully\00"
@str.12 = [19 x i8] c"failed programming\00"
@str.13 = [15 x i8] c"failed mc init\00"
define i32 @main(i32 %0, i8** %1) {
%3 = icmp slt i32 %0, 3
br i1 %3, label %4, label %14
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 %57
7: ; preds = %14, %10
%8 = phi i8* [ %11, %10 ], [ %16, %14 ]
%9 = tail call i32 @getopt(i32 %0, i8** %1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
switch i32 %9, label %17 [
i32 -1, label %21
i32 100, label %10
i32 102, label %12
]
10: ; preds = %7
%11 = load i8*, i8** @optarg
br label %7
12: ; preds = %7
%13 = load i8*, i8** @optarg
br label %14
14: ; preds = %2, %12
%15 = phi i8* [ %13, %12 ], [ undef, %2 ]
%16 = phi i8* [ %8, %12 ], [ undef, %2 ]
br label %7
17: ; preds = %7
%18 = load i8*, i8** %1
%19 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0), i8* %18)
%20 = tail call i32 @exit(i32 1)
unreachable
21: ; preds = %7
%22 = tail call i32 @strlen(i8* %15)
%23 = icmp slt i32 %22, 1
br i1 %23, label %27, label %24
24: ; preds = %21
%25 = tail call i32 @strlen(i8* %8)
%26 = icmp slt i32 %25, 1
br i1 %26, label %27, label %31
27: ; preds = %24, %21
%28 = load i8*, i8** %1
%29 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0), i8* %28)
%30 = tail call i32 @exit(i32 1)
unreachable
31: ; preds = %24
%32 = tail call i64 @dos2unix(i8* %15, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
%33 = icmp slt i64 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %31
%35 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([86 x i8], [86 x i8]* @.str.3, i64 0, i64 0), i8* %15)
br label %57
36: ; preds = %31
%37 = tail call i32 @open_port(i8* %8)
%38 = icmp slt i32 %37, 0
br i1 %38, label %39, label %41
39: ; preds = %36
%40 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), i8* %8)
br label %57
41: ; preds = %36
%42 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str, i64 0, i64 0))
%43 = tail call i64 @initmc(i32 %37)
%44 = icmp slt i64 %43, 0
br i1 %44, label %45, label %47
45: ; preds = %41
%46 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str.13, i64 0, i64 0))
br label %57
47: ; preds = %41
%48 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([41 x i8], [41 x i8]* @str.10, i64 0, i64 0))
%49 = tail call i64 @sendprog(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i32 %37)
%50 = icmp slt i64 %49, 0
br i1 %50, label %51, label %53
51: ; preds = %47
%52 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str.12, i64 0, i64 0))
br label %57
53: ; preds = %47
%54 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @str.11, i64 0, i64 0))
%55 = tail call i32 @unlink(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
%56 = tail call i32 @close(i32 %37)
br label %57
57: ; preds = %53, %51, %45, %39, %34, %4
%58 = phi i32 [ 1, %4 ], [ 1, %34 ], [ 1, %39 ], [ 1, %45 ], [ 1, %51 ], [ 0, %53 ]
ret i32 %58
}