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

/* Variables and functions */
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ strlen(char *);
int write(int, char *, int /*<<< orphan*/);
int main(void) {
  int fd = -1;
  char buf[] = "quick brown fox jumps over the lazy dog";
  ssize_t size;
  char filename[] = "test.txt";
  fd = open(filename, O_RDWR);
  if (-1 == fd) {
    printf("Open file %s failure,fd:%d\n", filename, fd);
  } else {
    printf("open file %s success,fd:%d\n", filename, fd);
  }
  size = write(fd, buf, strlen(buf));
  printf("write %d bytes to file %s\n", size, filename);
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0xffffffff,-0x8(%rbp)
lea    -0x30(%rbp),%rdi
lea    0xe7f(%rip),%rsi
mov    $0x28,%edx
call   1070 <memcpy@plt>
mov    0xe96(%rip),%rax
mov    %rax,-0x3d(%rbp)
mov    0xe94(%rip),%al
mov    %al,-0x35(%rbp)
lea    -0x3d(%rbp),%rdi
mov    0x2e3e(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x8(%rbp)
mov    $0xffffffff,%eax
cmp    -0x8(%rbp),%eax
jne    11dc <main+0x7c>
lea    -0x3d(%rbp),%rsi
mov    -0x8(%rbp),%edx
lea    0xe61(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    11f1 <main+0x91>
lea    -0x3d(%rbp),%rsi
mov    -0x8(%rbp),%edx
lea    0xe63(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x4c(%rbp)
lea    -0x30(%rbp),%rax
mov    %rax,-0x48(%rbp)
lea    -0x30(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x4c(%rbp),%edi
mov    -0x48(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%esi
lea    -0x3d(%rbp),%rdx
lea    0xe42(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x38,%rsp
movaps 0xeb2(%rip),%xmm0
movaps %xmm0,0x20(%rsp)
movaps 0xe96(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
movabs $0x676f6420797a61,%rax
mov    %rax,0x30(%rsp)
movabs $0x7478742e74736574,%rax
74 78 74
mov    %rax,(%rsp)
movb   $0x0,0x8(%rsp)
mov    0x2e50(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdi
call   1070 <open@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     11b7 <main+0x67>
lea    0xe9d(%rip),%rdi
mov    %rsp,%rsi
mov    %ebx,%edx
jmp    11c6 <main+0x76>
lea    0xe73(%rip),%rdi
mov    %rsp,%rsi
mov    $0xffffffff,%edx
xor    %eax,%eax
call   1050 <printf@plt>
lea    0x10(%rsp),%r14
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1030 <write@plt>
lea    0xe7c(%rip),%rdi
mov    %rsp,%rdx
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %ebx,%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@__const.main.buf = [40 x i8] c"quick brown fox jumps over the lazy dog\00"
@__const.main.filename = [9 x i8] c"test.txt\00"
@O_RDWR = global i32 0
@.str = [28 x i8] c"Open file %s failure,fd:%d\0A\00"
@.str.1 = [28 x i8] c"open file %s success,fd:%d\0A\00"
@.str.2 = [27 x i8] c"write %d bytes to file %s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [40 x i8]
%4 = alloca i32
%5 = alloca [9 x i8]
store i32 0, i32* %1
store i32 -1, i32* %2
%6 = bitcast [40 x i8]* %3 to i8*
%7 = bitcast [9 x i8]* %5 to i8*
%8 = getelementptr inbounds [9 x i8], [9 x i8]* %5, i64 0, i64 0
%9 = load i32, i32* @O_RDWR
%10 = call i32 @open(i8* %8, i32 %9)
store i32 %10, i32* %2
%11 = load i32, i32* %2
%12 = icmp eq i32 -1, %11
br i1 %12, label %13, label %17
13: ; preds = %0
%14 = getelementptr inbounds [9 x i8], [9 x i8]* %5, i64 0, i64 0
%15 = load i32, i32* %2
%16 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i8* %14, i32 %15)
br label %21
17: ; preds = %0
%18 = getelementptr inbounds [9 x i8], [9 x i8]* %5, i64 0, i64 0
%19 = load i32, i32* %2
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i8* %18, i32 %19)
br label %21
21: ; preds = %17, %13
%22 = load i32, i32* %2
%23 = getelementptr inbounds [40 x i8], [40 x i8]* %3, i64 0, i64 0
%24 = getelementptr inbounds [40 x i8], [40 x i8]* %3, i64 0, i64 0
%25 = call i32 @strlen(i8* %24)
%26 = call i32 @write(i32 %22, i8* %23, i32 %25)
store i32 %26, i32* %4
%27 = load i32, i32* %4
%28 = getelementptr inbounds [9 x i8], [9 x i8]* %5, i64 0, i64 0
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i32 %27, i8* %28)
%30 = load i32, i32* %2
%31 = call i32 @close(i32 %30)
ret i32 0
}

LLVM IR (-O2):
@__const.main.buf = [40 x i8] c"quick brown fox jumps over the lazy dog\00"
@__const.main.filename = [9 x i8] c"test.txt\00"
@O_RDWR = global i32 0
@.str = [28 x i8] c"Open file %s failure,fd:%d\0A\00"
@.str.1 = [28 x i8] c"open file %s success,fd:%d\0A\00"
@.str.2 = [27 x i8] c"write %d bytes to file %s\0A\00"
define i32 @main() {
%1 = alloca [40 x i8]
%2 = alloca [9 x i8]
%3 = getelementptr inbounds [40 x i8], [40 x i8]* %1, i64 0, i64 0
%4 = getelementptr inbounds [9 x i8], [9 x i8]* %2, i64 0, i64 0
%5 = load i32, i32* @O_RDWR
%6 = call i32 @open(i8* %4, i32 %5)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %10
8: ; preds = %0
%9 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i8* %4, i32 -1)
br label %12
10: ; preds = %0
%11 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i8* %4, i32 %6)
br label %12
12: ; preds = %10, %8
%13 = call i32 @strlen(i8* %3)
%14 = call i32 @write(i32 %6, i8* %3, i32 %13)
%15 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i32 %14, i8* %4)
%16 = call i32 @close(i32 %6)
ret i32 0
}