#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 */
struct paramlist {
  int dummy;
};

/* Variables and functions */
int O_CREAT;
int /*<<< orphan*/ O_RDONLY;
int O_TRUNC;
int O_WRONLY;
int S_IRUSR;
int S_IWUSR;
int /*<<< orphan*/ close(int);
int open(char *, int, ...);
int parsefds(int, int, struct paramlist *, int);
int parsefile(char *infile, char *outfile, struct paramlist *params,
              int leading) {
  int in, out, ret;
  if ((in = open(infile, O_RDONLY)) < 0)
    return -1;
  if ((out = open(outfile, O_RDONLY)) >= 0) {
    close(in);
    close(out);
    return 0;
  }
  if ((out = open(outfile, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR)) <
      0)
    return 1;
  ret = parsefds(in, out, params, leading);
  close(in);
  close(out);
  return ret;
}


x86 assembly (clang, -O0):
<parsefile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e86(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1040 <open@plt>
mov    %eax,-0x28(%rbp)
cmp    $0x0,%eax
jge    1173 <parsefile+0x43>
movl   $0xffffffff,-0x4(%rbp)
jmp    122a <parsefile+0xfa>
mov    -0x18(%rbp),%rdi
mov    0x2e5a(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1040 <open@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0x0,%eax
jl     11af <parsefile+0x7f>
mov    -0x28(%rbp),%edi
call   1030 <close@plt>
mov    -0x2c(%rbp),%edi
call   1030 <close@plt>
movl   $0x0,-0x4(%rbp)
jmp    122a <parsefile+0xfa>
mov    -0x18(%rbp),%rdi
mov    0x2df6(%rip),%rax
mov    (%rax),%esi
mov    0x2e35(%rip),%rax
or     (%rax),%esi
mov    0x2e24(%rip),%rax
or     (%rax),%esi
mov    0x2deb(%rip),%rax
mov    (%rax),%edx
mov    0x2df2(%rip),%rax
or     (%rax),%edx
mov    $0x0,%al
call   1040 <open@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0x0,%eax
jge    11ff <parsefile+0xcf>
movl   $0x1,-0x4(%rbp)
jmp    122a <parsefile+0xfa>
mov    -0x28(%rbp),%edi
mov    -0x2c(%rbp),%esi
mov    -0x20(%rbp),%rdx
mov    -0x24(%rbp),%ecx
call   1050 <parsefds@plt>
mov    %eax,-0x30(%rbp)
mov    -0x28(%rbp),%edi
call   1030 <close@plt>
mov    -0x2c(%rbp),%edi
call   1030 <close@plt>
mov    -0x30(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<parsefile>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %ecx,%r14d
mov    %rdx,%r15
mov    %rsi,%r12
mov    0x2e8d(%rip),%rbp
mov    0x0(%rbp),%esi
xor    %ebx,%ebx
xor    %eax,%eax
call   1040 <open@plt>
test   %eax,%eax
js     11d1 <parsefile+0xa1>
mov    %eax,%r13d
mov    0x0(%rbp),%esi
mov    %r12,%rdi
xor    %eax,%eax
call   1040 <open@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    11c0 <parsefile+0x90>
mov    0x2e38(%rip),%rax
mov    0x2e79(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e66(%rip),%rax
or     (%rax),%esi
mov    0x2e2d(%rip),%rax
mov    0x2e36(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    %r12,%rdi
xor    %eax,%eax
call   1040 <open@plt>
test   %eax,%eax
js     11d8 <parsefile+0xa8>
mov    %eax,%ebp
mov    %r13d,%edi
mov    %eax,%esi
mov    %r15,%rdx
mov    %r14d,%ecx
call   1050 <parsefds@plt>
mov    %eax,%ebx
mov    %r13d,%edi
call   1030 <close@plt>
mov    %ebp,%edi
call   1030 <close@plt>
jmp    11dd <parsefile+0xad>
mov    $0xffffffff,%ebx
jmp    11dd <parsefile+0xad>
mov    $0x1,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.paramlist = type { i32 }
@O_RDONLY = global i32 0
@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
define i32 @parsefile(i8* %0, i8* %1, %struct.paramlist* %2, i32 %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca %struct.paramlist*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
store i8* %0, i8** %6
store i8* %1, i8** %7
store %struct.paramlist* %2, %struct.paramlist** %8
store i32 %3, i32* %9
%13 = load i8*, i8** %6
%14 = load i32, i32* @O_RDONLY
%15 = call i32 (i8*, i32, ...) @open(i8* %13, i32 %14)
store i32 %15, i32* %10
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %4
store i32 -1, i32* %5
br label %52
18: ; preds = %4
%19 = load i8*, i8** %7
%20 = load i32, i32* @O_RDONLY
%21 = call i32 (i8*, i32, ...) @open(i8* %19, i32 %20)
store i32 %21, i32* %11
%22 = icmp sge i32 %21, 0
br i1 %22, label %23, label %28
23: ; preds = %18
%24 = load i32, i32* %10
%25 = call i32 @close(i32 %24)
%26 = load i32, i32* %11
%27 = call i32 @close(i32 %26)
store i32 0, i32* %5
br label %52
28: ; preds = %18
%29 = load i8*, i8** %7
%30 = load i32, i32* @O_WRONLY
%31 = load i32, i32* @O_CREAT
%32 = or i32 %30, %31
%33 = load i32, i32* @O_TRUNC
%34 = or i32 %32, %33
%35 = load i32, i32* @S_IRUSR
%36 = load i32, i32* @S_IWUSR
%37 = or i32 %35, %36
%38 = call i32 (i8*, i32, ...) @open(i8* %29, i32 %34, i32 %37)
store i32 %38, i32* %11
%39 = icmp slt i32 %38, 0
br i1 %39, label %40, label %41
40: ; preds = %28
store i32 1, i32* %5
br label %52
41: ; preds = %28
%42 = load i32, i32* %10
%43 = load i32, i32* %11
%44 = load %struct.paramlist*, %struct.paramlist** %8
%45 = load i32, i32* %9
%46 = call i32 @parsefds(i32 %42, i32 %43, %struct.paramlist* %44, i32 %45)
store i32 %46, i32* %12
%47 = load i32, i32* %10
%48 = call i32 @close(i32 %47)
%49 = load i32, i32* %11
%50 = call i32 @close(i32 %49)
%51 = load i32, i32* %12
store i32 %51, i32* %5
br label %52
52: ; preds = %41, %40, %23, %17
%53 = load i32, i32* %5
ret i32 %53
}

LLVM IR (-O2):
%struct.paramlist = type { i32 }
@O_RDONLY = global i32 0
@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
define i32 @parsefile(i8* nocapture %0, i8* nocapture %1, %struct.paramlist* %2, i32 %3) {
%5 = load i32, i32* @O_RDONLY
%6 = tail call i32 (i8*, i32, ...) @open(i8* %0, i32 %5)
%7 = icmp slt i32 %6, 0
br i1 %7, label %30, label %8
8: ; preds = %4
%9 = load i32, i32* @O_RDONLY
%10 = tail call i32 (i8*, i32, ...) @open(i8* %1, i32 %9)
%11 = icmp sgt i32 %10, -1
br i1 %11, label %25, label %12
12: ; preds = %8
%13 = load i32, i32* @O_WRONLY
%14 = load i32, i32* @O_CREAT
%15 = or i32 %14, %13
%16 = load i32, i32* @O_TRUNC
%17 = or i32 %15, %16
%18 = load i32, i32* @S_IRUSR
%19 = load i32, i32* @S_IWUSR
%20 = or i32 %19, %18
%21 = tail call i32 (i8*, i32, ...) @open(i8* %1, i32 %17, i32 %20)
%22 = icmp slt i32 %21, 0
br i1 %22, label %30, label %23
23: ; preds = %12
%24 = tail call i32 @parsefds(i32 %6, i32 %21, %struct.paramlist* %2, i32 %3)
br label %25
25: ; preds = %8, %23
%26 = phi i32 [ %21, %23 ], [ %10, %8 ]
%27 = phi i32 [ %24, %23 ], [ 0, %8 ]
%28 = tail call i32 @close(i32 %6)
%29 = tail call i32 @close(i32 %26)
br label %30
30: ; preds = %25, %12, %4
%31 = phi i32 [ -1, %4 ], [ 1, %12 ], [ %27, %25 ]
ret i32 %31
}