#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 */
typedef int /*<<< orphan*/ uint8_t;
typedef scalar_t__ ssize_t;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int EXIT_FAILURE;
int EXIT_SUCCESS;
int /*<<< orphan*/ O_RDONLY;
int STDIN_FILENO;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
scalar_t__ read(int, int /*<<< orphan*/ *, int);
int /*<<< orphan*/ stderr;
scalar_t__ strcmp(char *, char *);
int /*<<< orphan*/ usage();
scalar_t__ write(int, int /*<<< orphan*/ *, size_t);
int add_random(int fd, int argc, char *argv[]) {
  int inp_fd;
  if (argc != 2) {
    fprintf(stderr, "Incorrect amount of arguments\n");
    usage();
    return EXIT_FAILURE;
  }
  if (strcmp(argv[1], "-") == 0) {
    inp_fd = STDIN_FILENO;
  } else {
    inp_fd = open(argv[1], O_RDONLY);
    if (inp_fd == -1) {
      perror("Failed to open input file");
      return EXIT_FAILURE;
    }
  }
  uint8_t buf[4096];
  ssize_t rbytes = 0;
  do {
    rbytes = read(inp_fd, buf, sizeof(buf));
    if (rbytes < 0) {
      perror("Unable to read input file");
      close(inp_fd);
      return EXIT_FAILURE;
    }
    if (rbytes != 0) {
      uint8_t *bp = buf;
      size_t bytes_left = rbytes;
      while (bytes_left > 0) {
        ssize_t wbytes = write(fd, buf, bytes_left);
        if (wbytes < 0) {
          perror("Unable to write random file");
          close(inp_fd);
          return EXIT_FAILURE;
        }
        bytes_left -= wbytes;
        bp += wbytes;
      }
    }
  } while (rbytes > 0);
  if (inp_fd != STDIN_FILENO) {
    close(inp_fd);
  }
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<add_random>:
push   %rbp
mov    %rsp,%rbp
sub    $0x4040,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
cmpl   $0x2,-0xc(%rbp)
je     11cc <add_random+0x4c>
mov    0x2e32(%rip),%rax
mov    (%rax),%edi
lea    0xe51(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x0,%al
call   1080 <usage@plt>
mov    0x2e0e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1395 <add_random+0x215>
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe44(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
jne    11fb <add_random+0x7b>
mov    0x2dcf(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
jmp    1240 <add_random+0xc0>
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2dde(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    123b <add_random+0xbb>
lea    0xdfc(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2d9f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1395 <add_random+0x215>
jmp    1240 <add_random+0xc0>
movq   $0x0,-0x4028(%rbp)
mov    -0x1c(%rbp),%edi
lea    -0x4020(%rbp),%rsi
mov    $0x4000,%edx
call   1050 <read@plt>
mov    %rax,-0x4028(%rbp)
cmpq   $0x0,-0x4028(%rbp)
jge    1299 <add_random+0x119>
lea    0xdc0(%rip),%rdi
call   10a0 <perror@plt>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
mov    0x2d41(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1395 <add_random+0x215>
cmpq   $0x0,-0x4028(%rbp)
je     135c <add_random+0x1dc>
lea    -0x4020(%rbp),%rax
mov    %rax,-0x4030(%rbp)
mov    -0x4028(%rbp),%rax
mov    %rax,-0x4038(%rbp)
cmpq   $0x0,-0x4038(%rbp)
jbe    1357 <add_random+0x1d7>
mov    -0x8(%rbp),%edi
lea    -0x4020(%rbp),%rsi
mov    -0x4038(%rbp),%rdx
call   1030 <write@plt>
mov    %rax,-0x4040(%rbp)
cmpq   $0x0,-0x4040(%rbp)
jge    1321 <add_random+0x1a1>
lea    0xd52(%rip),%rdi
call   10a0 <perror@plt>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
mov    0x2cb9(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1395 <add_random+0x215>
mov    -0x4040(%rbp),%rcx
mov    -0x4038(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x4038(%rbp)
mov    -0x4040(%rbp),%rax
shl    $0x2,%rax
add    -0x4030(%rbp),%rax
mov    %rax,-0x4030(%rbp)
jmp    12c3 <add_random+0x143>
jmp    135c <add_random+0x1dc>
jmp    1361 <add_random+0x1e1>
cmpq   $0x0,-0x4028(%rbp)
jg     124b <add_random+0xcb>
mov    -0x1c(%rbp),%eax
mov    0x2c47(%rip),%rcx
cmp    (%rcx),%eax
je     1389 <add_random+0x209>
mov    -0x1c(%rbp),%edi
call   1040 <close@plt>
mov    0x2c38(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x4040,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<add_random>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x4008,%rsp
cmp    $0x2,%esi
jne    11d8 <add_random+0x58>
mov    %rdx,%rbp
mov    %edi,%ebx
mov    0x8(%rdx),%rdi
lea    0xe79(%rip),%rsi
call   1060 <strcmp@plt>
test   %rax,%rax
je     1202 <add_random+0x82>
mov    0x8(%rbp),%rdi
mov    0x2e2d(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,%r15d
cmp    $0xffffffff,%eax
jne    120c <add_random+0x8c>
lea    0xe50(%rip),%rdi
call   10a0 <perror@plt>
jmp    11f4 <add_random+0x74>
mov    0x2df9(%rip),%rax
mov    (%rax),%edi
lea    0xe18(%rip),%rsi
call   1070 <fprintf@plt>
xor    %eax,%eax
call   1080 <usage@plt>
mov    0x2dd5(%rip),%rax
mov    (%rax),%eax
jmp    1291 <add_random+0x111>
mov    0x2db7(%rip),%rax
mov    (%rax),%r15d
mov    %rsp,%r13
lea    0xe25(%rip),%r14
cs nopw 0x0(%rax,%rax,1)
mov    %r15d,%edi
mov    %r13,%rsi
mov    $0x4000,%edx
call   1050 <read@plt>
test   %rax,%rax
js     1278 <add_random+0xf8>
mov    %rax,%r12
mov    %rax,%rbp
je     125c <add_random+0xdc>
nopl   (%rax)
mov    %ebx,%edi
mov    %r13,%rsi
mov    %rbp,%rdx
call   1030 <write@plt>
test   %rax,%rax
js     1271 <add_random+0xf1>
sub    %rax,%rbp
jne    1240 <add_random+0xc0>
test   %r12,%r12
jg     1220 <add_random+0xa0>
mov    0x2d5d(%rip),%rax
mov    0x2d5e(%rip),%rbx
cmp    (%rax),%r15d
jne    1287 <add_random+0x107>
jmp    128f <add_random+0x10f>
lea    0xddd(%rip),%r14
mov    %r14,%rdi
call   10a0 <perror@plt>
mov    0x2d49(%rip),%rbx
mov    %r15d,%edi
call   1040 <close@plt>
mov    (%rbx),%eax
add    $0x4008,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [31 x i8] c"Incorrect amount of arguments\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [2 x i8] c"-\00"
@STDIN_FILENO = global i32 0
@O_RDONLY = global i32 0
@.str.2 = [26 x i8] c"Failed to open input file\00"
@.str.3 = [26 x i8] c"Unable to read input file\00"
@.str.4 = [28 x i8] c"Unable to write random file\00"
@EXIT_SUCCESS = global i32 0
define i32 @add_random(i32 %0, i32 %1, i8** %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8**
%8 = alloca i32
%9 = alloca [4096 x i32]
%10 = alloca i64
%11 = alloca i32*
%12 = alloca i64
%13 = alloca i64
store i32 %0, i32* %5
store i32 %1, i32* %6
store i8** %2, i8*** %7
%14 = load i32, i32* %6
%15 = icmp ne i32 %14, 2
br i1 %15, label %16, label %21
16: ; preds = %3
%17 = load i32, i32* @stderr
%18 = call i32 @fprintf(i32 %17, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0))
%19 = call i32 (...) @usage()
%20 = load i32, i32* @EXIT_FAILURE
store i32 %20, i32* %4
br label %95
21: ; preds = %3
%22 = load i8**, i8*** %7
%23 = getelementptr inbounds i8*, i8** %22, i64 1
%24 = load i8*, i8** %23
%25 = call i64 @strcmp(i8* %24, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%26 = icmp eq i64 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %21
%28 = load i32, i32* @STDIN_FILENO
store i32 %28, i32* %8
br label %41
29: ; preds = %21
%30 = load i8**, i8*** %7
%31 = getelementptr inbounds i8*, i8** %30, i64 1
%32 = load i8*, i8** %31
%33 = load i32, i32* @O_RDONLY
%34 = call i32 @open(i8* %32, i32 %33)
store i32 %34, i32* %8
%35 = load i32, i32* %8
%36 = icmp eq i32 %35, -1
br i1 %36, label %37, label %40
37: ; preds = %29
%38 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
%39 = load i32, i32* @EXIT_FAILURE
store i32 %39, i32* %4
br label %95
40: ; preds = %29
br label %41
41: ; preds = %40, %27
store i64 0, i64* %10
br label %42
42: ; preds = %83, %41
%43 = load i32, i32* %8
%44 = getelementptr inbounds [4096 x i32], [4096 x i32]* %9, i64 0, i64 0
%45 = call i64 @read(i32 %43, i32* %44, i32 16384)
store i64 %45, i64* %10
%46 = load i64, i64* %10
%47 = icmp slt i64 %46, 0
br i1 %47, label %48, label %53
48: ; preds = %42
%49 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0))
%50 = load i32, i32* %8
%51 = call i32 @close(i32 %50)
%52 = load i32, i32* @EXIT_FAILURE
store i32 %52, i32* %4
br label %95
53: ; preds = %42
%54 = load i64, i64* %10
%55 = icmp ne i64 %54, 0
br i1 %55, label %56, label %82
56: ; preds = %53
%57 = getelementptr inbounds [4096 x i32], [4096 x i32]* %9, i64 0, i64 0
store i32* %57, i32** %11
%58 = load i64, i64* %10
store i64 %58, i64* %12
br label %59
59: ; preds = %74, %56
%60 = load i64, i64* %12
%61 = icmp ugt i64 %60, 0
br i1 %61, label %62, label %81
62: ; preds = %59
%63 = load i32, i32* %5
%64 = getelementptr inbounds [4096 x i32], [4096 x i32]* %9, i64 0, i64 0
%65 = load i64, i64* %12
%66 = call i64 @write(i32 %63, i32* %64, i64 %65)
store i64 %66, i64* %13
%67 = load i64, i64* %13
%68 = icmp slt i64 %67, 0
br i1 %68, label %69, label %74
69: ; preds = %62
%70 = call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0))
%71 = load i32, i32* %8
%72 = call i32 @close(i32 %71)
%73 = load i32, i32* @EXIT_FAILURE
store i32 %73, i32* %4
br label %95
74: ; preds = %62
%75 = load i64, i64* %13
%76 = load i64, i64* %12
%77 = sub i64 %76, %75
store i64 %77, i64* %12
%78 = load i64, i64* %13
%79 = load i32*, i32** %11
%80 = getelementptr inbounds i32, i32* %79, i64 %78
store i32* %80, i32** %11
br label %59
81: ; preds = %59
br label %82
82: ; preds = %81, %53
br label %83
83: ; preds = %82
%84 = load i64, i64* %10
%85 = icmp sgt i64 %84, 0
br i1 %85, label %42, label %86
86: ; preds = %83
%87 = load i32, i32* %8
%88 = load i32, i32* @STDIN_FILENO
%89 = icmp ne i32 %87, %88
br i1 %89, label %90, label %93
90: ; preds = %86
%91 = load i32, i32* %8
%92 = call i32 @close(i32 %91)
br label %93
93: ; preds = %90, %86
%94 = load i32, i32* @EXIT_SUCCESS
store i32 %94, i32* %4
br label %95
95: ; preds = %93, %69, %48, %37, %16
%96 = load i32, i32* %4
ret i32 %96
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [31 x i8] c"Incorrect amount of arguments\0A\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [2 x i8] c"-\00"
@STDIN_FILENO = global i32 0
@O_RDONLY = global i32 0
@.str.2 = [26 x i8] c"Failed to open input file\00"
@.str.3 = [26 x i8] c"Unable to read input file\00"
@.str.4 = [28 x i8] c"Unable to write random file\00"
@EXIT_SUCCESS = global i32 0
define i32 @add_random(i32 %0, i32 %1, i8** nocapture %2) {
%4 = alloca [4096 x i32]
%5 = icmp eq i32 %1, 2
br i1 %5, label %11, label %6
6: ; preds = %3
%7 = load i32, i32* @stderr
%8 = tail call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0))
%9 = tail call i32 (...) @usage()
%10 = load i32, i32* @EXIT_FAILURE
br label %56
11: ; preds = %3
%12 = getelementptr inbounds i8*, i8** %2, i64 1
%13 = load i8*, i8** %12
%14 = tail call i64 @strcmp(i8* %13, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %11
%17 = load i32, i32* @STDIN_FILENO
br label %26
18: ; preds = %11
%19 = load i8*, i8** %12
%20 = load i32, i32* @O_RDONLY
%21 = tail call i32 @open(i8* %19, i32 %20)
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %26
23: ; preds = %18
%24 = tail call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0))
%25 = load i32, i32* @EXIT_FAILURE
br label %56
26: ; preds = %18, %16
%27 = phi i32 [ %17, %16 ], [ %21, %18 ]
%28 = bitcast [4096 x i32]* %4 to i8*
%29 = getelementptr inbounds [4096 x i32], [4096 x i32]* %4, i64 0, i64 0
br label %30
30: ; preds = %42, %26
%31 = call i64 @read(i32 %27, i32* %29, i32 16384)
%32 = icmp slt i64 %31, 0
br i1 %32, label %47, label %33
33: ; preds = %30
%34 = icmp eq i64 %31, 0
br i1 %34, label %44, label %37
35: ; preds = %37
%36 = icmp eq i64 %41, 0
br i1 %36, label %42, label %37
37: ; preds = %33, %35
%38 = phi i64 [ %41, %35 ], [ %31, %33 ]
%39 = call i64 @write(i32 %0, i32* %29, i64 %38)
%40 = icmp slt i64 %39, 0
%41 = sub i64 %38, %39
br i1 %40, label %47, label %35
42: ; preds = %35
%43 = icmp sgt i64 %31, 0
br i1 %43, label %30, label %44
44: ; preds = %33, %42
%45 = load i32, i32* @STDIN_FILENO
%46 = icmp eq i32 %27, %45
br i1 %46, label %53, label %50
47: ; preds = %30, %37
%48 = phi i8* [ getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0), %37 ], [ getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0), %30 ]
%49 = tail call i32 @perror(i8* %48)
br label %50
50: ; preds = %47, %44
%51 = phi i32* [ @EXIT_SUCCESS, %44 ], [ @EXIT_FAILURE, %47 ]
%52 = tail call i32 @close(i32 %27)
br label %53
53: ; preds = %50, %44
%54 = phi i32* [ @EXIT_SUCCESS, %44 ], [ %51, %50 ]
%55 = load i32, i32* %54
br label %56
56: ; preds = %53, %23, %6
%57 = phi i32 [ %10, %6 ], [ %55, %53 ], [ %25, %23 ]
ret i32 %57
}