#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 O_CREAT;
int O_RDWR;
int /*<<< orphan*/ SEEK_SET;
int S_IRGRP;
int S_IROTH;
int S_IRUSR;
int S_IWGRP;
int S_IWUSR;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ lseek(int, int, int /*<<< orphan*/);
int open(char const *, int, int);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ read(int, char *, int);
int /*<<< orphan*/ write(int, char *, int);
int main(int argc, char *argv[]) {
  const char *file = "test.tmp";
  char buf[4] = "zzz\n";
  ssize_t bytes_read = -1;
  int i = 0;
  int fd = open(file, O_RDWR | O_CREAT,
                S_IWUSR | S_IRUSR | S_IWGRP | S_IRGRP | S_IROTH);
  if (fd != -1) {
    write(fd, "abcdef", 7);
    lseek(fd, 1, SEEK_SET);
    read(fd, buf, 3);
    close(fd);
  }
  printf("Result: %s\n", buf);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe83(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    0xe95(%rip),%eax
mov    %eax,-0x1c(%rbp)
movl   $0xffffffff,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x18(%rbp),%rdi
mov    0x2e3d(%rip),%rax
mov    (%rax),%esi
mov    0x2e4c(%rip),%rax
or     (%rax),%esi
mov    0x2e1b(%rip),%rax
mov    (%rax),%edx
mov    0x2dea(%rip),%rax
or     (%rax),%edx
mov    0x2e11(%rip),%rax
or     (%rax),%edx
mov    0x2de8(%rip),%rax
or     (%rax),%edx
mov    0x2dd7(%rip),%rax
or     (%rax),%edx
call   1080 <open@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0xffffffff,-0x28(%rbp)
je     1230 <main+0xd0>
mov    -0x28(%rbp),%edi
lea    0xe12(%rip),%rsi
mov    $0x7,%edx
call   1030 <write@plt>
mov    -0x28(%rbp),%edi
mov    0x2db5(%rip),%rax
mov    (%rax),%edx
mov    $0x1,%esi
call   1050 <lseek@plt>
mov    -0x28(%rbp),%edi
lea    -0x1c(%rbp),%rsi
mov    $0x3,%edx
call   1070 <read@plt>
mov    -0x28(%rbp),%edi
call   1060 <close@plt>
lea    -0x1c(%rbp),%rsi
lea    0xdd5(%rip),%rdi
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
mov    0x2e74(%rip),%rax
mov    0x2e85(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e52(%rip),%rax
mov    0x2e23(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e48(%rip),%rax
or     (%rax),%edx
mov    0x2e1f(%rip),%rax
or     (%rax),%edx
movl   $0xa7a7a7a,0xc(%rsp)
0a
mov    0x2e06(%rip),%rax
or     (%rax),%edx
lea    0xe4d(%rip),%rdi
call   1080 <open@plt>
cmp    $0xffffffff,%eax
je     11ff <main+0x9f>
mov    %eax,%ebx
lea    0xe43(%rip),%rsi
mov    %eax,%edi
mov    $0x7,%edx
call   1030 <write@plt>
mov    0x2de7(%rip),%rax
mov    (%rax),%edx
mov    %ebx,%edi
mov    $0x1,%esi
call   1050 <lseek@plt>
lea    0xc(%rsp),%rsi
mov    %ebx,%edi
mov    $0x3,%edx
call   1070 <read@plt>
mov    %ebx,%edi
call   1060 <close@plt>
lea    0xe0a(%rip),%rdi
lea    0xc(%rsp),%rsi
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [9 x i8] c"test.tmp\00"
@__const.main.buf = [4 x i8] c"zzz\0A"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@S_IWUSR = global i32 0
@S_IRUSR = global i32 0
@S_IWGRP = global i32 0
@S_IRGRP = global i32 0
@S_IROTH = global i32 0
@.str.1 = [7 x i8] c"abcdef\00"
@SEEK_SET = global i32 0
@.str.2 = [12 x i8] c"Result: %s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca [4 x i8]
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8** %6
%11 = bitcast [4 x i8]* %7 to i8*
store i32 -1, i32* %8
store i32 0, i32* %9
%12 = load i8*, i8** %6
%13 = load i32, i32* @O_RDWR
%14 = load i32, i32* @O_CREAT
%15 = or i32 %13, %14
%16 = load i32, i32* @S_IWUSR
%17 = load i32, i32* @S_IRUSR
%18 = or i32 %16, %17
%19 = load i32, i32* @S_IWGRP
%20 = or i32 %18, %19
%21 = load i32, i32* @S_IRGRP
%22 = or i32 %20, %21
%23 = load i32, i32* @S_IROTH
%24 = or i32 %22, %23
%25 = call i32 @open(i8* %12, i32 %15, i32 %24)
store i32 %25, i32* %10
%26 = load i32, i32* %10
%27 = icmp ne i32 %26, -1
br i1 %27, label %28, label %39
28: ; preds = %2
%29 = load i32, i32* %10
%30 = call i32 @write(i32 %29, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32 7)
%31 = load i32, i32* %10
%32 = load i32, i32* @SEEK_SET
%33 = call i32 @lseek(i32 %31, i32 1, i32 %32)
%34 = load i32, i32* %10
%35 = getelementptr inbounds [4 x i8], [4 x i8]* %7, i64 0, i64 0
%36 = call i32 @read(i32 %34, i8* %35, i32 3)
%37 = load i32, i32* %10
%38 = call i32 @close(i32 %37)
br label %39
39: ; preds = %28, %2
%40 = getelementptr inbounds [4 x i8], [4 x i8]* %7, i64 0, i64 0
%41 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i8* %40)
ret i32 0
}

LLVM IR (-O2):
@.str = [9 x i8] c"test.tmp\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@S_IWUSR = global i32 0
@S_IRUSR = global i32 0
@S_IWGRP = global i32 0
@S_IRGRP = global i32 0
@S_IROTH = global i32 0
@.str.1 = [7 x i8] c"abcdef\00"
@SEEK_SET = global i32 0
@.str.2 = [12 x i8] c"Result: %s\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
store i32 175798906, i32* %3
%5 = load i32, i32* @O_RDWR
%6 = load i32, i32* @O_CREAT
%7 = or i32 %6, %5
%8 = load i32, i32* @S_IWUSR
%9 = load i32, i32* @S_IRUSR
%10 = or i32 %9, %8
%11 = load i32, i32* @S_IWGRP
%12 = or i32 %10, %11
%13 = load i32, i32* @S_IRGRP
%14 = or i32 %12, %13
%15 = load i32, i32* @S_IROTH
%16 = or i32 %14, %15
%17 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %7, i32 %16)
%18 = icmp eq i32 %17, -1
br i1 %18, label %25, label %19
19: ; preds = %2
%20 = tail call i32 @write(i32 %17, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32 7)
%21 = load i32, i32* @SEEK_SET
%22 = tail call i32 @lseek(i32 %17, i32 1, i32 %21)
%23 = call i32 @read(i32 %17, i8* %4, i32 3)
%24 = tail call i32 @close(i32 %17)
br label %25
25: ; preds = %19, %2
%26 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i8* %4)
ret i32 0
}