#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*/ printf(char *, ...);
int /*<<< orphan*/ read(int, int *, int);
int s_open(char *);
int /*<<< orphan*/ strcpy(char *, char *);
int main(int argc, char **argv) {
  int ser;
  int n, i;
  char port[64] = "/dev/ttyUSB0";
  if (argc >= 2) {
    strcpy(port, argv[1]);
  }
  ser = s_open(port);
  if (ser < 0) {
    printf("could not open serial port: %s\n", port);
    return 1;
  } else {
    printf("serial port opened: %s\n", port);
  }
  printf("reading bytes...\n");
  for (i = 0; i < 256; ++i) {
    read(ser, &n, 1);
    printf("%x\n", n);
  }
  printf("done!\n");
  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    -0x60(%rbp),%rdi
lea    0xe8f(%rip),%rsi
mov    $0x40,%edx
call   1070 <memcpy@plt>
cmpl   $0x2,-0x8(%rbp)
jl     1196 <main+0x46>
lea    -0x60(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
call   1030 <strcpy@plt>
lea    -0x60(%rbp),%rdi
call   1040 <s_open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11ca <main+0x7a>
lea    -0x60(%rbp),%rsi
lea    0xe89(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1243 <main+0xf3>
lea    -0x60(%rbp),%rsi
lea    0xe8b(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xe95(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x0,-0x1c(%rbp)
cmpl   $0x100,-0x1c(%rbp)
jge    122e <main+0xde>
mov    -0x14(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x1,%edx
call   1060 <read@plt>
mov    -0x18(%rbp),%esi
lea    0xe71(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11f1 <main+0xa1>
lea    0xe59(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@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   %rbx
sub    $0x58,%rsp
movaps 0xecf(%rip),%xmm0
movaps %xmm0,0x40(%rsp)
movaps 0xeb3(%rip),%xmm0
movaps %xmm0,0x30(%rsp)
movaps 0xe97(%rip),%xmm0
movaps %xmm0,0x20(%rsp)
movaps 0xe7b(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
cmp    $0x2,%edi
jl     119d <main+0x4d>
mov    0x8(%rsi),%rsi
lea    0x10(%rsp),%rdi
call   1030 <strcpy@plt>
lea    0x10(%rsp),%rdi
call   1050 <s_open@plt>
test   %eax,%eax
js     1212 <main+0xc2>
mov    %eax,%ebx
lea    0xeac(%rip),%rdi
lea    0x10(%rsp),%rsi
xor    %eax,%eax
call   1060 <printf@plt>
lea    0xeb5(%rip),%rdi
call   1040 <puts@plt>
mov    $0x100,%ebp
lea    0xc(%rsp),%r14
lea    0xe9b(%rip),%r15
nopl   (%rax)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1070 <read@plt>
mov    0xc(%rsp),%esi
mov    %r15,%rdi
xor    %eax,%eax
call   1060 <printf@plt>
add    $0xffffffff,%ebp
jne    11e0 <main+0x90>
lea    0xe84(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
jmp    122a <main+0xda>
lea    0xe27(%rip),%rdi
lea    0x10(%rsp),%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0x1,%eax
add    $0x58,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@__const.main.port = [64 x i8] c"/dev/ttyUSB0\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00"
@.str = [32 x i8] c"could not open serial port: %s\0A\00"
@.str.1 = [24 x i8] c"serial port opened: %s\0A\00"
@.str.2 = [18 x i8] c"reading bytes...\0A\00"
@.str.3 = [4 x i8] c"%x\0A\00"
@.str.4 = [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
%9 = alloca [64 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = bitcast [64 x i8]* %9 to i8*
%11 = load i32, i32* %4
%12 = icmp sge i32 %11, 2
br i1 %12, label %13, label %19
13: ; preds = %2
%14 = getelementptr inbounds [64 x i8], [64 x i8]* %9, i64 0, i64 0
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 1
%17 = load i8*, i8** %16
%18 = call i32 @strcpy(i8* %14, i8* %17)
br label %19
19: ; preds = %13, %2
%20 = getelementptr inbounds [64 x i8], [64 x i8]* %9, i64 0, i64 0
%21 = call i32 @s_open(i8* %20)
store i32 %21, i32* %6
%22 = load i32, i32* %6
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %27
24: ; preds = %19
%25 = getelementptr inbounds [64 x i8], [64 x i8]* %9, i64 0, i64 0
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i8* %25)
store i32 1, i32* %3
br label %45
27: ; preds = %19
%28 = getelementptr inbounds [64 x i8], [64 x i8]* %9, i64 0, i64 0
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8* %28)
br label %30
30: ; preds = %27
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %8
br label %32
32: ; preds = %40, %30
%33 = load i32, i32* %8
%34 = icmp slt i32 %33, 256
br i1 %34, label %35, label %43
35: ; preds = %32
%36 = load i32, i32* %6
%37 = call i32 @read(i32 %36, i32* %7, i32 1)
%38 = load i32, i32* %7
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 %38)
br label %40
40: ; preds = %35
%41 = load i32, i32* %8
%42 = add nsw i32 %41, 1
store i32 %42, i32* %8
br label %32
43: ; preds = %32
%44 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0))
store i32 0, i32* %3
br label %45
45: ; preds = %43, %24
%46 = load i32, i32* %3
ret i32 %46
}

LLVM IR (-O2):
@__const.main.port = [64 x i8] c"/dev/ttyUSB0\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00"
@.str = [32 x i8] c"could not open serial port: %s\0A\00"
@.str.1 = [24 x i8] c"serial port opened: %s\0A\00"
@.str.3 = [4 x i8] c"%x\0A\00"
@str = [17 x i8] c"reading bytes...\00"
@str.5 = [6 x i8] c"done!\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = alloca [64 x i8]
%5 = bitcast i32* %3 to i8*
%6 = getelementptr inbounds [64 x i8], [64 x i8]* %4, i64 0, i64 0
%7 = icmp sgt i32 %0, 1
br i1 %7, label %8, label %12
8: ; preds = %2
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
%11 = call i32 @strcpy(i8* %6, i8* %10)
br label %12
12: ; preds = %8, %2
%13 = call i32 @s_open(i8* %6)
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %12
%16 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i8* %6)
br label %29
17: ; preds = %12
%18 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8* %6)
%19 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str, i64 0, i64 0))
br label %20
20: ; preds = %17, %20
%21 = phi i32 [ 0, %17 ], [ %25, %20 ]
%22 = call i32 @read(i32 %13, i32* %3, i32 1)
%23 = load i32, i32* %3
%24 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 %23)
%25 = add nuw nsw i32 %21, 1
%26 = icmp eq i32 %25, 256
br i1 %26, label %27, label %20
27: ; preds = %20
%28 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @str.5, i64 0, i64 0))
br label %29
29: ; preds = %27, %15
%30 = phi i32 [ 1, %15 ], [ 0, %27 ]
ret i32 %30
}