#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 O_CREAT;
int O_TRUNC;
int O_WRONLY;
int STDOUT_FILENO;
int S_IRGRP;
int S_IROTH;
int S_IRUSR;
int S_IWUSR;
int /*<<< orphan*/ _exit(int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dprintf(int, char *, char *, int, int);
int dup2(int, int);
int /*<<< orphan*/ errno;
int open(char *, int, int);
int /*<<< orphan*/ puts(char *);
void _write2file() {
  int newfd = open("./fout.dat", O_WRONLY | O_CREAT | O_TRUNC,
                   S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
  if (0 > newfd) {
    puts("Error in opening "
         "./fout.dat"
         ".\n");
    _exit(errno);
  }
  int fd = dup2(newfd, STDOUT_FILENO);
  if (fd < 0 || fd != STDOUT_FILENO) {
    puts("Error in redirecting "
         "./fout.dat"
         " to STDOUT_FILENO.\n");
    _exit(errno);
  }
  close(newfd);
  dprintf(STDOUT_FILENO,
          "Hello! This is src file %s, line %d\n"
          "Fd %d has been opened correctly.\n",
          "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/"
          "2022-01-22/02-19-57/repos/mapleez/eztip/refs/heads/master/c/"
          "syscall_dup.c",
          73, fd);
}


x86 assembly (clang, -O0):
<_write2file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    0x2e29(%rip),%rax
mov    (%rax),%esi
mov    0x2e80(%rip),%rax
or     (%rax),%esi
mov    0x2e6f(%rip),%rax
or     (%rax),%esi
mov    0x2e1e(%rip),%rax
mov    (%rax),%edx
mov    0x2e3d(%rip),%rax
or     (%rax),%edx
mov    0x2e1c(%rip),%rax
or     (%rax),%edx
mov    0x2e0b(%rip),%rax
or     (%rax),%edx
lea    0xe52(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x4(%rbp)
xor    %eax,%eax
cmp    -0x4(%rbp),%eax
jle    11db <_write2file+0x7b>
lea    0xe43(%rip),%rdi
call   1040 <puts@plt>
mov    0x2dec(%rip),%rax
mov    (%rax),%edi
call   1030 <_exit@plt>
mov    -0x4(%rbp),%edi
mov    0x2dfb(%rip),%rax
mov    (%rax),%esi
call   1050 <dup2@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jl     120b <_write2file+0xab>
mov    -0x8(%rbp),%eax
mov    0x2ddd(%rip),%rcx
cmp    (%rcx),%eax
je     1225 <_write2file+0xc5>
lea    0xe17(%rip),%rdi
call   1040 <puts@plt>
mov    0x2da2(%rip),%rax
mov    (%rax),%edi
call   1030 <_exit@plt>
mov    -0x4(%rbp),%edi
call   1060 <close@plt>
mov    0x2dac(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%r8d
lea    0xe1b(%rip),%rsi
lea    0xe5a(%rip),%rdx
mov    $0x49,%ecx
call   1070 <dprintf@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_write2file>:
push   %rbp
push   %r14
push   %rbx
mov    0x2e2d(%rip),%rax
mov    0x2e86(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e73(%rip),%rax
or     (%rax),%esi
mov    0x2e22(%rip),%rax
mov    0x2e43(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e20(%rip),%rax
or     (%rax),%edx
mov    0x2e0f(%rip),%rax
or     (%rax),%edx
lea    0xe56(%rip),%rdi
call   1080 <open@plt>
test   %eax,%eax
js     11fa <_write2file+0x9a>
mov    %eax,%ebx
mov    0x2e24(%rip),%r14
mov    (%r14),%esi
mov    %eax,%edi
call   1050 <dup2@plt>
test   %eax,%eax
js     1203 <_write2file+0xa3>
mov    %eax,%ebp
cmp    (%r14),%eax
jne    1203 <_write2file+0xa3>
mov    %ebx,%edi
call   1060 <close@plt>
mov    (%r14),%edi
lea    0xe7a(%rip),%rsi
lea    0xeb9(%rip),%rdx
mov    $0x49,%ecx
mov    %ebp,%r8d
pop    %rbx
pop    %r14
pop    %rbp
jmp    1070 <dprintf@plt>
lea    0xe0a(%rip),%rdi
jmp    120a <_write2file+0xaa>
lea    0xe1f(%rip),%rdi
call   1040 <puts@plt>
mov    0x2daa(%rip),%rax
mov    (%rax),%edi
call   1030 <_exit@plt>

LLVM IR (-O0):
@.str = [11 x i8] c"./fout.dat\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IROTH = global i32 0
@.str.1 = [30 x i8] c"Error in opening ./fout.dat.\0A\00"
@errno = global i32 0
@STDOUT_FILENO = global i32 0
@.str.2 = [51 x i8] c"Error in redirecting ./fout.dat to STDOUT_FILENO.\0A\00"
@.str.3 = [70 x i8] c"Hello! This is src file %s, line %d\0AFd %d has been opened correctly.\0A\00"
@.str.4 = [138 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/mapleez/eztip/refs/heads/master/c/syscall_dup.c\00"
define void @_write2file() {
%1 = alloca i32
%2 = alloca i32
%3 = load i32, i32* @O_WRONLY
%4 = load i32, i32* @O_CREAT
%5 = or i32 %3, %4
%6 = load i32, i32* @O_TRUNC
%7 = or i32 %5, %6
%8 = load i32, i32* @S_IRUSR
%9 = load i32, i32* @S_IWUSR
%10 = or i32 %8, %9
%11 = load i32, i32* @S_IRGRP
%12 = or i32 %10, %11
%13 = load i32, i32* @S_IROTH
%14 = or i32 %12, %13
%15 = call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %7, i32 %14)
store i32 %15, i32* %1
%16 = load i32, i32* %1
%17 = icmp sgt i32 0, %16
br i1 %17, label %18, label %22
18: ; preds = %0
%19 = call i32 @puts(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0))
%20 = load i32, i32* @errno
%21 = call i32 @_exit(i32 %20)
unreachable
22: ; preds = %0
%23 = load i32, i32* %1
%24 = load i32, i32* @STDOUT_FILENO
%25 = call i32 @dup2(i32 %23, i32 %24)
store i32 %25, i32* %2
%26 = load i32, i32* %2
%27 = icmp slt i32 %26, 0
br i1 %27, label %32, label %28
28: ; preds = %22
%29 = load i32, i32* %2
%30 = load i32, i32* @STDOUT_FILENO
%31 = icmp ne i32 %29, %30
br i1 %31, label %32, label %36
32: ; preds = %28, %22
%33 = call i32 @puts(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.2, i64 0, i64 0))
%34 = load i32, i32* @errno
%35 = call i32 @_exit(i32 %34)
unreachable
36: ; preds = %28
%37 = load i32, i32* %1
%38 = call i32 @close(i32 %37)
%39 = load i32, i32* @STDOUT_FILENO
%40 = load i32, i32* %2
%41 = call i32 @dprintf(i32 %39, i8* getelementptr inbounds ([70 x i8], [70 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([138 x i8], [138 x i8]* @.str.4, i64 0, i64 0), i32 73, i32 %40)
ret void
}

LLVM IR (-O2):
@.str = [11 x i8] c"./fout.dat\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IROTH = global i32 0
@.str.1 = [30 x i8] c"Error in opening ./fout.dat.\0A\00"
@errno = global i32 0
@STDOUT_FILENO = global i32 0
@.str.2 = [51 x i8] c"Error in redirecting ./fout.dat to STDOUT_FILENO.\0A\00"
@.str.3 = [70 x i8] c"Hello! This is src file %s, line %d\0AFd %d has been opened correctly.\0A\00"
@.str.4 = [138 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/mapleez/eztip/refs/heads/master/c/syscall_dup.c\00"
define void @_write2file() {
%1 = load i32, i32* @O_WRONLY
%2 = load i32, i32* @O_CREAT
%3 = or i32 %2, %1
%4 = load i32, i32* @O_TRUNC
%5 = or i32 %3, %4
%6 = load i32, i32* @S_IRUSR
%7 = load i32, i32* @S_IWUSR
%8 = or i32 %7, %6
%9 = load i32, i32* @S_IRGRP
%10 = or i32 %8, %9
%11 = load i32, i32* @S_IROTH
%12 = or i32 %10, %11
%13 = tail call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %5, i32 %12)
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %19
15: ; preds = %0
%16 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0))
%17 = load i32, i32* @errno
%18 = tail call i32 @_exit(i32 %17)
unreachable
19: ; preds = %0
%20 = load i32, i32* @STDOUT_FILENO
%21 = tail call i32 @dup2(i32 %13, i32 %20)
%22 = icmp sgt i32 %21, -1
%23 = load i32, i32* @STDOUT_FILENO
%24 = icmp eq i32 %21, %23
%25 = select i1 %22, i1 %24, i1 false
br i1 %25, label %30, label %26
26: ; preds = %19
%27 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([51 x i8], [51 x i8]* @.str.2, i64 0, i64 0))
%28 = load i32, i32* @errno
%29 = tail call i32 @_exit(i32 %28)
unreachable
30: ; preds = %19
%31 = tail call i32 @close(i32 %13)
%32 = load i32, i32* @STDOUT_FILENO
%33 = tail call i32 @dprintf(i32 %32, i8* getelementptr inbounds ([70 x i8], [70 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([138 x i8], [138 x i8]* @.str.4, i64 0, i64 0), i32 73, i32 %21)
ret void
}