#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*/ O_WRONLY;
int atoi(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ sprintf(char *, char *, int);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int strlen(char *);
int write(int, char *, int);
int smd_main(int argc, char **argv) {
  int fd, len, r, port = 0;
  char devname[32];
  argc--;
  argv++;
  if ((argc > 0) && (argv[0][0] == '-')) {
    port = atoi(argv[0] + 1);
    argc--;
    argv++;
  }
  sprintf(devname, "/dev/smd%d", port);
  fd = open(devname, O_WRONLY);
  if (fd < 0) {
    fprintf(stderr, "failed to open smd0 - %s\n", strerror(errno));
    return -1;
  }
  while (argc > 0) {
    len = strlen(argv[0]);
    r = write(fd, argv[0], len);
    if (r != len) {
      fprintf(stderr, "failed to write smd0 (%d) %s\n", r, strerror(errno));
      return -1;
    }
    argc--;
    argv++;
    write(fd, argc ? " " : "\r", 1);
  }
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<smd_main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x8(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x8(%rbp)
mov    -0x10(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x10(%rbp)
cmpl   $0x0,-0x8(%rbp)
jle    11f0 <smd_main+0x70>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
movsbl (%rax),%eax
cmp    $0x2d,%eax
jne    11f0 <smd_main+0x70>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
add    $0x1,%rdi
call   1080 <atoi@plt>
mov    %eax,-0x20(%rbp)
mov    -0x8(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x8(%rbp)
mov    -0x10(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x10(%rbp)
lea    -0x40(%rbp),%rdi
mov    -0x20(%rbp),%edx
lea    0xe02(%rip),%rsi
call   1090 <sprintf@plt>
lea    -0x40(%rbp),%rdi
mov    0x2dba(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    125c <smd_main+0xdc>
mov    0x2db7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
mov    0x2da3(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x44(%rbp),%edi
mov    %rax,%rdx
lea    0xdc2(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    132c <smd_main+0x1ac>
jmp    1261 <smd_main+0xe1>
cmpl   $0x0,-0x8(%rbp)
jle    131d <smd_main+0x19d>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
mov    -0x18(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x18(%rbp),%eax
je     12de <smd_main+0x15e>
mov    0x2d3e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4c(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x48(%rbp)
mov    0x2d24(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x4c(%rbp),%edi
mov    -0x48(%rbp),%edx
mov    %rax,%rcx
lea    0xd5a(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    132c <smd_main+0x1ac>
mov    -0x8(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x8(%rbp)
mov    -0x10(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x10(%rbp)
mov    -0x14(%rbp),%edi
mov    -0x8(%rbp),%ecx
lea    0xd45(%rip),%rsi
lea    0xd3c(%rip),%rax
cmp    $0x0,%ecx
cmovne %rax,%rsi
mov    $0x1,%edx
call   1030 <write@plt>
jmp    1261 <smd_main+0xe1>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<smd_main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %edi,%ebx
lea    -0x1(%rbx),%r15d
lea    0x8(%rsi),%r12
xor    %edx,%edx
cmp    $0x2,%edi
jl     11c3 <smd_main+0x43>
mov    (%r12),%rdi
cmpb   $0x2d,(%rdi)
jne    11c3 <smd_main+0x43>
mov    %rsi,%r14
add    $0x1,%rdi
call   1080 <atoi@plt>
mov    %eax,%edx
add    $0xfffffffe,%ebx
add    $0x10,%r14
mov    %r14,%r12
mov    %ebx,%r15d
lea    0xe36(%rip),%rsi
mov    %rsp,%rbx
mov    %rbx,%rdi
call   1090 <sprintf@plt>
mov    0x2dec(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1070 <open@plt>
test   %eax,%eax
js     1256 <smd_main+0xd6>
mov    %eax,%r14d
test   %r15d,%r15d
jle    124a <smd_main+0xca>
add    $0x1,%r15d
lea    0xe46(%rip),%r13
nopl   (%rax)
mov    (%r12),%rdi
call   1040 <strlen@plt>
mov    %eax,%ebx
mov    (%r12),%rsi
mov    %r14d,%edi
mov    %eax,%edx
call   1030 <write@plt>
cmp    %ebx,%eax
jne    1282 <smd_main+0x102>
add    $0x8,%r12
cmp    $0x2,%r15d
mov    %r13,%rsi
lea    0xe16(%rip),%rax
cmove  %rax,%rsi
mov    %r14d,%edi
mov    $0x1,%edx
call   1030 <write@plt>
add    $0xffffffff,%r15d
cmp    $0x1,%r15d
jg     1200 <smd_main+0x80>
mov    %r14d,%edi
call   1050 <close@plt>
xor    %eax,%eax
jmp    12b5 <smd_main+0x135>
mov    0x2d83(%rip),%rax
mov    (%rax),%ebx
mov    0x2d72(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xd97(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1060 <fprintf@plt>
jmp    12b0 <smd_main+0x130>
mov    %eax,%ebp
mov    0x2d55(%rip),%rax
mov    (%rax),%ebx
mov    0x2d44(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xd83(%rip),%rsi
mov    %ebx,%edi
mov    %ebp,%edx
mov    %rax,%rcx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0xffffffff,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [11 x i8] c"/dev/smd%d\00"
@O_WRONLY = global i32 0
@stderr = global i32 0
@.str.1 = [26 x i8] c"failed to open smd0 - %s\0A\00"
@errno = global i32 0
@.str.2 = [30 x i8] c"failed to write smd0 (%d) %s\0A\00"
@.str.3 = [2 x i8] c" \00"
@.str.4 = [2 x i8] c"\0D\00"
define i32 @smd_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 i32
%10 = alloca [32 x i8]
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %9
%11 = load i32, i32* %4
%12 = add nsw i32 %11, -1
store i32 %12, i32* %4
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i32 1
store i8** %14, i8*** %5
%15 = load i32, i32* %4
%16 = icmp sgt i32 %15, 0
br i1 %16, label %17, label %35
17: ; preds = %2
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 0
%20 = load i8*, i8** %19
%21 = getelementptr inbounds i8, i8* %20, i64 0
%22 = load i8, i8* %21
%23 = sext i8 %22 to i32
%24 = icmp eq i32 %23, 45
br i1 %24, label %25, label %35
25: ; preds = %17
%26 = load i8**, i8*** %5
%27 = getelementptr inbounds i8*, i8** %26, i64 0
%28 = load i8*, i8** %27
%29 = getelementptr inbounds i8, i8* %28, i64 1
%30 = call i32 @atoi(i8* %29)
store i32 %30, i32* %9
%31 = load i32, i32* %4
%32 = add nsw i32 %31, -1
store i32 %32, i32* %4
%33 = load i8**, i8*** %5
%34 = getelementptr inbounds i8*, i8** %33, i32 1
store i8** %34, i8*** %5
br label %35
35: ; preds = %25, %17, %2
%36 = getelementptr inbounds [32 x i8], [32 x i8]* %10, i64 0, i64 0
%37 = load i32, i32* %9
%38 = call i32 @sprintf(i8* %36, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %37)
%39 = getelementptr inbounds [32 x i8], [32 x i8]* %10, i64 0, i64 0
%40 = load i32, i32* @O_WRONLY
%41 = call i32 @open(i8* %39, i32 %40)
store i32 %41, i32* %6
%42 = load i32, i32* %6
%43 = icmp slt i32 %42, 0
br i1 %43, label %44, label %49
44: ; preds = %35
%45 = load i32, i32* @stderr
%46 = load i32, i32* @errno
%47 = call i8* @strerror(i32 %46)
%48 = call i32 (i32, i8*, ...) @fprintf(i32 %45, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i8* %47)
store i32 -1, i32* %3
br label %87
49: ; preds = %35
br label %50
50: ; preds = %73, %49
%51 = load i32, i32* %4
%52 = icmp sgt i32 %51, 0
br i1 %52, label %53, label %84
53: ; preds = %50
%54 = load i8**, i8*** %5
%55 = getelementptr inbounds i8*, i8** %54, i64 0
%56 = load i8*, i8** %55
%57 = call i32 @strlen(i8* %56)
store i32 %57, i32* %7
%58 = load i32, i32* %6
%59 = load i8**, i8*** %5
%60 = getelementptr inbounds i8*, i8** %59, i64 0
%61 = load i8*, i8** %60
%62 = load i32, i32* %7
%63 = call i32 @write(i32 %58, i8* %61, i32 %62)
store i32 %63, i32* %8
%64 = load i32, i32* %8
%65 = load i32, i32* %7
%66 = icmp ne i32 %64, %65
br i1 %66, label %67, label %73
67: ; preds = %53
%68 = load i32, i32* @stderr
%69 = load i32, i32* %8
%70 = load i32, i32* @errno
%71 = call i8* @strerror(i32 %70)
%72 = call i32 (i32, i8*, ...) @fprintf(i32 %68, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), i32 %69, i8* %71)
store i32 -1, i32* %3
br label %87
73: ; preds = %53
%74 = load i32, i32* %4
%75 = add nsw i32 %74, -1
store i32 %75, i32* %4
%76 = load i8**, i8*** %5
%77 = getelementptr inbounds i8*, i8** %76, i32 1
store i8** %77, i8*** %5
%78 = load i32, i32* %6
%79 = load i32, i32* %4
%80 = icmp ne i32 %79, 0
%81 = zext i1 %80 to i64
%82 = select i1 %80, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0)
%83 = call i32 @write(i32 %78, i8* %82, i32 1)
br label %50
84: ; preds = %50
%85 = load i32, i32* %6
%86 = call i32 @close(i32 %85)
store i32 0, i32* %3
br label %87
87: ; preds = %84, %67, %44
%88 = load i32, i32* %3
ret i32 %88
}

LLVM IR (-O2):
@.str = [11 x i8] c"/dev/smd%d\00"
@O_WRONLY = global i32 0
@stderr = global i32 0
@.str.1 = [26 x i8] c"failed to open smd0 - %s\0A\00"
@errno = global i32 0
@.str.2 = [30 x i8] c"failed to write smd0 (%d) %s\0A\00"
@.str.3 = [2 x i8] c" \00"
@.str.4 = [2 x i8] c"\0D\00"
define i32 @smd_main(i32 %0, i8** nocapture %1) {
%3 = alloca [32 x i8]
%4 = getelementptr inbounds [32 x i8], [32 x i8]* %3, i64 0, i64 0
%5 = add nsw i32 %0, -1
%6 = getelementptr inbounds i8*, i8** %1, i64 1
%7 = icmp sgt i32 %0, 1
br i1 %7, label %8, label %17
8: ; preds = %2
%9 = load i8*, i8** %6
%10 = load i8, i8* %9
%11 = icmp eq i8 %10, 45
br i1 %11, label %12, label %17
12: ; preds = %8
%13 = getelementptr inbounds i8, i8* %9, i64 1
%14 = tail call i32 @atoi(i8* %13)
%15 = add nsw i32 %0, -2
%16 = getelementptr inbounds i8*, i8** %1, i64 2
br label %17
17: ; preds = %12, %8, %2
%18 = phi i8** [ %16, %12 ], [ %6, %8 ], [ %6, %2 ]
%19 = phi i32 [ %15, %12 ], [ %5, %8 ], [ %5, %2 ]
%20 = phi i32 [ %14, %12 ], [ 0, %8 ], [ 0, %2 ]
%21 = call i32 @sprintf(i8* %4, i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %20)
%22 = load i32, i32* @O_WRONLY
%23 = call i32 @open(i8* %4, i32 %22)
%24 = icmp slt i32 %23, 0
br i1 %24, label %27, label %25
25: ; preds = %17
%26 = icmp sgt i32 %19, 0
br i1 %26, label %32, label %52
27: ; preds = %17
%28 = load i32, i32* @stderr
%29 = load i32, i32* @errno
%30 = tail call i8* @strerror(i32 %29)
%31 = tail call i32 (i32, i8*, ...) @fprintf(i32 %28, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i8* %30)
br label %54
32: ; preds = %25, %45
%33 = phi i32 [ %46, %45 ], [ %19, %25 ]
%34 = phi i8** [ %47, %45 ], [ %18, %25 ]
%35 = load i8*, i8** %34
%36 = tail call i32 @strlen(i8* %35)
%37 = load i8*, i8** %34
%38 = tail call i32 @write(i32 %23, i8* %37, i32 %36)
%39 = icmp eq i32 %38, %36
br i1 %39, label %45, label %40
40: ; preds = %32
%41 = load i32, i32* @stderr
%42 = load i32, i32* @errno
%43 = tail call i8* @strerror(i32 %42)
%44 = tail call i32 (i32, i8*, ...) @fprintf(i32 %41, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), i32 %38, i8* %43)
br label %54
45: ; preds = %32
%46 = add nsw i32 %33, -1
%47 = getelementptr inbounds i8*, i8** %34, i64 1
%48 = icmp eq i32 %46, 0
%49 = select i1 %48, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0)
%50 = tail call i32 @write(i32 %23, i8* %49, i32 1)
%51 = icmp sgt i32 %33, 1
br i1 %51, label %32, label %52
52: ; preds = %45, %25
%53 = tail call i32 @close(i32 %23)
br label %54
54: ; preds = %52, %40, %27
%55 = phi i32 [ -1, %27 ], [ -1, %40 ], [ 0, %52 ]
ret i32 %55
}