#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_RDWR;
int O_TRUNC;
int atoi(char *);
int /*<<< orphan*/ close(int);
int open(char *, int);
int /*<<< orphan*/ printf(char *, int);
int rand();
int /*<<< orphan*/ srand(int /*<<< orphan*/);
int /*<<< orphan*/ time(int /*<<< orphan*/ *);
scalar_t__ write(int, int *, int);
int main(int argc, char **argv) {
  int i, num = 10;
  int fd;
  int data;
  if (--argc > 0)
    num = atoi(argv[1]);
  if (fd = open("sort_data", O_RDWR | O_CREAT | O_TRUNC)) {
    srand(time(NULL));
    for (i = num; i > 0; i--) {
      data = rand() % 100;
      if (write(fd, &data, sizeof(int)))
        ;
    }
    printf("%d integer written to file!\n", num - i);
    close(fd);
    return 0;
  }
  return -1;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0xa,-0x18(%rbp)
mov    -0x8(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
jle    11bf <main+0x3f>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1090 <atoi@plt>
mov    %eax,-0x18(%rbp)
mov    0x2e12(%rip),%rax
mov    (%rax),%esi
mov    0x2e29(%rip),%rax
or     (%rax),%esi
mov    0x2e18(%rip),%rax
or     (%rax),%esi
lea    0xe1f(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
je     127d <main+0xfd>
xor    %eax,%eax
mov    %eax,%edi
call   1070 <time@plt>
mov    %eax,%edi
call   1060 <srand@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jle    1257 <main+0xd7>
mov    $0x0,%al
call   10a0 <rand@plt>
mov    $0x64,%ecx
cltd
idiv   %ecx
mov    %edx,-0x20(%rbp)
mov    -0x1c(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0x4,%edx
call   1030 <write@plt>
cmp    $0x0,%rax
je     1244 <main+0xc4>
jmp    1244 <main+0xc4>
jmp    1249 <main+0xc9>
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
jmp    1208 <main+0x88>
mov    -0x18(%rbp),%esi
sub    -0x14(%rbp),%esi
lea    0xda6(%rip),%rdi
call   1040 <printf@plt>
mov    -0x1c(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
jmp    1284 <main+0x104>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    $0xa,%r15d
cmp    $0x2,%edi
jl     11a2 <main+0x22>
mov    0x8(%rsi),%rdi
call   1090 <atoi@plt>
mov    %eax,%r15d
mov    0x2e2f(%rip),%rax
mov    0x2e48(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e35(%rip),%rax
or     (%rax),%esi
lea    0xe3c(%rip),%rdi
call   1080 <open@plt>
test   %eax,%eax
je     125a <main+0xda>
mov    %eax,%r13d
xor    %r14d,%r14d
xor    %edi,%edi
call   1070 <time@plt>
mov    %eax,%edi
call   1060 <srand@plt>
mov    %r15d,%ebp
test   %r15d,%r15d
jle    123e <main+0xbe>
lea    0x1(%r15),%ebx
lea    0x4(%rsp),%r12
xor    %ebp,%ebp
nopl   0x0(%rax,%rax,1)
xor    %eax,%eax
call   10a0 <rand@plt>
cltq
imul   $0x51eb851f,%rax,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x25,%rcx
add    %edx,%ecx
imul   $0x64,%ecx,%ecx
sub    %ecx,%eax
mov    %eax,0x4(%rsp)
mov    %r13d,%edi
mov    %r12,%rsi
mov    $0x4,%edx
call   1030 <write@plt>
add    $0xffffffff,%ebx
cmp    $0x1,%ebx
ja     1200 <main+0x80>
sub    %ebp,%r15d
lea    0xdc2(%rip),%rdi
mov    %r15d,%esi
call   1040 <printf@plt>
mov    %r13d,%edi
call   1050 <close@plt>
jmp    1260 <main+0xe0>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [10 x i8] c"sort_data\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@.str.1 = [29 x i8] c"%d integer written to file!\0A\00"
define i32 @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
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 10, i32* %7
%10 = load i32, i32* %4
%11 = add nsw i32 %10, -1
store i32 %11, i32* %4
%12 = icmp sgt i32 %11, 0
br i1 %12, label %13, label %18
13: ; preds = %2
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 1
%16 = load i8*, i8** %15
%17 = call i32 @atoi(i8* %16)
store i32 %17, i32* %7
br label %18
18: ; preds = %13, %2
%19 = load i32, i32* @O_RDWR
%20 = load i32, i32* @O_CREAT
%21 = or i32 %19, %20
%22 = load i32, i32* @O_TRUNC
%23 = or i32 %21, %22
%24 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %23)
store i32 %24, i32* %8
%25 = icmp ne i32 %24, 0
br i1 %25, label %26, label %51
26: ; preds = %18
%27 = call i32 @time(i32* null)
%28 = call i32 @srand(i32 %27)
%29 = load i32, i32* %7
store i32 %29, i32* %6
br label %30
30: ; preds = %41, %26
%31 = load i32, i32* %6
%32 = icmp sgt i32 %31, 0
br i1 %32, label %33, label %44
33: ; preds = %30
%34 = call i32 (...) @rand()
%35 = srem i32 %34, 100
store i32 %35, i32* %9
%36 = load i32, i32* %8
%37 = call i64 @write(i32 %36, i32* %9, i32 4)
%38 = icmp ne i64 %37, 0
br i1 %38, label %39, label %40
39: ; preds = %33
br label %40
40: ; preds = %39, %33
br label %41
41: ; preds = %40
%42 = load i32, i32* %6
%43 = add nsw i32 %42, -1
store i32 %43, i32* %6
br label %30
44: ; preds = %30
%45 = load i32, i32* %7
%46 = load i32, i32* %6
%47 = sub nsw i32 %45, %46
%48 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i32 %47)
%49 = load i32, i32* %8
%50 = call i32 @close(i32 %49)
store i32 0, i32* %3
br label %52
51: ; preds = %18
store i32 -1, i32* %3
br label %52
52: ; preds = %51, %44
%53 = load i32, i32* %3
ret i32 %53
}

LLVM IR (-O2):
@.str = [10 x i8] c"sort_data\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@.str.1 = [29 x i8] c"%d integer written to file!\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = icmp sgt i32 %0, 1
br i1 %5, label %6, label %10
6: ; preds = %2
%7 = getelementptr inbounds i8*, i8** %1, i64 1
%8 = load i8*, i8** %7
%9 = tail call i32 @atoi(i8* %8)
br label %10
10: ; preds = %6, %2
%11 = phi i32 [ %9, %6 ], [ 10, %2 ]
%12 = load i32, i32* @O_RDWR
%13 = load i32, i32* @O_CREAT
%14 = or i32 %13, %12
%15 = load i32, i32* @O_TRUNC
%16 = or i32 %14, %15
%17 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %16)
%18 = icmp eq i32 %17, 0
br i1 %18, label %35, label %19
19: ; preds = %10
%20 = tail call i32 @time(i32* null)
%21 = tail call i32 @srand(i32 %20)
%22 = icmp sgt i32 %11, 0
br i1 %22, label %23, label %30
23: ; preds = %19, %23
%24 = phi i32 [ %28, %23 ], [ %11, %19 ]
%25 = tail call i32 (...) @rand()
%26 = srem i32 %25, 100
store i32 %26, i32* %3
%27 = call i64 @write(i32 %17, i32* %3, i32 4)
%28 = add nsw i32 %24, -1
%29 = icmp ugt i32 %24, 1
br i1 %29, label %23, label %30
30: ; preds = %23, %19
%31 = phi i32 [ %11, %19 ], [ 0, %23 ]
%32 = sub nsw i32 %11, %31
%33 = tail call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i32 %32)
%34 = tail call i32 @close(i32 %17)
br label %35
35: ; preds = %10, %30
%36 = phi i32 [ 0, %30 ], [ -1, %10 ]
ret i32 %36
}