#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*/ ClearStr(char *);
int /*<<< orphan*/ StrCopy(char *, char *);
int StrLength(char *);
void *SubStr(char *, int, int);
int server(char *str, char *hstr) {
  char ch;
  int n = StrLength(str);
  int i = 1;
  int k = 0;
  for (i = 1, k = 0; i <= n || k != 0; ++i) {
    ch = SubStr(str, i, 1);
    if (ch == '(')
      ++k;
    else if (ch == ')')
      --k;
  }
  if (i <= n) {
    hstr = SubStr(str, 1, i - 2);
    str = SubStr(str, i, n - i + 1);
  } else {
    StrCopy(hstr, str);
    ClearStr(str);
  }
  return 0;
}


x86 assembly (clang, -O0):
<server>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
call   1060 <StrLength@plt>
mov    %eax,-0x18(%rbp)
movl   $0x1,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
movl   $0x1,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x1c(%rbp),%ecx
mov    $0x1,%al
cmp    -0x18(%rbp),%ecx
mov    %al,-0x21(%rbp)
jle    1193 <server+0x53>
cmpl   $0x0,-0x20(%rbp)
setne  %al
mov    %al,-0x21(%rbp)
mov    -0x21(%rbp),%al
test   $0x1,%al
jne    11a3 <server+0x63>
jmp    1200 <server+0xc0>
mov    -0x8(%rbp),%rdi
mov    -0x1c(%rbp),%esi
mov    $0x1,%edx
call   1040 <SubStr@plt>
mov    %al,-0x11(%rbp)
movsbl -0x11(%rbp),%eax
cmp    $0x28,%eax
jne    11d2 <server+0x92>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    11ed <server+0xad>
movsbl -0x11(%rbp),%eax
cmp    $0x29,%eax
jne    11e8 <server+0xa8>
mov    -0x20(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x20(%rbp)
jmp    11ed <server+0xad>
jmp    11f2 <server+0xb2>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1178 <server+0x38>
mov    -0x1c(%rbp),%eax
cmp    -0x18(%rbp),%eax
jg     1242 <server+0x102>
mov    -0x8(%rbp),%rdi
mov    -0x1c(%rbp),%edx
sub    $0x2,%edx
mov    $0x1,%esi
call   1040 <SubStr@plt>
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x1c(%rbp),%esi
mov    -0x18(%rbp),%edx
sub    -0x1c(%rbp),%edx
add    $0x1,%edx
call   1040 <SubStr@plt>
mov    %rax,-0x8(%rbp)
jmp    1258 <server+0x118>
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1030 <StrCopy@plt>
mov    -0x8(%rbp),%rdi
call   1050 <ClearStr@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<server>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%r14
mov    %rdi,%r15
call   1060 <StrLength@plt>
test   %eax,%eax
jle    11b3 <server+0x73>
mov    %eax,%r12d
mov    $0x1,%ebx
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r15,%rdi
mov    %ebx,%esi
mov    $0x1,%edx
call   1040 <SubStr@plt>
shl    $0x18,%eax
cmp    $0x29000000,%eax
je     11a0 <server+0x60>
cmp    $0x28000000,%eax
jne    11a3 <server+0x63>
add    $0x1,%ebp
jmp    11a3 <server+0x63>
cs nopw 0x0(%rax,%rax,1)
nop
add    $0xffffffff,%ebp
lea    0x1(%rbx),%eax
cmp    %r12d,%ebx
mov    %eax,%ebx
jl     1170 <server+0x30>
mov    %eax,%ebx
test   %ebp,%ebp
jne    1170 <server+0x30>
mov    %r14,%rdi
mov    %r15,%rsi
call   1030 <StrCopy@plt>
mov    %r15,%rdi
call   1050 <ClearStr@plt>
xor    %eax,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @server(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %3
store i8* %1, i8** %4
%9 = load i8*, i8** %3
%10 = call i32 @StrLength(i8* %9)
store i32 %10, i32* %6
store i32 1, i32* %7
store i32 0, i32* %8
store i32 1, i32* %7
store i32 0, i32* %8
br label %11
11: ; preds = %40, %2
%12 = load i32, i32* %7
%13 = load i32, i32* %6
%14 = icmp sle i32 %12, %13
br i1 %14, label %18, label %15
15: ; preds = %11
%16 = load i32, i32* %8
%17 = icmp ne i32 %16, 0
br label %18
18: ; preds = %15, %11
%19 = phi i1 [ true, %11 ], [ %17, %15 ]
br i1 %19, label %20, label %43
20: ; preds = %18
%21 = load i8*, i8** %3
%22 = load i32, i32* %7
%23 = call i8* @SubStr(i8* %21, i32 %22, i32 1)
%24 = ptrtoint i8* %23 to i8
store i8 %24, i8* %5
%25 = load i8, i8* %5
%26 = sext i8 %25 to i32
%27 = icmp eq i32 %26, 40
br i1 %27, label %28, label %31
28: ; preds = %20
%29 = load i32, i32* %8
%30 = add nsw i32 %29, 1
store i32 %30, i32* %8
br label %39
31: ; preds = %20
%32 = load i8, i8* %5
%33 = sext i8 %32 to i32
%34 = icmp eq i32 %33, 41
br i1 %34, label %35, label %38
35: ; preds = %31
%36 = load i32, i32* %8
%37 = add nsw i32 %36, -1
store i32 %37, i32* %8
br label %38
38: ; preds = %35, %31
br label %39
39: ; preds = %38, %28
br label %40
40: ; preds = %39
%41 = load i32, i32* %7
%42 = add nsw i32 %41, 1
store i32 %42, i32* %7
br label %11
43: ; preds = %18
%44 = load i32, i32* %7
%45 = load i32, i32* %6
%46 = icmp sle i32 %44, %45
br i1 %46, label %47, label %59
47: ; preds = %43
%48 = load i8*, i8** %3
%49 = load i32, i32* %7
%50 = sub nsw i32 %49, 2
%51 = call i8* @SubStr(i8* %48, i32 1, i32 %50)
store i8* %51, i8** %4
%52 = load i8*, i8** %3
%53 = load i32, i32* %7
%54 = load i32, i32* %6
%55 = load i32, i32* %7
%56 = sub nsw i32 %54, %55
%57 = add nsw i32 %56, 1
%58 = call i8* @SubStr(i8* %52, i32 %53, i32 %57)
store i8* %58, i8** %3
br label %65
59: ; preds = %43
%60 = load i8*, i8** %4
%61 = load i8*, i8** %3
%62 = call i32 @StrCopy(i8* %60, i8* %61)
%63 = load i8*, i8** %3
%64 = call i32 @ClearStr(i8* %63)
br label %65
65: ; preds = %59, %47
ret i32 0
}

LLVM IR (-O2):
define i32 @server(i8* %0, i8* %1) {
%3 = tail call i32 @StrLength(i8* %0)
%4 = icmp sgt i32 %3, 0
br i1 %4, label %5, label %22
5: ; preds = %2, %16
%6 = phi i32 [ %17, %16 ], [ 0, %2 ]
%7 = phi i32 [ %18, %16 ], [ 1, %2 ]
%8 = tail call i8* @SubStr(i8* %0, i32 %7, i32 1)
%9 = ptrtoint i8* %8 to i64
%10 = trunc i64 %9 to i32
%11 = shl i32 %10, 24
switch i32 %11, label %16 [
i32 671088640, label %12
i32 687865856, label %14
]
12: ; preds = %5
%13 = add nsw i32 %6, 1
br label %16
14: ; preds = %5
%15 = add nsw i32 %6, -1
br label %16
16: ; preds = %5, %12, %14
%17 = phi i32 [ %13, %12 ], [ %15, %14 ], [ %6, %5 ]
%18 = add nuw nsw i32 %7, 1
%19 = icmp slt i32 %7, %3
%20 = icmp ne i32 %17, 0
%21 = select i1 %19, i1 true, i1 %20
br i1 %21, label %5, label %22
22: ; preds = %16, %2
%23 = tail call i32 @StrCopy(i8* %1, i8* %0)
%24 = tail call i32 @ClearStr(i8* %0)
ret i32 0
}