#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 */
typedef int /*<<< orphan*/ pid_t;
typedef int /*<<< orphan*/ lockfile;

/* Variables and functions */
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ qprintf(char *, int, char *, char *);
char *strchr(char *, char);
char *strdup(char const *);
scalar_t__ strncmp(char const *, char *, int);
int /*<<< orphan*/ unlink(char *);
pid_t drv_generic_serial_unlock_port(const char *Port) {
  char lockfile[256];
  char *port, *p;
  if (strncmp(Port, "/dev/", 5) == 0) {
    port = strdup(Port + 5);
  } else {
    port = strdup(Port);
  }
  while ((p = strchr(port, '/')) != NULL) {
    *p = '_';
  }
  qprintf(lockfile, sizeof(lockfile), "/var/lock/LCK..%s", port);
  unlink(lockfile);
  free(port);
  return 0;
}


x86 assembly (clang, -O0):
<drv_generic_serial_unlock_port>:
push   %rbp
mov    %rsp,%rbp
sub    $0x120,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe86(%rip),%rsi
mov    $0x5,%edx
call   1050 <strncmp@plt>
cmp    $0x0,%rax
jne    11a7 <drv_generic_serial_unlock_port+0x47>
mov    -0x8(%rbp),%rdi
add    $0x5,%rdi
call   1080 <strdup@plt>
mov    %rax,-0x118(%rbp)
jmp    11b7 <drv_generic_serial_unlock_port+0x57>
mov    -0x8(%rbp),%rdi
call   1080 <strdup@plt>
mov    %rax,-0x118(%rbp)
jmp    11bc <drv_generic_serial_unlock_port+0x5c>
mov    -0x118(%rbp),%rdi
mov    $0x2f,%esi
call   1060 <strchr@plt>
mov    %rax,-0x120(%rbp)
cmp    $0x0,%rax
je     11ed <drv_generic_serial_unlock_port+0x8d>
mov    -0x120(%rbp),%rax
movb   $0x5f,(%rax)
jmp    11bc <drv_generic_serial_unlock_port+0x5c>
lea    -0x110(%rbp),%rdi
mov    -0x118(%rbp),%rcx
mov    $0x100,%esi
lea    0xdff(%rip),%rdx
call   1070 <qprintf@plt>
lea    -0x110(%rbp),%rdi
call   1040 <unlink@plt>
mov    -0x118(%rbp),%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x120,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<drv_generic_serial_unlock_port>:
push   %r14
push   %rbx
sub    $0x108,%rsp
mov    %rdi,%rbx
lea    0xe8c(%rip),%rsi
mov    $0x5,%edx
call   1050 <strncmp@plt>
test   %rax,%rax
jne    1187 <drv_generic_serial_unlock_port+0x27>
add    $0x5,%rbx
mov    %rbx,%rdi
call   1080 <strdup@plt>
mov    %rax,%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %rbx,%rdi
mov    $0x2f,%esi
call   1060 <strchr@plt>
test   %rax,%rax
je     11b7 <drv_generic_serial_unlock_port+0x57>
movb   $0x5f,(%rax)
jmp    11a0 <drv_generic_serial_unlock_port+0x40>
lea    0xe48(%rip),%rdx
mov    %rsp,%r14
mov    %r14,%rdi
mov    $0x100,%esi
mov    %rbx,%rcx
call   1070 <qprintf@plt>
mov    %r14,%rdi
call   1040 <unlink@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
xor    %eax,%eax
add    $0x108,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [6 x i8] c"/dev/\00"
@.str.1 = [18 x i8] c"/var/lock/LCK..%s\00"
define i32 @drv_generic_serial_unlock_port(i8* %0) {
%2 = alloca i8*
%3 = alloca [256 x i8]
%4 = alloca i8*
%5 = alloca i8*
store i8* %0, i8** %2
%6 = load i8*, i8** %2
%7 = call i64 @strncmp(i8* %6, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 5)
%8 = icmp eq i64 %7, 0
br i1 %8, label %9, label %13
9: ; preds = %1
%10 = load i8*, i8** %2
%11 = getelementptr inbounds i8, i8* %10, i64 5
%12 = call i8* @strdup(i8* %11)
store i8* %12, i8** %4
br label %16
13: ; preds = %1
%14 = load i8*, i8** %2
%15 = call i8* @strdup(i8* %14)
store i8* %15, i8** %4
br label %16
16: ; preds = %13, %9
br label %17
17: ; preds = %21, %16
%18 = load i8*, i8** %4
%19 = call i8* @strchr(i8* %18, i8 signext 47)
store i8* %19, i8** %5
%20 = icmp ne i8* %19, null
br i1 %20, label %21, label %23
21: ; preds = %17
%22 = load i8*, i8** %5
store i8 95, i8* %22
br label %17
23: ; preds = %17
%24 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%25 = load i8*, i8** %4
%26 = call i32 @qprintf(i8* %24, i32 256, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* %25)
%27 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%28 = call i32 @unlink(i8* %27)
%29 = load i8*, i8** %4
%30 = call i32 @free(i8* %29)
ret i32 0
}

LLVM IR (-O2):
@.str = [6 x i8] c"/dev/\00"
@.str.1 = [18 x i8] c"/var/lock/LCK..%s\00"
define i32 @drv_generic_serial_unlock_port(i8* %0) {
%2 = alloca [256 x i8]
%3 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%4 = tail call i64 @strncmp(i8* %0, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 5)
%5 = icmp eq i64 %4, 0
br i1 %5, label %6, label %9
6: ; preds = %1
%7 = getelementptr inbounds i8, i8* %0, i64 5
%8 = tail call i8* @strdup(i8* %7)
br label %11
9: ; preds = %1
%10 = tail call i8* @strdup(i8* %0)
br label %11
11: ; preds = %9, %6
%12 = phi i8* [ %8, %6 ], [ %10, %9 ]
%13 = tail call i8* @strchr(i8* dereferenceable(1) %12, i8 signext 47)
%14 = icmp eq i8* %13, null
br i1 %14, label %19, label %15
15: ; preds = %11, %15
%16 = phi i8* [ %17, %15 ], [ %13, %11 ]
store i8 95, i8* %16
%17 = tail call i8* @strchr(i8* dereferenceable(1) %12, i8 signext 47)
%18 = icmp eq i8* %17, null
br i1 %18, label %19, label %15
19: ; preds = %15, %11
%20 = call i32 @qprintf(i8* %3, i32 256, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* %12)
%21 = call i32 @unlink(i8* %3)
%22 = call i32 @free(i8* %12)
ret i32 0
}