#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 rlimit {
  int rlim_cur;
  int rlim_max;
};

/* Variables and functions */
int O_CREAT;
int /*<<< orphan*/ O_RDONLY;
int O_TRUNC;
int O_WRONLY;
int /*<<< orphan*/ RLIMIT_CPU;
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ STDOUT_FILENO;
scalar_t__ dup2(int, int /*<<< orphan*/);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, ...);
int open(char *, int, ...);
int /*<<< orphan*/ setrlimit(int /*<<< orphan*/, struct rlimit *);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int setup_job(int timeout, char *in, char *out) {
  struct rlimit limit;
  limit.rlim_cur = timeout;
  limit.rlim_max = 2 * timeout;
  setrlimit(RLIMIT_CPU, &limit);
  int fd_in = open(in, O_RDONLY);
  if (fd_in < 0) {
    fprintf(stderr, "Error opening job input file `%s`: %s\n", in,
            strerror(errno));
    return 1;
  }
  if (dup2(fd_in, STDIN_FILENO) < 0) {
    fprintf(stderr, "Error duplicating input fd: %s\n", strerror(errno));
    return 1;
  }
  int fd_out = open(out, O_WRONLY | O_CREAT | O_TRUNC, 0666);
  if (fd_out < 0) {
    fprintf(stderr, "Error opening job output file `%s`: %s\n", out,
            strerror(errno));
    return 1;
  }
  if (dup2(fd_out, STDOUT_FILENO) < 0) {
    fprintf(stderr, "Error duplicating output fd: %s\n", strerror(errno));
    return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<setup_job>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x8(%rbp),%eax
shl    $0x1,%eax
mov    %eax,-0x1c(%rbp)
mov    0x2e1f(%rip),%rax
mov    (%rax),%edi
lea    -0x20(%rbp),%rsi
call   1050 <setrlimit@plt>
mov    -0x10(%rbp),%rdi
mov    0x2e41(%rip),%rax
mov    (%rax),%esi
mov    $0x0,%al
call   1060 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    11eb <setup_job+0x9b>
mov    0x2e14(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    0x2df8(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x34(%rbp),%edi
mov    -0x30(%rbp),%rdx
mov    %rax,%rcx
lea    0xe28(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    131a <setup_job+0x1ca>
mov    -0x24(%rbp),%edi
mov    0x2dbb(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
cmp    $0x0,%rax
jge    1240 <setup_job+0xf0>
mov    0x2db3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
mov    0x2d9f(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x38(%rbp),%edi
mov    %rax,%rdx
lea    0xdfa(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    131a <setup_job+0x1ca>
mov    -0x18(%rbp),%rdi
mov    0x2d55(%rip),%rax
mov    (%rax),%esi
mov    0x2da4(%rip),%rax
or     (%rax),%esi
mov    0x2d93(%rip),%rax
or     (%rax),%esi
mov    $0x1b6,%edx
mov    $0x0,%al
call   1060 <open@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jge    12be <setup_job+0x16e>
mov    0x2d41(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    0x2d25(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x44(%rbp),%edi
mov    -0x40(%rbp),%rdx
mov    %rax,%rcx
lea    0xd9c(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    131a <setup_job+0x1ca>
mov    -0x28(%rbp),%edi
mov    0x2d18(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
cmp    $0x0,%rax
jge    1313 <setup_job+0x1c3>
mov    0x2ce0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x48(%rbp)
mov    0x2ccc(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x48(%rbp),%edi
mov    %rax,%rdx
lea    0xd6f(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    131a <setup_job+0x1ca>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<setup_job>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%r14
mov    %rsi,%rbx
mov    %edi,0x8(%rsp)
add    %edi,%edi
mov    %edi,0xc(%rsp)
mov    0x2e29(%rip),%rax
mov    (%rax),%edi
lea    0x8(%rsp),%rsi
call   1050 <setrlimit@plt>
mov    0x2e4e(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
xor    %eax,%eax
call   1060 <open@plt>
test   %eax,%eax
js     121c <setup_job+0xcc>
mov    0x2e13(%rip),%rcx
mov    (%rcx),%esi
mov    %eax,%edi
call   1030 <dup2@plt>
test   %rax,%rax
js     1241 <setup_job+0xf1>
mov    0x2dea(%rip),%rax
mov    0x2e3b(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e28(%rip),%rax
or     (%rax),%esi
xor    %ebx,%ebx
mov    %r14,%rdi
mov    $0x1b6,%edx
xor    %eax,%eax
call   1060 <open@plt>
test   %eax,%eax
js     126d <setup_job+0x11d>
mov    0x2df6(%rip),%rcx
mov    (%rcx),%esi
mov    %eax,%edi
call   1030 <dup2@plt>
test   %rax,%rax
jns    129f <setup_job+0x14f>
mov    0x2dbd(%rip),%rax
mov    (%rax),%ebx
mov    0x2dac(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xe55(%rip),%rsi
jmp    125f <setup_job+0x10f>
mov    0x2d9d(%rip),%rax
mov    (%rax),%ebp
mov    0x2d8c(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xdc6(%rip),%rsi
mov    %ebp,%edi
mov    %rbx,%rdx
jmp    1290 <setup_job+0x140>
mov    0x2d78(%rip),%rax
mov    (%rax),%ebx
mov    0x2d67(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xdc8(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
xor    %eax,%eax
call   1040 <fprintf@plt>
jmp    129a <setup_job+0x14a>
mov    0x2d4c(%rip),%rax
mov    (%rax),%ebx
mov    0x2d3b(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xdbc(%rip),%rsi
mov    %ebx,%edi
mov    %r14,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    $0x1,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.rlimit = type { i32, i32 }
@RLIMIT_CPU = global i32 0
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str = [39 x i8] c"Error opening job input file `%s`: %s\0A\00"
@errno = global i32 0
@STDIN_FILENO = global i32 0
@.str.1 = [32 x i8] c"Error duplicating input fd: %s\0A\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@.str.2 = [40 x i8] c"Error opening job output file `%s`: %s\0A\00"
@STDOUT_FILENO = global i32 0
@.str.3 = [33 x i8] c"Error duplicating output fd: %s\0A\00"
define i32 @setup_job(i32 %0, i8* %1, i8* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca %struct.rlimit
%9 = alloca i32
%10 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i8* %2, i8** %7
%11 = load i32, i32* %5
%12 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %8, i32 0, i32 0
store i32 %11, i32* %12
%13 = load i32, i32* %5
%14 = mul nsw i32 2, %13
%15 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %8, i32 0, i32 1
store i32 %14, i32* %15
%16 = load i32, i32* @RLIMIT_CPU
%17 = call i32 @setrlimit(i32 %16, %struct.rlimit* %8)
%18 = load i8*, i8** %6
%19 = load i32, i32* @O_RDONLY
%20 = call i32 (i8*, i32, ...) @open(i8* %18, i32 %19)
store i32 %20, i32* %9
%21 = load i32, i32* %9
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %29
23: ; preds = %3
%24 = load i32, i32* @stderr
%25 = load i8*, i8** %6
%26 = load i32, i32* @errno
%27 = call i8* @strerror(i32 %26)
%28 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %24, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0), i8* %25, i8* %27)
store i32 1, i32* %4
br label %66
29: ; preds = %3
%30 = load i32, i32* %9
%31 = load i32, i32* @STDIN_FILENO
%32 = call i64 @dup2(i32 %30, i32 %31)
%33 = icmp slt i64 %32, 0
br i1 %33, label %34, label %39
34: ; preds = %29
%35 = load i32, i32* @stderr
%36 = load i32, i32* @errno
%37 = call i8* @strerror(i32 %36)
%38 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %35, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i8* %37)
store i32 1, i32* %4
br label %66
39: ; preds = %29
%40 = load i8*, i8** %7
%41 = load i32, i32* @O_WRONLY
%42 = load i32, i32* @O_CREAT
%43 = or i32 %41, %42
%44 = load i32, i32* @O_TRUNC
%45 = or i32 %43, %44
%46 = call i32 (i8*, i32, ...) @open(i8* %40, i32 %45, i32 438)
store i32 %46, i32* %10
%47 = load i32, i32* %10
%48 = icmp slt i32 %47, 0
br i1 %48, label %49, label %55
49: ; preds = %39
%50 = load i32, i32* @stderr
%51 = load i8*, i8** %7
%52 = load i32, i32* @errno
%53 = call i8* @strerror(i32 %52)
%54 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %50, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0), i8* %51, i8* %53)
store i32 1, i32* %4
br label %66
55: ; preds = %39
%56 = load i32, i32* %10
%57 = load i32, i32* @STDOUT_FILENO
%58 = call i64 @dup2(i32 %56, i32 %57)
%59 = icmp slt i64 %58, 0
br i1 %59, label %60, label %65
60: ; preds = %55
%61 = load i32, i32* @stderr
%62 = load i32, i32* @errno
%63 = call i8* @strerror(i32 %62)
%64 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %61, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0), i8* %63)
store i32 1, i32* %4
br label %66
65: ; preds = %55
store i32 0, i32* %4
br label %66
66: ; preds = %65, %60, %49, %34, %23
%67 = load i32, i32* %4
ret i32 %67
}

LLVM IR (-O2):
%struct.rlimit = type { i32, i32 }
@RLIMIT_CPU = global i32 0
@O_RDONLY = global i32 0
@stderr = global i32 0
@.str = [39 x i8] c"Error opening job input file `%s`: %s\0A\00"
@errno = global i32 0
@STDIN_FILENO = global i32 0
@.str.1 = [32 x i8] c"Error duplicating input fd: %s\0A\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@.str.2 = [40 x i8] c"Error opening job output file `%s`: %s\0A\00"
@STDOUT_FILENO = global i32 0
@.str.3 = [33 x i8] c"Error duplicating output fd: %s\0A\00"
define i32 @setup_job(i32 %0, i8* %1, i8* %2) {
%4 = alloca %struct.rlimit
%5 = bitcast %struct.rlimit* %4 to i8*
%6 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %4, i64 0, i32 0
store i32 %0, i32* %6
%7 = shl nsw i32 %0, 1
%8 = getelementptr inbounds %struct.rlimit, %struct.rlimit* %4, i64 0, i32 1
store i32 %7, i32* %8
%9 = load i32, i32* @RLIMIT_CPU
%10 = call i32 @setrlimit(i32 %9, %struct.rlimit* %4)
%11 = load i32, i32* @O_RDONLY
%12 = call i32 (i8*, i32, ...) @open(i8* %1, i32 %11)
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %19
14: ; preds = %3
%15 = load i32, i32* @stderr
%16 = load i32, i32* @errno
%17 = call i8* @strerror(i32 %16)
%18 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str, i64 0, i64 0), i8* %1, i8* %17)
br label %50
19: ; preds = %3
%20 = load i32, i32* @STDIN_FILENO
%21 = call i64 @dup2(i32 %12, i32 %20)
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %28
23: ; preds = %19
%24 = load i32, i32* @stderr
%25 = load i32, i32* @errno
%26 = call i8* @strerror(i32 %25)
%27 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %24, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.1, i64 0, i64 0), i8* %26)
br label %50
28: ; preds = %19
%29 = load i32, i32* @O_WRONLY
%30 = load i32, i32* @O_CREAT
%31 = or i32 %30, %29
%32 = load i32, i32* @O_TRUNC
%33 = or i32 %31, %32
%34 = call i32 (i8*, i32, ...) @open(i8* %2, i32 %33, i32 438)
%35 = icmp slt i32 %34, 0
br i1 %35, label %36, label %41
36: ; preds = %28
%37 = load i32, i32* @stderr
%38 = load i32, i32* @errno
%39 = call i8* @strerror(i32 %38)
%40 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %37, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0), i8* %2, i8* %39)
br label %50
41: ; preds = %28
%42 = load i32, i32* @STDOUT_FILENO
%43 = call i64 @dup2(i32 %34, i32 %42)
%44 = icmp slt i64 %43, 0
br i1 %44, label %45, label %50
45: ; preds = %41
%46 = load i32, i32* @stderr
%47 = load i32, i32* @errno
%48 = call i8* @strerror(i32 %47)
%49 = call i32 (i32, i8*, i8*, ...) @fprintf(i32 %46, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.3, i64 0, i64 0), i8* %48)
br label %50
50: ; preds = %36, %45, %41, %23, %14
%51 = phi i32 [ 1, %14 ], [ 1, %23 ], [ 1, %36 ], [ 1, %45 ], [ 0, %41 ]
ret i32 %51
}