#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*/ strncpy(char *, char const *, int);
char *xmalloc(int);
char *get_reg(const char *gadget, int first) {
  char *p;
  p = xmalloc(4 * sizeof(char));
  while (*gadget != '(' && *gadget != '\0')
    gadget++;
  gadget += (first ? -4 : 2);
  strncpy(p, gadget, 3);
  return p;
}


x86 assembly (clang, -O0):
<get_reg>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    $0x4,%edi
call   1040 <xmalloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rax
movsbl (%rax),%ecx
xor    %eax,%eax
cmp    $0x28,%ecx
mov    %al,-0x19(%rbp)
je     1162 <get_reg+0x42>
mov    -0x8(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
setne  %al
mov    %al,-0x19(%rbp)
mov    -0x19(%rbp),%al
test   $0x1,%al
jne    1172 <get_reg+0x52>
jmp    1183 <get_reg+0x63>
mov    -0x8(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
jmp    113d <get_reg+0x1d>
mov    -0xc(%rbp),%edx
mov    $0x2,%ecx
mov    $0xfffffffc,%eax
cmp    $0x0,%edx
cmovne %eax,%ecx
mov    -0x8(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x8(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    $0x3,%edx
call   1030 <strncpy@plt>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_reg>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%ebp
mov    %rdi,%rbx
mov    $0x4,%edi
call   1040 <xmalloc@plt>
mov    %rax,%r14
cs nopw 0x0(%rax,%rax,1)
movzbl (%rbx),%eax
test   %al,%al
je     1151 <get_reg+0x31>
cmp    $0x28,%al
je     1151 <get_reg+0x31>
add    $0x1,%rbx
jmp    1140 <get_reg+0x20>
lea    -0x4(%rbx),%rax
add    $0x2,%rbx
test   %ebp,%ebp
cmovne %rax,%rbx
mov    %r14,%rdi
mov    %rbx,%rsi
mov    $0x3,%edx
call   1030 <strncpy@plt>
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define i8* @get_reg(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8*
store i8* %0, i8** %3
store i32 %1, i32* %4
%6 = call i8* @xmalloc(i32 4)
store i8* %6, i8** %5
br label %7
7: ; preds = %19, %2
%8 = load i8*, i8** %3
%9 = load i8, i8* %8
%10 = sext i8 %9 to i32
%11 = icmp ne i32 %10, 40
br i1 %11, label %12, label %17
12: ; preds = %7
%13 = load i8*, i8** %3
%14 = load i8, i8* %13
%15 = sext i8 %14 to i32
%16 = icmp ne i32 %15, 0
br label %17
17: ; preds = %12, %7
%18 = phi i1 [ false, %7 ], [ %16, %12 ]
br i1 %18, label %19, label %22
19: ; preds = %17
%20 = load i8*, i8** %3
%21 = getelementptr inbounds i8, i8* %20, i32 1
store i8* %21, i8** %3
br label %7
22: ; preds = %17
%23 = load i32, i32* %4
%24 = icmp ne i32 %23, 0
%25 = zext i1 %24 to i64
%26 = select i1 %24, i32 -4, i32 2
%27 = load i8*, i8** %3
%28 = sext i32 %26 to i64
%29 = getelementptr inbounds i8, i8* %27, i64 %28
store i8* %29, i8** %3
%30 = load i8*, i8** %5
%31 = load i8*, i8** %3
%32 = call i32 @strncpy(i8* %30, i8* %31, i32 3)
%33 = load i8*, i8** %5
ret i8* %33
}

LLVM IR (-O2):
define i8* @get_reg(i8* %0, i32 %1) {
%3 = tail call i8* @xmalloc(i32 4)
br label %4
4: ; preds = %7, %2
%5 = phi i8* [ %0, %2 ], [ %8, %7 ]
%6 = load i8, i8* %5
switch i8 %6, label %7 [
i8 40, label %9
i8 0, label %9
]
7: ; preds = %4
%8 = getelementptr inbounds i8, i8* %5, i64 1
br label %4
9: ; preds = %4, %4
%10 = icmp eq i32 %1, 0
%11 = select i1 %10, i64 2, i64 -4
%12 = getelementptr inbounds i8, i8* %5, i64 %11
%13 = tail call i32 @strncpy(i8* %3, i8* %12, i32 3)
ret i8* %3
}