#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 /*<<< orphan*/ O_WRONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int, int);
scalar_t__ memchr(char *, char, size_t);
int /*<<< orphan*/ memcpy(char *, char *, size_t);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ sscanf(char *, char *, int *);
int /*<<< orphan*/ trim(char **, size_t *);
int open0(char *args, size_t length) {
  char *arg_fname;
  size_t arg_fname_len;
  char *gt = (char *)memchr(args, '>', length);
  int fd0, fd;
  if (gt == NULL)
    return -1;
  arg_fname = args;
  arg_fname_len = gt - args;
  length -= gt - args;
  trim(&arg_fname, &arg_fname_len);
  gt++;
  length--;
  trim(&gt, &length);
  sscanf(gt, "%d", &fd);
  fd += 100;
  char filename[255];
  memcpy(filename, arg_fname, arg_fname_len);
  filename[arg_fname_len] = '\0';
  fd0 = open(filename, O_WRONLY);
  if (fd0 < 0)
    return fd0;
  dup2(fd0, fd);
  close(fd0);
  return 0;
}


x86 assembly (clang, -O0):
<open0>:
push   %rbp
mov    %rsp,%rbp
sub    $0x140,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    $0x3e,%esi
call   1060 <memchr@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11b0 <open0+0x40>
movl   $0xffffffff,-0x4(%rbp)
jmp    1293 <open0+0x123>
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x30(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x30(%rbp),%rcx
mov    -0x10(%rbp),%rax
sub    %rax,%rcx
mov    -0x18(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x18(%rbp)
lea    -0x20(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1040 <trim@plt>
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x18(%rbp)
lea    -0x30(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1040 <trim@plt>
mov    -0x30(%rbp),%rdi
lea    0xde6(%rip),%rsi
lea    -0x38(%rbp),%rdx
call   1080 <sscanf@plt>
mov    -0x38(%rbp),%eax
add    $0x64,%eax
mov    %eax,-0x38(%rbp)
lea    -0x140(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1070 <memcpy@plt>
mov    -0x28(%rbp),%rax
movb   $0x0,-0x140(%rbp,%rax,1)
lea    -0x140(%rbp),%rdi
mov    0x2d7e(%rip),%rax
mov    (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jge    1279 <open0+0x109>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1293 <open0+0x123>
mov    -0x34(%rbp),%edi
mov    -0x38(%rbp),%esi
call   1030 <dup2@plt>
mov    -0x34(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x140,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open0>:
push   %r15
push   %r14
push   %rbx
sub    $0x130,%rsp
mov    %rsi,%r14
mov    %rdi,%r15
mov    $0x3e,%esi
mov    %r14,%rdx
call   1060 <memchr@plt>
test   %rax,%rax
je     1248 <open0+0xd8>
mov    %rax,%rbx
mov    %r15,0x20(%rsp)
sub    %r15,%rax
mov    %rax,0x10(%rsp)
sub    %rbx,%r15
lea    0x20(%rsp),%rdi
lea    0x10(%rsp),%rsi
call   1040 <trim@plt>
add    $0x1,%rbx
mov    %rbx,0x18(%rsp)
lea    (%r15,%r14,1),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,0x28(%rsp)
lea    0x18(%rsp),%rdi
lea    0x28(%rsp),%rsi
call   1040 <trim@plt>
mov    0x18(%rsp),%rdi
lea    0xe15(%rip),%rsi
lea    0xc(%rsp),%rdx
call   1080 <sscanf@plt>
addl   $0x64,0xc(%rsp)
mov    0x20(%rsp),%rsi
mov    0x10(%rsp),%rdx
lea    0x30(%rsp),%rbx
mov    %rbx,%rdi
call   1070 <memcpy@plt>
mov    0x10(%rsp),%rax
movb   $0x0,0x30(%rsp,%rax,1)
mov    0x2db6(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1090 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
js     124d <open0+0xdd>
mov    0xc(%rsp),%esi
mov    %ebx,%edi
call   1030 <dup2@plt>
mov    %ebx,%edi
call   1050 <close@plt>
xor    %ebx,%ebx
jmp    124d <open0+0xdd>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x130,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [3 x i8] c"%d\00"
@O_WRONLY = global i32 0
define i32 @open0(i8* %0, i64 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
%11 = alloca [255 x i8]
store i8* %0, i8** %4
store i64 %1, i64* %5
%12 = load i8*, i8** %4
%13 = load i64, i64* %5
%14 = call i64 @memchr(i8* %12, i8 signext 62, i64 %13)
%15 = inttoptr i64 %14 to i8*
store i8* %15, i8** %8
%16 = load i8*, i8** %8
%17 = icmp eq i8* %16, null
br i1 %17, label %18, label %19
18: ; preds = %2
store i32 -1, i32* %3
br label %62
19: ; preds = %2
%20 = load i8*, i8** %4
store i8* %20, i8** %6
%21 = load i8*, i8** %8
%22 = load i8*, i8** %4
%23 = ptrtoint i8* %21 to i64
%24 = ptrtoint i8* %22 to i64
%25 = sub i64 %23, %24
store i64 %25, i64* %7
%26 = load i8*, i8** %8
%27 = load i8*, i8** %4
%28 = ptrtoint i8* %26 to i64
%29 = ptrtoint i8* %27 to i64
%30 = sub i64 %28, %29
%31 = load i64, i64* %5
%32 = sub i64 %31, %30
store i64 %32, i64* %5
%33 = call i32 @trim(i8** %6, i64* %7)
%34 = load i8*, i8** %8
%35 = getelementptr inbounds i8, i8* %34, i32 1
store i8* %35, i8** %8
%36 = load i64, i64* %5
%37 = add i64 %36, -1
store i64 %37, i64* %5
%38 = call i32 @trim(i8** %8, i64* %5)
%39 = load i8*, i8** %8
%40 = call i32 @sscanf(i8* %39, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %10)
%41 = load i32, i32* %10
%42 = add nsw i32 %41, 100
store i32 %42, i32* %10
%43 = getelementptr inbounds [255 x i8], [255 x i8]* %11, i64 0, i64 0
%44 = load i8*, i8** %6
%45 = load i64, i64* %7
%46 = call i32 @memcpy(i8* %43, i8* %44, i64 %45)
%47 = load i64, i64* %7
%48 = getelementptr inbounds [255 x i8], [255 x i8]* %11, i64 0, i64 %47
store i8 0, i8* %48
%49 = getelementptr inbounds [255 x i8], [255 x i8]* %11, i64 0, i64 0
%50 = load i32, i32* @O_WRONLY
%51 = call i32 @open(i8* %49, i32 %50)
store i32 %51, i32* %9
%52 = load i32, i32* %9
%53 = icmp slt i32 %52, 0
br i1 %53, label %54, label %56
54: ; preds = %19
%55 = load i32, i32* %9
store i32 %55, i32* %3
br label %62
56: ; preds = %19
%57 = load i32, i32* %9
%58 = load i32, i32* %10
%59 = call i32 @dup2(i32 %57, i32 %58)
%60 = load i32, i32* %9
%61 = call i32 @close(i32 %60)
store i32 0, i32* %3
br label %62
62: ; preds = %56, %54, %18
%63 = load i32, i32* %3
ret i32 %63
}

LLVM IR (-O2):
@.str = [3 x i8] c"%d\00"
@O_WRONLY = global i32 0
define i32 @open0(i8* %0, i64 %1) {
%3 = alloca i64
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i8*
%7 = alloca i32
%8 = alloca [255 x i8]
%9 = bitcast i8** %4 to i8*
%10 = bitcast i64* %5 to i8*
%11 = bitcast i8** %6 to i8*
%12 = tail call i64 @memchr(i8* %0, i8 signext 62, i64 %1)
%13 = bitcast i32* %7 to i8*
%14 = icmp eq i64 %12, 0
br i1 %14, label %44, label %15
15: ; preds = %2
%16 = inttoptr i64 %12 to i8*
store i8* %0, i8** %4
%17 = ptrtoint i8* %0 to i64
%18 = sub i64 %12, %17
store i64 %18, i64* %5
%19 = sub i64 %17, %12
%20 = add i64 %19, %1
%21 = call i32 @trim(i8** %4, i64* %5)
%22 = getelementptr inbounds i8, i8* %16, i64 1
store i8* %22, i8** %6
%23 = add i64 %20, -1
store i64 %23, i64* %3
%24 = call i32 @trim(i8** %6, i64* %3)
%25 = load i8*, i8** %6
%26 = call i32 @sscanf(i8* %25, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32* %7)
%27 = load i32, i32* %7
%28 = add nsw i32 %27, 100
store i32 %28, i32* %7
%29 = getelementptr inbounds [255 x i8], [255 x i8]* %8, i64 0, i64 0
%30 = load i8*, i8** %4
%31 = load i64, i64* %5
%32 = call i32 @memcpy(i8* %29, i8* %30, i64 %31)
%33 = load i64, i64* %5
%34 = getelementptr inbounds [255 x i8], [255 x i8]* %8, i64 0, i64 %33
store i8 0, i8* %34
%35 = load i32, i32* @O_WRONLY
%36 = call i32 @open(i8* %29, i32 %35)
%37 = icmp slt i32 %36, 0
br i1 %37, label %42, label %38
38: ; preds = %15
%39 = load i32, i32* %7
%40 = call i32 @dup2(i32 %36, i32 %39)
%41 = call i32 @close(i32 %36)
br label %42
42: ; preds = %15, %38
%43 = phi i32 [ 0, %38 ], [ %36, %15 ]
br label %44
44: ; preds = %2, %42
%45 = phi i32 [ %43, %42 ], [ -1, %2 ]
ret i32 %45
}