#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 EXIT_FAILURE;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ O_WRONLY;
int atoi(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int read_pattern(int);
int /*<<< orphan*/ stderr;
int write_pattern(int, char *, int);
int main(int argc, char **argv) {
  int fd, r;
  if (argc < 2) {
    fprintf(stderr, "usage: %s <device> [pattern [writesz]]\n", argv[0]);
    return EXIT_FAILURE;
  }
  fd = open(argv[1], (argc > 2) ? O_WRONLY : O_RDONLY);
  if (fd < 0) {
    perror("open");
    return EXIT_FAILURE;
  }
  if (argc > 2)
    r = write_pattern(fd, argv[2], argv[3] ? atoi(argv[3]) : 4096);
  else
    r = read_pattern(fd);
  close(fd);
  return r;
}


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   $0x2,-0x8(%rbp)
jge    11bd <main+0x4d>
mov    0x2e41(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe59(%rip),%rsi
call   1060 <fprintf@plt>
mov    0x2e1d(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    12a3 <main+0x133>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x20(%rbp)
cmpl   $0x2,-0x8(%rbp)
jle    11e4 <main+0x74>
mov    0x2de6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
jmp    11f0 <main+0x80>
mov    0x2dfd(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x24(%rbp),%esi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1226 <main+0xb6>
lea    0xe18(%rip),%rdi
call   1080 <perror@plt>
mov    0x2db4(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    12a3 <main+0x133>
cmpl   $0x2,-0x8(%rbp)
jle    128a <main+0x11a>
mov    -0x14(%rbp),%eax
mov    %eax,-0x34(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rax
cmpq   $0x0,0x18(%rax)
je     1266 <main+0xf6>
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
call   1090 <atoi@plt>
mov    %eax,-0x38(%rbp)
jmp    1273 <main+0x103>
mov    $0x1000,%eax
mov    %eax,-0x38(%rbp)
jmp    1273 <main+0x103>
mov    -0x30(%rbp),%rsi
mov    -0x34(%rbp),%edi
mov    -0x38(%rbp),%edx
call   1050 <write_pattern@plt>
mov    %eax,-0x18(%rbp)
jmp    1295 <main+0x125>
mov    -0x14(%rbp),%edi
call   1030 <read_pattern@plt>
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%rbx
cmp    $0x1,%edi
jg     119f <main+0x2f>
mov    0x2e55(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%rdx
lea    0xe71(%rip),%rsi
call   1060 <fprintf@plt>
mov    0x2e35(%rip),%rax
mov    (%rax),%ebx
jmp    1207 <main+0x97>
mov    %edi,%ebp
mov    0x8(%rbx),%rdi
cmp    $0x2,%ebp
jne    11b3 <main+0x43>
mov    0x2e37(%rip),%rax
jmp    11ba <main+0x4a>
mov    0x2e06(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
test   %eax,%eax
js     120e <main+0x9e>
mov    %eax,%r14d
cmp    $0x2,%ebp
jne    11d7 <main+0x67>
mov    %r14d,%edi
call   1030 <read_pattern@plt>
jmp    11fd <main+0x8d>
mov    0x10(%rbx),%rbp
mov    0x18(%rbx),%rdi
test   %rdi,%rdi
je     11ed <main+0x7d>
call   1090 <atoi@plt>
mov    %eax,%edx
jmp    11f2 <main+0x82>
mov    $0x1000,%edx
mov    %r14d,%edi
mov    %rbp,%rsi
call   1050 <write_pattern@plt>
mov    %eax,%ebx
mov    %r14d,%edi
call   1040 <close@plt>
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe13(%rip),%rdi
call   1080 <perror@plt>
jmp    1194 <main+0x24>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [40 x i8] c"usage: %s <device> [pattern [writesz]]\0A\00"
@EXIT_FAILURE = global i32 0
@O_WRONLY = global i32 0
@O_RDONLY = global i32 0
@.str.1 = [5 x i8] c"open\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = load i32, i32* %4
%9 = icmp slt i32 %8, 2
br i1 %9, label %10, label %17
10: ; preds = %2
%11 = load i32, i32* @stderr
%12 = load i8**, i8*** %5
%13 = getelementptr inbounds i8*, i8** %12, i64 0
%14 = load i8*, i8** %13
%15 = call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0), i8* %14)
%16 = load i32, i32* @EXIT_FAILURE
store i32 %16, i32* %3
br label %63
17: ; preds = %2
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 1
%20 = load i8*, i8** %19
%21 = load i32, i32* %4
%22 = icmp sgt i32 %21, 2
br i1 %22, label %23, label %25
23: ; preds = %17
%24 = load i32, i32* @O_WRONLY
br label %27
25: ; preds = %17
%26 = load i32, i32* @O_RDONLY
br label %27
27: ; preds = %25, %23
%28 = phi i32 [ %24, %23 ], [ %26, %25 ]
%29 = call i32 @open(i8* %20, i32 %28)
store i32 %29, i32* %6
%30 = load i32, i32* %6
%31 = icmp slt i32 %30, 0
br i1 %31, label %32, label %35
32: ; preds = %27
%33 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%34 = load i32, i32* @EXIT_FAILURE
store i32 %34, i32* %3
br label %63
35: ; preds = %27
%36 = load i32, i32* %4
%37 = icmp sgt i32 %36, 2
br i1 %37, label %38, label %56
38: ; preds = %35
%39 = load i32, i32* %6
%40 = load i8**, i8*** %5
%41 = getelementptr inbounds i8*, i8** %40, i64 2
%42 = load i8*, i8** %41
%43 = load i8**, i8*** %5
%44 = getelementptr inbounds i8*, i8** %43, i64 3
%45 = load i8*, i8** %44
%46 = icmp ne i8* %45, null
br i1 %46, label %47, label %52
47: ; preds = %38
%48 = load i8**, i8*** %5
%49 = getelementptr inbounds i8*, i8** %48, i64 3
%50 = load i8*, i8** %49
%51 = call i32 @atoi(i8* %50)
br label %53
52: ; preds = %38
br label %53
53: ; preds = %52, %47
%54 = phi i32 [ %51, %47 ], [ 4096, %52 ]
%55 = call i32 @write_pattern(i32 %39, i8* %42, i32 %54)
store i32 %55, i32* %7
br label %59
56: ; preds = %35
%57 = load i32, i32* %6
%58 = call i32 @read_pattern(i32 %57)
store i32 %58, i32* %7
br label %59
59: ; preds = %56, %53
%60 = load i32, i32* %6
%61 = call i32 @close(i32 %60)
%62 = load i32, i32* %7
store i32 %62, i32* %3
br label %63
63: ; preds = %59, %32, %10
%64 = load i32, i32* %3
ret i32 %64
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [40 x i8] c"usage: %s <device> [pattern [writesz]]\0A\00"
@EXIT_FAILURE = global i32 0
@O_WRONLY = global i32 0
@O_RDONLY = global i32 0
@.str.1 = [5 x i8] c"open\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = icmp slt i32 %0, 2
br i1 %3, label %4, label %9
4: ; preds = %2
%5 = load i32, i32* @stderr
%6 = load i8*, i8** %1
%7 = tail call i32 @fprintf(i32 %5, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0), i8* %6)
%8 = load i32, i32* @EXIT_FAILURE
br label %38
9: ; preds = %2
%10 = getelementptr inbounds i8*, i8** %1, i64 1
%11 = load i8*, i8** %10
%12 = icmp eq i32 %0, 2
%13 = load i32, i32* @O_WRONLY
%14 = load i32, i32* @O_RDONLY
%15 = select i1 %12, i32 %14, i32 %13
%16 = tail call i32 @open(i8* %11, i32 %15)
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %9
%19 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%20 = load i32, i32* @EXIT_FAILURE
br label %38
21: ; preds = %9
br i1 %12, label %33, label %22
22: ; preds = %21
%23 = getelementptr inbounds i8*, i8** %1, i64 2
%24 = load i8*, i8** %23
%25 = getelementptr inbounds i8*, i8** %1, i64 3
%26 = load i8*, i8** %25
%27 = icmp eq i8* %26, null
br i1 %27, label %30, label %28
28: ; preds = %22
%29 = tail call i32 @atoi(i8* %26)
br label %30
30: ; preds = %22, %28
%31 = phi i32 [ %29, %28 ], [ 4096, %22 ]
%32 = tail call i32 @write_pattern(i32 %16, i8* %24, i32 %31)
br label %35
33: ; preds = %21
%34 = tail call i32 @read_pattern(i32 %16)
br label %35
35: ; preds = %33, %30
%36 = phi i32 [ %32, %30 ], [ %34, %33 ]
%37 = tail call i32 @close(i32 %16)
br label %38
38: ; preds = %35, %18, %4
%39 = phi i32 [ %8, %4 ], [ %20, %18 ], [ %36, %35 ]
ret i32 %39
}