#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 forth_instance {
  int dummy;
};

/* Variables and functions */
int O_RDONLY;
int O_RDWR;
int O_WRONLY;
int /*<<< orphan*/ iswhitespace(char);
int open(char *, int /*<<< orphan*/, int);
int st_pop(struct forth_instance *);
int /*<<< orphan*/ st_push(struct forth_instance *, int);
void open_func(struct forth_instance *fi) {
  int i = 0, fd, mode;
  char *name, back;
  name = (char *)st_pop(fi);
  mode = st_pop(fi);
  while (name[i] && !iswhitespace(name[i]))
    i++;
  back = name[i];
  name[i] = '\0';
  if (mode == 0)
    mode = O_RDONLY;
  else if (mode == 1)
    mode = O_WRONLY;
  else if (mode == 2)
    mode = O_RDWR;
  fd = open(name, 0, mode);
  if (fd == -1)
    st_push(fi, (int)0);
  else
    st_push(fi, (int)fd);
  name[i] = back;
}


x86 assembly (clang, -O0):
<open_func>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <st_pop@plt>
cltq
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <st_pop@plt>
mov    %eax,-0x14(%rbp)
mov    -0x20(%rbp),%rax
movslq -0xc(%rbp),%rcx
movsbl (%rax,%rcx,1),%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x22(%rbp)
je     11a4 <open_func+0x64>
mov    -0x20(%rbp),%rax
movslq -0xc(%rbp),%rcx
movsbl (%rax,%rcx,1),%edi
call   1030 <iswhitespace@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
mov    %al,-0x22(%rbp)
mov    -0x22(%rbp),%al
test   $0x1,%al
jne    11b4 <open_func+0x74>
jmp    11c2 <open_func+0x82>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    116e <open_func+0x2e>
mov    -0x20(%rbp),%rax
movslq -0xc(%rbp),%rcx
mov    (%rax,%rcx,1),%al
mov    %al,-0x21(%rbp)
mov    -0x20(%rbp),%rax
movslq -0xc(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
cmpl   $0x0,-0x14(%rbp)
jne    11f7 <open_func+0xb7>
mov    0x2df3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
jmp    1232 <open_func+0xf2>
cmpl   $0x1,-0x14(%rbp)
jne    1212 <open_func+0xd2>
mov    0x2dc0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
jmp    122d <open_func+0xed>
cmpl   $0x2,-0x14(%rbp)
jne    1228 <open_func+0xe8>
mov    0x2dc5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
jmp    122d <open_func+0xed>
jmp    1232 <open_func+0xf2>
mov    -0x20(%rbp),%rdi
mov    -0x14(%rbp),%edx
xor    %esi,%esi
call   1050 <open@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
jne    125d <open_func+0x11d>
mov    -0x8(%rbp),%rdi
xor    %esi,%esi
call   1060 <st_push@plt>
jmp    1269 <open_func+0x129>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%esi
call   1060 <st_push@plt>
mov    -0x21(%rbp),%dl
mov    -0x20(%rbp),%rax
movslq -0xc(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_func>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
call   1040 <st_pop@plt>
movslq %eax,%rbx
mov    %r14,%rdi
call   1040 <st_pop@plt>
mov    %eax,%r15d
mov    (%rbx),%al
test   %al,%al
je     1191 <open_func+0x51>
xor    %ebp,%ebp
nopl   0x0(%rax)
movsbl %al,%edi
call   1030 <iswhitespace@plt>
test   %eax,%eax
jne    1189 <open_func+0x49>
movzbl 0x1(%rbx,%rbp,1),%eax
add    $0x1,%rbp
test   %al,%al
jne    1170 <open_func+0x30>
mov    %ebp,%eax
mov    (%rbx,%rax,1),%r12b
jmp    1196 <open_func+0x56>
xor    %r12d,%r12d
xor    %ebp,%ebp
mov    %ebp,%r13d
movb   $0x0,(%rbx,%r13,1)
cmp    $0x2,%r15d
je     11b8 <open_func+0x78>
cmp    $0x1,%r15d
je     11c1 <open_func+0x81>
test   %r15d,%r15d
jne    11cb <open_func+0x8b>
mov    0x2e2a(%rip),%rax
jmp    11c8 <open_func+0x88>
mov    0x2e29(%rip),%rax
jmp    11c8 <open_func+0x88>
mov    0x2e00(%rip),%rax
mov    (%rax),%r15d
xor    %ebp,%ebp
mov    %rbx,%rdi
xor    %esi,%esi
mov    %r15d,%edx
call   1050 <open@plt>
cmp    $0xffffffff,%eax
cmovne %eax,%ebp
mov    %r14,%rdi
mov    %ebp,%esi
call   1060 <st_push@plt>
mov    %r12b,(%rbx,%r13,1)
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.forth_instance = type { i32 }
@O_RDONLY = global i32 0
@O_WRONLY = global i32 0
@O_RDWR = global i32 0
define void @open_func(%struct.forth_instance* %0) {
%2 = alloca %struct.forth_instance*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8
store %struct.forth_instance* %0, %struct.forth_instance** %2
store i32 0, i32* %3
%8 = load %struct.forth_instance*, %struct.forth_instance** %2
%9 = call i32 @st_pop(%struct.forth_instance* %8)
%10 = sext i32 %9 to i64
%11 = inttoptr i64 %10 to i8*
store i8* %11, i8** %6
%12 = load %struct.forth_instance*, %struct.forth_instance** %2
%13 = call i32 @st_pop(%struct.forth_instance* %12)
store i32 %13, i32* %5
br label %14
14: ; preds = %33, %1
%15 = load i8*, i8** %6
%16 = load i32, i32* %3
%17 = sext i32 %16 to i64
%18 = getelementptr inbounds i8, i8* %15, i64 %17
%19 = load i8, i8* %18
%20 = sext i8 %19 to i32
%21 = icmp ne i32 %20, 0
br i1 %21, label %22, label %31
22: ; preds = %14
%23 = load i8*, i8** %6
%24 = load i32, i32* %3
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds i8, i8* %23, i64 %25
%27 = load i8, i8* %26
%28 = call i32 @iswhitespace(i8 signext %27)
%29 = icmp ne i32 %28, 0
%30 = xor i1 %29, true
br label %31
31: ; preds = %22, %14
%32 = phi i1 [ false, %14 ], [ %30, %22 ]
br i1 %32, label %33, label %36
33: ; preds = %31
%34 = load i32, i32* %3
%35 = add nsw i32 %34, 1
store i32 %35, i32* %3
br label %14
36: ; preds = %31
%37 = load i8*, i8** %6
%38 = load i32, i32* %3
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds i8, i8* %37, i64 %39
%41 = load i8, i8* %40
store i8 %41, i8* %7
%42 = load i8*, i8** %6
%43 = load i32, i32* %3
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds i8, i8* %42, i64 %44
store i8 0, i8* %45
%46 = load i32, i32* %5
%47 = icmp eq i32 %46, 0
br i1 %47, label %48, label %50
48: ; preds = %36
%49 = load i32, i32* @O_RDONLY
store i32 %49, i32* %5
br label %62
50: ; preds = %36
%51 = load i32, i32* %5
%52 = icmp eq i32 %51, 1
br i1 %52, label %53, label %55
53: ; preds = %50
%54 = load i32, i32* @O_WRONLY
store i32 %54, i32* %5
br label %61
55: ; preds = %50
%56 = load i32, i32* %5
%57 = icmp eq i32 %56, 2
br i1 %57, label %58, label %60
58: ; preds = %55
%59 = load i32, i32* @O_RDWR
store i32 %59, i32* %5
br label %60
60: ; preds = %58, %55
br label %61
61: ; preds = %60, %53
br label %62
62: ; preds = %61, %48
%63 = load i8*, i8** %6
%64 = load i32, i32* %5
%65 = call i32 @open(i8* %63, i32 0, i32 %64)
store i32 %65, i32* %4
%66 = load i32, i32* %4
%67 = icmp eq i32 %66, -1
br i1 %67, label %68, label %71
68: ; preds = %62
%69 = load %struct.forth_instance*, %struct.forth_instance** %2
%70 = call i32 @st_push(%struct.forth_instance* %69, i32 0)
br label %75
71: ; preds = %62
%72 = load %struct.forth_instance*, %struct.forth_instance** %2
%73 = load i32, i32* %4
%74 = call i32 @st_push(%struct.forth_instance* %72, i32 %73)
br label %75
75: ; preds = %71, %68
%76 = load i8, i8* %7
%77 = load i8*, i8** %6
%78 = load i32, i32* %3
%79 = sext i32 %78 to i64
%80 = getelementptr inbounds i8, i8* %77, i64 %79
store i8 %76, i8* %80
ret void
}

LLVM IR (-O2):
%struct.forth_instance = type { i32 }
@O_RDONLY = global i32 0
@O_WRONLY = global i32 0
@O_RDWR = global i32 0
define void @open_func(%struct.forth_instance* %0) {
%2 = tail call i32 @st_pop(%struct.forth_instance* %0)
%3 = sext i32 %2 to i64
%4 = inttoptr i64 %3 to i8*
%5 = tail call i32 @st_pop(%struct.forth_instance* %0)
%6 = load i8, i8* %4
%7 = icmp eq i8 %6, 0
br i1 %7, label %27, label %8
8: ; preds = %1, %14
%9 = phi i64 [ %15, %14 ], [ 0, %1 ]
%10 = phi i8 [ %18, %14 ], [ %6, %1 ]
%11 = phi i32 [ %16, %14 ], [ 0, %1 ]
%12 = tail call i32 @iswhitespace(i8 signext %10)
%13 = icmp eq i32 %12, 0
br i1 %13, label %14, label %20
14: ; preds = %8
%15 = add nuw i64 %9, 1
%16 = add nuw nsw i32 %11, 1
%17 = getelementptr inbounds i8, i8* %4, i64 %15
%18 = load i8, i8* %17
%19 = icmp eq i8 %18, 0
br i1 %19, label %22, label %8
20: ; preds = %8
%21 = trunc i64 %9 to i32
br label %22
22: ; preds = %14, %20
%23 = phi i32 [ %21, %20 ], [ %16, %14 ]
%24 = zext i32 %23 to i64
%25 = getelementptr inbounds i8, i8* %4, i64 %24
%26 = load i8, i8* %25
br label %27
27: ; preds = %1, %22
%28 = phi i8 [ %26, %22 ], [ 0, %1 ]
%29 = phi i32 [ %23, %22 ], [ 0, %1 ]
%30 = zext i32 %29 to i64
%31 = getelementptr inbounds i8, i8* %4, i64 %30
store i8 0, i8* %31
switch i32 %5, label %38 [
i32 0, label %32
i32 1, label %34
i32 2, label %36
]
32: ; preds = %27
%33 = load i32, i32* @O_RDONLY
br label %38
34: ; preds = %27
%35 = load i32, i32* @O_WRONLY
br label %38
36: ; preds = %27
%37 = load i32, i32* @O_RDWR
br label %38
38: ; preds = %27, %34, %36, %32
%39 = phi i32 [ %33, %32 ], [ %35, %34 ], [ %37, %36 ], [ %5, %27 ]
%40 = tail call i32 @open(i8* %4, i32 0, i32 %39)
%41 = icmp eq i32 %40, -1
%42 = select i1 %41, i32 0, i32 %40
%43 = tail call i32 @st_push(%struct.forth_instance* %0, i32 %42)
store i8 %28, i8* %31
ret void
}