#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_RDWR;
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
void wpa_supplicant_fd_workaround(void) {
  int s, i;
  for (i = 0; i < 3; i++) {
    s = open("/dev/null", O_RDWR);
    if (s > 2) {
      close(s);
      break;
    }
  }
}


x86 assembly (clang, -O0):
<wpa_supplicant_fd_workaround>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x8(%rbp)
cmpl   $0x3,-0x8(%rbp)
jge    117b <wpa_supplicant_fd_workaround+0x5b>
mov    0x2ea8(%rip),%rax
mov    (%rax),%esi
lea    0xeb7(%rip),%rdi
call   1040 <open@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0x2,-0x4(%rbp)
jle    1168 <wpa_supplicant_fd_workaround+0x48>
mov    -0x4(%rbp),%edi
call   1030 <close@plt>
jmp    117b <wpa_supplicant_fd_workaround+0x5b>
jmp    116d <wpa_supplicant_fd_workaround+0x4d>
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    112f <wpa_supplicant_fd_workaround+0xf>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<wpa_supplicant_fd_workaround>:
push   %rbx
mov    0x2ec0(%rip),%rbx
mov    (%rbx),%esi
lea    0xecf(%rip),%rdi
call   1040 <open@plt>
cmp    $0x2,%eax
jg     1161 <wpa_supplicant_fd_workaround+0x41>
mov    (%rbx),%esi
lea    0xebc(%rip),%rdi
call   1040 <open@plt>
cmp    $0x2,%eax
jg     1161 <wpa_supplicant_fd_workaround+0x41>
mov    (%rbx),%esi
lea    0xea9(%rip),%rdi
call   1040 <open@plt>
cmp    $0x3,%eax
jl     1169 <wpa_supplicant_fd_workaround+0x49>
mov    %eax,%edi
pop    %rbx
jmp    1030 <close@plt>
pop    %rbx
ret

LLVM IR (-O0):
@.str = [10 x i8] c"/dev/null\00"
@O_RDWR = global i32 0
define void @wpa_supplicant_fd_workaround() {
%1 = alloca i32
%2 = alloca i32
store i32 0, i32* %2
br label %3
3: ; preds = %15, %0
%4 = load i32, i32* %2
%5 = icmp slt i32 %4, 3
br i1 %5, label %6, label %18
6: ; preds = %3
%7 = load i32, i32* @O_RDWR
%8 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %7)
store i32 %8, i32* %1
%9 = load i32, i32* %1
%10 = icmp sgt i32 %9, 2
br i1 %10, label %11, label %14
11: ; preds = %6
%12 = load i32, i32* %1
%13 = call i32 @close(i32 %12)
br label %18
14: ; preds = %6
br label %15
15: ; preds = %14
%16 = load i32, i32* %2
%17 = add nsw i32 %16, 1
store i32 %17, i32* %2
br label %3
18: ; preds = %11, %3
ret void
}

LLVM IR (-O2):
@.str = [10 x i8] c"/dev/null\00"
@O_RDWR = global i32 0
define void @wpa_supplicant_fd_workaround() {
%1 = load i32, i32* @O_RDWR
%2 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %1)
%3 = icmp sgt i32 %2, 2
br i1 %3, label %12, label %4
4: ; preds = %0
%5 = load i32, i32* @O_RDWR
%6 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = icmp sgt i32 %6, 2
br i1 %7, label %12, label %8
8: ; preds = %4
%9 = load i32, i32* @O_RDWR
%10 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %9)
%11 = icmp sgt i32 %10, 2
br i1 %11, label %12, label %15
12: ; preds = %8, %4, %0
%13 = phi i32 [ %2, %0 ], [ %6, %4 ], [ %10, %8 ]
%14 = tail call i32 @close(i32 %13)
br label %15
15: ; preds = %8, %12
ret void
}