#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 */
scalar_t__ EINTR;
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ FD_CLOEXEC;
int /*<<< orphan*/ F_SETFD;
int dup(int const);
scalar_t__ errno;
int /*<<< orphan*/ error(int /*<<< orphan*/, scalar_t__, char *, int const);
int fcntl(int, int /*<<< orphan*/, int /*<<< orphan*/);
void m_file_dup(int *const dst_fd, const int src_fd) {
  for (;;) {
    *dst_fd = dup(src_fd);
    if (*dst_fd != -1) {
      if (fcntl(*dst_fd, F_SETFD, FD_CLOEXEC) == -1) {
        error(EXIT_FAILURE, errno, "fcntl(fd=%d, F_SETFD, FD_CLOEXEC)",
              *dst_fd);
      }
      return;
    }
    if (errno != EINTR) {
      error(EXIT_FAILURE, errno, "dup(fd=%d)", src_fd);
    }
  }
}


x86 assembly (clang, -O0):
<m_file_dup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0xc(%rbp),%edi
call   1040 <dup@plt>
mov    %eax,%ecx
mov    -0x8(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x8(%rbp),%rax
cmpl   $0xffffffff,(%rax)
je     11ad <m_file_dup+0x7d>
mov    -0x8(%rbp),%rax
mov    (%rax),%edi
mov    0x2e77(%rip),%rax
mov    (%rax),%esi
mov    0x2e66(%rip),%rax
mov    (%rax),%edx
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
jne    11a7 <m_file_dup+0x77>
mov    0x2e3f(%rip),%rax
mov    (%rax),%edi
mov    0x2e3e(%rip),%rax
mov    (%rax),%rsi
mov    -0x8(%rbp),%rax
mov    (%rax),%ecx
lea    0xe5e(%rip),%rdx
call   1050 <error@plt>
add    $0x10,%rsp
pop    %rbp
ret
mov    0x2e1c(%rip),%rax
mov    (%rax),%rax
mov    0x2e02(%rip),%rcx
cmp    (%rcx),%rax
je     11e9 <m_file_dup+0xb9>
mov    0x2dfa(%rip),%rax
mov    (%rax),%edi
mov    0x2df9(%rip),%rax
mov    (%rax),%rsi
mov    -0xc(%rbp),%ecx
lea    0xe3e(%rip),%rdx
call   1050 <error@plt>
jmp    113f <m_file_dup+0xf>

x86 assembly (clang, -O2):
<m_file_dup>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %rdi,%rbx
mov    %esi,%edi
call   1040 <dup@plt>
mov    %eax,(%rbx)
cmp    $0xffffffff,%eax
je     117b <m_file_dup+0x4b>
mov    0x2e8b(%rip),%rcx
mov    (%rcx),%esi
mov    0x2e7a(%rip),%rcx
mov    (%rcx),%edx
mov    %eax,%edi
call   1030 <fcntl@plt>
cmp    $0xffffffff,%eax
je     11c7 <m_file_dup+0x97>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2e4e(%rip),%r15
mov    0x2e37(%rip),%r12
mov    0x2e38(%rip),%r13
lea    0xe8b(%rip),%r14
jmp    11ae <m_file_dup+0x7e>
nopl   0x0(%rax)
mov    %ebp,%edi
call   1040 <dup@plt>
mov    %eax,(%rbx)
cmp    $0xffffffff,%eax
jne    114e <m_file_dup+0x1e>
mov    (%r15),%rsi
cmp    (%r12),%rsi
je     11a0 <m_file_dup+0x70>
mov    0x0(%r13),%edi
mov    %r14,%rdx
mov    %ebp,%ecx
call   1050 <error@plt>
jmp    11a0 <m_file_dup+0x70>
mov    0x2dfa(%rip),%rax
mov    (%rax),%edi
mov    0x2df9(%rip),%rax
mov    (%rax),%rsi
mov    (%rbx),%ecx
lea    0xe1d(%rip),%rdx
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <error@plt>

LLVM IR (-O0):
@F_SETFD = global i32 0
@FD_CLOEXEC = global i32 0
@EXIT_FAILURE = global i32 0
@errno = global i64 0
@.str = [34 x i8] c"fcntl(fd=%d, F_SETFD, FD_CLOEXEC)\00"
@EINTR = global i64 0
@.str.1 = [11 x i8] c"dup(fd=%d)\00"
define void @m_file_dup(i32* %0, i32 %1) {
%3 = alloca i32*
%4 = alloca i32
store i32* %0, i32** %3
store i32 %1, i32* %4
br label %5
5: ; preds = %35, %2
%6 = load i32, i32* %4
%7 = call i32 @dup(i32 %6)
%8 = load i32*, i32** %3
store i32 %7, i32* %8
%9 = load i32*, i32** %3
%10 = load i32, i32* %9
%11 = icmp ne i32 %10, -1
br i1 %11, label %12, label %26
12: ; preds = %5
%13 = load i32*, i32** %3
%14 = load i32, i32* %13
%15 = load i32, i32* @F_SETFD
%16 = load i32, i32* @FD_CLOEXEC
%17 = call i32 @fcntl(i32 %14, i32 %15, i32 %16)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %25
19: ; preds = %12
%20 = load i32, i32* @EXIT_FAILURE
%21 = load i64, i64* @errno
%22 = load i32*, i32** %3
%23 = load i32, i32* %22
%24 = call i32 @error(i32 %20, i64 %21, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0), i32 %23)
br label %25
25: ; preds = %19, %12
ret void
26: ; preds = %5
%27 = load i64, i64* @errno
%28 = load i64, i64* @EINTR
%29 = icmp ne i64 %27, %28
br i1 %29, label %30, label %35
30: ; preds = %26
%31 = load i32, i32* @EXIT_FAILURE
%32 = load i64, i64* @errno
%33 = load i32, i32* %4
%34 = call i32 @error(i32 %31, i64 %32, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 %33)
br label %35
35: ; preds = %30, %26
br label %5
}

