#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*/ atoi(char *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, ...);
int /*<<< orphan*/ free(void *);
int open_sf_source(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ putchar(char);
unsigned char *read_sf_packet(int, int *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
int main(int argc, char **argv) {
  int fd;
  if (argc != 3) {
    fprintf(stderr,
            "Usage: %s <host> <port> - dump packets from a serial forwarder\n",
            argv[0]);
    exit(2);
  }
  fd = open_sf_source(argv[1], atoi(argv[2]));
  if (fd < 0) {
    fprintf(stderr, "Couldn't open serial forwarder at %s:%s\n", argv[1],
            argv[2]);
    exit(1);
  }
  for (;;) {
    int len, i;
    const unsigned char *packet = read_sf_packet(fd, &len);
    printf("rec'd: ");
    if (!packet)
      exit(0);
    for (i = 0; i < len; i++)
      printf("%02x ", packet[i]);
    putchar('\n');
    fflush(stdout);
    free((void *)packet);
  }
}


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)
cmpl   $0x3,-0x8(%rbp)
je     11d8 <main+0x48>
mov    0x2e29(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe39(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    $0x2,%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1080 <atoi@plt>
mov    -0x30(%rbp),%rdi
mov    %eax,%esi
call   10a0 <open_sf_source@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    123a <main+0xaa>
mov    0x2dd0(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rcx
lea    0xe17(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
jmp    123f <main+0xaf>
mov    -0x14(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   10b0 <read_sf_packet@plt>
mov    %rax,-0x28(%rbp)
lea    0xe13(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
cmpq   $0x0,-0x28(%rbp)
jne    126f <main+0xdf>
xor    %edi,%edi
call   1090 <exit@plt>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    12aa <main+0x11a>
mov    -0x28(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movzbl (%rax,%rcx,1),%esi
lea    0xddc(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1276 <main+0xe6>
mov    $0xa,%edi
call   1040 <putchar@plt>
mov    0x2d1d(%rip),%rax
mov    (%rax),%edi
call   1070 <fflush@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
jmp    123f <main+0xaf>

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
jne    11e7 <main+0x57>
mov    0x8(%rbx),%rbp
mov    0x10(%rbx),%rdi
call   1080 <atoi@plt>
mov    %rbp,%rdi
mov    %eax,%esi
call   10a0 <open_sf_source@plt>
test   %eax,%eax
jns    120b <main+0x7b>
mov    0x2e1b(%rip),%rax
mov    (%rax),%edi
mov    0x8(%rbx),%rdx
mov    0x10(%rbx),%rcx
lea    0xe6a(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    0x2df2(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%rdx
lea    0xe06(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0x2,%edi
call   1090 <exit@plt>
mov    %eax,%r14d
lea    0x4(%rsp),%rsi
mov    %eax,%edi
call   10b0 <read_sf_packet@plt>
mov    %rax,%rbx
lea    0xe45(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
test   %rbx,%rbx
jne    1237 <main+0xa7>
xor    %edi,%edi
call   1090 <exit@plt>
lea    0xe33(%rip),%rbp
lea    0x4(%rsp),%r15
lea    0xe1f(%rip),%r12
jmp    128d <main+0xfd>
nopl   0x0(%rax)
mov    $0xa,%edi
call   1040 <putchar@plt>
mov    0x2d77(%rip),%rax
mov    (%rax),%edi
call   1070 <fflush@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r14d,%edi
mov    %r15,%rsi
call   10b0 <read_sf_packet@plt>
mov    %rax,%rbx
mov    %r12,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
test   %rbx,%rbx
je     1230 <main+0xa0>
cmpl   $0x0,0x4(%rsp)
jle    1250 <main+0xc0>
xor    %r13d,%r13d
nopw   0x0(%rax,%rax,1)
movzbl (%rbx,%r13,1),%esi
mov    %rbp,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
add    $0x1,%r13
movslq 0x4(%rsp),%rax
cmp    %rax,%r13
jl     12a0 <main+0x110>
jmp    1250 <main+0xc0>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [64 x i8] c"Usage: %s <host> <port> - dump packets from a serial forwarder\0A\00"
@.str.1 = [41 x i8] c"Couldn't open serial forwarder at %s:%s\0A\00"
@.str.2 = [8 x i8] c"rec'd: \00"
@.str.3 = [6 x i8] c"%02x \00"
@stdout = global i32 0
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 i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* %4
%11 = icmp ne i32 %10, 3
br i1 %11, label %12, label %19
12: ; preds = %2
%13 = load i32, i32* @stderr
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 0
%16 = load i8*, i8** %15
%17 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %13, i8* getelementptr inbounds ([64 x i8], [64 x i8]* @.str, i64 0, i64 0), i8* %16)
%18 = call i32 @exit(i32 2)
unreachable
19: ; preds = %2
%20 = load i8**, i8*** %5
%21 = getelementptr inbounds i8*, i8** %20, i64 1
%22 = load i8*, i8** %21
%23 = load i8**, i8*** %5
%24 = getelementptr inbounds i8*, i8** %23, i64 2
%25 = load i8*, i8** %24
%26 = call i32 @atoi(i8* %25)
%27 = call i32 @open_sf_source(i8* %22, i32 %26)
store i32 %27, i32* %6
%28 = load i32, i32* %6
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %40
30: ; preds = %19
%31 = load i32, i32* @stderr
%32 = load i8**, i8*** %5
%33 = getelementptr inbounds i8*, i8** %32, i64 1
%34 = load i8*, i8** %33
%35 = load i8**, i8*** %5
%36 = getelementptr inbounds i8*, i8** %35, i64 2
%37 = load i8*, i8** %36
%38 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %31, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0), i8* %34, i8* %37)
%39 = call i32 @exit(i32 1)
unreachable
40: ; preds = %19
br label %41
41: ; preds = %65, %40
%42 = load i32, i32* %6
%43 = call i8* @read_sf_packet(i32 %42, i32* %7)
store i8* %43, i8** %9
%44 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
%45 = load i8*, i8** %9
%46 = icmp ne i8* %45, null
br i1 %46, label %49, label %47
47: ; preds = %41
%48 = call i32 @exit(i32 0)
unreachable
49: ; preds = %41
store i32 0, i32* %8
br label %50
50: ; preds = %62, %49
%51 = load i32, i32* %8
%52 = load i32, i32* %7
%53 = icmp slt i32 %51, %52
br i1 %53, label %54, label %65
54: ; preds = %50
%55 = load i8*, i8** %9
%56 = load i32, i32* %8
%57 = sext i32 %56 to i64
%58 = getelementptr inbounds i8, i8* %55, i64 %57
%59 = load i8, i8* %58
%60 = zext i8 %59 to i32
%61 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i32 %60)
br label %62
62: ; preds = %54
%63 = load i32, i32* %8
%64 = add nsw i32 %63, 1
store i32 %64, i32* %8
br label %50
65: ; preds = %50
%66 = call i32 @putchar(i8 signext 10)
%67 = load i32, i32* @stdout
%68 = call i32 @fflush(i32 %67)
%69 = load i8*, i8** %9
%70 = call i32 @free(i8* %69)
br label %41
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [64 x i8] c"Usage: %s <host> <port> - dump packets from a serial forwarder\0A\00"
@.str.1 = [41 x i8] c"Couldn't open serial forwarder at %s:%s\0A\00"
@.str.2 = [8 x i8] c"rec'd: \00"
@.str.3 = [6 x i8] c"%02x \00"
@stdout = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = icmp eq i32 %0, 3
br i1 %4, label %10, label %5
5: ; preds = %2
%6 = load i32, i32* @stderr
%7 = load i8*, i8** %1
%8 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %6, i8* getelementptr inbounds ([64 x i8], [64 x i8]* @.str, i64 0, i64 0), i8* %7)
%9 = tail call i32 @exit(i32 2)
unreachable
10: ; preds = %2
%11 = getelementptr inbounds i8*, i8** %1, i64 1
%12 = load i8*, i8** %11
%13 = getelementptr inbounds i8*, i8** %1, i64 2
%14 = load i8*, i8** %13
%15 = tail call i32 @atoi(i8* %14)
%16 = tail call i32 @open_sf_source(i8* %12, i32 %15)
%17 = icmp slt i32 %16, 0
br i1 %17, label %23, label %18
18: ; preds = %10
%19 = bitcast i32* %3 to i8*
%20 = call i8* @read_sf_packet(i32 %16, i32* %3)
%21 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
%22 = icmp eq i8* %20, null
br i1 %22, label %33, label %29
23: ; preds = %10
%24 = load i32, i32* @stderr
%25 = load i8*, i8** %11
%26 = load i8*, i8** %13
%27 = tail call i32 (i32, i8*, i8*, ...) @fprintf(i32 %24, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.1, i64 0, i64 0), i8* %25, i8* %26)
%28 = tail call i32 @exit(i32 1)
unreachable
29: ; preds = %18, %45
%30 = phi i8* [ %50, %45 ], [ %20, %18 ]
%31 = load i32, i32* %3
%32 = icmp sgt i32 %31, 0
br i1 %32, label %35, label %45
33: ; preds = %45, %18
%34 = call i32 @exit(i32 0)
unreachable
35: ; preds = %29, %35
%36 = phi i64 [ %41, %35 ], [ 0, %29 ]
%37 = getelementptr inbounds i8, i8* %30, i64 %36
%38 = load i8, i8* %37
%39 = zext i8 %38 to i32
%40 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i32 %39)
%41 = add nuw nsw i64 %36, 1
%42 = load i32, i32* %3
%43 = sext i32 %42 to i64
%44 = icmp slt i64 %41, %43
br i1 %44, label %35, label %45
45: ; preds = %35, %29
%46 = call i32 @putchar(i8 signext 10)
%47 = load i32, i32* @stdout
%48 = call i32 @fflush(i32 %47)
%49 = call i32 @free(i8* %30)
%50 = call i8* @read_sf_packet(i32 %16, i32* %3)
%51 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
%52 = icmp eq i8* %50, null
br i1 %52, label %33, label %29
}