#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*/ hexprintf(unsigned char const *, int);
int /*<<< orphan*/ memcpy(unsigned char *, unsigned char const *, int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ tap_set_macaddr(unsigned char *);
int process_P_response(void *dev, const unsigned char *buf, int len) {
  printf("P!:\n");
  unsigned char macaddr[6];
  int i;
  char resp[] = {0x57, 0x50, 0x04, 0x00, 0x00, 0x00,
                 0x00, 0x06, 0x00, 0x01, 0x52, 0x00};
  switch (buf[2]) {
  case 0x12:
    memcpy(macaddr, buf + 10, 6);
    for (i = 0; i < 6; i++) {
      printf("%02x:", macaddr[i]);
    }
    printf("\n");
    tap_set_macaddr(macaddr);
    break;
  case 0x0e:
    hexprintf(buf, len);
    break;
  default:
    printf("UNKNOWN!\n");
    hexprintf(buf, len);
    break;
  }
  return 0;
}


x86 assembly (clang, -O0):
<process_P_response>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
lea    0xea6(%rip),%rdi
xor    %eax,%eax
call   1030 <printf@plt>
movabs $0x600000000045057,%rax
mov    %rax,-0x30(%rbp)
movl   $0x520100,-0x28(%rbp)
mov    -0x10(%rbp),%rax
movzbl 0x2(%rax),%eax
mov    %eax,-0x34(%rbp)
sub    $0xe,%eax
je     1208 <process_P_response+0xc8>
jmp    118f <process_P_response+0x4f>
mov    -0x34(%rbp),%eax
sub    $0x12,%eax
jne    1219 <process_P_response+0xd9>
jmp    11a0 <process_P_response+0x60>
lea    -0x1a(%rbp),%rdi
mov    -0x10(%rbp),%rsi
add    $0xa,%rsi
mov    $0x6,%edx
call   1050 <memcpy@plt>
movl   $0x0,-0x20(%rbp)
cmpl   $0x6,-0x20(%rbp)
jge    11ec <process_P_response+0xac>
movslq -0x20(%rbp),%rax
movzbl -0x1a(%rbp,%rax,1),%esi
lea    0xe2e(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    11bd <process_P_response+0x7d>
lea    0xe20(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    -0x1a(%rbp),%rdi
call   1040 <tap_set_macaddr@plt>
jmp    1233 <process_P_response+0xf3>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1060 <hexprintf@plt>
jmp    1233 <process_P_response+0xf3>
lea    0xdeb(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   1060 <hexprintf@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<process_P_response>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edx,%ebp
mov    %rsi,%rbx
lea    0xe92(%rip),%rdi
call   1040 <puts@plt>
mov    0x2(%rbx),%al
cmp    $0xe,%al
je     1226 <process_P_response+0xc6>
cmp    $0x12,%al
jne    121a <process_P_response+0xba>
add    $0xa,%rbx
lea    0xa(%rsp),%r14
mov    %r14,%rdi
mov    %rbx,%rsi
mov    $0x6,%edx
call   1070 <memcpy@plt>
movzbl 0xa(%rsp),%esi
lea    0xe4f(%rip),%rbx
mov    %rbx,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
movzbl 0xb(%rsp),%esi
mov    %rbx,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
movzbl 0xc(%rsp),%esi
mov    %rbx,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
movzbl 0xd(%rsp),%esi
mov    %rbx,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
movzbl 0xe(%rsp),%esi
mov    %rbx,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
movzbl 0xf(%rsp),%esi
mov    %rbx,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    %r14,%rdi
call   1060 <tap_set_macaddr@plt>
jmp    1230 <process_P_response+0xd0>
lea    0xde9(%rip),%rdi
call   1040 <puts@plt>
mov    %rbx,%rdi
mov    %ebp,%esi
call   1080 <hexprintf@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [5 x i8] c"P!:\0A\00"
@__const.process_P_response.resp = [12 x i8] c"WP\04\00\00\00\00\06\00\01R\00"
@.str.1 = [6 x i8] c"%02x:\00"
@.str.2 = [2 x i8] c"\0A\00"
@.str.3 = [10 x i8] c"UNKNOWN!\0A\00"
define i32 @process_P_response(i8* %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca [6 x i8]
%8 = alloca i32
%9 = alloca [12 x i8]
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%10 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%11 = bitcast [12 x i8]* %9 to i8*
%12 = load i8*, i8** %5
%13 = getelementptr inbounds i8, i8* %12, i64 2
%14 = load i8, i8* %13
%15 = zext i8 %14 to i32
switch i32 %15, label %42 [
i32 18, label %16
i32 14, label %38
]
16: ; preds = %3
%17 = getelementptr inbounds [6 x i8], [6 x i8]* %7, i64 0, i64 0
%18 = load i8*, i8** %5
%19 = getelementptr inbounds i8, i8* %18, i64 10
%20 = call i32 @memcpy(i8* %17, i8* %19, i32 6)
store i32 0, i32* %8
br label %21
21: ; preds = %31, %16
%22 = load i32, i32* %8
%23 = icmp slt i32 %22, 6
br i1 %23, label %24, label %34
24: ; preds = %21
%25 = load i32, i32* %8
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds [6 x i8], [6 x i8]* %7, i64 0, i64 %26
%28 = load i8, i8* %27
%29 = zext i8 %28 to i32
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 %29)
br label %31
31: ; preds = %24
%32 = load i32, i32* %8
%33 = add nsw i32 %32, 1
store i32 %33, i32* %8
br label %21
34: ; preds = %21
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%36 = getelementptr inbounds [6 x i8], [6 x i8]* %7, i64 0, i64 0
%37 = call i32 @tap_set_macaddr(i8* %36)
br label %47
38: ; preds = %3
%39 = load i8*, i8** %5
%40 = load i32, i32* %6
%41 = call i32 @hexprintf(i8* %39, i32 %40)
br label %47
42: ; preds = %3
%43 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0))
%44 = load i8*, i8** %5
%45 = load i32, i32* %6
%46 = call i32 @hexprintf(i8* %44, i32 %45)
br label %47
47: ; preds = %42, %38, %34
ret i32 0
}

LLVM IR (-O2):
@.str.1 = [6 x i8] c"%02x:\00"
@str = [4 x i8] c"P!:\00"
@str.4 = [9 x i8] c"UNKNOWN!\00"
define i32 @process_P_response(i8* nocapture readnone %0, i8* %1, i32 %2) {
%4 = alloca [6 x i8]
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @str, i64 0, i64 0))
%6 = getelementptr inbounds [6 x i8], [6 x i8]* %4, i64 0, i64 0
%7 = getelementptr inbounds i8, i8* %1, i64 2
%8 = load i8, i8* %7
switch i8 %8, label %39 [
i8 18, label %9
i8 14, label %37
]
9: ; preds = %3
%10 = getelementptr inbounds i8, i8* %1, i64 10
%11 = call i32 @memcpy(i8* %6, i8* %10, i32 6)
%12 = load i8, i8* %6
%13 = zext i8 %12 to i32
%14 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 %13)
%15 = getelementptr inbounds [6 x i8], [6 x i8]* %4, i64 0, i64 1
%16 = load i8, i8* %15
%17 = zext i8 %16 to i32
%18 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 %17)
%19 = getelementptr inbounds [6 x i8], [6 x i8]* %4, i64 0, i64 2
%20 = load i8, i8* %19
%21 = zext i8 %20 to i32
%22 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 %21)
%23 = getelementptr inbounds [6 x i8], [6 x i8]* %4, i64 0, i64 3
%24 = load i8, i8* %23
%25 = zext i8 %24 to i32
%26 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 %25)
%27 = getelementptr inbounds [6 x i8], [6 x i8]* %4, i64 0, i64 4
%28 = load i8, i8* %27
%29 = zext i8 %28 to i32
%30 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 %29)
%31 = getelementptr inbounds [6 x i8], [6 x i8]* %4, i64 0, i64 5
%32 = load i8, i8* %31
%33 = zext i8 %32 to i32
%34 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 %33)
%35 = call i32 @putchar(i32 10)
%36 = call i32 @tap_set_macaddr(i8* %6)
br label %42
37: ; preds = %3
%38 = tail call i32 @hexprintf(i8* %1, i32 %2)
br label %42
39: ; preds = %3
%40 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @str.4, i64 0, i64 0))
%41 = tail call i32 @hexprintf(i8* %1, i32 %2)
br label %42
42: ; preds = %39, %37, %9
ret i32 0
}