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

/* Variables and functions */
int /*<<< orphan*/ F_GETFL;
int /*<<< orphan*/ F_SETFL;
long O_APPEND;
int O_CREAT;
int O_WRONLY;
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int, int /*<<< orphan*/);
scalar_t__ fcntl(int, int /*<<< orphan*/, ...);
int open(char *, int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ system(char *);
int foo1(void) {
  printf("this is fcntl demo\n");
  int fd = open("1.txt", O_WRONLY | O_CREAT, 0666);
  if (fd < 0) {
    perror("open");
    goto err;
  }
  long flag = 0;
  if ((flag = fcntl(fd, F_GETFL)) < 0) {
    perror("fcnt;");
    goto err;
  }
  printf("flag = %x\n", flag);
  flag |= O_APPEND;
  printf("O_APPEND flag = %x\n", flag);
  if (fcntl(fd, F_SETFL, flag) < 0) {
    perror("fcntl");
    goto err;
  }
  dup2(fd, STDOUT_FILENO);
  int i;
  for (i = 0; i < 10; ++i) {
    system("date");
    sleep(1);
  }
  close(fd);
  return 0;
err:
  return -1;
}


x86 assembly (clang, -O0):
<foo1>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
lea    0xe71(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    0x2e1b(%rip),%rax
mov    (%rax),%esi
mov    0x2e52(%rip),%rax
or     (%rax),%esi
lea    0xe65(%rip),%rdi
mov    $0x1b6,%edx
call   1080 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    11d7 <foo1+0x57>
lea    0xe4d(%rip),%rdi
call   1090 <perror@plt>
jmp    12d3 <foo1+0x153>
movq   $0x0,-0x10(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2ddf(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1030 <fcntl@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jge    1211 <foo1+0x91>
lea    0xe18(%rip),%rdi
call   1090 <perror@plt>
jmp    12d3 <foo1+0x153>
mov    -0x10(%rbp),%rsi
lea    0xe12(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    0x2d86(%rip),%rax
mov    (%rax),%rax
or     -0x10(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rsi
lea    0xde5(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x8(%rbp),%edi
mov    0x2d7f(%rip),%rax
mov    (%rax),%esi
mov    -0x10(%rbp),%rdx
mov    $0x0,%al
call   1030 <fcntl@plt>
cmp    $0x0,%rax
jge    1279 <foo1+0xf9>
lea    0xdca(%rip),%rdi
call   1090 <perror@plt>
jmp    12d3 <foo1+0x153>
mov    -0x8(%rbp),%edi
mov    0x2d65(%rip),%rax
mov    (%rax),%esi
call   1050 <dup2@plt>
movl   $0x0,-0x14(%rbp)
cmpl   $0xa,-0x14(%rbp)
jge    12bf <foo1+0x13f>
lea    0xd9d(%rip),%rdi
call   1040 <system@plt>
mov    $0x1,%edi
call   10a0 <sleep@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1291 <foo1+0x111>
mov    -0x8(%rbp),%edi
call   1070 <close@plt>
movl   $0x0,-0x4(%rbp)
jmp    12da <foo1+0x15a>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<foo1>:
push   %rbp
push   %r14
push   %rbx
lea    0xe95(%rip),%rdi
call   1030 <puts@plt>
mov    0x2e11(%rip),%rax
mov    0x2e4a(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe47(%rip),%rdi
mov    $0x1b6,%edx
call   1090 <open@plt>
test   %eax,%eax
js     1312 <foo1+0x182>
mov    %eax,%ebx
mov    0x2df4(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
xor    %eax,%eax
call   1040 <fcntl@plt>
test   %rax,%rax
js     131b <foo1+0x18b>
mov    %rax,%rbp
lea    0xe28(%rip),%rdi
xor    %r14d,%r14d
mov    %rax,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
mov    0x2daa(%rip),%rax
or     (%rax),%rbp
lea    0xe01(%rip),%rdi
mov    %rbp,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
mov    0x2daf(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %rbp,%rdx
xor    %eax,%eax
call   1040 <fcntl@plt>
test   %rax,%rax
js     1324 <foo1+0x194>
mov    0x2da9(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
call   1060 <dup2@plt>
lea    0xddc(%rip),%rbp
mov    %rbp,%rdi
call   1050 <system@plt>
mov    $0x1,%edi
call   10b0 <sleep@plt>
mov    %rbp,%rdi
call   1050 <system@plt>
mov    $0x1,%edi
call   10b0 <sleep@plt>
mov    %rbp,%rdi
call   1050 <system@plt>
mov    $0x1,%edi
call   10b0 <sleep@plt>
mov    %rbp,%rdi
call   1050 <system@plt>
mov    $0x1,%edi
call   10b0 <sleep@plt>
mov    %rbp,%rdi
call   1050 <system@plt>
mov    $0x1,%edi
call   10b0 <sleep@plt>
mov    %rbp,%rdi
call   1050 <system@plt>
mov    $0x1,%edi
call   10b0 <sleep@plt>
mov    %rbp,%rdi
call   1050 <system@plt>
mov    $0x1,%edi
call   10b0 <sleep@plt>
mov    %rbp,%rdi
call   1050 <system@plt>
mov    $0x1,%edi
call   10b0 <sleep@plt>
mov    %rbp,%rdi
call   1050 <system@plt>
mov    $0x1,%edi
call   10b0 <sleep@plt>
mov    %rbp,%rdi
call   1050 <system@plt>
mov    $0x1,%edi
call   10b0 <sleep@plt>
mov    %ebx,%edi
call   1080 <close@plt>
mov    %r14d,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xced(%rip),%rdi
jmp    132b <foo1+0x19b>
lea    0xce9(%rip),%rdi
jmp    132b <foo1+0x19b>
lea    0xcfa(%rip),%rdi
call   10a0 <perror@plt>
mov    $0xffffffff,%r14d
jmp    130a <foo1+0x17a>

LLVM IR (-O0):
@.str = [20 x i8] c"this is fcntl demo\0A\00"
@.str.1 = [6 x i8] c"1.txt\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@.str.2 = [5 x i8] c"open\00"
@F_GETFL = global i32 0
@.str.3 = [6 x i8] c"fcnt;\00"
@.str.4 = [11 x i8] c"flag = %x\0A\00"
@O_APPEND = global i64 0
@.str.5 = [20 x i8] c"O_APPEND flag = %x\0A\00"
@F_SETFL = global i32 0
@.str.6 = [6 x i8] c"fcntl\00"
@STDOUT_FILENO = global i32 0
@.str.7 = [5 x i8] c"date\00"
define i32 @foo1() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i64
%4 = alloca i32
%5 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
%6 = load i32, i32* @O_WRONLY
%7 = load i32, i32* @O_CREAT
%8 = or i32 %6, %7
%9 = call i32 @open(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 %8, i32 438)
store i32 %9, i32* %2
%10 = load i32, i32* %2
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %14
12: ; preds = %0
%13 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
br label %52
14: ; preds = %0
store i64 0, i64* %3
%15 = load i32, i32* %2
%16 = load i32, i32* @F_GETFL
%17 = call i64 (i32, i32, ...) @fcntl(i32 %15, i32 %16)
store i64 %17, i64* %3
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %14
%20 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
br label %52
21: ; preds = %14
%22 = load i64, i64* %3
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0), i64 %22)
%24 = load i64, i64* @O_APPEND
%25 = load i64, i64* %3
%26 = or i64 %25, %24
store i64 %26, i64* %3
%27 = load i64, i64* %3
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0), i64 %27)
%29 = load i32, i32* %2
%30 = load i32, i32* @F_SETFL
%31 = load i64, i64* %3
%32 = call i64 (i32, i32, ...) @fcntl(i32 %29, i32 %30, i64 %31)
%33 = icmp slt i64 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %21
%35 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i64 0, i64 0))
br label %52
36: ; preds = %21
%37 = load i32, i32* %2
%38 = load i32, i32* @STDOUT_FILENO
%39 = call i32 @dup2(i32 %37, i32 %38)
store i32 0, i32* %4
br label %40
40: ; preds = %46, %36
%41 = load i32, i32* %4
%42 = icmp slt i32 %41, 10
br i1 %42, label %43, label %49
43: ; preds = %40
%44 = call i32 @system(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.7, i64 0, i64 0))
%45 = call i32 @sleep(i32 1)
br label %46
46: ; preds = %43
%47 = load i32, i32* %4
%48 = add nsw i32 %47, 1
store i32 %48, i32* %4
br label %40
49: ; preds = %40
%50 = load i32, i32* %2
%51 = call i32 @close(i32 %50)
store i32 0, i32* %1
br label %53
52: ; preds = %34, %19, %12
store i32 -1, i32* %1
br label %53
53: ; preds = %52, %49
%54 = load i32, i32* %1
ret i32 %54
}

LLVM IR (-O2):
@.str.1 = [6 x i8] c"1.txt\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@.str.2 = [5 x i8] c"open\00"
@F_GETFL = global i32 0
@.str.3 = [6 x i8] c"fcnt;\00"
@.str.4 = [11 x i8] c"flag = %x\0A\00"
@O_APPEND = global i64 0
@.str.5 = [20 x i8] c"O_APPEND flag = %x\0A\00"
@F_SETFL = global i32 0
@.str.6 = [6 x i8] c"fcntl\00"
@STDOUT_FILENO = global i32 0
@.str.7 = [5 x i8] c"date\00"
@str = [19 x i8] c"this is fcntl demo\00"
define i32 @foo1() {
%1 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str, i64 0, i64 0))
%2 = load i32, i32* @O_WRONLY
%3 = load i32, i32* @O_CREAT
%4 = or i32 %3, %2
%5 = tail call i32 @open(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 %4, i32 438)
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %0
%8 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
br label %49
9: ; preds = %0
%10 = load i32, i32* @F_GETFL
%11 = tail call i64 (i32, i32, ...) @fcntl(i32 %5, i32 %10)
%12 = icmp slt i64 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %9
%14 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
br label %49
15: ; preds = %9
%16 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0), i64 %11)
%17 = load i64, i64* @O_APPEND
%18 = or i64 %17, %11
%19 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0), i64 %18)
%20 = load i32, i32* @F_SETFL
%21 = tail call i64 (i32, i32, ...) @fcntl(i32 %5, i32 %20, i64 %18)
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %15
%24 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i64 0, i64 0))
br label %49
25: ; preds = %15
%26 = load i32, i32* @STDOUT_FILENO
%27 = tail call i32 @dup2(i32 %5, i32 %26)
%28 = tail call i32 @system(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.7, i64 0, i64 0))
%29 = tail call i32 @sleep(i32 1)
%30 = tail call i32 @system(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.7, i64 0, i64 0))
%31 = tail call i32 @sleep(i32 1)
%32 = tail call i32 @system(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.7, i64 0, i64 0))
%33 = tail call i32 @sleep(i32 1)
%34 = tail call i32 @system(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.7, i64 0, i64 0))
%35 = tail call i32 @sleep(i32 1)
%36 = tail call i32 @system(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.7, i64 0, i64 0))
%37 = tail call i32 @sleep(i32 1)
%38 = tail call i32 @system(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.7, i64 0, i64 0))
%39 = tail call i32 @sleep(i32 1)
%40 = tail call i32 @system(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.7, i64 0, i64 0))
%41 = tail call i32 @sleep(i32 1)
%42 = tail call i32 @system(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.7, i64 0, i64 0))
%43 = tail call i32 @sleep(i32 1)
%44 = tail call i32 @system(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.7, i64 0, i64 0))
%45 = tail call i32 @sleep(i32 1)
%46 = tail call i32 @system(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.7, i64 0, i64 0))
%47 = tail call i32 @sleep(i32 1)
%48 = tail call i32 @close(i32 %5)
br label %49
49: ; preds = %7, %13, %23, %25
%50 = phi i32 [ 0, %25 ], [ -1, %23 ], [ -1, %13 ], [ -1, %7 ]
ret i32 %50
}