#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*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int main(void) {
  int fd = 0;
  char filename[] = "test.txt";
  int i = 0;
  for (i = 0; fd >= 0; i++) {
    fd = open(filename, O_RDONLY);
    if (fd > 0) {
      printf("fd:%d\n", fd);
      close(3);
    } else {
      printf("error,can't open file\n");
      exit(0);
    }
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
mov    0xea3(%rip),%rax
mov    %rax,-0x11(%rbp)
mov    0xea1(%rip),%al
mov    %al,-0x9(%rbp)
movl   $0x0,-0x18(%rbp)
movl   $0x0,-0x18(%rbp)
cmpl   $0x0,-0x8(%rbp)
jl     11e9 <main+0xa9>
lea    -0x11(%rbp),%rdi
mov    0x2e5b(%rip),%rax
mov    (%rax),%esi
call   1050 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jle    11c1 <main+0x81>
mov    -0x8(%rbp),%esi
lea    0xe5e(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x3,%edi
call   1040 <close@plt>
jmp    11d6 <main+0x96>
lea    0xe48(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
xor    %edi,%edi
call   1060 <exit@plt>
jmp    11db <main+0x9b>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1178 <main+0x38>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
movabs $0x7478742e74736574,%rax
74 78 74
mov    %rax,(%rsp)
movb   $0x0,0x8(%rsp)
mov    0x2e75(%rip),%r15
mov    (%r15),%esi
mov    %rsp,%rdi
call   1060 <open@plt>
test   %eax,%eax
jle    11b5 <main+0x65>
lea    0xe80(%rip),%r14
mov    %rsp,%rbx
nopl   0x0(%rax)
mov    %r14,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x3,%edi
call   1050 <close@plt>
mov    (%r15),%esi
mov    %rbx,%rdi
call   1060 <open@plt>
test   %eax,%eax
jg     1190 <main+0x40>
lea    0xe54(%rip),%rdi
call   1030 <puts@plt>
xor    %edi,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@__const.main.filename = [9 x i8] c"test.txt\00"
@O_RDONLY = global i32 0
@.str = [7 x i8] c"fd:%d\0A\00"
@.str.1 = [23 x i8] c"error,can't open file\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca [9 x i8]
%4 = alloca i32
store i32 0, i32* %1
store i32 0, i32* %2
%5 = bitcast [9 x i8]* %3 to i8*
store i32 0, i32* %4
store i32 0, i32* %4
br label %6
6: ; preds = %23, %0
%7 = load i32, i32* %2
%8 = icmp sge i32 %7, 0
br i1 %8, label %9, label %26
9: ; preds = %6
%10 = getelementptr inbounds [9 x i8], [9 x i8]* %3, i64 0, i64 0
%11 = load i32, i32* @O_RDONLY
%12 = call i32 @open(i8* %10, i32 %11)
store i32 %12, i32* %2
%13 = load i32, i32* %2
%14 = icmp sgt i32 %13, 0
br i1 %14, label %15, label %19
15: ; preds = %9
%16 = load i32, i32* %2
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 %16)
%18 = call i32 @close(i32 3)
br label %22
19: ; preds = %9
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
%21 = call i32 @exit(i32 0)
unreachable
22: ; preds = %15
br label %23
23: ; preds = %22
%24 = load i32, i32* %4
%25 = add nsw i32 %24, 1
store i32 %25, i32* %4
br label %6
26: ; preds = %6
%27 = load i32, i32* %1
ret i32 %27
}

LLVM IR (-O2):
@__const.main.filename = [9 x i8] c"test.txt\00"
@O_RDONLY = global i32 0
@.str = [7 x i8] c"fd:%d\0A\00"
@str = [22 x i8] c"error,can't open file\00"
define i32 @main() {
%1 = alloca [9 x i8]
%2 = getelementptr inbounds [9 x i8], [9 x i8]* %1, i64 0, i64 0
%3 = load i32, i32* @O_RDONLY
%4 = call i32 @open(i8* %2, i32 %3)
%5 = icmp sgt i32 %4, 0
br i1 %5, label %6, label %13
6: ; preds = %0, %6
%7 = phi i32 [ %11, %6 ], [ %4, %0 ]
%8 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0), i32 %7)
%9 = tail call i32 @close(i32 3)
%10 = load i32, i32* @O_RDONLY
%11 = call i32 @open(i8* %2, i32 %10)
%12 = icmp sgt i32 %11, 0
br i1 %12, label %6, label %13
13: ; preds = %6, %0
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str, i64 0, i64 0))
%15 = tail call i32 @exit(i32 0)
unreachable
}