LLVM IR (-O2):
@F_SETFD = global i32 0
@FD_CLOEXEC = global i32 0
@EXIT_FAILURE = global i32 0
@errno = global i64 0
@.str = [34 x i8] c"fcntl(fd=%d, F_SETFD, FD_CLOEXEC)\00"
@EINTR = global i64 0
@.str.1 = [11 x i8] c"dup(fd=%d)\00"
define void @m_file_dup(i32* nocapture %0, i32 %1) {
%3 = tail call i32 @dup(i32 %1)
store i32 %3, i32* %0
%4 = icmp eq i32 %3, -1
br i1 %4, label %17, label %5
5: ; preds = %24, %2
%6 = phi i32 [ %3, %2 ], [ %25, %24 ]
%7 = load i32, i32* @F_SETFD
%8 = load i32, i32* @FD_CLOEXEC
%9 = tail call i32 @fcntl(i32 %6, i32 %7, i32 %8)
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %16
11: ; preds = %5
%12 = load i32, i32* @EXIT_FAILURE
%13 = load i64, i64* @errno
%14 = load i32, i32* %0
%15 = tail call i32 @error(i32 %12, i64 %13, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0), i32 %14)
br label %16
16: ; preds = %11, %5
ret void
17: ; preds = %2, %24
%18 = load i64, i64* @errno
%19 = load i64, i64* @EINTR
%20 = icmp eq i64 %18, %19
br i1 %20, label %24, label %21
21: ; preds = %17
%22 = load i32, i32* @EXIT_FAILURE
%23 = tail call i32 @error(i32 %22, i64 %18, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i32 %1)
br label %24
24: ; preds = %21, %17
%25 = tail call i32 @dup(i32 %1)
store i32 %25, i32* %0
%26 = icmp eq i32 %25, -1
br i1 %26, label %17, label %5
}