#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 S_IRUSR;
int S_IRWXU;
int S_IWUSR;
int S_IXUSR;
int close(int);
int /*<<< orphan*/ exit(int);
scalar_t__ malloc(int);
int open(char *, int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int, char *);
int write(int, char *, int);
void write_file(char *fname) {
  int fd;
  int ret;
  char *wbuf;
  fd = open(fname, O_RDWR | O_CREAT, S_IRWXU | S_IRUSR | S_IWUSR | S_IXUSR);
  if (fd == -1) {
    perror("open file fail ");
    exit(-1);
  }
  wbuf = (char *)malloc(1024);
  for (int i = 0; i < 1024; i++) {
    wbuf[i] = i % 256;
  }
  ret = write(fd, wbuf, 1024);
  if (ret < 1024) {
    perror("write file unsuccessful  ");
    close(fd);
    exit(-2);
  }
  printf("write %d bytes to file %s\n", ret, fname);
  ret = close(fd);
  if (ret) {
    perror("close fail ");
    exit(-3);
  }
}


x86 assembly (clang, -O0):
<write_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e51(%rip),%rax
mov    (%rax),%esi
mov    0x2e68(%rip),%rax
or     (%rax),%esi
mov    0x2e47(%rip),%rax
mov    (%rax),%edx
mov    0x2e16(%rip),%rax
or     (%rax),%edx
mov    0x2e1d(%rip),%rax
or     (%rax),%edx
mov    0x2e1c(%rip),%rax
or     (%rax),%edx
call   1070 <open@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    11de <write_file+0x6e>
lea    0xe31(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>
mov    $0x400,%edi
call   1060 <malloc@plt>
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
cmpl   $0x400,-0x1c(%rbp)
jge    1224 <write_file+0xb4>
mov    -0x1c(%rbp),%eax
mov    $0x100,%ecx
cltd
idiv   %ecx
mov    -0x18(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11f3 <write_file+0x83>
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    $0x400,%edx
call   1030 <write@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x400,-0x10(%rbp)
jge    1263 <write_file+0xf3>
lea    0xdc4(%rip),%rdi
call   1080 <perror@plt>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
mov    $0xfffffffe,%edi
call   1090 <exit@plt>
mov    -0x10(%rbp),%esi
mov    -0x8(%rbp),%rdx
lea    0xdb9(%rip),%rdi
call   1040 <printf@plt>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x10(%rbp)
je     12a1 <write_file+0x131>
lea    0xdb3(%rip),%rdi
call   1080 <perror@plt>
mov    $0xfffffffd,%edi
call   1090 <exit@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_file>:
push   %r14
push   %rbx
push   %rax
mov    0x2e5d(%rip),%rax
mov    0x2e76(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e53(%rip),%rax
mov    0x2e24(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e29(%rip),%rax
or     (%rax),%edx
mov    %rdi,%r14
mov    0x2e25(%rip),%rax
or     (%rax),%edx
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     12d4 <write_file+0x164>
mov    %eax,%ebx
mov    $0x400,%edi
call   1060 <malloc@plt>
movdqa 0xe31(%rip),%xmm2
mov    $0x70,%ecx
movdqa 0xe33(%rip),%xmm8
movdqa 0xe3a(%rip),%xmm9
movdqa 0xe42(%rip),%xmm3
movdqa 0xe4a(%rip),%xmm4
movdqa 0xe52(%rip),%xmm5
movdqa 0xe5a(%rip),%xmm6
movdqa 0xe62(%rip),%xmm7
movdqa 0xe6a(%rip),%xmm1
cs nopw 0x0(%rax,%rax,1)
movdqu %xmm2,-0x70(%rax,%rcx,1)
movdqa %xmm2,%xmm0
paddb  %xmm8,%xmm0
movdqu %xmm0,-0x60(%rax,%rcx,1)
movdqa %xmm2,%xmm0
paddb  %xmm9,%xmm0
movdqu %xmm0,-0x50(%rax,%rcx,1)
movdqa %xmm2,%xmm0
paddb  %xmm3,%xmm0
movdqu %xmm0,-0x40(%rax,%rcx,1)
movdqa %xmm2,%xmm0
paddb  %xmm4,%xmm0
movdqu %xmm0,-0x30(%rax,%rcx,1)
movdqa %xmm2,%xmm0
paddb  %xmm5,%xmm0
movdqu %xmm0,-0x20(%rax,%rcx,1)
movdqa %xmm2,%xmm0
paddb  %xmm6,%xmm0
movdqu %xmm0,-0x10(%rax,%rcx,1)
movdqa %xmm2,%xmm0
paddb  %xmm7,%xmm0
movdqu %xmm0,(%rax,%rcx,1)
pxor   %xmm1,%xmm2
sub    $0xffffffffffffff80,%rcx
cmp    $0x470,%rcx
jne    1220 <write_file+0xb0>
mov    %ebx,%edi
mov    %rax,%rsi
mov    $0x400,%edx
call   1030 <write@plt>
cmp    $0x3ff,%eax
jle    12ea <write_file+0x17a>
lea    0xe03(%rip),%rdi
mov    %eax,%esi
mov    %r14,%rdx
call   1040 <printf@plt>
mov    %ebx,%edi
call   1050 <close@plt>
test   %eax,%eax
jne    1307 <write_file+0x197>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xdb5(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>
lea    0xdaf(%rip),%rdi
call   1080 <perror@plt>
mov    %ebx,%edi
call   1050 <close@plt>
mov    $0xfffffffe,%edi
call   1090 <exit@plt>
lea    0xdc7(%rip),%rdi
call   1080 <perror@plt>
mov    $0xfffffffd,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@S_IRWXU = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IXUSR = global i32 0
@.str = [16 x i8] c"open file fail \00"
@.str.1 = [26 x i8] c"write file unsuccessful \00"
@.str.2 = [27 x i8] c"write %d bytes to file %s\0A\00"
@.str.3 = [12 x i8] c"close fail \00"
define void @write_file(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
store i8* %0, i8** %2
%7 = load i8*, i8** %2
%8 = load i32, i32* @O_RDWR
%9 = load i32, i32* @O_CREAT
%10 = or i32 %8, %9
%11 = load i32, i32* @S_IRWXU
%12 = load i32, i32* @S_IRUSR
%13 = or i32 %11, %12
%14 = load i32, i32* @S_IWUSR
%15 = or i32 %13, %14
%16 = load i32, i32* @S_IXUSR
%17 = or i32 %15, %16
%18 = call i32 @open(i8* %7, i32 %10, i32 %17)
store i32 %18, i32* %3
%19 = load i32, i32* %3
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %24
21: ; preds = %1
%22 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%23 = call i32 @exit(i32 -1)
unreachable
24: ; preds = %1
%25 = call i64 @malloc(i32 1024)
%26 = inttoptr i64 %25 to i8*
store i8* %26, i8** %5
store i32 0, i32* %6
br label %27
27: ; preds = %38, %24
%28 = load i32, i32* %6
%29 = icmp slt i32 %28, 1024
br i1 %29, label %30, label %41
30: ; preds = %27
%31 = load i32, i32* %6
%32 = srem i32 %31, 256
%33 = trunc i32 %32 to i8
%34 = load i8*, i8** %5
%35 = load i32, i32* %6
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds i8, i8* %34, i64 %36
store i8 %33, i8* %37
br label %38
38: ; preds = %30
%39 = load i32, i32* %6
%40 = add nsw i32 %39, 1
store i32 %40, i32* %6
br label %27
41: ; preds = %27
%42 = load i32, i32* %3
%43 = load i8*, i8** %5
%44 = call i32 @write(i32 %42, i8* %43, i32 1024)
store i32 %44, i32* %4
%45 = load i32, i32* %4
%46 = icmp slt i32 %45, 1024
br i1 %46, label %47, label %52
47: ; preds = %41
%48 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
%49 = load i32, i32* %3
%50 = call i32 @close(i32 %49)
%51 = call i32 @exit(i32 -2)
unreachable
52: ; preds = %41
%53 = load i32, i32* %4
%54 = load i8*, i8** %2
%55 = call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i32 %53, i8* %54)
%56 = load i32, i32* %3
%57 = call i32 @close(i32 %56)
store i32 %57, i32* %4
%58 = load i32, i32* %4
%59 = icmp ne i32 %58, 0
br i1 %59, label %60, label %63
60: ; preds = %52
%61 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
%62 = call i32 @exit(i32 -3)
unreachable
63: ; preds = %52
ret void
}

LLVM IR (-O2):
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@S_IRWXU = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IXUSR = global i32 0
@.str = [16 x i8] c"open file fail \00"
@.str.1 = [26 x i8] c"write file unsuccessful \00"
@.str.2 = [27 x i8] c"write %d bytes to file %s\0A\00"
@.str.3 = [12 x i8] c"close fail \00"
define void @write_file(i8* %0) {
%2 = load i32, i32* @O_RDWR
%3 = load i32, i32* @O_CREAT
%4 = or i32 %3, %2
%5 = load i32, i32* @S_IRWXU
%6 = load i32, i32* @S_IRUSR
%7 = or i32 %6, %5
%8 = load i32, i32* @S_IWUSR
%9 = or i32 %7, %8
%10 = load i32, i32* @S_IXUSR
%11 = or i32 %9, %10
%12 = tail call i32 @open(i8* %0, i32 %4, i32 %11)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %17
14: ; preds = %1
%15 = tail call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0))
%16 = tail call i32 @exit(i32 -1)
unreachable
17: ; preds = %1
%18 = tail call i64 @malloc(i32 1024)
%19 = inttoptr i64 %18 to i8*
br label %20
20: ; preds = %20, %17
%21 = phi i64 [ 0, %17 ], [ %53, %20 ]
%22 = phi <16 x i8> [ <i8 0, i8 1, i8 2, i8 3, i8 4, i8 5, i8 6, i8 7, i8 8, i8 9, i8 10, i8 11, i8 12, i8 13, i8 14, i8 15>, %17 ], [ %54, %20 ]
%23 = getelementptr inbounds i8, i8* %19, i64 %21
%24 = bitcast i8* %23 to <16 x i8>*
store <16 x i8> %22, <16 x i8>* %24
%25 = or i64 %21, 16
%26 = add <16 x i8> %22, <i8 16, i8 16, i8 16, i8 16, i8 16, i8 16, i8 16, i8 16, i8 16, i8 16, i8 16, i8 16, i8 16, i8 16, i8 16, i8 16>
%27 = getelementptr inbounds i8, i8* %19, i64 %25
%28 = bitcast i8* %27 to <16 x i8>*
store <16 x i8> %26, <16 x i8>* %28
%29 = or i64 %21, 32
%30 = add <16 x i8> %22, <i8 32, i8 32, i8 32, i8 32, i8 32, i8 32, i8 32, i8 32, i8 32, i8 32, i8 32, i8 32, i8 32, i8 32, i8 32, i8 32>
%31 = getelementptr inbounds i8, i8* %19, i64 %29
%32 = bitcast i8* %31 to <16 x i8>*
store <16 x i8> %30, <16 x i8>* %32
%33 = or i64 %21, 48
%34 = add <16 x i8> %22, <i8 48, i8 48, i8 48, i8 48, i8 48, i8 48, i8 48, i8 48, i8 48, i8 48, i8 48, i8 48, i8 48, i8 48, i8 48, i8 48>
%35 = getelementptr inbounds i8, i8* %19, i64 %33
%36 = bitcast i8* %35 to <16 x i8>*
store <16 x i8> %34, <16 x i8>* %36
%37 = or i64 %21, 64
%38 = add <16 x i8> %22, <i8 64, i8 64, i8 64, i8 64, i8 64, i8 64, i8 64, i8 64, i8 64, i8 64, i8 64, i8 64, i8 64, i8 64, i8 64, i8 64>
%39 = getelementptr inbounds i8, i8* %19, i64 %37
%40 = bitcast i8* %39 to <16 x i8>*
store <16 x i8> %38, <16 x i8>* %40
%41 = or i64 %21, 80
%42 = add <16 x i8> %22, <i8 80, i8 80, i8 80, i8 80, i8 80, i8 80, i8 80, i8 80, i8 80, i8 80, i8 80, i8 80, i8 80, i8 80, i8 80, i8 80>
%43 = getelementptr inbounds i8, i8* %19, i64 %41
%44 = bitcast i8* %43 to <16 x i8>*
store <16 x i8> %42, <16 x i8>* %44
%45 = or i64 %21, 96
%46 = add <16 x i8> %22, <i8 96, i8 96, i8 96, i8 96, i8 96, i8 96, i8 96, i8 96, i8 96, i8 96, i8 96, i8 96, i8 96, i8 96, i8 96, i8 96>
%47 = getelementptr inbounds i8, i8* %19, i64 %45
%48 = bitcast i8* %47 to <16 x i8>*
store <16 x i8> %46, <16 x i8>* %48
%49 = or i64 %21, 112
%50 = add <16 x i8> %22, <i8 112, i8 112, i8 112, i8 112, i8 112, i8 112, i8 112, i8 112, i8 112, i8 112, i8 112, i8 112, i8 112, i8 112, i8 112, i8 112>
%51 = getelementptr inbounds i8, i8* %19, i64 %49
%52 = bitcast i8* %51 to <16 x i8>*
store <16 x i8> %50, <16 x i8>* %52
%53 = add nuw nsw i64 %21, 128
%54 = xor <16 x i8> %22, <i8 -128, i8 -128, i8 -128, i8 -128, i8 -128, i8 -128, i8 -128, i8 -128, i8 -128, i8 -128, i8 -128, i8 -128, i8 -128, i8 -128, i8 -128, i8 -128>
%55 = icmp eq i64 %53, 1024
br i1 %55, label %56, label %20
56: ; preds = %20
%57 = tail call i32 @write(i32 %12, i8* %19, i32 1024)
%58 = icmp slt i32 %57, 1024
br i1 %58, label %59, label %63
59: ; preds = %56
%60 = tail call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
%61 = tail call i32 @close(i32 %12)
%62 = tail call i32 @exit(i32 -2)
unreachable
63: ; preds = %56
%64 = tail call i32 @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i32 %57, i8* %0)
%65 = tail call i32 @close(i32 %12)
%66 = icmp eq i32 %65, 0
br i1 %66, label %70, label %67
67: ; preds = %63
%68 = tail call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
%69 = tail call i32 @exit(i32 -3)
unreachable
70: ; preds = %63
ret void
}