#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 mode_t;

/* Variables and functions */
int O_CREAT;
int O_RDWR;
int O_TRUNC;
int S_IRUSR;
int S_IWUSR;
int /*<<< orphan*/ bail(char *, ...);
int /*<<< orphan*/ close(int);
char *malloc(int);
int open(char *, int, int);
int /*<<< orphan*/ prep_output(char *);
int /*<<< orphan*/ printf(char *, long, ...);
int /*<<< orphan*/ rand_str(char *, int);
int /*<<< orphan*/ sync();
int write(int, char *, int);
void writefiles(char **output_files, int *output_sizes, int bufsize,
                int num_output) {
  int i;
  char *buffer = malloc(bufsize * sizeof(char));
  if (NULL == buffer)
    bail("cannot allocate buffer (%d bytes)", bufsize);
  for (i = 0; i < num_output; i++) {
    int fd;
    int size;
    int count = 0;
    long int total_size = 0;
    prep_output(output_files[i]);
    mode_t mode = S_IRUSR | S_IWUSR;
    fd = open(output_files[i], O_CREAT | O_TRUNC | O_RDWR, mode);
    if (fd < 0)
      bail("cannot open 2 %s", output_files[i]);
    while (total_size < output_sizes[i]) {
      rand_str(buffer, bufsize);
      if (output_sizes[i] - total_size > bufsize)
        size = write(fd, buffer, bufsize);
      else
        size = write(fd, buffer, output_sizes[i] - total_size);
      if (size < 0)
        bail("cannot write to %s", output_files[i]);
      sync();
      total_size = total_size + size;
      count++;
      if (count % 1000 == 0)
        printf("write operations: %d total_size: %ld output_size: %d\n", count,
               total_size, output_sizes[i]);
    }
    printf("write: %ld bytes to %s with bufsize: %d\n", total_size,
           output_files[i], bufsize);
    close(fd);
  }
  return;
}


