#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 ber_element {
  int dummy;
};
struct ber {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ ber_free_elements(struct ber_element *);
struct ber_element *ber_get_element(int /*<<< orphan*/);
int ber_read_element(struct ber *, struct ber_element *);
struct ber_element *ber_read_elements(struct ber *ber,
                                      struct ber_element *elm) {
  struct ber_element *root = elm;
  if (root == NULL) {
    if ((root = ber_get_element(0)) == NULL)
      return NULL;
  }
  do {
  } while (0);
  if (ber_read_element(ber, root) == -1) {
    if (elm == NULL)
      ber_free_elements(root);
    return NULL;
  }
  return root;
}


x86 assembly (clang, -O0):
<ber_read_elements>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    117a <ber_read_elements+0x4a>
xor    %edi,%edi
call   1040 <ber_get_element@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    1175 <ber_read_elements+0x45>
movq   $0x0,-0x8(%rbp)
jmp    11c3 <ber_read_elements+0x93>
jmp    117a <ber_read_elements+0x4a>
jmp    117f <ber_read_elements+0x4f>
jmp    1184 <ber_read_elements+0x54>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1050 <ber_read_element@plt>
cmp    $0xffffffff,%eax
jne    11bb <ber_read_elements+0x8b>
cmpq   $0x0,-0x18(%rbp)
jne    11ae <ber_read_elements+0x7e>
mov    -0x20(%rbp),%rdi
call   1030 <ber_free_elements@plt>
movq   $0x0,-0x8(%rbp)
jmp    11c3 <ber_read_elements+0x93>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ber_read_elements>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r12
mov    %rsi,%r15
test   %rsi,%rsi
jne    1157 <ber_read_elements+0x27>
xor    %ebx,%ebx
xor    %edi,%edi
call   1040 <ber_get_element@plt>
mov    %rax,%r15
test   %rax,%rax
je     117c <ber_read_elements+0x4c>
mov    %r12,%rdi
mov    %r15,%rsi
call   1050 <ber_read_element@plt>
xor    %ebx,%ebx
cmp    $0xffffffff,%eax
cmovne %r15,%rbx
jne    117c <ber_read_elements+0x4c>
test   %r14,%r14
jne    117c <ber_read_elements+0x4c>
mov    %r15,%rdi
call   1030 <ber_free_elements@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.ber = type { i32 }
%struct.ber_element = type { i32 }
define %struct.ber_element* @ber_read_elements(%struct.ber* %0, %struct.ber_element* %1) {
%3 = alloca %struct.ber_element*
%4 = alloca %struct.ber*
%5 = alloca %struct.ber_element*
%6 = alloca %struct.ber_element*
store %struct.ber* %0, %struct.ber** %4
store %struct.ber_element* %1, %struct.ber_element** %5
%7 = load %struct.ber_element*, %struct.ber_element** %5
store %struct.ber_element* %7, %struct.ber_element** %6
%8 = load %struct.ber_element*, %struct.ber_element** %6
%9 = icmp eq %struct.ber_element* %8, null
br i1 %9, label %10, label %15
10: ; preds = %2
%11 = call %struct.ber_element* @ber_get_element(i32 0)
store %struct.ber_element* %11, %struct.ber_element** %6
%12 = icmp eq %struct.ber_element* %11, null
br i1 %12, label %13, label %14
13: ; preds = %10
store %struct.ber_element* null, %struct.ber_element** %3
br label %31
14: ; preds = %10
br label %15
15: ; preds = %14, %2
br label %16
16: ; preds = %15
br label %17
17: ; preds = %16
%18 = load %struct.ber*, %struct.ber** %4
%19 = load %struct.ber_element*, %struct.ber_element** %6
%20 = call i32 @ber_read_element(%struct.ber* %18, %struct.ber_element* %19)
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %29
22: ; preds = %17
%23 = load %struct.ber_element*, %struct.ber_element** %5
%24 = icmp eq %struct.ber_element* %23, null
br i1 %24, label %25, label %28
25: ; preds = %22
%26 = load %struct.ber_element*, %struct.ber_element** %6
%27 = call i32 @ber_free_elements(%struct.ber_element* %26)
br label %28
28: ; preds = %25, %22
store %struct.ber_element* null, %struct.ber_element** %3
br label %31
29: ; preds = %17
%30 = load %struct.ber_element*, %struct.ber_element** %6
store %struct.ber_element* %30, %struct.ber_element** %3
br label %31
31: ; preds = %29, %28, %13
%32 = load %struct.ber_element*, %struct.ber_element** %3
ret %struct.ber_element* %32
}

LLVM IR (-O2):
%struct.ber = type { i32 }
%struct.ber_element = type { i32 }
define %struct.ber_element* @ber_read_elements(%struct.ber* %0, %struct.ber_element* %1) {
%3 = icmp eq %struct.ber_element* %1, null
br i1 %3, label %4, label %7
4: ; preds = %2
%5 = tail call %struct.ber_element* @ber_get_element(i32 0)
%6 = icmp eq %struct.ber_element* %5, null
br i1 %6, label %16, label %7
7: ; preds = %2, %4
%8 = phi %struct.ber_element* [ %5, %4 ], [ %1, %2 ]
%9 = tail call i32 @ber_read_element(%struct.ber* %0, %struct.ber_element* %8)
%10 = icmp ne i32 %9, -1
%11 = xor i1 %3, true
%12 = or i1 %10, %11
%13 = select i1 %10, %struct.ber_element* %8, %struct.ber_element* null
br i1 %12, label %16, label %14
14: ; preds = %7
%15 = tail call i32 @ber_free_elements(%struct.ber_element* %8)
br label %16
16: ; preds = %7, %14, %4
%17 = phi %struct.ber_element* [ null, %4 ], [ null, %14 ], [ %13, %7 ]
ret %struct.ber_element* %17
}