#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 */
typedef void FILE;

/* Variables and functions */
int O_RDONLY;
void *_fdopen(int, char const *);
int _open_osfhandle(int, int);
int /*<<< orphan*/ strcmp(char const *, char *);
int /*<<< orphan*/ swerr1(char *, char const *);
int /*<<< orphan*/ swerr_SetPos(char *, int);
int /*<<< orphan*/ write_log(int, int, char *, ...);
void *os_AttachFILE(int handle, char const *mode) {
  int fmode = 0;
  int fd = 0;
  FILE *f;
  if (!strcmp(mode, "r")) {
    fmode |= O_RDONLY;
  } else {
    do {
      swerr_SetPos("/scratch/repos/new/home/jordi_armengol_estape/c-scraper/"
                   "outputs/2022-01-22/02-19-57/repos/stden/ejudge/refs/heads/"
                   "master/win32/reuse_attachfile.c",
                   40);
      swerr1("os_AttachFILE: bad mode: %s", mode);
    } while (0);
  }
  fd = _open_osfhandle(handle, fmode);
  if (fd < 0) {
    write_log(3, 5, "os_AttachFILE: _open_osfhandle(%u, %u) failed",
              (unsigned int)handle, (unsigned int)fmode);
    return NULL;
  }
  write_log(3, 1, "os_AttachFILE: fd = %d", fd);
  f = _fdopen(fd, mode);
  if (!f) {
    write_log(3, 1, "_fdopen failed");
    return NULL;
  }
  return f;
}


