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

/* Variables and functions */
int O_RDWR;
int O_TRUNC;
int /*<<< orphan*/ close(int);
int open(char *, int);
int /*<<< orphan*/ printf(char *, ...);
int write(int, char *, int);
int main(int argc, char *argv[]) {
  int fd, wr_ret;
  char buf[] = "linux";
  fd = open("./a.c", O_RDWR | O_TRUNC);
  if (fd < 0) {
    printf("open file ./a.c failure\n");
    return -1;
  }
  printf("open file ./a.c success, fd=%d\n", fd);
  wr_ret = write(fd, buf, sizeof(buf));
  printf("wr_ret=%d\n", wr_ret);
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0xea4(%rip),%eax
mov    %eax,-0x1e(%rbp)
mov    0xe9e(%rip),%ax
mov    %ax,-0x1a(%rbp)
mov    0x2e6f(%rip),%rax
mov    (%rax),%esi
mov    0x2e7e(%rip),%rax
or     (%rax),%esi
lea    0xe83(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11af <main+0x6f>
lea    0xe70(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11f4 <main+0xb4>
mov    -0x14(%rbp),%esi
lea    0xe6c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x14(%rbp),%edi
lea    -0x1e(%rbp),%rsi
mov    $0x6,%edx
call   1030 <write@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%esi
lea    0xe67(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
movw   $0x78,0x4(%rsp)
movl   $0x756e696c,(%rsp)
mov    0x2e78(%rip),%rax
mov    0x2e89(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe8c(%rip),%rdi
call   1070 <open@plt>
test   %eax,%eax
js     11bf <main+0x6f>
mov    %eax,%ebp
lea    0xe80(%rip),%rdi
xor    %ebx,%ebx
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rsp,%rsi
mov    %ebp,%edi
mov    $0x6,%edx
call   1040 <write@plt>
lea    0xe7f(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %ebp,%edi
call   1060 <close@plt>
jmp    11d0 <main+0x80>
lea    0xe71(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@__const.main.buf = [6 x i8] c"linux\00"
@.str = [6 x i8] c"./a.c\00"
@O_RDWR = global i32 0
@O_TRUNC = global i32 0
@.str.1 = [25 x i8] c"open file ./a.c failure\0A\00"
@.str.2 = [32 x i8] c"open file ./a.c success, fd=%d\0A\00"
@.str.3 = [11 x i8] c"wr_ret=%d\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 [6 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = bitcast [6 x i8]* %8 to i8*
%10 = load i32, i32* @O_RDWR
%11 = load i32, i32* @O_TRUNC
%12 = or i32 %10, %11
%13 = call i32 @open(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 %12)
store i32 %13, i32* %6
%14 = load i32, i32* %6
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %2
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %3
br label %28
18: ; preds = %2
%19 = load i32, i32* %6
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), i32 %19)
%21 = load i32, i32* %6
%22 = getelementptr inbounds [6 x i8], [6 x i8]* %8, i64 0, i64 0
%23 = call i32 @write(i32 %21, i8* %22, i32 6)
store i32 %23, i32* %7
%24 = load i32, i32* %7
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i32 %24)
%26 = load i32, i32* %6
%27 = call i32 @close(i32 %26)
store i32 0, i32* %3
br label %28
28: ; preds = %18, %16
%29 = load i32, i32* %3
ret i32 %29
}

LLVM IR (-O2):
@__const.main.buf = [6 x i8] c"linux\00"
@.str = [6 x i8] c"./a.c\00"
@O_RDWR = global i32 0
@O_TRUNC = global i32 0
@.str.2 = [32 x i8] c"open file ./a.c success, fd=%d\0A\00"
@.str.3 = [11 x i8] c"wr_ret=%d\0A\00"
@str = [24 x i8] c"open file ./a.c failure\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [6 x i8]
%4 = getelementptr inbounds [6 x i8], [6 x i8]* %3, i64 0, i64 0
%5 = load i32, i32* @O_RDWR
%6 = load i32, i32* @O_TRUNC
%7 = or i32 %6, %5
%8 = tail call i32 @open(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 %7)
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %2
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str, i64 0, i64 0))
br label %17
12: ; preds = %2
%13 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), i32 %8)
%14 = call i32 @write(i32 %8, i8* %4, i32 6)
%15 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i32 %14)
%16 = tail call i32 @close(i32 %8)
br label %17
17: ; preds = %12, %10
%18 = phi i32 [ -1, %10 ], [ 0, %12 ]
ret i32 %18
}