#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 /*<<< orphan*/ SEEK_SET;
unsigned char atoi(char *);
int /*<<< orphan*/ close(int);
int lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int write(int, unsigned char *, int);
int main(int argc, char **argv) {
  unsigned char swidth_mactive;
  unsigned char batstack_id;
  int fd;
  unsigned char buf[1024];
  int i;
  if (argc < 2 || argc > 3) {
    printf("Usage: %s filename [BatStack ID]\n", argv[0]);
    return 1;
  }
  if (argc == 3) {
    batstack_id = atoi(argv[2]);
  } else {
    batstack_id = 0x00;
  }
  if ((fd = open(argv[1], O_WRONLY)) == -1) {
    perror("open");
    return -1;
  }
  if (lseek(fd, 0, SEEK_SET) == -1) {
    perror("lseek");
    return -1;
  }
  for (i = 0; i < 1024; i++)
    *(buf + i) = 0x00;
  *buf = batstack_id;
  swidth_mactive = 0xAF;
  *(buf + 3) = swidth_mactive;
  i = write(fd, buf, 1024);
  if (i == -1) {
    perror("write");
    return -1;
  }
  printf("%d bytes written.\n", i);
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jl     119d <main+0x2d>
cmpl   $0x3,-0x8(%rbp)
jle    11be <main+0x4e>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe55(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1304 <main+0x194>
cmpl   $0x3,-0x8(%rbp)
jne    11dd <main+0x6d>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1090 <atoi@plt>
mov    %al,-0x12(%rbp)
jmp    11e1 <main+0x71>
movb   $0x0,-0x12(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2de0(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,-0x18(%rbp)
cmp    $0xffffffff,%eax
jne    121b <main+0xab>
lea    0xe18(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1304 <main+0x194>
mov    -0x18(%rbp),%edi
mov    0x2dbb(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1050 <lseek@plt>
cmp    $0xffffffff,%eax
jne    124f <main+0xdf>
lea    0xde9(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1304 <main+0x194>
movl   $0x0,-0x424(%rbp)
cmpl   $0x400,-0x424(%rbp)
jge    128c <main+0x11c>
movslq -0x424(%rbp),%rax
movb   $0x0,-0x420(%rbp,%rax,1)
mov    -0x424(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x424(%rbp)
jmp    1259 <main+0xe9>
mov    -0x12(%rbp),%al
mov    %al,-0x420(%rbp)
movb   $0xaf,-0x11(%rbp)
mov    -0x11(%rbp),%al
mov    %al,-0x41d(%rbp)
mov    -0x18(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    $0x400,%edx
call   1030 <write@plt>
mov    %eax,-0x424(%rbp)
cmpl   $0xffffffff,-0x424(%rbp)
jne    12e1 <main+0x171>
lea    0xd5d(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1304 <main+0x194>
mov    -0x424(%rbp),%esi
lea    0xd45(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x408,%rsp
mov    %rsi,%rbx
lea    -0x4(%rdi),%eax
cmp    $0xfffffffd,%eax
ja     11b3 <main+0x33>
mov    (%rbx),%rsi
lea    0xe5e(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x1,%ebp
jmp    1241 <main+0xc1>
cmp    $0x3,%edi
jne    11c6 <main+0x46>
mov    0x10(%rbx),%rdi
call   10a0 <atoi@plt>
mov    %eax,%r14d
jmp    11c9 <main+0x49>
xor    %r14d,%r14d
mov    0x8(%rbx),%rdi
mov    0x2dfc(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     1251 <main+0xd1>
mov    %eax,%ebx
mov    0x2df7(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
xor    %esi,%esi
call   1050 <lseek@plt>
cmp    $0xffffffff,%eax
je     125a <main+0xda>
mov    %rsp,%r15
xor    %ebp,%ebp
mov    $0x400,%edx
mov    %r15,%rdi
xor    %esi,%esi
call   1060 <memset@plt>
mov    %r14b,(%rsp)
movb   $0xaf,0x3(%rsp)
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0x400,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
je     1263 <main+0xe3>
lea    0xe02(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    %ebx,%edi
call   1070 <close@plt>
mov    %ebp,%eax
add    $0x408,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdca(%rip),%rdi
jmp    126a <main+0xea>
lea    0xdc6(%rip),%rdi
jmp    126a <main+0xea>
lea    0xdc3(%rip),%rdi
call   1090 <perror@plt>
mov    $0xffffffff,%ebp
jmp    1241 <main+0xc1>

LLVM IR (-O0):
@.str = [34 x i8] c"Usage: %s filename [BatStack ID]\0A\00"
@O_WRONLY = global i32 0
@.str.1 = [5 x i8] c"open\00"
@SEEK_SET = global i32 0
@.str.2 = [6 x i8] c"lseek\00"
@.str.3 = [6 x i8] c"write\00"
@.str.4 = [19 x i8] c"%d bytes written.\0A\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 i32
%9 = alloca [1024 x i8]
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* %4
%12 = icmp slt i32 %11, 2
br i1 %12, label %16, label %13
13: ; preds = %2
%14 = load i32, i32* %4
%15 = icmp sgt i32 %14, 3
br i1 %15, label %16, label %21
16: ; preds = %13, %2
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 0
%19 = load i8*, i8** %18
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0), i8* %19)
store i32 1, i32* %3
br label %76
21: ; preds = %13
%22 = load i32, i32* %4
%23 = icmp eq i32 %22, 3
br i1 %23, label %24, label %29
24: ; preds = %21
%25 = load i8**, i8*** %5
%26 = getelementptr inbounds i8*, i8** %25, i64 2
%27 = load i8*, i8** %26
%28 = call zeroext i8 @atoi(i8* %27)
store i8 %28, i8* %7
br label %30
29: ; preds = %21
store i8 0, i8* %7
br label %30
30: ; preds = %29, %24
%31 = load i8**, i8*** %5
%32 = getelementptr inbounds i8*, i8** %31, i64 1
%33 = load i8*, i8** %32
%34 = load i32, i32* @O_WRONLY
%35 = call i32 @open(i8* %33, i32 %34)
store i32 %35, i32* %8
%36 = icmp eq i32 %35, -1
br i1 %36, label %37, label %39
37: ; preds = %30
%38 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %76
39: ; preds = %30
%40 = load i32, i32* %8
%41 = load i32, i32* @SEEK_SET
%42 = call i32 @lseek(i32 %40, i32 0, i32 %41)
%43 = icmp eq i32 %42, -1
br i1 %43, label %44, label %46
44: ; preds = %39
%45 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %3
br label %76
46: ; preds = %39
store i32 0, i32* %10
br label %47
47: ; preds = %55, %46
%48 = load i32, i32* %10
%49 = icmp slt i32 %48, 1024
br i1 %49, label %50, label %58
50: ; preds = %47
%51 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%52 = load i32, i32* %10
%53 = sext i32 %52 to i64
%54 = getelementptr inbounds i8, i8* %51, i64 %53
store i8 0, i8* %54
br label %55
55: ; preds = %50
%56 = load i32, i32* %10
%57 = add nsw i32 %56, 1
store i32 %57, i32* %10
br label %47
58: ; preds = %47
%59 = load i8, i8* %7
%60 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
store i8 %59, i8* %60
store i8 -81, i8* %6
%61 = load i8, i8* %6
%62 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%63 = getelementptr inbounds i8, i8* %62, i64 3
store i8 %61, i8* %63
%64 = load i32, i32* %8
%65 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%66 = call i32 @write(i32 %64, i8* %65, i32 1024)
store i32 %66, i32* %10
%67 = load i32, i32* %10
%68 = icmp eq i32 %67, -1
br i1 %68, label %69, label %71
69: ; preds = %58
%70 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %3
br label %76
71: ; preds = %58
%72 = load i32, i32* %10
%73 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), i32 %72)
%74 = load i32, i32* %8
%75 = call i32 @close(i32 %74)
store i32 0, i32* %3
br label %76
76: ; preds = %71, %69, %44, %37, %16
%77 = load i32, i32* %3
ret i32 %77
}

LLVM IR (-O2):
@.str = [34 x i8] c"Usage: %s filename [BatStack ID]\0A\00"
@O_WRONLY = global i32 0
@.str.1 = [5 x i8] c"open\00"
@SEEK_SET = global i32 0
@.str.2 = [6 x i8] c"lseek\00"
@.str.3 = [6 x i8] c"write\00"
@.str.4 = [19 x i8] c"%d bytes written.\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [1024 x i8]
%4 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%5 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%6 = add i32 %0, -4
%7 = icmp ult i32 %6, -2
br i1 %7, label %8, label %11
8: ; preds = %2
%9 = load i8*, i8** %1
%10 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0), i8* %9)
br label %41
11: ; preds = %2
%12 = icmp eq i32 %0, 3
br i1 %12, label %13, label %17
13: ; preds = %11
%14 = getelementptr inbounds i8*, i8** %1, i64 2
%15 = load i8*, i8** %14
%16 = tail call zeroext i8 @atoi(i8* %15)
br label %17
17: ; preds = %11, %13
%18 = phi i8 [ %16, %13 ], [ 0, %11 ]
%19 = getelementptr inbounds i8*, i8** %1, i64 1
%20 = load i8*, i8** %19
%21 = load i32, i32* @O_WRONLY
%22 = tail call i32 @open(i8* %20, i32 %21)
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %26
24: ; preds = %17
%25 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %41
26: ; preds = %17
%27 = load i32, i32* @SEEK_SET
%28 = tail call i32 @lseek(i32 %22, i32 0, i32 %27)
%29 = icmp eq i32 %28, -1
br i1 %29, label %34, label %30
30: ; preds = %26
store i8 %18, i8* %5
%31 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 3
store i8 -81, i8* %31
%32 = call i32 @write(i32 %22, i8* %5, i32 1024)
%33 = icmp eq i32 %32, -1
br i1 %33, label %36, label %38
34: ; preds = %26
%35 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
br label %41
36: ; preds = %30
%37 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
br label %41
38: ; preds = %30
%39 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.4, i64 0, i64 0), i32 %32)
%40 = tail call i32 @close(i32 %22)
br label %41
41: ; preds = %38, %36, %34, %24, %8
%42 = phi i32 [ 1, %8 ], [ -1, %24 ], [ -1, %34 ], [ -1, %36 ], [ 0, %38 ]
ret i32 %42
}