x86 assembly (clang, -O0):
<os_AttachFILE>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0xc(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe78(%rip),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%eax
jne    11aa <os_AttachFILE+0x4a>
mov    0x2e4b(%rip),%rax
mov    (%rax),%eax
or     -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
jmp    11d5 <os_AttachFILE+0x75>
jmp    11af <os_AttachFILE+0x4f>
lea    0xe4c(%rip),%rdi
mov    $0x28,%esi
call   1070 <swerr_SetPos@plt>
mov    -0x18(%rbp),%rsi
lea    0xec9(%rip),%rdi
call   1080 <swerr1@plt>
jmp    11d5 <os_AttachFILE+0x75>
mov    -0xc(%rbp),%edi
mov    -0x1c(%rbp),%esi
call   1030 <_open_osfhandle@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    1219 <os_AttachFILE+0xb9>
mov    -0xc(%rbp),%ecx
mov    -0x1c(%rbp),%r8d
mov    $0x3,%edi
mov    $0x5,%esi
lea    0xeab(%rip),%rdx
mov    $0x0,%al
call   1060 <write_log@plt>
movq   $0x0,-0x8(%rbp)
jmp    127c <os_AttachFILE+0x11c>
mov    -0x20(%rbp),%ecx
mov    $0x3,%edi
mov    $0x1,%esi
lea    0xeb1(%rip),%rdx
mov    $0x0,%al
call   1060 <write_log@plt>
mov    -0x20(%rbp),%edi
mov    -0x18(%rbp),%rsi
call   1040 <_fdopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1274 <os_AttachFILE+0x114>
mov    $0x3,%edi
mov    $0x1,%esi
lea    0xe95(%rip),%rdx
mov    $0x0,%al
call   1060 <write_log@plt>
movq   $0x0,-0x8(%rbp)
jmp    127c <os_AttachFILE+0x11c>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<os_AttachFILE>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r15
mov    %edi,%r14d
lea    0xe8c(%rip),%rsi
mov    %r15,%rdi
call   1050 <strcmp@plt>
test   %eax,%eax
je     11a4 <os_AttachFILE+0x44>
lea    0xe7b(%rip),%rdi
mov    $0x28,%esi
call   1070 <swerr_SetPos@plt>
lea    0xefc(%rip),%rdi
mov    %r15,%rsi
call   1080 <swerr1@plt>
xor    %ebp,%ebp
jmp    11ad <os_AttachFILE+0x4d>
mov    0x2e3d(%rip),%rax
mov    (%rax),%ebp
mov    %r14d,%edi
mov    %ebp,%esi
call   1030 <_open_osfhandle@plt>
test   %eax,%eax
js     1205 <os_AttachFILE+0xa5>
mov    %eax,%ebx
lea    0xf1a(%rip),%rdx
mov    $0x3,%edi
mov    $0x1,%esi
mov    %eax,%ecx
xor    %eax,%eax
call   1060 <write_log@plt>
mov    %ebx,%edi
mov    %r15,%rsi
call   1040 <_fdopen@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    1225 <os_AttachFILE+0xc5>
lea    0xf05(%rip),%rdx
xor    %ebx,%ebx
mov    $0x3,%edi
mov    $0x1,%esi
xor    %eax,%eax
call   1060 <write_log@plt>
jmp    1225 <os_AttachFILE+0xc5>
lea    0xea4(%rip),%rdx
xor    %ebx,%ebx
mov    $0x3,%edi
mov    $0x5,%esi
mov    %r14d,%ecx
mov    %ebp,%r8d
xor    %eax,%eax
call   1060 <write_log@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@O_RDONLY = global i32 0
@.str.1 = [146 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/stden/ejudge/refs/heads/master/win32/reuse_attachfile.c\00"
@.str.2 = [28 x i8] c"os_AttachFILE: bad mode: %s\00"
@.str.3 = [46 x i8] c"os_AttachFILE: _open_osfhandle(%u, %u) failed\00"
@.str.4 = [23 x i8] c"os_AttachFILE: fd = %d\00"
@.str.5 = [15 x i8] c"_fdopen failed\00"
define i8* @os_AttachFILE(i32 %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
store i32 %0, i32* %4
store i8* %1, i8** %5
store i32 0, i32* %6
store i32 0, i32* %7
%9 = load i8*, i8** %5
%10 = call i32 @strcmp(i8* %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%11 = icmp ne i32 %10, 0
br i1 %11, label %16, label %12
12: ; preds = %2
%13 = load i32, i32* @O_RDONLY
%14 = load i32, i32* %6
%15 = or i32 %14, %13
store i32 %15, i32* %6
br label %22
16: ; preds = %2
br label %17
17: ; preds = %16
%18 = call i32 @swerr_SetPos(i8* getelementptr inbounds ([146 x i8], [146 x i8]* @.str.1, i64 0, i64 0), i32 40)
%19 = load i8*, i8** %5
%20 = call i32 @swerr1(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i8* %19)
br label %21
21: ; preds = %17
br label %22
22: ; preds = %21, %12
%23 = load i32, i32* %4
%24 = load i32, i32* %6
%25 = call i32 @_open_osfhandle(i32 %23, i32 %24)
store i32 %25, i32* %7
%26 = load i32, i32* %7
%27 = icmp slt i32 %26, 0
br i1 %27, label %28, label %32
28: ; preds = %22
%29 = load i32, i32* %4
%30 = load i32, i32* %6
%31 = call i32 (i32, i32, i8*, ...) @write_log(i32 3, i32 5, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.3, i64 0, i64 0), i32 %29, i32 %30)
store i8* null, i8** %3
br label %44
32: ; preds = %22
%33 = load i32, i32* %7
%34 = call i32 (i32, i32, i8*, ...) @write_log(i32 3, i32 1, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0), i32 %33)
%35 = load i32, i32* %7
%36 = load i8*, i8** %5
%37 = call i8* @_fdopen(i32 %35, i8* %36)
store i8* %37, i8** %8
%38 = load i8*, i8** %8
%39 = icmp ne i8* %38, null
br i1 %39, label %42, label %40
40: ; preds = %32
%41 = call i32 (i32, i32, i8*, ...) @write_log(i32 3, i32 1, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0))
store i8* null, i8** %3
br label %44
42: ; preds = %32
%43 = load i8*, i8** %8
store i8* %43, i8** %3
br label %44
44: ; preds = %42, %40, %28
%45 = load i8*, i8** %3
ret i8* %45
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@O_RDONLY = global i32 0
@.str.1 = [146 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/stden/ejudge/refs/heads/master/win32/reuse_attachfile.c\00"
@.str.2 = [28 x i8] c"os_AttachFILE: bad mode: %s\00"
@.str.3 = [46 x i8] c"os_AttachFILE: _open_osfhandle(%u, %u) failed\00"
@.str.4 = [23 x i8] c"os_AttachFILE: fd = %d\00"
@.str.5 = [15 x i8] c"_fdopen failed\00"
define i8* @os_AttachFILE(i32 %0, i8* %1) {
%3 = tail call i32 @strcmp(i8* dereferenceable(1) %1, i8* dereferenceable(2) getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i32 %3, 0
br i1 %4, label %5, label %7
5: ; preds = %2
%6 = load i32, i32* @O_RDONLY
br label %10
7: ; preds = %2
%8 = tail call i32 @swerr_SetPos(i8* getelementptr inbounds ([146 x i8], [146 x i8]* @.str.1, i64 0, i64 0), i32 40)
%9 = tail call i32 @swerr1(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i8* %1)
br label %10
10: ; preds = %7, %5
%11 = phi i32 [ 0, %7 ], [ %6, %5 ]
%12 = tail call i32 @_open_osfhandle(i32 %0, i32 %11)
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %10
%15 = tail call i32 (i32, i32, i8*, ...) @write_log(i32 3, i32 5, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.3, i64 0, i64 0), i32 %0, i32 %11)
br label %22
16: ; preds = %10
%17 = tail call i32 (i32, i32, i8*, ...) @write_log(i32 3, i32 1, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0), i32 %12)
%18 = tail call i8* @_fdopen(i32 %12, i8* %1)
%19 = icmp eq i8* %18, null
br i1 %19, label %20, label %22
20: ; preds = %16
%21 = tail call i32 (i32, i32, i8*, ...) @write_log(i32 3, i32 1, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0))
br label %22
22: ; preds = %16, %20, %14
%23 = phi i8* [ null, %14 ], [ null, %20 ], [ %18, %16 ]
ret i8* %23
}