#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 /*<<< orphan*/ SEEK_SET;
int /*<<< orphan*/ bzero(char *, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ free(char *);
scalar_t__ lseek(int, int /*<<< orphan*/, int /*<<< orphan*/);
char *malloc(int);
int /*<<< orphan*/ printf(char *);
int write(int, char *, int);
void write_log(int fd) {
  char *buf, *big;
  long long i;
  int ret, j;
  buf = malloc((8 * 1024));
  big = malloc((10 * 1024 * 1024));
  if (buf == NULL || big == NULL) {
    printf("ERROR: malloc buffers.\n");
    exit(1);
  }
  bzero(buf, (8 * 1024));
  bzero(big, (10 * 1024 * 1024));
  for (;;) {
    for (i = 0, j = 0; i < (10 * 1024 * 1024);) {
      if ((j++ % 100) == 0) {
        ret = write(fd, big, (10 * 1024 * 1024));
        i += (10 * 1024 * 1024);
      } else {
        ret = write(fd, buf, (8 * 1024));
        i += (8 * 1024);
      }
      if (ret < 0) {
        printf("ERROR: write error.\n");
        exit(2);
      }
    }
    if (lseek(fd, 0, SEEK_SET) < 0) {
      printf("ERROR: seek() failed.\n");
      exit(3);
    }
  }
  free(buf);
  free(big);
}


x86 assembly (clang, -O0):
<write_log>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    $0x2000,%edi
call   1060 <malloc@plt>
mov    %rax,-0x10(%rbp)
mov    $0xa00000,%edi
call   1060 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     119d <write_log+0x3d>
cmpq   $0x0,-0x18(%rbp)
jne    11b3 <write_log+0x53>
lea    0xe5c(%rip),%rdi
call   1040 <printf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x10(%rbp),%rdi
mov    $0x2000,%esi
call   1070 <bzero@plt>
mov    -0x18(%rbp),%rdi
mov    $0xa00000,%esi
call   1070 <bzero@plt>
movq   $0x0,-0x20(%rbp)
movl   $0x0,-0x28(%rbp)
cmpq   $0xa00000,-0x20(%rbp)
jge    1276 <write_log+0x116>
mov    -0x28(%rbp),%eax
mov    %eax,%ecx
add    $0x1,%ecx
mov    %ecx,-0x28(%rbp)
mov    $0x64,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
jne    122f <write_log+0xcf>
mov    -0x4(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    $0xa00000,%edx
call   1030 <write@plt>
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%rax
add    $0xa00000,%rax
mov    %rax,-0x20(%rbp)
jmp    1251 <write_log+0xf1>
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x2000,%edx
call   1030 <write@plt>
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%rax
add    $0x2000,%rax
mov    %rax,-0x20(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    1271 <write_log+0x111>
lea    0xdb6(%rip),%rdi
call   1040 <printf@plt>
mov    $0x2,%edi
call   1080 <exit@plt>
jmp    11de <write_log+0x7e>
mov    -0x4(%rbp),%edi
mov    0x2d60(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1050 <lseek@plt>
cmp    $0x0,%rax
jge    12a9 <write_log+0x149>
lea    0xd93(%rip),%rdi
call   1040 <printf@plt>
mov    $0x3,%edi
call   1080 <exit@plt>
jmp    11cf <write_log+0x6f>

x86 assembly (clang, -O2):
<write_log>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edi,%r13d
mov    $0x2000,%edi
call   1060 <malloc@plt>
mov    %rax,%r14
mov    $0xa00000,%edi
call   1060 <malloc@plt>
test   %r14,%r14
je     1252 <write_log+0xf2>
mov    %rax,%r15
test   %rax,%rax
je     1252 <write_log+0xf2>
mov    %r14,%rdi
mov    $0x2000,%esi
call   1070 <bzero@plt>
mov    %r15,%rdi
mov    $0xa00000,%esi
call   1070 <bzero@plt>
xor    %ebx,%ebx
mov    0x2e23(%rip),%r12
xor    %ebp,%ebp
jmp    11ea <write_log+0x8a>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    (%r12),%edx
xor    %ebx,%ebx
mov    %r13d,%edi
xor    %esi,%esi
call   1050 <lseek@plt>
mov    $0x0,%ebp
test   %rax,%rax
js     1268 <write_log+0x108>
cmp    $0x9fffff,%rbx
ja     11d0 <write_log+0x70>
imul   $0xc28f5c29,%ebp,%eax
ror    $0x2,%eax
mov    %r13d,%edi
cmp    $0x28f5c28,%eax
jbe    1220 <write_log+0xc0>
mov    %r14,%rsi
mov    $0x2000,%edx
call   1040 <write@plt>
mov    $0x2000,%ecx
jmp    1232 <write_log+0xd2>
nopw   0x0(%rax,%rax,1)
mov    %r15,%rsi
mov    $0xa00000,%edx
call   1040 <write@plt>
mov    $0xa00000,%ecx
add    $0x1,%ebp
add    %rcx,%rbx
test   %eax,%eax
jns    11ea <write_log+0x8a>
lea    0xdd3(%rip),%rdi
call   1030 <puts@plt>
mov    $0x2,%edi
call   1080 <exit@plt>
lea    0xdd1(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
lea    0xd91(%rip),%rdi
call   1030 <puts@plt>
mov    $0x3,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [24 x i8] c"ERROR: malloc buffers.\0A\00"
@.str.1 = [21 x i8] c"ERROR: write error.\0A\00"
@SEEK_SET = global i32 0
@.str.2 = [23 x i8] c"ERROR: seek() failed.\0A\00"
define void @write_log(i32 %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %2
%8 = call i8* @malloc(i32 8192)
store i8* %8, i8** %3
%9 = call i8* @malloc(i32 10485760)
store i8* %9, i8** %4
%10 = load i8*, i8** %3
%11 = icmp eq i8* %10, null
br i1 %11, label %15, label %12
12: ; preds = %1
%13 = load i8*, i8** %4
%14 = icmp eq i8* %13, null
br i1 %14, label %15, label %18
15: ; preds = %12, %1
%16 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %12
%19 = load i8*, i8** %3
%20 = call i32 @bzero(i8* %19, i32 8192)
%21 = load i8*, i8** %4
%22 = call i32 @bzero(i8* %21, i32 10485760)
br label %23
23: ; preds = %59, %18
store i64 0, i64* %5
store i32 0, i32* %7
br label %24
24: ; preds = %50, %23
%25 = load i64, i64* %5
%26 = icmp slt i64 %25, 10485760
br i1 %26, label %27, label %51
27: ; preds = %24
%28 = load i32, i32* %7
%29 = add nsw i32 %28, 1
store i32 %29, i32* %7
%30 = srem i32 %28, 100
%31 = icmp eq i32 %30, 0
br i1 %31, label %32, label %38
32: ; preds = %27
%33 = load i32, i32* %2
%34 = load i8*, i8** %4
%35 = call i32 @write(i32 %33, i8* %34, i32 10485760)
store i32 %35, i32* %6
%36 = load i64, i64* %5
%37 = add nsw i64 %36, 10485760
store i64 %37, i64* %5
br label %44
38: ; preds = %27
%39 = load i32, i32* %2
%40 = load i8*, i8** %3
%41 = call i32 @write(i32 %39, i8* %40, i32 8192)
store i32 %41, i32* %6
%42 = load i64, i64* %5
%43 = add nsw i64 %42, 8192
store i64 %43, i64* %5
br label %44
44: ; preds = %38, %32
%45 = load i32, i32* %6
%46 = icmp slt i32 %45, 0
br i1 %46, label %47, label %50
47: ; preds = %44
%48 = call i32 @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0))
%49 = call i32 @exit(i32 2)
unreachable
50: ; preds = %44
br label %24
51: ; preds = %24
%52 = load i32, i32* %2
%53 = load i32, i32* @SEEK_SET
%54 = call i64 @lseek(i32 %52, i32 0, i32 %53)
%55 = icmp slt i64 %54, 0
br i1 %55, label %56, label %59
56: ; preds = %51
%57 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
%58 = call i32 @exit(i32 3)
unreachable
59: ; preds = %51
br label %23
}

LLVM IR (-O2):
@SEEK_SET = global i32 0
@str = [22 x i8] c"ERROR: seek() failed.\00"
@str.3 = [20 x i8] c"ERROR: write error.\00"
@str.4 = [23 x i8] c"ERROR: malloc buffers.\00"
define void @write_log(i32 %0) {
%2 = tail call dereferenceable_or_null(8192) i8* @malloc(i32 8192)
%3 = tail call dereferenceable_or_null(10485760) i8* @malloc(i32 10485760)
%4 = icmp eq i8* %2, null
%5 = icmp eq i8* %3, null
%6 = or i1 %4, %5
br i1 %6, label %7, label %10
7: ; preds = %1
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str.4, i64 0, i64 0))
%9 = tail call i32 @exit(i32 1)
unreachable
10: ; preds = %1
%11 = tail call i32 @bzero(i8* %2, i32 8192)
%12 = tail call i32 @bzero(i8* %3, i32 10485760)
br label %13
13: ; preds = %30, %10
%14 = phi i64 [ 0, %10 ], [ %31, %30 ]
%15 = phi i32 [ 0, %10 ], [ %32, %30 ]
%16 = icmp ult i64 %14, 10485760
br i1 %16, label %17, label %36
17: ; preds = %13
%18 = add nuw nsw i32 %15, 1
%19 = urem i32 %15, 100
%20 = icmp eq i32 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %17
%22 = tail call i32 @write(i32 %0, i8* %3, i32 10485760)
br label %25
23: ; preds = %17
%24 = tail call i32 @write(i32 %0, i8* %2, i32 8192)
br label %25
25: ; preds = %23, %21
%26 = phi i64 [ 8192, %23 ], [ 10485760, %21 ]
%27 = phi i32 [ %24, %23 ], [ %22, %21 ]
%28 = add nuw nsw i64 %14, %26
%29 = icmp slt i32 %27, 0
br i1 %29, label %33, label %30
30: ; preds = %25, %36
%31 = phi i64 [ %28, %25 ], [ 0, %36 ]
%32 = phi i32 [ %18, %25 ], [ 0, %36 ]
br label %13
33: ; preds = %25
%34 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str.3, i64 0, i64 0))
%35 = tail call i32 @exit(i32 2)
unreachable
36: ; preds = %13
%37 = load i32, i32* @SEEK_SET
%38 = tail call i64 @lseek(i32 %0, i32 0, i32 %37)
%39 = icmp slt i64 %38, 0
br i1 %39, label %40, label %30
40: ; preds = %36
%41 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str, i64 0, i64 0))
%42 = tail call i32 @exit(i32 3)
unreachable
}