#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*/ SEEK_CUR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ lseek(int, int, int /*<<< orphan*/);
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);
  lseek(fd, -3, SEEK_CUR);
  rd_ret = read(fd, read_buf, 128);
  printf("read_buf=%s\n", read_buf);
  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    0xe71(%rip),%eax
mov    %eax,-0x22(%rbp)
mov    0xe6b(%rip),%ax
mov    %ax,-0x1e(%rbp)
lea    -0xa0(%rbp),%rdi
xor    %esi,%esi
mov    $0x78,%edx
call   1060 <memset@plt>
mov    0x2e29(%rip),%rax
mov    (%rax),%esi
mov    0x2e38(%rip),%rax
or     (%rax),%esi
lea    0xe3d(%rip),%rdi
call   1090 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11f5 <main+0x85>
lea    0xe2a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    127c <main+0x10c>
mov    -0x14(%rbp),%esi
lea    0xe26(%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    0xe21(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x14(%rbp),%edi
mov    0x2da3(%rip),%rax
mov    (%rax),%edx
mov    $0xfffffffd,%esi
call   1050 <lseek@plt>
mov    -0x14(%rbp),%edi
lea    -0xa0(%rbp),%rsi
mov    $0x80,%edx
call   1080 <read@plt>
mov    %eax,-0x1c(%rbp)
lea    -0xa0(%rbp),%rsi
lea    0xdea(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xa0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x90,%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    0x2e1d(%rip),%rax
mov    0x2e2e(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe31(%rip),%rdi
call   1090 <open@plt>
test   %eax,%eax
js     1257 <main+0xe7>
mov    %eax,%ebp
lea    0xe25(%rip),%rdi
xor    %r14d,%r14d
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    0xe21(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2dbd(%rip),%rax
mov    (%rax),%edx
mov    %ebp,%edi
mov    $0xfffffffd,%esi
call   1060 <lseek@plt>
lea    0x10(%rsp),%rbx
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x80,%edx
call   1080 <read@plt>
lea    0xdf3(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %ebp,%edi
call   1070 <close@plt>
jmp    1269 <main+0xf9>
lea    0xde6(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
add    $0x90,%rsp
pop    %rbx
pop    %r14
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"
@SEEK_CUR = global i32 0
@.str.4 = [13 x i8] c"read_buf=%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 [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 %39
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 = load i32, i32* @SEEK_CUR
%31 = call i32 @lseek(i32 %29, i32 -3, i32 %30)
%32 = load i32, i32* %6
%33 = getelementptr inbounds [120 x i8], [120 x i8]* %10, i64 0, i64 0
%34 = call i32 @read(i32 %32, i8* %33, i32 128)
store i32 %34, i32* %8
%35 = getelementptr inbounds [120 x i8], [120 x i8]* %10, i64 0, i64 0
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0), i8* %35)
%37 = load i32, i32* %6
%38 = call i32 @close(i32 %37)
store i32 0, i32* %3
br label %39
39: ; preds = %21, %19
%40 = load i32, i32* %3
ret i32 %40
}

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"
@SEEK_CUR = global i32 0
@.str.4 = [13 x i8] c"read_buf=%s\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 %23
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 = load i32, i32* @SEEK_CUR
%19 = tail call i32 @lseek(i32 %10, i32 -3, i32 %18)
%20 = call i32 @read(i32 %10, i8* %6, i32 128)
%21 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0), i8* %6)
%22 = call i32 @close(i32 %10)
br label %23
23: ; preds = %14, %12
%24 = phi i32 [ -1, %12 ], [ 0, %14 ]
ret i32 %24
}