#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*/ time_t;

/* Variables and functions */
int O_CREAT;
int O_WRONLY;
int S_IRUSR;
int S_IWUSR;
int /*<<< orphan*/ close(int);
int difftime(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int open(char *, int, int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int /*<<< orphan*/ time(int /*<<< orphan*/ *);
int write(int, char *, int);
int cmd_file_write(int argc, char **argv) {
  int fd;
  static char buffer[(1024)];
  char *ptr;
  int bytes_num;
  int counter = 0;
  time_t time_start, time_end;
  unsigned int speed;
  int i;
  if (argc != 2) {
    fprintf(stderr, "Usage: %s to_file_name\n", argv[0]);
    exit(1);
  }
  if ((fd = open(argv[1], O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR)) == -1) {
    fprintf(stderr, "Open file [%s] error [%s]\n", argv[1], strerror(errno));
    exit(1);
  }
  for (i = 0; i < (1024); i++) {
    ptr = buffer + i;
    *(unsigned int *)ptr = 0xDEADBEEF;
    i += sizeof(*ptr);
  }
  printf("Begin to Write File: %s\n", argv[1]);
  time_start = time(NULL);
  counter = 0;
  while (counter < (256 * 1024 * 1024)) {
    if ((bytes_num = write(fd, buffer, (1024))) == -1) {
      fprintf(stderr, "Write file error.\n");
      break;
    }
    counter += bytes_num;
  }
  close(fd);
  time_end = time(NULL);
  speed = counter / difftime(time_end, time_start) / (1024);
  printf("File Write Speed : %d KB/s\n", speed);
  return 0;
}


x86 assembly (clang, -O0):
<cmd_file_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x28(%rbp)
cmpl   $0x2,-0x4(%rbp)
je     11d8 <cmd_file_write+0x48>
mov    0x2e19(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe39(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2dc9(%rip),%rax
mov    (%rax),%esi
mov    0x2e08(%rip),%rax
or     (%rax),%esi
mov    0x2dc7(%rip),%rax
mov    (%rax),%edx
mov    0x2dde(%rip),%rax
or     (%rax),%edx
call   1090 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    125d <cmd_file_write+0xcd>
mov    0x2db4(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x40(%rbp)
mov    0x2d94(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    -0x44(%rbp),%edi
mov    -0x40(%rbp),%rdx
mov    %rax,%rcx
lea    0xdcc(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
movl   $0x0,-0x38(%rbp)
cmpl   $0x400,-0x38(%rbp)
jge    12a6 <cmd_file_write+0x116>
movslq -0x38(%rbp),%rcx
lea    0x2e04(%rip),%rax
add    %rcx,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
movl   $0xdeadbeef,(%rax)
movslq -0x38(%rbp),%rax
add    $0x1,%rax
mov    %eax,-0x38(%rbp)
mov    -0x38(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x38(%rbp)
jmp    1264 <cmd_file_write+0xd4>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    0xd7e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1080 <time@plt>
mov    %eax,-0x2c(%rbp)
movl   $0x0,-0x28(%rbp)
cmpl   $0x10000000,-0x28(%rbp)
jge    1326 <cmd_file_write+0x196>
mov    -0x14(%rbp),%edi
lea    0x2d9a(%rip),%rsi
mov    $0x400,%edx
call   1030 <write@plt>
mov    %eax,-0x24(%rbp)
cmp    $0xffffffff,%eax
jne    1318 <cmd_file_write+0x188>
mov    0x2ccd(%rip),%rax
mov    (%rax),%edi
lea    0xd40(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
jmp    1326 <cmd_file_write+0x196>
mov    -0x24(%rbp),%eax
add    -0x28(%rbp),%eax
mov    %eax,-0x28(%rbp)
jmp    12cf <cmd_file_write+0x13f>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1080 <time@plt>
mov    %eax,-0x30(%rbp)
mov    -0x28(%rbp),%eax
mov    %eax,-0x48(%rbp)
mov    -0x30(%rbp),%edi
mov    -0x2c(%rbp),%esi
call   1050 <difftime@plt>
mov    %eax,%ecx
mov    -0x48(%rbp),%eax
cltd
idiv   %ecx
mov    $0x400,%ecx
cltd
idiv   %ecx
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%esi
lea    0xcf7(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cmd_file_write>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
cmp    $0x2,%edi
jne    12ef <cmd_file_write+0x15f>
mov    0x8(%rbx),%rdi
mov    0x2e02(%rip),%rax
mov    0x2e43(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e00(%rip),%rax
mov    0x2e19(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
call   1090 <open@plt>
cmp    $0xffffffff,%eax
je     1313 <cmd_file_write+0x183>
mov    %eax,%ebp
mov    $0xfffffffffffffffe,%rax
lea    0x2e97(%rip),%rcx
nopl   0x0(%rax)
movl   $0xdeadbeef,0x2(%rax,%rcx,1)
de
movl   $0xdeadbeef,0x4(%rax,%rcx,1)
de
movl   $0xdeadbeef,0x6(%rax,%rcx,1)
de
movl   $0xdeadbeef,0x8(%rax,%rcx,1)
de
movl   $0xdeadbeef,0xa(%rax,%rcx,1)
de
movl   $0xdeadbeef,0xc(%rax,%rcx,1)
de
movl   $0xdeadbeef,0xe(%rax,%rcx,1)
de
movl   $0xdeadbeef,0x10(%rax,%rcx,1)
de
add    $0x10,%rax
cmp    $0x3fe,%rax
jb     11f0 <cmd_file_write+0x60>
mov    0x8(%rbx),%rsi
lea    0xdec(%rip),%rdi
xor    %ebx,%ebx
xor    %eax,%eax
call   1040 <printf@plt>
xor    %edi,%edi
call   1080 <time@plt>
mov    %eax,%r14d
lea    0x2e1f(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0x400,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
je     1290 <cmd_file_write+0x100>
add    %eax,%ebx
cmp    $0xfffffff,%ebx
jle    1270 <cmd_file_write+0xe0>
jmp    12a7 <cmd_file_write+0x117>
mov    0x2d39(%rip),%rax
mov    (%rax),%edi
lea    0xdac(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    %ebp,%edi
call   1060 <close@plt>
xor    %edi,%edi
call   1080 <time@plt>
mov    %eax,%edi
mov    %r14d,%esi
call   1050 <difftime@plt>
mov    %eax,%ecx
mov    %ebx,%eax
cltd
idiv   %ecx
lea    0x3ff(%rax),%esi
test   %eax,%eax
cmovns %eax,%esi
sar    $0xa,%esi
lea    0xd84(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2cda(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%rdx
lea    0xcfe(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    0x2cb6(%rip),%rax
mov    (%rax),%ebp
mov    0x8(%rbx),%rbx
mov    0x2ca1(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xce3(%rip),%rsi
mov    %ebp,%edi
mov    %rbx,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1070 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@cmd_file_write.buffer = internal global [1024 x i8] zeroinitializer
@stderr = global i32 0
@.str = [24 x i8] c"Usage: %s to_file_name\0A\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str.1 = [27 x i8] c"Open file [%s] error [%s]\0A\00"
@errno = global i32 0
@.str.2 = [25 x i8] c"Begin to Write File: %s\0A\00"
@.str.3 = [19 x i8] c"Write file error.\0A\00"
@.str.4 = [28 x i8] c"File Write Speed : %d KB/s\0A\00"
define i32 @cmd_file_write(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i8**
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
store i32 %0, i32* %3
store i8** %1, i8*** %4
store i32 0, i32* %8
%13 = load i32, i32* %3
%14 = icmp ne i32 %13, 2
br i1 %14, label %15, label %22
15: ; preds = %2
%16 = load i32, i32* @stderr
%17 = load i8**, i8*** %4
%18 = getelementptr inbounds i8*, i8** %17, i64 0
%19 = load i8*, i8** %18
%20 = call i32 (i32, i8*, ...) @fprintf(i32 %16, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %19)
%21 = call i32 @exit(i32 1)
unreachable
22: ; preds = %2
%23 = load i8**, i8*** %4
%24 = getelementptr inbounds i8*, i8** %23, i64 1
%25 = load i8*, i8** %24
%26 = load i32, i32* @O_WRONLY
%27 = load i32, i32* @O_CREAT
%28 = or i32 %26, %27
%29 = load i32, i32* @S_IRUSR
%30 = load i32, i32* @S_IWUSR
%31 = or i32 %29, %30
%32 = call i32 @open(i8* %25, i32 %28, i32 %31)
store i32 %32, i32* %5
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %43
34: ; preds = %22
%35 = load i32, i32* @stderr
%36 = load i8**, i8*** %4
%37 = getelementptr inbounds i8*, i8** %36, i64 1
%38 = load i8*, i8** %37
%39 = load i32, i32* @errno
%40 = call i8* @strerror(i32 %39)
%41 = call i32 (i32, i8*, ...) @fprintf(i32 %35, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i8* %38, i8* %40)
%42 = call i32 @exit(i32 1)
unreachable
43: ; preds = %22
store i32 0, i32* %12
br label %44
44: ; preds = %57, %43
%45 = load i32, i32* %12
%46 = icmp slt i32 %45, 1024
br i1 %46, label %47, label %60
47: ; preds = %44
%48 = load i32, i32* %12
%49 = sext i32 %48 to i64
%50 = getelementptr inbounds i8, i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @cmd_file_write.buffer, i64 0, i64 0), i64 %49
store i8* %50, i8** %6
%51 = load i8*, i8** %6
%52 = bitcast i8* %51 to i32*
store i32 -559038737, i32* %52
%53 = load i32, i32* %12
%54 = sext i32 %53 to i64
%55 = add i64 %54, 1
%56 = trunc i64 %55 to i32
store i32 %56, i32* %12
br label %57
57: ; preds = %47
%58 = load i32, i32* %12
%59 = add nsw i32 %58, 1
store i32 %59, i32* %12
br label %44
60: ; preds = %44
%61 = load i8**, i8*** %4
%62 = getelementptr inbounds i8*, i8** %61, i64 1
%63 = load i8*, i8** %62
%64 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i8* %63)
%65 = call i32 @time(i32* null)
store i32 %65, i32* %9
store i32 0, i32* %8
br label %66
66: ; preds = %76, %60
%67 = load i32, i32* %8
%68 = icmp slt i32 %67, 268435456
br i1 %68, label %69, label %80
69: ; preds = %66
%70 = load i32, i32* %5
%71 = call i32 @write(i32 %70, i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @cmd_file_write.buffer, i64 0, i64 0), i32 1024)
store i32 %71, i32* %7
%72 = icmp eq i32 %71, -1
br i1 %72, label %73, label %76
73: ; preds = %69
%74 = load i32, i32* @stderr
%75 = call i32 (i32, i8*, ...) @fprintf(i32 %74, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0))
br label %80
76: ; preds = %69
%77 = load i32, i32* %7
%78 = load i32, i32* %8
%79 = add nsw i32 %78, %77
store i32 %79, i32* %8
br label %66
80: ; preds = %73, %66
%81 = load i32, i32* %5
%82 = call i32 @close(i32 %81)
%83 = call i32 @time(i32* null)
store i32 %83, i32* %10
%84 = load i32, i32* %8
%85 = load i32, i32* %10
%86 = load i32, i32* %9
%87 = call i32 @difftime(i32 %85, i32 %86)
%88 = sdiv i32 %84, %87
%89 = sdiv i32 %88, 1024
store i32 %89, i32* %11
%90 = load i32, i32* %11
%91 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0), i32 %90)
ret i32 0
}

LLVM IR (-O2):
@cmd_file_write.buffer = internal global [1024 x i8] zeroinitializer
@stderr = global i32 0
@.str = [24 x i8] c"Usage: %s to_file_name\0A\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str.1 = [27 x i8] c"Open file [%s] error [%s]\0A\00"
@errno = global i32 0
@.str.2 = [25 x i8] c"Begin to Write File: %s\0A\00"
@.str.3 = [19 x i8] c"Write file error.\0A\00"
@.str.4 = [28 x i8] c"File Write Speed : %d KB/s\0A\00"
define i32 @cmd_file_write(i32 %0, i8** nocapture %1) {
%3 = icmp eq i32 %0, 2
br i1 %3, label %9, label %4
4: ; preds = %2
%5 = load i32, i32* @stderr
%6 = load i8*, i8** %1
%7 = tail call i32 (i32, i8*, ...) @fprintf(i32 %5, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %6)
%8 = tail call i32 @exit(i32 1)
unreachable
9: ; preds = %2
%10 = getelementptr inbounds i8*, i8** %1, i64 1
%11 = load i8*, i8** %10
%12 = load i32, i32* @O_WRONLY
%13 = load i32, i32* @O_CREAT
%14 = or i32 %13, %12
%15 = load i32, i32* @S_IRUSR
%16 = load i32, i32* @S_IWUSR
%17 = or i32 %16, %15
%18 = tail call i32 @open(i8* %11, i32 %14, i32 %17)
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %27
20: ; preds = %9
%21 = load i32, i32* @stderr
%22 = load i8*, i8** %10
%23 = load i32, i32* @errno
%24 = tail call i8* @strerror(i32 %23)
%25 = tail call i32 (i32, i8*, ...) @fprintf(i32 %21, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i8* %22, i8* %24)
%26 = tail call i32 @exit(i32 1)
unreachable
27: ; preds = %9, %27
%28 = phi i64 [ %52, %27 ], [ 0, %9 ]
%29 = getelementptr inbounds [1024 x i8], [1024 x i8]* @cmd_file_write.buffer, i64 0, i64 %28
%30 = bitcast i8* %29 to i32*
store i32 -559038737, i32* %30
%31 = or i64 %28, 2
%32 = getelementptr inbounds [1024 x i8], [1024 x i8]* @cmd_file_write.buffer, i64 0, i64 %31
%33 = bitcast i8* %32 to i32*
store i32 -559038737, i32* %33
%34 = or i64 %28, 4
%35 = getelementptr inbounds [1024 x i8], [1024 x i8]* @cmd_file_write.buffer, i64 0, i64 %34
%36 = bitcast i8* %35 to i32*
store i32 -559038737, i32* %36
%37 = or i64 %28, 6
%38 = getelementptr inbounds [1024 x i8], [1024 x i8]* @cmd_file_write.buffer, i64 0, i64 %37
%39 = bitcast i8* %38 to i32*
store i32 -559038737, i32* %39
%40 = or i64 %28, 8
%41 = getelementptr inbounds [1024 x i8], [1024 x i8]* @cmd_file_write.buffer, i64 0, i64 %40
%42 = bitcast i8* %41 to i32*
store i32 -559038737, i32* %42
%43 = or i64 %28, 10
%44 = getelementptr inbounds [1024 x i8], [1024 x i8]* @cmd_file_write.buffer, i64 0, i64 %43
%45 = bitcast i8* %44 to i32*
store i32 -559038737, i32* %45
%46 = or i64 %28, 12
%47 = getelementptr inbounds [1024 x i8], [1024 x i8]* @cmd_file_write.buffer, i64 0, i64 %46
%48 = bitcast i8* %47 to i32*
store i32 -559038737, i32* %48
%49 = or i64 %28, 14
%50 = getelementptr inbounds [1024 x i8], [1024 x i8]* @cmd_file_write.buffer, i64 0, i64 %49
%51 = bitcast i8* %50 to i32*
store i32 -559038737, i32* %51
%52 = add nuw nsw i64 %28, 16
%53 = icmp ult i64 %49, 1022
br i1 %53, label %27, label %54
54: ; preds = %27
%55 = load i8*, i8** %10
%56 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i8* %55)
%57 = tail call i32 @time(i32* null)
br label %60
58: ; preds = %60
%59 = icmp slt i32 %64, 268435456
br i1 %59, label %60, label %68
60: ; preds = %54, %58
%61 = phi i32 [ 0, %54 ], [ %64, %58 ]
%62 = tail call i32 @write(i32 %18, i8* getelementptr inbounds ([1024 x i8], [1024 x i8]* @cmd_file_write.buffer, i64 0, i64 0), i32 1024)
%63 = icmp eq i32 %62, -1
%64 = add nsw i32 %62, %61
br i1 %63, label %65, label %58
65: ; preds = %60
%66 = load i32, i32* @stderr
%67 = tail call i32 (i32, i8*, ...) @fprintf(i32 %66, i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0))
br label %68
68: ; preds = %58, %65
%69 = phi i32 [ %61, %65 ], [ %64, %58 ]
%70 = tail call i32 @close(i32 %18)
%71 = tail call i32 @time(i32* null)
%72 = tail call i32 @difftime(i32 %71, i32 %57)
%73 = sdiv i32 %69, %72
%74 = sdiv i32 %73, 1024
%75 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0), i32 %74)
ret i32 0
}