x86 assembly (clang, -O0):
<writefiles>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
movslq -0x14(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1070 <malloc@plt>
mov    %rax,-0x28(%rbp)
xor    %eax,%eax
cmp    -0x28(%rbp),%rax
jne    11d6 <writefiles+0x46>
mov    -0x14(%rbp),%esi
lea    0xe31(%rip),%rdi
mov    $0x0,%al
call   10a0 <bail@plt>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    13a7 <writefiles+0x217>
movl   $0x0,-0x34(%rbp)
movq   $0x0,-0x40(%rbp)
mov    -0x8(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
call   10b0 <prep_output@plt>
mov    0x2db0(%rip),%rax
mov    (%rax),%eax
mov    0x2db7(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x44(%rbp)
mov    -0x8(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
mov    0x2dc7(%rip),%rax
mov    (%rax),%esi
mov    0x2db6(%rip),%rax
or     (%rax),%esi
mov    0x2d95(%rip),%rax
or     (%rax),%esi
mov    -0x44(%rbp),%edx
call   1090 <open@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jge    1274 <writefiles+0xe4>
mov    -0x8(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,8),%rsi
lea    0xdb5(%rip),%rdi
mov    $0x0,%al
call   10a0 <bail@plt>
jmp    1279 <writefiles+0xe9>
mov    -0x40(%rbp),%rax
mov    -0x10(%rbp),%rcx
movslq -0x1c(%rbp),%rdx
movslq (%rcx,%rdx,4),%rcx
cmp    %rcx,%rax
jge    1370 <writefiles+0x1e0>
mov    -0x28(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1080 <rand_str@plt>
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movslq (%rax,%rcx,4),%rax
sub    -0x40(%rbp),%rax
movslq -0x14(%rbp),%rcx
cmp    %rcx,%rax
jle    12d2 <writefiles+0x142>
mov    -0x2c(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x30(%rbp)
jmp    12f3 <writefiles+0x163>
mov    -0x2c(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movslq (%rax,%rcx,4),%rax
sub    -0x40(%rbp),%rax
mov    %eax,%edx
call   1030 <write@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0x0,-0x30(%rbp)
jge    1317 <writefiles+0x187>
mov    -0x8(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,8),%rsi
lea    0xd23(%rip),%rdi
mov    $0x0,%al
call   10a0 <bail@plt>
mov    $0x0,%al
call   1060 <sync@plt>
mov    -0x40(%rbp),%rax
movslq -0x30(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x40(%rbp)
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
jne    136b <writefiles+0x1db>
movslq -0x34(%rbp),%rsi
mov    -0x40(%rbp),%rdx
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,4),%ecx
lea    0xce2(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1279 <writefiles+0xe9>
mov    -0x40(%rbp),%rsi
mov    -0x8(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,8),%rdx
mov    -0x14(%rbp),%ecx
lea    0xcf2(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x2c(%rbp),%edi
call   1050 <close@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11dd <writefiles+0x4d>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<writefiles>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %ecx,%r14d
mov    %rsi,0x10(%rsp)
mov    %rdi,%rbx
mov    %edx,0x8(%rsp)
mov    %edx,%edi
call   1070 <malloc@plt>
mov    %rax,0x30(%rsp)
test   %rax,%rax
je     11c8 <writefiles+0x38>
test   %r14d,%r14d
jg     11e3 <writefiles+0x53>
jmp    1363 <writefiles+0x1d3>
lea    0xe31(%rip),%rdi
mov    0x8(%rsp),%esi
xor    %eax,%eax
call   10a0 <bail@plt>
test   %r14d,%r14d
jle    1363 <writefiles+0x1d3>
movslq 0x8(%rsp),%rax
mov    %rax,0x28(%rsp)
mov    %r14d,%eax
mov    %rax,0x20(%rsp)
xor    %r15d,%r15d
mov    %rbx,0x18(%rsp)
jmp    1239 <writefiles+0xa9>
nop
xor    %r12d,%r12d
mov    0x18(%rsp),%rbx
mov    (%rbx,%r15,8),%rdx
lea    0xe69(%rip),%rdi
mov    %r12,%rsi
mov    0x8(%rsp),%ecx
xor    %eax,%eax
call   1040 <printf@plt>
mov    0xc(%rsp),%edi
call   1050 <close@plt>
add    $0x1,%r15
cmp    0x20(%rsp),%r15
je     1363 <writefiles+0x1d3>
mov    (%rbx,%r15,8),%rdi
call   10b0 <prep_output@plt>
mov    0x2d87(%rip),%rax
mov    (%rax),%edx
mov    0x2d6e(%rip),%rax
or     (%rax),%edx
mov    (%rbx,%r15,8),%rdi
mov    0x2d91(%rip),%rax
mov    (%rax),%esi
mov    0x2d90(%rip),%rax
or     (%rax),%esi
mov    0x2d67(%rip),%rax
or     (%rax),%esi
call   1090 <open@plt>
mov    %eax,0xc(%rsp)
test   %eax,%eax
jns    1292 <writefiles+0x102>
mov    (%rbx,%r15,8),%rsi
lea    0xd97(%rip),%rdi
xor    %eax,%eax
call   10a0 <bail@plt>
mov    0x10(%rsp),%rax
cmpl   $0x0,(%rax,%r15,4)
jle    1200 <writefiles+0x70>
mov    $0x1,%r14d
xor    %r12d,%r12d
jmp    12c6 <writefiles+0x136>
nopl   (%rax)
mov    0x10(%rsp),%rax
movslq (%rax,%r15,4),%rax
add    $0x1,%r14
cmp    %rax,%r12
jge    1203 <writefiles+0x73>
mov    %r14d,%eax
imul   $0x10624dd3,%rax,%rax
shr    $0x26,%rax
imul   $0xfffffc18,%eax,%ebp
mov    0x30(%rsp),%r13
mov    %r13,%rdi
mov    0x8(%rsp),%ebx
mov    %ebx,%esi
call   1080 <rand_str@plt>
mov    0x10(%rsp),%rax
movslq (%rax,%r15,4),%rdx
sub    %r12,%rdx
cmp    0x28(%rsp),%rdx
cmovg  %ebx,%edx
mov    0xc(%rsp),%edi
mov    %r13,%rsi
call   1030 <write@plt>
mov    %eax,%r13d
test   %eax,%eax
jns    132b <writefiles+0x19b>
mov    0x18(%rsp),%rax
mov    (%rax,%r15,8),%rsi
lea    0xd0f(%rip),%rdi
xor    %eax,%eax
call   10a0 <bail@plt>
xor    %eax,%eax
call   1060 <sync@plt>
movslq %r13d,%rax
add    %rax,%r12
add    %r14d,%ebp
jne    12b0 <writefiles+0x120>
mov    0x10(%rsp),%rax
mov    (%rax,%r15,4),%ecx
lea    0xcf5(%rip),%rdi
mov    %r14,%rsi
mov    %r12,%rdx
xor    %eax,%eax
call   1040 <printf@plt>
jmp    12b0 <writefiles+0x120>
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [34 x i8] c"cannot allocate buffer (%d bytes)\00"
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@O_RDWR = global i32 0
@.str.1 = [17 x i8] c"cannot open 2 %s\00"
@.str.2 = [19 x i8] c"cannot write to %s\00"
@.str.3 = [54 x i8] c"write operations: %d total_size: %ld output_size: %d\0A\00"
@.str.4 = [41 x i8] c"write: %ld bytes to %s with bufsize: %d\0A\00"
define void @writefiles(i8** %0, i32* %1, i32 %2, i32 %3) {
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i64
%15 = alloca i32
store i8** %0, i8*** %5
store i32* %1, i32** %6
store i32 %2, i32* %7
store i32 %3, i32* %8
%16 = load i32, i32* %7
%17 = sext i32 %16 to i64
%18 = mul i64 %17, 1
%19 = trunc i64 %18 to i32
%20 = call i8* @malloc(i32 %19)
store i8* %20, i8** %10
%21 = load i8*, i8** %10
%22 = icmp eq i8* null, %21
br i1 %22, label %23, label %26
23: ; preds = %4
%24 = load i32, i32* %7
%25 = call i32 (i8*, ...) @bail(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0), i32 %24)
br label %26
26: ; preds = %23, %4
store i32 0, i32* %9
br label %27
27: ; preds = %148, %26
%28 = load i32, i32* %9
%29 = load i32, i32* %8
%30 = icmp slt i32 %28, %29
br i1 %30, label %31, label %151
31: ; preds = %27
store i32 0, i32* %13
store i64 0, i64* %14
%32 = load i8**, i8*** %5
%33 = load i32, i32* %9
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds i8*, i8** %32, i64 %34
%36 = load i8*, i8** %35
%37 = call i32 @prep_output(i8* %36)
%38 = load i32, i32* @S_IRUSR
%39 = load i32, i32* @S_IWUSR
%40 = or i32 %38, %39
store i32 %40, i32* %15
%41 = load i8**, i8*** %5
%42 = load i32, i32* %9
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds i8*, i8** %41, i64 %43
%45 = load i8*, i8** %44
%46 = load i32, i32* @O_CREAT
%47 = load i32, i32* @O_TRUNC
%48 = or i32 %46, %47
%49 = load i32, i32* @O_RDWR
%50 = or i32 %48, %49
%51 = load i32, i32* %15
%52 = call i32 @open(i8* %45, i32 %50, i32 %51)
store i32 %52, i32* %11
%53 = load i32, i32* %11
%54 = icmp slt i32 %53, 0
br i1 %54, label %55, label %62
55: ; preds = %31
%56 = load i8**, i8*** %5
%57 = load i32, i32* %9
%58 = sext i32 %57 to i64
%59 = getelementptr inbounds i8*, i8** %56, i64 %58
%60 = load i8*, i8** %59
%61 = call i32 (i8*, ...) @bail(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* %60)
br label %62
62: ; preds = %55, %31
br label %63
63: ; preds = %136, %62
%64 = load i64, i64* %14
%65 = load i32*, i32** %6
%66 = load i32, i32* %9
%67 = sext i32 %66 to i64
%68 = getelementptr inbounds i32, i32* %65, i64 %67
%69 = load i32, i32* %68
%70 = sext i32 %69 to i64
%71 = icmp slt i64 %64, %70
br i1 %71, label %72, label %137
72: ; preds = %63
%73 = load i8*, i8** %10
%74 = load i32, i32* %7
%75 = call i32 @rand_str(i8* %73, i32 %74)
%76 = load i32*, i32** %6
%77 = load i32, i32* %9
%78 = sext i32 %77 to i64
%79 = getelementptr inbounds i32, i32* %76, i64 %78
%80 = load i32, i32* %79
%81 = sext i32 %80 to i64
%82 = load i64, i64* %14
%83 = sub nsw i64 %81, %82
%84 = load i32, i32* %7
%85 = sext i32 %84 to i64
%86 = icmp sgt i64 %83, %85
br i1 %86, label %87, label %92
87: ; preds = %72
%88 = load i32, i32* %11
%89 = load i8*, i8** %10
%90 = load i32, i32* %7
%91 = call i32 @write(i32 %88, i8* %89, i32 %90)
store i32 %91, i32* %12
br label %105
92: ; preds = %72
%93 = load i32, i32* %11
%94 = load i8*, i8** %10
%95 = load i32*, i32** %6
%96 = load i32, i32* %9
%97 = sext i32 %96 to i64
%98 = getelementptr inbounds i32, i32* %95, i64 %97
%99 = load i32, i32* %98
%100 = sext i32 %99 to i64
%101 = load i64, i64* %14
%102 = sub nsw i64 %100, %101
%103 = trunc i64 %102 to i32
%104 = call i32 @write(i32 %93, i8* %94, i32 %103)
store i32 %104, i32* %12
br label %105
105: ; preds = %92, %87
%106 = load i32, i32* %12
%107 = icmp slt i32 %106, 0
br i1 %107, label %108, label %115
108: ; preds = %105
%109 = load i8**, i8*** %5
%110 = load i32, i32* %9
%111 = sext i32 %110 to i64
%112 = getelementptr inbounds i8*, i8** %109, i64 %111
%113 = load i8*, i8** %112
%114 = call i32 (i8*, ...) @bail(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i8* %113)
br label %115
115: ; preds = %108, %105
%116 = call i32 (...) @sync()
%117 = load i64, i64* %14
%118 = load i32, i32* %12
%119 = sext i32 %118 to i64
%120 = add nsw i64 %117, %119
store i64 %120, i64* %14
%121 = load i32, i32* %13
%122 = add nsw i32 %121, 1
store i32 %122, i32* %13
%123 = load i32, i32* %13
%124 = srem i32 %123, 1000
%125 = icmp eq i32 %124, 0
br i1 %125, label %126, label %136
126: ; preds = %115
%127 = load i32, i32* %13
%128 = sext i32 %127 to i64
%129 = load i64, i64* %14
%130 = load i32*, i32** %6
%131 = load i32, i32* %9
%132 = sext i32 %131 to i64
%133 = getelementptr inbounds i32, i32* %130, i64 %132
%134 = load i32, i32* %133
%135 = call i32 (i8*, i64, ...) @printf(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.3, i64 0, i64 0), i64 %128, i64 %129, i32 %134)
br label %136
136: ; preds = %126, %115
br label %63
137: ; preds = %63
%138 = load i64, i64* %14
%139 = load i8**, i8*** %5
%140 = load i32, i32* %9
%141 = sext i32 %140 to i64
%142 = getelementptr inbounds i8*, i8** %139, i64 %141
%143 = load i8*, i8** %142
%144 = load i32, i32* %7
%145 = call i32 (i8*, i64, ...) @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.4, i64 0, i64 0), i64 %138, i8* %143, i32 %144)
%146 = load i32, i32* %11
%147 = call i32 @close(i32 %146)
br label %148
148: ; preds = %137
%149 = load i32, i32* %9
%150 = add nsw i32 %149, 1
store i32 %150, i32* %9
br label %27
151: ; preds = %27
ret void
}

LLVM IR (-O2):
@.str = [34 x i8] c"cannot allocate buffer (%d bytes)\00"
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@O_RDWR = global i32 0
@.str.1 = [17 x i8] c"cannot open 2 %s\00"
@.str.2 = [19 x i8] c"cannot write to %s\00"
@.str.3 = [54 x i8] c"write operations: %d total_size: %ld output_size: %d\0A\00"
@.str.4 = [41 x i8] c"write: %ld bytes to %s with bufsize: %d\0A\00"
define void @writefiles(i8** nocapture %0, i32* nocapture %1, i32 %2, i32 %3) {
%5 = sext i32 %2 to i64
%6 = tail call i8* @malloc(i32 %2)
%7 = icmp eq i8* %6, null
br i1 %7, label %8, label %10
8: ; preds = %4
%9 = tail call i32 (i8*, ...) @bail(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0), i32 %2)
br label %10
10: ; preds = %8, %4
%11 = icmp sgt i32 %3, 0
br i1 %11, label %12, label %74
12: ; preds = %10
%13 = zext i32 %3 to i64
br label %14
14: ; preds = %12, %67
%15 = phi i64 [ 0, %12 ], [ %72, %67 ]
%16 = getelementptr inbounds i8*, i8** %0, i64 %15
%17 = load i8*, i8** %16
%18 = tail call i32 @prep_output(i8* %17)
%19 = load i32, i32* @S_IRUSR
%20 = load i32, i32* @S_IWUSR
%21 = or i32 %20, %19
%22 = load i8*, i8** %16
%23 = load i32, i32* @O_CREAT
%24 = load i32, i32* @O_TRUNC
%25 = or i32 %24, %23
%26 = load i32, i32* @O_RDWR
%27 = or i32 %25, %26
%28 = tail call i32 @open(i8* %22, i32 %27, i32 %21)
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %33
30: ; preds = %14
%31 = load i8*, i8** %16
%32 = tail call i32 (i8*, ...) @bail(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* %31)
br label %33
33: ; preds = %30, %14
%34 = getelementptr inbounds i32, i32* %1, i64 %15
%35 = load i32, i32* %34
%36 = icmp sgt i32 %35, 0
br i1 %36, label %37, label %67
37: ; preds = %33, %63
%38 = phi i64 [ %56, %63 ], [ 0, %33 ]
%39 = phi i64 [ %55, %63 ], [ 0, %33 ]
%40 = tail call i32 @rand_str(i8* %6, i32 %2)
%41 = load i32, i32* %34
%42 = sext i32 %41 to i64
%43 = sub nsw i64 %42, %39
%44 = icmp sgt i64 %43, %5
%45 = trunc i64 %43 to i32
%46 = select i1 %44, i32 %2, i32 %45
%47 = tail call i32 @write(i32 %28, i8* %6, i32 %46)
%48 = icmp slt i32 %47, 0
br i1 %48, label %49, label %52
49: ; preds = %37
%50 = load i8*, i8** %16
%51 = tail call i32 (i8*, ...) @bail(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i8* %50)
br label %52
52: ; preds = %49, %37
%53 = tail call i32 (...) @sync()
%54 = sext i32 %47 to i64
%55 = add nsw i64 %39, %54
%56 = add nuw i64 %38, 1
%57 = trunc i64 %56 to i32
%58 = urem i32 %57, 1000
%59 = icmp eq i32 %58, 0
br i1 %59, label %60, label %63
60: ; preds = %52
%61 = load i32, i32* %34
%62 = tail call i32 (i8*, i64, ...) @printf(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.3, i64 0, i64 0), i64 %56, i64 %55, i32 %61)
br label %63
63: ; preds = %60, %52
%64 = load i32, i32* %34
%65 = sext i32 %64 to i64
%66 = icmp slt i64 %55, %65
br i1 %66, label %37, label %67
67: ; preds = %63, %33
%68 = phi i64 [ 0, %33 ], [ %55, %63 ]
%69 = load i8*, i8** %16
%70 = tail call i32 (i8*, i64, ...) @printf(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.4, i64 0, i64 0), i64 %68, i8* %69, i32 %2)
%71 = tail call i32 @close(i32 %28)
%72 = add nuw nsw i64 %15, 1
%73 = icmp eq i64 %72, %13
br i1 %73, label %74, label %14
74: ; preds = %67, %10
ret void
}