#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 /*<<< orphan*/ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int fscanf(int /*<<< orphan*/ *, char *, int *, int *);
char getopt(int, char **, char *);
int /*<<< orphan*/ *open_file(char *, char *);
int open_port(char *);
char *optarg;
scalar_t__ optind;
int /*<<< orphan*/ sprintf(char *, char *, int);
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ usleep(int);
int /*<<< orphan*/ write_port(int, char *);
int main(int argc, char *argv[]) {
  char ch;
  char *filename = "little_lamb.txt";
  char *port = "/dev/ttyACM0";
  int frequency = 0;
  int duration = 0;
  while ((ch = getopt(argc, argv, "f:p:h")) != -1) {
    switch (ch) {
    case 'f':
      filename = optarg;
      break;
    case 'p':
      port = optarg;
      break;
    case 'h':
      fprintf(stdout, "Optargs:\n"
                      "   -f, define the song file name\n"
                      "   -p, define the serial port\n"
                      "   -h, help\n");
      return 0;
    }
  }
  argc -= optind;
  argv += optind;
  FILE *song = open_file(filename, "r");
  int fd = open_port(port);
  while (fscanf(song, "%d, %d", &frequency, &duration) == 2) {
    char data[20];
    sprintf(data, "%d", frequency);
    write_port(fd, data);
    usleep(duration * 1000);
  }
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe53(%rip),%rax
mov    %rax,-0x20(%rbp)
lea    0xe58(%rip),%rax
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
movl   $0x0,-0x30(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
lea    0xe45(%rip),%rdx
call   1080 <getopt@plt>
mov    %al,-0x11(%rbp)
movsbl %al,%eax
cmp    $0xffffffff,%eax
je     126f <main+0xdf>
movsbl -0x11(%rbp),%eax
mov    %eax,-0x54(%rbp)
sub    $0x66,%eax
je     1223 <main+0x93>
jmp    1201 <main+0x71>
mov    -0x54(%rbp),%eax
sub    $0x68,%eax
je     1249 <main+0xb9>
jmp    1212 <main+0x82>
mov    -0x54(%rbp),%eax
sub    $0x70,%eax
je     1236 <main+0xa6>
jmp    126a <main+0xda>
mov    0x2dce(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x20(%rbp)
jmp    126a <main+0xda>
mov    0x2dbb(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
jmp    126a <main+0xda>
mov    0x2d80(%rip),%rax
mov    (%rax),%edi
lea    0xdca(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1319 <main+0x189>
jmp    11ca <main+0x3a>
mov    0x2d6a(%rip),%rax
mov    (%rax),%rcx
movslq -0x8(%rbp),%rax
sub    %rcx,%rax
mov    %eax,-0x8(%rbp)
mov    0x2d56(%rip),%rax
mov    (%rax),%rax
shl    $0x3,%rax
add    -0x10(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x20(%rbp),%rdi
lea    0xdd4(%rip),%rsi
call   1030 <open_file@plt>
mov    %rax,-0x38(%rbp)
mov    -0x28(%rbp),%rdi
call   10a0 <open_port@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x38(%rbp),%rdi
lea    0xdb6(%rip),%rsi
lea    -0x2c(%rbp),%rdx
lea    -0x30(%rbp),%rcx
call   1040 <fscanf@plt>
cmp    $0x2,%eax
jne    130a <main+0x17a>
lea    -0x50(%rbp),%rdi
mov    -0x2c(%rbp),%edx
lea    0xd96(%rip),%rsi
call   1090 <sprintf@plt>
mov    -0x3c(%rbp),%edi
lea    -0x50(%rbp),%rsi
call   1070 <write_port@plt>
imul   $0x3e8,-0x30(%rbp),%edi
call   10b0 <usleep@plt>
jmp    12b9 <main+0x129>
mov    -0x3c(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rsi,%rbx
mov    %edi,%ebp
movl   $0x0,0xc(%rsp)
movl   $0x0,0x8(%rsp)
lea    0xe63(%rip),%rdx
call   1080 <getopt@plt>
cmp    $0xff,%al
je     1226 <main+0x96>
lea    0xe36(%rip),%r15
lea    0xe3f(%rip),%r14
mov    0x2e20(%rip),%r13
lea    0xe3e(%rip),%r12
jmp    1205 <main+0x75>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    0x0(%r13),%r15
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r12,%rdx
call   1080 <getopt@plt>
cmp    $0xff,%al
je     1234 <main+0xa4>
movsbl %al,%eax
cmp    $0x66,%eax
je     11f0 <main+0x60>
cmp    $0x70,%eax
je     1220 <main+0x90>
cmp    $0x68,%eax
jne    11f4 <main+0x64>
jmp    12e8 <main+0x158>
nopl   0x0(%rax)
mov    0x0(%r13),%r14
jmp    11f4 <main+0x64>
lea    0xde3(%rip),%r14
lea    0xdcc(%rip),%r15
lea    0xe3d(%rip),%rsi
mov    %r15,%rdi
call   1030 <open_file@plt>
mov    %rax,%r15
mov    %r14,%rdi
call   10a0 <open_port@plt>
mov    %eax,%ebp
lea    0xe23(%rip),%rsi
lea    0xc(%rsp),%rdx
lea    0x8(%rsp),%rcx
mov    %r15,%rdi
call   1040 <fscanf@plt>
cmp    $0x2,%eax
jne    12d0 <main+0x140>
lea    0x10(%rsp),%rbx
lea    0xe00(%rip),%r12
lea    0xc(%rsp),%r13
lea    0x8(%rsp),%r14
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    0xc(%rsp),%edx
mov    %rbx,%rdi
lea    0xde0(%rip),%rsi
call   1090 <sprintf@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
call   1070 <write_port@plt>
imul   $0x3e8,0x8(%rsp),%edi
call   10b0 <usleep@plt>
mov    %r15,%rdi
mov    %r12,%rsi
mov    %r13,%rdx
mov    %r14,%rcx
call   1040 <fscanf@plt>
cmp    $0x2,%eax
je     1290 <main+0x100>
mov    %ebp,%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2ce9(%rip),%rax
mov    (%rax),%edi
lea    0xd2b(%rip),%rsi
call   1060 <fprintf@plt>
jmp    12d7 <main+0x147>

LLVM IR (-O0):
@.str = [16 x i8] c"little_lamb.txt\00"
@.str.1 = [13 x i8] c"/dev/ttyACM0\00"
@.str.2 = [6 x i8] c"f:p:h\00"
@optarg = global i8* null
@stdout = global i32 0
@.str.3 = [85 x i8] c"Optargs:\0A -f, define the song file name\0A -p, define the serial port\0A -h, help\0A\00"
@optind = global i64 0
@.str.4 = [2 x i8] c"r\00"
@.str.5 = [7 x i8] c"%d, %d\00"
@.str.6 = [3 x i8] c"%d\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32*
%12 = alloca i32
%13 = alloca [20 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8** %7
store i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i8** %8
store i32 0, i32* %9
store i32 0, i32* %10
br label %14
14: ; preds = %30, %2
%15 = load i32, i32* %4
%16 = load i8**, i8*** %5
%17 = call signext i8 @getopt(i32 %15, i8** %16, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
store i8 %17, i8* %6
%18 = sext i8 %17 to i32
%19 = icmp ne i32 %18, -1
br i1 %19, label %20, label %31
20: ; preds = %14
%21 = load i8, i8* %6
%22 = sext i8 %21 to i32
switch i32 %22, label %30 [
i32 102, label %23
i32 112, label %25
i32 104, label %27
]
23: ; preds = %20
%24 = load i8*, i8** @optarg
store i8* %24, i8** %7
br label %30
25: ; preds = %20
%26 = load i8*, i8** @optarg
store i8* %26, i8** %8
br label %30
27: ; preds = %20
%28 = load i32, i32* @stdout
%29 = call i32 @fprintf(i32 %28, i8* getelementptr inbounds ([85 x i8], [85 x i8]* @.str.3, i64 0, i64 0))
store i32 0, i32* %3
br label %61
30: ; preds = %20, %25, %23
br label %14
31: ; preds = %14
%32 = load i64, i64* @optind
%33 = load i32, i32* %4
%34 = sext i32 %33 to i64
%35 = sub nsw i64 %34, %32
%36 = trunc i64 %35 to i32
store i32 %36, i32* %4
%37 = load i64, i64* @optind
%38 = load i8**, i8*** %5
%39 = getelementptr inbounds i8*, i8** %38, i64 %37
store i8** %39, i8*** %5
%40 = load i8*, i8** %7
%41 = call i32* @open_file(i8* %40, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
store i32* %41, i32** %11
%42 = load i8*, i8** %8
%43 = call i32 @open_port(i8* %42)
store i32 %43, i32* %12
br label %44
44: ; preds = %48, %31
%45 = load i32*, i32** %11
%46 = call i32 @fscanf(i32* %45, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0), i32* %9, i32* %10)
%47 = icmp eq i32 %46, 2
br i1 %47, label %48, label %58
48: ; preds = %44
%49 = getelementptr inbounds [20 x i8], [20 x i8]* %13, i64 0, i64 0
%50 = load i32, i32* %9
%51 = call i32 @sprintf(i8* %49, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0), i32 %50)
%52 = load i32, i32* %12
%53 = getelementptr inbounds [20 x i8], [20 x i8]* %13, i64 0, i64 0
%54 = call i32 @write_port(i32 %52, i8* %53)
%55 = load i32, i32* %10
%56 = mul nsw i32 %55, 1000
%57 = call i32 @usleep(i32 %56)
br label %44
58: ; preds = %44
%59 = load i32, i32* %12
%60 = call i32 @close(i32 %59)
store i32 0, i32* %3
br label %61
61: ; preds = %58, %27
%62 = load i32, i32* %3
ret i32 %62
}

LLVM IR (-O2):
@.str = [16 x i8] c"little_lamb.txt\00"
@.str.1 = [13 x i8] c"/dev/ttyACM0\00"
@.str.2 = [6 x i8] c"f:p:h\00"
@optarg = global i8* null
@stdout = global i32 0
@.str.3 = [85 x i8] c"Optargs:\0A -f, define the song file name\0A -p, define the serial port\0A -h, help\0A\00"
@optind = global i64 0
@.str.4 = [2 x i8] c"r\00"
@.str.5 = [7 x i8] c"%d, %d\00"
@.str.6 = [3 x i8] c"%d\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca [20 x i8]
%6 = bitcast i32* %3 to i8*
store i32 0, i32* %3
%7 = bitcast i32* %4 to i8*
store i32 0, i32* %4
%8 = tail call signext i8 @getopt(i32 %0, i8** %1, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%9 = icmp eq i8 %8, -1
br i1 %9, label %27, label %10
10: ; preds = %2, %22
%11 = phi i8 [ %25, %22 ], [ %8, %2 ]
%12 = phi i8* [ %24, %22 ], [ getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), %2 ]
%13 = phi i8* [ %23, %22 ], [ getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), %2 ]
%14 = sext i8 %11 to i32
switch i32 %14, label %22 [
i32 102, label %15
i32 112, label %17
i32 104, label %19
]
15: ; preds = %10
%16 = load i8*, i8** @optarg
br label %22
17: ; preds = %10
%18 = load i8*, i8** @optarg
br label %22
19: ; preds = %10
%20 = load i32, i32* @stdout
%21 = tail call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([85 x i8], [85 x i8]* @.str.3, i64 0, i64 0))
br label %47
22: ; preds = %10, %17, %15
%23 = phi i8* [ %13, %10 ], [ %13, %17 ], [ %16, %15 ]
%24 = phi i8* [ %12, %10 ], [ %18, %17 ], [ %12, %15 ]
%25 = tail call signext i8 @getopt(i32 %0, i8** %1, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%26 = icmp eq i8 %25, -1
br i1 %26, label %27, label %10
27: ; preds = %22, %2
%28 = phi i8* [ getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), %2 ], [ %23, %22 ]
%29 = phi i8* [ getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), %2 ], [ %24, %22 ]
%30 = tail call i32* @open_file(i8* %28, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
%31 = tail call i32 @open_port(i8* %29)
%32 = call i32 @fscanf(i32* %30, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0), i32* %3, i32* %4)
%33 = icmp eq i32 %32, 2
br i1 %33, label %34, label %45
34: ; preds = %27
%35 = getelementptr inbounds [20 x i8], [20 x i8]* %5, i64 0, i64 0
br label %36
36: ; preds = %34, %36
%37 = load i32, i32* %3
%38 = call i32 @sprintf(i8* %35, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.6, i64 0, i64 0), i32 %37)
%39 = call i32 @write_port(i32 %31, i8* %35)
%40 = load i32, i32* %4
%41 = mul nsw i32 %40, 1000
%42 = call i32 @usleep(i32 %41)
%43 = call i32 @fscanf(i32* %30, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0), i32* %3, i32* %4)
%44 = icmp eq i32 %43, 2
br i1 %44, label %36, label %45
45: ; preds = %36, %27
%46 = call i32 @close(i32 %31)
br label %47
47: ; preds = %45, %19
ret i32 0
}