#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 O_CREAT;
int O_RDONLY;
int O_SYNC;
int O_WRONLY;
int atol(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fsync(int);
int open(char *, int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ read(int, char *, int);
int /*<<< orphan*/ write(int, char *, int);
int main(int argc, char *argv[]) {
  char buffer[1024 * 1024];
  long count = atol(argv[1]) / (1024 * 1024), i;
  int bytesleft = atol(argv[1]) % (1024 * 1024);
  int fp, rand;
  rand = open(argv[2], O_WRONLY | O_SYNC | O_CREAT, 0777);
  if (rand < 0) {
    perror("Error in opening file");
    exit(1);
  }
  fp = open("/dev/urandom", O_RDONLY, 0777);
  for (i = 0; i < count; i++) {
    read(fp, buffer, 1024 * 1024);
    write(rand, buffer, 1024 * 1024);
  }
  if (bytesleft > 0) {
    read(fp, buffer, bytesleft);
    write(rand, buffer, bytesleft);
  }
  fsync(rand);
  close(rand);
  close(fp);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x100030,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1070 <atol@plt>
mov    $0x100000,%ecx
cltd
idiv   %ecx
cltq
mov    %rax,-0x100018(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1070 <atol@plt>
mov    $0x100000,%ecx
cltd
idiv   %ecx
mov    %edx,-0x100024(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
mov    0x2ddf(%rip),%rax
mov    (%rax),%esi
mov    0x2df6(%rip),%rax
or     (%rax),%esi
mov    0x2e05(%rip),%rax
or     (%rax),%esi
mov    $0x1ff,%edx
call   1080 <open@plt>
mov    %eax,-0x10002c(%rbp)
cmpl   $0x0,-0x10002c(%rbp)
jge    1228 <main+0xa8>
lea    0xde7(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    0x2da9(%rip),%rax
mov    (%rax),%esi
lea    0xdde(%rip),%rdi
mov    $0x1ff,%edx
call   1080 <open@plt>
mov    %eax,-0x100028(%rbp)
movq   $0x0,-0x100020(%rbp)
mov    -0x100020(%rbp),%rax
cmp    -0x100018(%rbp),%rax
jge    12ac <main+0x12c>
mov    -0x100028(%rbp),%edi
lea    -0x100010(%rbp),%rsi
mov    $0x100000,%edx
call   1050 <read@plt>
mov    -0x10002c(%rbp),%edi
lea    -0x100010(%rbp),%rsi
mov    $0x100000,%edx
call   1030 <write@plt>
mov    -0x100020(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x100020(%rbp)
jmp    1253 <main+0xd3>
cmpl   $0x0,-0x100024(%rbp)
jle    12e9 <main+0x169>
mov    -0x100028(%rbp),%edi
lea    -0x100010(%rbp),%rsi
mov    -0x100024(%rbp),%edx
call   1050 <read@plt>
mov    -0x10002c(%rbp),%edi
lea    -0x100010(%rbp),%rsi
mov    -0x100024(%rbp),%edx
call   1030 <write@plt>
mov    -0x10002c(%rbp),%edi
call   1060 <fsync@plt>
mov    -0x10002c(%rbp),%edi
call   1040 <close@plt>
mov    -0x100028(%rbp),%edi
call   1040 <close@plt>
mov    -0x4(%rbp),%eax
add    $0x100030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x100008,%rsp
mov    %rsi,%rbp
mov    0x8(%rsi),%rdi
call   1070 <atol@plt>
mov    %eax,%r15d
lea    0xfffff(%r15),%ebx
test   %eax,%eax
cmovns %eax,%ebx
mov    0x2e0d(%rip),%rax
mov    0x2e26(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x10(%rbp),%rdi
mov    0x2e2f(%rip),%rax
or     (%rax),%esi
mov    $0x1ff,%edx
call   1080 <open@plt>
test   %eax,%eax
js     1296 <main+0x116>
mov    %eax,%r12d
mov    %ebx,%eax
and    $0xfff00000,%eax
mov    %r15d,%r14d
sub    %eax,%r14d
mov    0x2de4(%rip),%rax
mov    (%rax),%esi
lea    0xe19(%rip),%rdi
mov    $0x1ff,%edx
call   1080 <open@plt>
mov    %eax,%r13d
cmp    $0x100000,%r15d
jl     1246 <main+0xc6>
sar    $0x14,%ebx
movslq %ebx,%rbp
mov    %rsp,%rbx
nopl   0x0(%rax)
mov    %r13d,%edi
mov    %rbx,%rsi
mov    $0x100000,%edx
call   1050 <read@plt>
mov    %r12d,%edi
mov    %rbx,%rsi
mov    $0x100000,%edx
call   1030 <write@plt>
add    $0xffffffffffffffff,%rbp
jne    1220 <main+0xa0>
test   %r14d,%r14d
jle    126a <main+0xea>
mov    %rsp,%rbx
mov    %r13d,%edi
mov    %rbx,%rsi
mov    %r14d,%edx
call   1050 <read@plt>
mov    %r12d,%edi
mov    %rbx,%rsi
mov    %r14d,%edx
call   1030 <write@plt>
mov    %r12d,%edi
call   1060 <fsync@plt>
mov    %r12d,%edi
call   1040 <close@plt>
mov    %r13d,%edi
call   1040 <close@plt>
xor    %eax,%eax
add    $0x100008,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd63(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@O_WRONLY = global i32 0
@O_SYNC = global i32 0
@O_CREAT = global i32 0
@.str = [22 x i8] c"Error in opening file\00"
@.str.1 = [13 x i8] c"/dev/urandom\00"
@O_RDONLY = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [1048576 x i8]
%7 = alloca i64
%8 = alloca i64
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = load i8**, i8*** %5
%13 = getelementptr inbounds i8*, i8** %12, i64 1
%14 = load i8*, i8** %13
%15 = call i32 @atol(i8* %14)
%16 = sdiv i32 %15, 1048576
%17 = sext i32 %16 to i64
store i64 %17, i64* %7
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 1
%20 = load i8*, i8** %19
%21 = call i32 @atol(i8* %20)
%22 = srem i32 %21, 1048576
store i32 %22, i32* %9
%23 = load i8**, i8*** %5
%24 = getelementptr inbounds i8*, i8** %23, i64 2
%25 = load i8*, i8** %24
%26 = load i32, i32* @O_WRONLY
%27 = load i32, i32* @O_SYNC
%28 = or i32 %26, %27
%29 = load i32, i32* @O_CREAT
%30 = or i32 %28, %29
%31 = call i32 @open(i8* %25, i32 %30, i32 511)
store i32 %31, i32* %11
%32 = load i32, i32* %11
%33 = icmp slt i32 %32, 0
br i1 %33, label %34, label %37
34: ; preds = %2
%35 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
%36 = call i32 @exit(i32 1)
unreachable
37: ; preds = %2
%38 = load i32, i32* @O_RDONLY
%39 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i32 %38, i32 511)
store i32 %39, i32* %10
store i64 0, i64* %8
br label %40
40: ; preds = %51, %37
%41 = load i64, i64* %8
%42 = load i64, i64* %7
%43 = icmp slt i64 %41, %42
br i1 %43, label %44, label %54
44: ; preds = %40
%45 = load i32, i32* %10
%46 = getelementptr inbounds [1048576 x i8], [1048576 x i8]* %6, i64 0, i64 0
%47 = call i32 @read(i32 %45, i8* %46, i32 1048576)
%48 = load i32, i32* %11
%49 = getelementptr inbounds [1048576 x i8], [1048576 x i8]* %6, i64 0, i64 0
%50 = call i32 @write(i32 %48, i8* %49, i32 1048576)
br label %51
51: ; preds = %44
%52 = load i64, i64* %8
%53 = add nsw i64 %52, 1
store i64 %53, i64* %8
br label %40
54: ; preds = %40
%55 = load i32, i32* %9
%56 = icmp sgt i32 %55, 0
br i1 %56, label %57, label %66
57: ; preds = %54
%58 = load i32, i32* %10
%59 = getelementptr inbounds [1048576 x i8], [1048576 x i8]* %6, i64 0, i64 0
%60 = load i32, i32* %9
%61 = call i32 @read(i32 %58, i8* %59, i32 %60)
%62 = load i32, i32* %11
%63 = getelementptr inbounds [1048576 x i8], [1048576 x i8]* %6, i64 0, i64 0
%64 = load i32, i32* %9
%65 = call i32 @write(i32 %62, i8* %63, i32 %64)
br label %66
66: ; preds = %57, %54
%67 = load i32, i32* %11
%68 = call i32 @fsync(i32 %67)
%69 = load i32, i32* %11
%70 = call i32 @close(i32 %69)
%71 = load i32, i32* %10
%72 = call i32 @close(i32 %71)
%73 = load i32, i32* %3
ret i32 %73
}

LLVM IR (-O2):
@O_WRONLY = global i32 0
@O_SYNC = global i32 0
@O_CREAT = global i32 0
@.str = [22 x i8] c"Error in opening file\00"
@.str.1 = [13 x i8] c"/dev/urandom\00"
@O_RDONLY = global i32 0
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [1048576 x i8]
%4 = getelementptr inbounds [1048576 x i8], [1048576 x i8]* %3, i64 0, i64 0
%5 = getelementptr inbounds i8*, i8** %1, i64 1
%6 = load i8*, i8** %5
%7 = tail call i32 @atol(i8* %6)
%8 = sdiv i32 %7, 1048576
%9 = sext i32 %8 to i64
%10 = srem i32 %7, 1048576
%11 = getelementptr inbounds i8*, i8** %1, i64 2
%12 = load i8*, i8** %11
%13 = load i32, i32* @O_WRONLY
%14 = load i32, i32* @O_SYNC
%15 = or i32 %14, %13
%16 = load i32, i32* @O_CREAT
%17 = or i32 %15, %16
%18 = tail call i32 @open(i8* %12, i32 %17, i32 511)
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %2
%21 = tail call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
%22 = tail call i32 @exit(i32 1)
unreachable
23: ; preds = %2
%24 = load i32, i32* @O_RDONLY
%25 = tail call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i32 %24, i32 511)
%26 = icmp sgt i32 %7, 1048575
br i1 %26, label %27, label %33
27: ; preds = %23, %27
%28 = phi i64 [ %31, %27 ], [ 0, %23 ]
%29 = call i32 @read(i32 %25, i8* %4, i32 1048576)
%30 = call i32 @write(i32 %18, i8* %4, i32 1048576)
%31 = add nuw nsw i64 %28, 1
%32 = icmp eq i64 %31, %9
br i1 %32, label %33, label %27
33: ; preds = %27, %23
%34 = icmp sgt i32 %10, 0
br i1 %34, label %35, label %38
35: ; preds = %33
%36 = call i32 @read(i32 %25, i8* %4, i32 %10)
%37 = call i32 @write(i32 %18, i8* %4, i32 %10)
br label %38
38: ; preds = %35, %33
%39 = tail call i32 @fsync(i32 %18)
%40 = tail call i32 @close(i32 %18)
%41 = tail call i32 @close(i32 %25)
ret i32 0
}