#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*/ lf;

/* Variables and functions */
int /*<<< orphan*/ close(int);
int open_port(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ read_response(int);
int /*<<< orphan*/ sleep(int);
int strlen(char *);
int /*<<< orphan*/ write(int, char *, int);
int main(int argc, char *argv[]) {
  int fd;
  if (argc < 3) {
    printf("Usage: serial-control <device> <data1> ... <dataN> \n");
    return 0;
  }
  if (strlen(argv[2]) > 10) {
    printf("Data too long\n");
    return 0;
  }
  int n = 3;
  do {
    fd = open_port(argv[1]);
    n--;
    if (fd <= 0 && n > 0) {
      printf("Unable to open device %s retrying...\n", argv[1]);
      sleep(1);
    }
  } while (fd <= 0 && n > 0);
  if (fd <= 0) {
    printf("Unable to open device %s\n", argv[1]);
    return 0;
  }
  char lf = '\n';
  int i;
  for (i = 2; i < argc; i++) {
    write(fd, argv[i], strlen(argv[i]));
    write(fd, &lf, sizeof(lf));
    printf("Sent %s to %s\n", argv[i], argv[1]);
  }
  read_response(fd);
  close(fd);
  return 1;
}


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)
jge    11aa <main+0x3a>
lea    0xe69(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1327 <main+0x1b7>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1040 <strlen@plt>
cmp    $0xa,%eax
jle    11da <main+0x6a>
lea    0xe6e(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1327 <main+0x1b7>
movl   $0x3,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1080 <open_port@plt>
mov    %eax,-0x14(%rbp)
mov    -0x18(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x14(%rbp)
jg     122e <main+0xbe>
cmpl   $0x0,-0x18(%rbp)
jle    122e <main+0xbe>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xe27(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x1,%edi
call   1090 <sleep@plt>
jmp    1233 <main+0xc3>
xor    %eax,%eax
cmpl   $0x0,-0x14(%rbp)
mov    %al,-0x21(%rbp)
jg     124c <main+0xdc>
cmpl   $0x0,-0x18(%rbp)
setg   %al
mov    %al,-0x21(%rbp)
mov    -0x21(%rbp),%al
test   $0x1,%al
jne    11e1 <main+0x71>
cmpl   $0x0,-0x14(%rbp)
jg     1283 <main+0x113>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xdfa(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1327 <main+0x1b7>
movb   $0xa,-0x19(%rbp)
movl   $0x2,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    1310 <main+0x1a0>
mov    -0x14(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x20(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x20(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
call   1040 <strlen@plt>
mov    -0x34(%rbp),%edi
mov    -0x30(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    -0x14(%rbp),%edi
lea    -0x19(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
mov    -0x10(%rbp),%rax
movslq -0x20(%rbp),%rcx
mov    (%rax,%rcx,8),%rsi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xd89(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    128e <main+0x11e>
mov    -0x14(%rbp),%edi
call   1070 <read_response@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
cmp    $0x2,%edi
jg     1199 <main+0x19>
lea    0xec6(%rip),%rdi
jmp    11b4 <main+0x34>
mov    %rsi,%r12
mov    %edi,%r14d
mov    0x10(%rsi),%rdi
call   1050 <strlen@plt>
cmp    $0xa,%eax
jle    11c0 <main+0x40>
lea    0xe9b(%rip),%rdi
call   1030 <puts@plt>
xor    %ebx,%ebx
jmp    1248 <main+0xc8>
mov    0x8(%r12),%rdi
call   1090 <open_port@plt>
test   %eax,%eax
jle    1259 <main+0xd9>
mov    %eax,%ebp
movb   $0xa,0x7(%rsp)
mov    %r14d,%r13d
mov    $0x2,%r14d
lea    0xe57(%rip),%r15
nopl   0x0(%rax)
mov    (%r12,%r14,8),%rbx
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1040 <write@plt>
mov    %ebp,%edi
lea    0x7(%rsp),%rsi
mov    $0x1,%edx
call   1040 <write@plt>
mov    (%r12,%r14,8),%rsi
mov    0x8(%r12),%rdx
mov    %r15,%rdi
xor    %eax,%eax
call   1060 <printf@plt>
add    $0x1,%r14
cmp    %r14,%r13
jne    11f0 <main+0x70>
mov    %ebp,%edi
call   1080 <read_response@plt>
mov    %ebp,%edi
call   1070 <close@plt>
mov    $0x1,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0x3,%ebx
lea    0xd9b(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
nop
mov    0x8(%r12),%rsi
mov    %r15,%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0x1,%edi
call   10a0 <sleep@plt>
mov    0x8(%r12),%rdi
call   1090 <open_port@plt>
mov    %eax,%ebp
test   %eax,%eax
jg     12a3 <main+0x123>
lea    -0x1(%rbx),%eax
cmp    $0x2,%ebx
mov    %eax,%ebx
ja     1270 <main+0xf0>
test   %ebp,%ebp
jle    12bb <main+0x13b>
movb   $0xa,0x7(%rsp)
cmp    $0x3,%r14d
jge    11d9 <main+0x59>
jmp    1235 <main+0xb5>
mov    0x8(%r12),%rsi
lea    0xd5f(%rip),%rdi
xor    %ebx,%ebx
xor    %eax,%eax
call   1060 <printf@plt>
jmp    1248 <main+0xc8>

LLVM IR (-O0):
@.str = [53 x i8] c"Usage: serial-control <device> <data1> ... <dataN> \0A\00"
@.str.1 = [15 x i8] c"Data too long\0A\00"
@.str.2 = [38 x i8] c"Unable to open device %s retrying...\0A\00"
@.str.3 = [26 x i8] c"Unable to open device %s\0A\00"
@.str.4 = [15 x i8] c"Sent %s to %s\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 i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* %4
%11 = icmp slt i32 %10, 3
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %3
br label %96
14: ; preds = %2
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 2
%17 = load i8*, i8** %16
%18 = call i32 @strlen(i8* %17)
%19 = icmp sgt i32 %18, 10
br i1 %19, label %20, label %22
20: ; preds = %14
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %3
br label %96
22: ; preds = %14
store i32 3, i32* %7
br label %23
23: ; preds = %48, %22
%24 = load i8**, i8*** %5
%25 = getelementptr inbounds i8*, i8** %24, i64 1
%26 = load i8*, i8** %25
%27 = call i32 @open_port(i8* %26)
store i32 %27, i32* %6
%28 = load i32, i32* %7
%29 = add nsw i32 %28, -1
store i32 %29, i32* %7
%30 = load i32, i32* %6
%31 = icmp sle i32 %30, 0
br i1 %31, label %32, label %41
32: ; preds = %23
%33 = load i32, i32* %7
%34 = icmp sgt i32 %33, 0
br i1 %34, label %35, label %41
35: ; preds = %32
%36 = load i8**, i8*** %5
%37 = getelementptr inbounds i8*, i8** %36, i64 1
%38 = load i8*, i8** %37
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0), i8* %38)
%40 = call i32 @sleep(i32 1)
br label %41
41: ; preds = %35, %32, %23
br label %42
42: ; preds = %41
%43 = load i32, i32* %6
%44 = icmp sle i32 %43, 0
br i1 %44, label %45, label %48
45: ; preds = %42
%46 = load i32, i32* %7
%47 = icmp sgt i32 %46, 0
br label %48
48: ; preds = %45, %42
%49 = phi i1 [ false, %42 ], [ %47, %45 ]
br i1 %49, label %23, label %50
50: ; preds = %48
%51 = load i32, i32* %6
%52 = icmp sle i32 %51, 0
br i1 %52, label %53, label %58
53: ; preds = %50
%54 = load i8**, i8*** %5
%55 = getelementptr inbounds i8*, i8** %54, i64 1
%56 = load i8*, i8** %55
%57 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0), i8* %56)
store i32 0, i32* %3
br label %96
58: ; preds = %50
store i8 10, i8* %8
store i32 2, i32* %9
br label %59
59: ; preds = %88, %58
%60 = load i32, i32* %9
%61 = load i32, i32* %4
%62 = icmp slt i32 %60, %61
br i1 %62, label %63, label %91
63: ; preds = %59
%64 = load i32, i32* %6
%65 = load i8**, i8*** %5
%66 = load i32, i32* %9
%67 = sext i32 %66 to i64
%68 = getelementptr inbounds i8*, i8** %65, i64 %67
%69 = load i8*, i8** %68
%70 = load i8**, i8*** %5
%71 = load i32, i32* %9
%72 = sext i32 %71 to i64
%73 = getelementptr inbounds i8*, i8** %70, i64 %72
%74 = load i8*, i8** %73
%75 = call i32 @strlen(i8* %74)
%76 = call i32 @write(i32 %64, i8* %69, i32 %75)
%77 = load i32, i32* %6
%78 = call i32 @write(i32 %77, i8* %8, i32 1)
%79 = load i8**, i8*** %5
%80 = load i32, i32* %9
%81 = sext i32 %80 to i64
%82 = getelementptr inbounds i8*, i8** %79, i64 %81
%83 = load i8*, i8** %82
%84 = load i8**, i8*** %5
%85 = getelementptr inbounds i8*, i8** %84, i64 1
%86 = load i8*, i8** %85
%87 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0), i8* %83, i8* %86)
br label %88
88: ; preds = %63
%89 = load i32, i32* %9
%90 = add nsw i32 %89, 1
store i32 %90, i32* %9
br label %59
91: ; preds = %59
%92 = load i32, i32* %6
%93 = call i32 @read_response(i32 %92)
%94 = load i32, i32* %6
%95 = call i32 @close(i32 %94)
store i32 1, i32* %3
br label %96
96: ; preds = %91, %53, %20, %12
%97 = load i32, i32* %3
ret i32 %97
}

LLVM IR (-O2):
@.str.2 = [38 x i8] c"Unable to open device %s retrying...\0A\00"
@.str.3 = [26 x i8] c"Unable to open device %s\0A\00"
@.str.4 = [15 x i8] c"Sent %s to %s\0A\00"
@str = [14 x i8] c"Data too long\00"
@str.5 = [52 x i8] c"Usage: serial-control <device> <data1> ... <dataN> \00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i8
%4 = icmp slt i32 %0, 3
br i1 %4, label %5, label %7
5: ; preds = %2
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([52 x i8], [52 x i8]* @str.5, i64 0, i64 0))
br label %56
7: ; preds = %2
%8 = getelementptr inbounds i8*, i8** %1, i64 2
%9 = load i8*, i8** %8
%10 = tail call i32 @strlen(i8* %9)
%11 = icmp sgt i32 %10, 10
br i1 %11, label %18, label %12
12: ; preds = %7
%13 = getelementptr inbounds i8*, i8** %1, i64 1
%14 = load i8*, i8** %13
%15 = tail call i32 @open_port(i8* %14)
%16 = icmp slt i32 %15, 1
br i1 %16, label %20, label %17
17: ; preds = %12
store i8 10, i8* %3
br label %37
18: ; preds = %7
%19 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str, i64 0, i64 0))
br label %56
20: ; preds = %12, %20
%21 = phi i32 [ %22, %20 ], [ 3, %12 ]
%22 = add nsw i32 %21, -1
%23 = load i8*, i8** %13
%24 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0), i8* %23)
%25 = tail call i32 @sleep(i32 1)
%26 = load i8*, i8** %13
%27 = tail call i32 @open_port(i8* %26)
%28 = icmp slt i32 %27, 1
%29 = icmp ugt i32 %21, 2
%30 = select i1 %28, i1 %29, i1 false
br i1 %30, label %20, label %31
31: ; preds = %20
br i1 %28, label %32, label %35
32: ; preds = %31
%33 = load i8*, i8** %13
%34 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0), i8* %33)
br label %56
35: ; preds = %31
store i8 10, i8* %3
%36 = icmp sgt i32 %0, 2
br i1 %36, label %37, label %52
37: ; preds = %17, %35
%38 = phi i32 [ %15, %17 ], [ %27, %35 ]
%39 = zext i32 %0 to i64
br label %40
40: ; preds = %37, %40
%41 = phi i64 [ 2, %37 ], [ %50, %40 ]
%42 = getelementptr inbounds i8*, i8** %1, i64 %41
%43 = load i8*, i8** %42
%44 = tail call i32 @strlen(i8* %43)
%45 = tail call i32 @write(i32 %38, i8* %43, i32 %44)
%46 = call i32 @write(i32 %38, i8* %3, i32 1)
%47 = load i8*, i8** %42
%48 = load i8*, i8** %13
%49 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0), i8* %47, i8* %48)
%50 = add nuw nsw i64 %41, 1
%51 = icmp eq i64 %50, %39
br i1 %51, label %52, label %40
52: ; preds = %40, %35
%53 = phi i32 [ %27, %35 ], [ %38, %40 ]
%54 = tail call i32 @read_response(i32 %53)
%55 = tail call i32 @close(i32 %53)
br label %56
56: ; preds = %32, %52, %18, %5
%57 = phi i32 [ 0, %5 ], [ 0, %18 ], [ 0, %32 ], [ 1, %52 ]
ret i32 %57
}