#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 assoc_label(char *);
int read_num(char *);
unsigned calc_disp(int pc, int op, char *oprnd) {
  int addr;
  if (oprnd[0] == '0') {
    addr = read_num(oprnd);
  } else {
    addr = assoc_label(oprnd);
  }
  if (op == 1) {
    return addr - pc - 4;
  } else {
    return addr;
  }
}


x86 assembly (clang, -O0):
<calc_disp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x30,%eax
jne    1153 <calc_disp+0x33>
mov    -0x18(%rbp),%rdi
call   1040 <read_num@plt>
mov    %eax,-0x1c(%rbp)
jmp    115f <calc_disp+0x3f>
mov    -0x18(%rbp),%rdi
call   1030 <assoc_label@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x1,-0xc(%rbp)
jne    117a <calc_disp+0x5a>
mov    -0x1c(%rbp),%eax
sub    -0x8(%rbp),%eax
sub    $0x4,%eax
mov    %eax,-0x4(%rbp)
jmp    1180 <calc_disp+0x60>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<calc_disp>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebx
mov    %edi,%ebp
cmpb   $0x30,(%rdx)
jne    1136 <calc_disp+0x16>
mov    %rdx,%rdi
call   1040 <read_num@plt>
jmp    113e <calc_disp+0x1e>
mov    %rdx,%rdi
call   1030 <assoc_label@plt>
mov    $0xfffffffc,%edx
sub    %ebp,%edx
xor    %ecx,%ecx
cmp    $0x1,%ebx
cmove  %edx,%ecx
add    %eax,%ecx
mov    %ecx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
define i32 @calc_disp(i32 %0, i32 %1, i8* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
store i32 %0, i32* %5
store i32 %1, i32* %6
store i8* %2, i8** %7
%9 = load i8*, i8** %7
%10 = getelementptr inbounds i8, i8* %9, i64 0
%11 = load i8, i8* %10
%12 = sext i8 %11 to i32
%13 = icmp eq i32 %12, 48
br i1 %13, label %14, label %17
14: ; preds = %3
%15 = load i8*, i8** %7
%16 = call i32 @read_num(i8* %15)
store i32 %16, i32* %8
br label %20
17: ; preds = %3
%18 = load i8*, i8** %7
%19 = call i32 @assoc_label(i8* %18)
store i32 %19, i32* %8
br label %20
20: ; preds = %17, %14
%21 = load i32, i32* %6
%22 = icmp eq i32 %21, 1
br i1 %22, label %23, label %28
23: ; preds = %20
%24 = load i32, i32* %8
%25 = load i32, i32* %5
%26 = sub nsw i32 %24, %25
%27 = sub nsw i32 %26, 4
store i32 %27, i32* %4
br label %30
28: ; preds = %20
%29 = load i32, i32* %8
store i32 %29, i32* %4
br label %30
30: ; preds = %28, %23
%31 = load i32, i32* %4
ret i32 %31
}

LLVM IR (-O2):
define i32 @calc_disp(i32 %0, i32 %1, i8* %2) {
%4 = load i8, i8* %2
%5 = icmp eq i8 %4, 48
br i1 %5, label %6, label %8
6: ; preds = %3
%7 = tail call i32 @read_num(i8* %2)
br label %10
8: ; preds = %3
%9 = tail call i32 @assoc_label(i8* %2)
br label %10
10: ; preds = %8, %6
%11 = phi i32 [ %7, %6 ], [ %9, %8 ]
%12 = icmp eq i32 %1, 1
%13 = sub i32 -4, %0
%14 = select i1 %12, i32 %13, i32 0
%15 = add i32 %11, %14
ret i32 %15
}