#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 read(int, char *, int);
int write(int, char *, int);
int main(int argc, char *argv[]) {
  int fd, wr_ret, rd_ret;
  char buf[] = "linux";
  char read_buf[120] = {0};
  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);
  rd_ret = read(fd, read_buf, 128);
  printf("read_buf=%d\n", rd_ret);
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xa0,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0xe81(%rip),%eax
mov    %eax,-0x22(%rbp)
mov    0xe7b(%rip),%ax
mov    %ax,-0x1e(%rbp)
lea    -0xa0(%rbp),%rdi
xor    %esi,%esi
mov    $0x78,%edx
call   1050 <memset@plt>
mov    0x2e39(%rip),%rax
mov    (%rax),%esi
mov    0x2e48(%rip),%rax
or     (%rax),%esi
lea    0xe4d(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11e5 <main+0x85>
lea    0xe3a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1252 <main+0xf2>
mov    -0x14(%rbp),%esi
lea    0xe36(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x14(%rbp),%edi
lea    -0x22(%rbp),%rsi
mov    $0x6,%edx
call   1030 <write@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%esi
lea    0xe31(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x14(%rbp),%edi
lea    -0xa0(%rbp),%rsi
mov    $0x80,%edx
call   1070 <read@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%esi
lea    0xe14(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xa0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
sub    $0x88,%rsp
movw   $0x78,0xc(%rsp)
movl   $0x756e696c,0x8(%rsp)
75
xorps  %xmm0,%xmm0
movaps %xmm0,0x70(%rsp)
movaps %xmm0,0x60(%rsp)
movaps %xmm0,0x50(%rsp)
movaps %xmm0,0x40(%rsp)
movaps %xmm0,0x30(%rsp)
movaps %xmm0,0x20(%rsp)
movaps %xmm0,0x10(%rsp)
movq   $0x0,0x80(%rsp)
mov    0x2e2f(%rip),%rax
mov    0x2e40(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe43(%rip),%rdi
call   1080 <open@plt>
test   %eax,%eax
js     122b <main+0xcb>
mov    %eax,%ebp
lea    0xe37(%rip),%rdi
xor    %ebx,%ebx
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0x8(%rsp),%rsi
mov    %ebp,%edi
mov    $0x6,%edx
call   1040 <write@plt>
lea    0xe34(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0x10(%rsp),%rsi
mov    %ebp,%edi
mov    $0x80,%edx
call   1070 <read@plt>
lea    0xe1e(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %ebp,%edi
call   1060 <close@plt>
jmp    123c <main+0xdc>
lea    0xe12(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x88,%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"
@.str.4 = [13 x i8] c"read_buf=%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 i32
%9 = alloca [6 x i8]
%10 = alloca [120 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = bitcast [6 x i8]* %9 to i8*
%12 = bitcast [120 x i8]* %10 to i8*
%13 = load i32, i32* @O_RDWR
%14 = load i32, i32* @O_TRUNC
%15 = or i32 %13, %14
%16 = call i32 @open(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 %15)
store i32 %16, i32* %6
%17 = load i32, i32* %6
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %2
%20 = 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 %36
21: ; preds = %2
%22 = load i32, i32* %6
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), i32 %22)
%24 = load i32, i32* %6
%25 = getelementptr inbounds [6 x i8], [6 x i8]* %9, i64 0, i64 0
%26 = call i32 @write(i32 %24, i8* %25, i32 6)
store i32 %26, i32* %7
%27 = load i32, i32* %7
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i32 %27)
%29 = load i32, i32* %6
%30 = getelementptr inbounds [120 x i8], [120 x i8]* %10, i64 0, i64 0
%31 = call i32 @read(i32 %29, i8* %30, i32 128)
store i32 %31, i32* %8
%32 = load i32, i32* %8
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0), i32 %32)
%34 = load i32, i32* %6
%35 = call i32 @close(i32 %34)
store i32 0, i32* %3
br label %36
36: ; preds = %21, %19
%37 = load i32, i32* %3
ret i32 %37
}

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.4 = [13 x i8] c"read_buf=%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 = alloca [120 x i8]
%5 = getelementptr inbounds [6 x i8], [6 x i8]* %3, i64 0, i64 0
%6 = getelementptr inbounds [120 x i8], [120 x i8]* %4, i64 0, i64 0
%7 = load i32, i32* @O_RDWR
%8 = load i32, i32* @O_TRUNC
%9 = or i32 %8, %7
%10 = tail call i32 @open(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 %9)
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %2
%13 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str, i64 0, i64 0))
br label %21
14: ; preds = %2
%15 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), i32 %10)
%16 = call i32 @write(i32 %10, i8* %5, i32 6)
%17 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i32 %16)
%18 = call i32 @read(i32 %10, i8* %6, i32 128)
%19 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0), i32 %18)
%20 = tail call i32 @close(i32 %10)
br label %21
21: ; preds = %14, %12
%22 = phi i32 [ -1, %12 ], [ 0, %14 ]
ret i32 %22
}