#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_SYNC;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ memset(char *, int, int);
int open(char *, int);
int /*<<< orphan*/ printf(char *, int);
int write(int, char *, int);
int main() {
  int fd = 0;
  char buf[5120] = {0};
  int count = 0;
  fd = open("/tmp/rw_test_file", O_CREAT | O_RDWR | O_SYNC);
  if (fd < 0) {
    printf("open failed,fd=%d\n", fd);
    return -1;
  }
  memset(buf, 1, 5120);
  count = write(fd, buf, 4096);
  if (count < 0) {
    printf("write failed,count=%d\n", count);
    close(fd);
    return -2;
  }
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1420,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
lea    -0x1410(%rbp),%rdi
xor    %esi,%esi
mov    $0x1400,%edx
call   1050 <memset@plt>
movl   $0x0,-0x1414(%rbp)
mov    0x2e6b(%rip),%rax
mov    (%rax),%esi
mov    0x2e4a(%rip),%rax
or     (%rax),%esi
mov    0x2e39(%rip),%rax
or     (%rax),%esi
lea    0xe58(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    11d5 <main+0x85>
mov    -0x8(%rbp),%esi
lea    0xe4e(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1247 <main+0xf7>
lea    -0x1410(%rbp),%rdi
mov    $0x1,%esi
mov    $0x1400,%edx
call   1050 <memset@plt>
mov    -0x8(%rbp),%edi
lea    -0x1410(%rbp),%rsi
mov    $0x1000,%edx
call   1030 <write@plt>
mov    %eax,-0x1414(%rbp)
cmpl   $0x0,-0x1414(%rbp)
jge    1238 <main+0xe8>
mov    -0x1414(%rbp),%esi
lea    0xe06(%rip),%rdi
call   1040 <printf@plt>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
movl   $0xfffffffe,-0x4(%rbp)
jmp    1247 <main+0xf7>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x1408,%rsp
mov    %rsp,%rdi
mov    $0x1400,%edx
xor    %esi,%esi
call   1050 <memset@plt>
mov    0x2e88(%rip),%rax
mov    0x2e69(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e56(%rip),%rax
or     (%rax),%esi
lea    0xe75(%rip),%rdi
call   1070 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
js     11c9 <main+0x79>
mov    %rsp,%r14
mov    %r14,%rdi
mov    $0x1,%esi
mov    $0x1400,%edx
call   1050 <memset@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x1000,%edx
call   1030 <write@plt>
test   %eax,%eax
js     11de <main+0x8e>
mov    %ebx,%edi
call   1060 <close@plt>
xor    %eax,%eax
jmp    11f8 <main+0xa8>
lea    0xe42(%rip),%rdi
mov    %ebx,%esi
call   1040 <printf@plt>
mov    $0xffffffff,%eax
jmp    11f8 <main+0xa8>
lea    0xe40(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
mov    %ebx,%edi
call   1060 <close@plt>
mov    $0xfffffffe,%eax
add    $0x1408,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [18 x i8] c"/tmp/rw_test_file\00"
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@O_SYNC = global i32 0
@.str.1 = [19 x i8] c"open failed,fd=%d\0A\00"
@.str.2 = [23 x i8] c"write failed,count=%d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [5120 x i8]
%4 = alloca i32
store i32 0, i32* %1
store i32 0, i32* %2
%5 = bitcast [5120 x i8]* %3 to i8*
store i32 0, i32* %4
%6 = load i32, i32* @O_CREAT
%7 = load i32, i32* @O_RDWR
%8 = or i32 %6, %7
%9 = load i32, i32* @O_SYNC
%10 = or i32 %8, %9
%11 = call i32 @open(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %10)
store i32 %11, i32* %2
%12 = load i32, i32* %2
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %0
%15 = load i32, i32* %2
%16 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i32 %15)
store i32 -1, i32* %1
br label %33
17: ; preds = %0
%18 = getelementptr inbounds [5120 x i8], [5120 x i8]* %3, i64 0, i64 0
%19 = call i32 @memset(i8* %18, i32 1, i32 5120)
%20 = load i32, i32* %2
%21 = getelementptr inbounds [5120 x i8], [5120 x i8]* %3, i64 0, i64 0
%22 = call i32 @write(i32 %20, i8* %21, i32 4096)
store i32 %22, i32* %4
%23 = load i32, i32* %4
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %30
25: ; preds = %17
%26 = load i32, i32* %4
%27 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i32 %26)
%28 = load i32, i32* %2
%29 = call i32 @close(i32 %28)
store i32 -2, i32* %1
br label %33
30: ; preds = %17
%31 = load i32, i32* %2
%32 = call i32 @close(i32 %31)
store i32 0, i32* %1
br label %33
33: ; preds = %30, %25, %14
%34 = load i32, i32* %1
ret i32 %34
}

LLVM IR (-O2):
@.str = [18 x i8] c"/tmp/rw_test_file\00"
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@O_SYNC = global i32 0
@.str.1 = [19 x i8] c"open failed,fd=%d\0A\00"
@.str.2 = [23 x i8] c"write failed,count=%d\0A\00"
define i32 @main() {
%1 = alloca [5120 x i8]
%2 = getelementptr inbounds [5120 x i8], [5120 x i8]* %1, i64 0, i64 0
%3 = load i32, i32* @O_CREAT
%4 = load i32, i32* @O_RDWR
%5 = or i32 %4, %3
%6 = load i32, i32* @O_SYNC
%7 = or i32 %5, %6
%8 = tail call i32 @open(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %7)
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %0
%11 = tail call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i32 %8)
br label %21
12: ; preds = %0
%13 = call i32 @memset(i8* %2, i32 1, i32 5120)
%14 = call i32 @write(i32 %8, i8* %2, i32 4096)
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %12
%17 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0), i32 %14)
%18 = call i32 @close(i32 %8)
br label %21
19: ; preds = %12
%20 = call i32 @close(i32 %8)
br label %21
21: ; preds = %19, %16, %10
%22 = phi i32 [ -1, %10 ], [ -2, %16 ], [ 0, %19 ]
ret i32 %22
}