#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(int argc, char *argv[]) {
  int fd = -1, i;
  ssize_t size = -1;
  int input = 0;
  char buf[] = "quick brown fox jumps over the lazy dog\n";
  char filename[] = "test.txt";
  fd = open(filename, O_RDWR);
  if (-1 == fd) {
    printf("Open file %s failure,fd:%d\n", filename, fd);
    close(fd);
    return -1;
  } 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    $0x70,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0xffffffff,-0x14(%rbp)
movl   $0xffffffff,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
lea    -0x50(%rbp),%rdi
lea    0xe6a(%rip),%rsi
mov    $0x29,%edx
call   1070 <memcpy@plt>
mov    0xe82(%rip),%rax
mov    %rax,-0x59(%rbp)
mov    0xe80(%rip),%al
mov    %al,-0x51(%rbp)
lea    -0x59(%rbp),%rdi
mov    0x2e29(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x14(%rbp)
mov    $0xffffffff,%eax
cmp    -0x14(%rbp),%eax
jne    1200 <main+0xa0>
lea    -0x59(%rbp),%rsi
mov    -0x14(%rbp),%edx
lea    0xe4d(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1261 <main+0x101>
lea    -0x59(%rbp),%rsi
mov    -0x14(%rbp),%edx
lea    0xe40(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x6c(%rbp)
lea    -0x50(%rbp),%rax
mov    %rax,-0x68(%rbp)
lea    -0x50(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x6c(%rbp),%edi
mov    -0x68(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%esi
lea    -0x59(%rbp),%rdx
lea    0xe1f(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x48,%rsp
movups 0xeb8(%rip),%xmm0
movups %xmm0,0x29(%rsp)
movaps 0xea3(%rip),%xmm0
movaps %xmm0,0x20(%rsp)
movaps 0xe87(%rip),%xmm0
movaps %xmm0,0x10(%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>
cmp    $0xffffffff,%eax
je     11ef <main+0x9f>
mov    %eax,%ebx
lea    0xe9e(%rip),%rdi
xor    %ebp,%ebp
mov    %rsp,%r14
mov    %r14,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1050 <printf@plt>
lea    0x10(%rsp),%r15
mov    %r15,%rdi
call   1040 <strlen@plt>
mov    %ebx,%edi
mov    %r15,%rsi
mov    %eax,%edx
call   1030 <write@plt>
lea    0xe89(%rip),%rdi
mov    %eax,%esi
mov    %r14,%rdx
xor    %eax,%eax
call   1050 <printf@plt>
jmp    120f <main+0xbf>
lea    0xe3c(%rip),%rdi
mov    %rsp,%rsi
mov    $0xffffffff,%ebx
mov    $0xffffffff,%edx
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0xffffffff,%ebp
mov    %ebx,%edi
call   1060 <close@plt>
mov    %ebp,%eax
add    $0x48,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@__const.main.buf = [41 x i8] c"quick brown fox jumps over the lazy dog\0A\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(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca [41 x i8]
%11 = alloca [9 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 -1, i32* %6
store i32 -1, i32* %8
store i32 0, i32* %9
%12 = bitcast [41 x i8]* %10 to i8*
%13 = bitcast [9 x i8]* %11 to i8*
%14 = getelementptr inbounds [9 x i8], [9 x i8]* %11, i64 0, i64 0
%15 = load i32, i32* @O_RDWR
%16 = call i32 @open(i8* %14, i32 %15)
store i32 %16, i32* %6
%17 = load i32, i32* %6
%18 = icmp eq i32 -1, %17
br i1 %18, label %19, label %25
19: ; preds = %2
%20 = getelementptr inbounds [9 x i8], [9 x i8]* %11, i64 0, i64 0
%21 = load i32, i32* %6
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i8* %20, i32 %21)
%23 = load i32, i32* %6
%24 = call i32 @close(i32 %23)
store i32 -1, i32* %3
br label %40
25: ; preds = %2
%26 = getelementptr inbounds [9 x i8], [9 x i8]* %11, i64 0, i64 0
%27 = load i32, i32* %6
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i8* %26, i32 %27)
br label %29
29: ; preds = %25
%30 = load i32, i32* %6
%31 = getelementptr inbounds [41 x i8], [41 x i8]* %10, i64 0, i64 0
%32 = getelementptr inbounds [41 x i8], [41 x i8]* %10, i64 0, i64 0
%33 = call i32 @strlen(i8* %32)
%34 = call i32 @write(i32 %30, i8* %31, i32 %33)
store i32 %34, i32* %8
%35 = load i32, i32* %8
%36 = getelementptr inbounds [9 x i8], [9 x i8]* %11, i64 0, i64 0
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i32 %35, i8* %36)
%38 = load i32, i32* %6
%39 = call i32 @close(i32 %38)
store i32 0, i32* %3
br label %40
40: ; preds = %29, %19
%41 = load i32, i32* %3
ret i32 %41
}

LLVM IR (-O2):
@__const.main.buf = [41 x i8] c"quick brown fox jumps over the lazy dog\0A\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(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [41 x i8]
%4 = alloca [9 x i8]
%5 = getelementptr inbounds [41 x i8], [41 x i8]* %3, i64 0, i64 0
%6 = getelementptr inbounds [9 x i8], [9 x i8]* %4, i64 0, i64 0
%7 = load i32, i32* @O_RDWR
%8 = call i32 @open(i8* %6, i32 %7)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %12
10: ; preds = %2
%11 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0), i8* %6, i32 -1)
br label %17
12: ; preds = %2
%13 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i8* %6, i32 %8)
%14 = call i32 @strlen(i8* %5)
%15 = call i32 @write(i32 %8, i8* %5, i32 %14)
%16 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0), i32 %15, i8* %6)
br label %17
17: ; preds = %12, %10
%18 = phi i32 [ %8, %12 ], [ -1, %10 ]
%19 = phi i32 [ 0, %12 ], [ -1, %10 ]
%20 = call i32 @close(i32 %18)
ret i32 %19
}