#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 EINVAL;
int /*<<< orphan*/ strcmp(char const *, char *);
char const *strstr(char const *, char *);
int strtol(char const *, int /*<<< orphan*/ *, int /*<<< orphan*/);
int parse_switch_addr(const char *addr, int *swaddr) {
  const char *num;
  int offs;
  if (strstr(addr, "phy") == addr) {
    num = &addr[3];
    offs = 0x0;
  } else if (strstr(addr, "port") == addr) {
    num = &addr[4];
    offs = 0x10;
  } else if (!strcmp(addr, "serdes")) {
    *swaddr = 0xf;
    return 0;
  } else if (strstr(addr, "global") == addr) {
    num = &addr[6];
    offs = 0x1a;
  } else {
    num = addr;
    offs = 0x0;
  }
  *swaddr = strtol(num, NULL, 0);
  if (*swaddr < 0)
    return -EINVAL;
  *swaddr += offs;
  return 0;
}


x86 assembly (clang, -O0):
<parse_switch_addr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xeb5(%rip),%rsi
call   1050 <strstr@plt>
cmp    -0x10(%rbp),%rax
jne    1172 <parse_switch_addr+0x42>
mov    -0x10(%rbp),%rax
add    $0x3,%rax
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
jmp    1223 <parse_switch_addr+0xf3>
mov    -0x10(%rbp),%rdi
lea    0xe87(%rip),%rsi
call   1050 <strstr@plt>
cmp    -0x10(%rbp),%rax
jne    11a4 <parse_switch_addr+0x74>
mov    -0x10(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x20(%rbp)
movl   $0x10,-0x24(%rbp)
jmp    121e <parse_switch_addr+0xee>
mov    -0x10(%rbp),%rdi
lea    0xe5a(%rip),%rsi
call   1030 <strcmp@plt>
cmp    $0x0,%eax
jne    11d3 <parse_switch_addr+0xa3>
mov    -0x18(%rbp),%rax
movl   $0xf,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    126c <parse_switch_addr+0x13c>
mov    -0x10(%rbp),%rdi
lea    0xe32(%rip),%rsi
call   1050 <strstr@plt>
cmp    -0x10(%rbp),%rax
jne    1205 <parse_switch_addr+0xd5>
mov    -0x10(%rbp),%rax
add    $0x6,%rax
mov    %rax,-0x20(%rbp)
movl   $0x1a,-0x24(%rbp)
jmp    1214 <parse_switch_addr+0xe4>
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
jmp    1219 <parse_switch_addr+0xe9>
jmp    121e <parse_switch_addr+0xee>
jmp    1223 <parse_switch_addr+0xf3>
mov    -0x20(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1040 <strtol@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x18(%rbp),%rax
cmpl   $0x0,(%rax)
jge    125a <parse_switch_addr+0x12a>
xor    %eax,%eax
mov    0x2d90(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    126c <parse_switch_addr+0x13c>
mov    -0x24(%rbp),%ecx
mov    -0x18(%rbp),%rax
add    (%rax),%ecx
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<parse_switch_addr>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%rbx
lea    0xebc(%rip),%rsi
mov    $0x3,%edx
call   1030 <strncmp@plt>
test   %eax,%eax
je     11a7 <parse_switch_addr+0x77>
lea    0xeab(%rip),%rsi
mov    $0x4,%edx
mov    %rbx,%rdi
call   1030 <strncmp@plt>
test   %eax,%eax
je     11af <parse_switch_addr+0x7f>
lea    0xe98(%rip),%rsi
mov    %rbx,%rdi
call   1040 <strcmp@plt>
test   %eax,%eax
je     11e4 <parse_switch_addr+0xb4>
lea    0xe8c(%rip),%rsi
mov    $0x6,%edx
mov    %rbx,%rdi
call   1030 <strncmp@plt>
lea    0x6(%rbx),%rcx
xor    %edx,%edx
test   %eax,%eax
cmove  %rcx,%rbx
mov    $0x1a,%ebp
cmovne %edx,%ebp
jmp    11b8 <parse_switch_addr+0x88>
add    $0x3,%rbx
xor    %ebp,%ebp
jmp    11b8 <parse_switch_addr+0x88>
add    $0x4,%rbx
mov    $0x10,%ebp
xor    %r15d,%r15d
mov    %rbx,%rdi
xor    %esi,%esi
xor    %edx,%edx
call   1050 <strtol@plt>
mov    %eax,(%r14)
test   %eax,%eax
js     11d5 <parse_switch_addr+0xa5>
add    %ebp,%eax
mov    %eax,(%r14)
jmp    11ee <parse_switch_addr+0xbe>
mov    0x2e04(%rip),%rax
xor    %r15d,%r15d
sub    (%rax),%r15d
jmp    11ee <parse_switch_addr+0xbe>
movl   $0xf,(%r14)
xor    %r15d,%r15d
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [4 x i8] c"phy\00"
@.str.1 = [5 x i8] c"port\00"
@.str.2 = [7 x i8] c"serdes\00"
@.str.3 = [7 x i8] c"global\00"
@EINVAL = global i32 0
define i32 @parse_switch_addr(i8* %0, i32* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i8*
%7 = alloca i32
store i8* %0, i8** %4
store i32* %1, i32** %5
%8 = load i8*, i8** %4
%9 = call i8* @strstr(i8* %8, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%10 = load i8*, i8** %4
%11 = icmp eq i8* %9, %10
br i1 %11, label %12, label %15
12: ; preds = %2
%13 = load i8*, i8** %4
%14 = getelementptr inbounds i8, i8* %13, i64 3
store i8* %14, i8** %6
store i32 0, i32* %7
br label %42
15: ; preds = %2
%16 = load i8*, i8** %4
%17 = call i8* @strstr(i8* %16, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%18 = load i8*, i8** %4
%19 = icmp eq i8* %17, %18
br i1 %19, label %20, label %23
20: ; preds = %15
%21 = load i8*, i8** %4
%22 = getelementptr inbounds i8, i8* %21, i64 4
store i8* %22, i8** %6
store i32 16, i32* %7
br label %41
23: ; preds = %15
%24 = load i8*, i8** %4
%25 = call i32 @strcmp(i8* %24, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%26 = icmp ne i32 %25, 0
br i1 %26, label %29, label %27
27: ; preds = %23
%28 = load i32*, i32** %5
store i32 15, i32* %28
store i32 0, i32* %3
br label %57
29: ; preds = %23
%30 = load i8*, i8** %4
%31 = call i8* @strstr(i8* %30, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
%32 = load i8*, i8** %4
%33 = icmp eq i8* %31, %32
br i1 %33, label %34, label %37
34: ; preds = %29
%35 = load i8*, i8** %4
%36 = getelementptr inbounds i8, i8* %35, i64 6
store i8* %36, i8** %6
store i32 26, i32* %7
br label %39
37: ; preds = %29
%38 = load i8*, i8** %4
store i8* %38, i8** %6
store i32 0, i32* %7
br label %39
39: ; preds = %37, %34
br label %40
40: ; preds = %39
br label %41
41: ; preds = %40, %20
br label %42
42: ; preds = %41, %12
%43 = load i8*, i8** %6
%44 = call i32 @strtol(i8* %43, i32* null, i32 0)
%45 = load i32*, i32** %5
store i32 %44, i32* %45
%46 = load i32*, i32** %5
%47 = load i32, i32* %46
%48 = icmp slt i32 %47, 0
br i1 %48, label %49, label %52
49: ; preds = %42
%50 = load i32, i32* @EINVAL
%51 = sub nsw i32 0, %50
store i32 %51, i32* %3
br label %57
52: ; preds = %42
%53 = load i32, i32* %7
%54 = load i32*, i32** %5
%55 = load i32, i32* %54
%56 = add nsw i32 %55, %53
store i32 %56, i32* %54
store i32 0, i32* %3
br label %57
57: ; preds = %52, %49, %27
%58 = load i32, i32* %3
ret i32 %58
}

LLVM IR (-O2):
@.str = [4 x i8] c"phy\00"
@.str.1 = [5 x i8] c"port\00"
@.str.2 = [7 x i8] c"serdes\00"
@.str.3 = [7 x i8] c"global\00"
@EINVAL = global i32 0
define i32 @parse_switch_addr(i8* nocapture %0, i32* nocapture writeonly %1) {
%3 = tail call i32 @strncmp(i8* dereferenceable(1) %0, i8* dereferenceable(4) getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i64 3)
%4 = icmp eq i32 %3, 0
br i1 %4, label %5, label %7
5: ; preds = %2
%6 = getelementptr inbounds i8, i8* %0, i64 3
br label %22
7: ; preds = %2
%8 = tail call i32 @strncmp(i8* dereferenceable(1) %0, i8* dereferenceable(5) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i64 4)
%9 = icmp eq i32 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %7
%11 = getelementptr inbounds i8, i8* %0, i64 4
br label %22
12: ; preds = %7
%13 = tail call i32 @strcmp(i8* dereferenceable(1) %0, i8* dereferenceable(7) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %16
15: ; preds = %12
store i32 15, i32* %1
br label %32
16: ; preds = %12
%17 = tail call i32 @strncmp(i8* dereferenceable(1) %0, i8* dereferenceable(7) getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i64 6)
%18 = icmp eq i32 %17, 0
%19 = select i1 %18, i64 6, i64 0
%20 = getelementptr i8, i8* %0, i64 %19
%21 = select i1 %18, i32 26, i32 0
br label %22
22: ; preds = %16, %10, %5
%23 = phi i8* [ %6, %5 ], [ %11, %10 ], [ %20, %16 ]
%24 = phi i32 [ 0, %5 ], [ 16, %10 ], [ %21, %16 ]
%25 = tail call i32 @strtol(i8* nocapture %23, i32* null, i32 0)
store i32 %25, i32* %1
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %22
%28 = load i32, i32* @EINVAL
%29 = sub nsw i32 0, %28
br label %32
30: ; preds = %22
%31 = add nuw nsw i32 %25, %24
store i32 %31, i32* %1
br label %32
32: ; preds = %30, %27, %15
%33 = phi i32 [ %29, %27 ], [ 0, %30 ], [ 0, %15 ]
ret i32 %33
}