#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*/ ENODEV;
int /*<<< orphan*/ errno;
int get_id_lun(char const *);
scalar_t__ get_sg_path(int, char *, size_t, int /*<<< orphan*/);
int is_sd_device(char const *);
int is_sg_device(char const *);
int /*<<< orphan*/ *pathcpy(char *, char const *, size_t);
int convert_to_sg_path(const char *sd, char *sg, size_t len) {
  int idlun;
  int flag;
  if ((flag = is_sg_device(sd)) < 0) {
    return -1;
  }
  if (flag) {
    if (pathcpy(sg, sd, len) == NULL) {
      return -1;
    }
    return 0;
  }
  if ((flag = is_sd_device(sd)) < 0) {
    return -1;
  }
  if (!flag) {
    errno = ENODEV;
    return -1;
  }
  if ((idlun = get_id_lun(sd)) == -1) {
    return -1;
  }
  if (get_sg_path(idlun, sg, len, 0) < 0) {
    errno = -errno;
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<convert_to_sg_path>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1070 <is_sg_device@plt>
mov    %eax,-0x28(%rbp)
cmp    $0x0,%eax
jge    1185 <convert_to_sg_path+0x35>
movl   $0xffffffff,-0x4(%rbp)
jmp    126f <convert_to_sg_path+0x11f>
cmpl   $0x0,-0x28(%rbp)
je     11c2 <convert_to_sg_path+0x72>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1050 <pathcpy@plt>
cmp    $0x0,%rax
jne    11b6 <convert_to_sg_path+0x66>
movl   $0xffffffff,-0x4(%rbp)
jmp    126f <convert_to_sg_path+0x11f>
movl   $0x0,-0x4(%rbp)
jmp    126f <convert_to_sg_path+0x11f>
mov    -0x10(%rbp),%rdi
call   1060 <is_sd_device@plt>
mov    %eax,-0x28(%rbp)
cmp    $0x0,%eax
jge    11e3 <convert_to_sg_path+0x93>
movl   $0xffffffff,-0x4(%rbp)
jmp    126f <convert_to_sg_path+0x11f>
cmpl   $0x0,-0x28(%rbp)
jne    120b <convert_to_sg_path+0xbb>
mov    0x2de4(%rip),%rax
mov    (%rax),%ecx
mov    0x2de3(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    126f <convert_to_sg_path+0x11f>
mov    -0x10(%rbp),%rdi
call   1030 <get_id_lun@plt>
mov    %eax,-0x24(%rbp)
cmp    $0xffffffff,%eax
jne    122c <convert_to_sg_path+0xdc>
movl   $0xffffffff,-0x4(%rbp)
jmp    126f <convert_to_sg_path+0x11f>
mov    -0x24(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
xor    %ecx,%ecx
call   1040 <get_sg_path@plt>
cmp    $0x0,%rax
jge    1268 <convert_to_sg_path+0x118>
xor    %ecx,%ecx
mov    0x2d8f(%rip),%rax
sub    (%rax),%ecx
mov    0x2d86(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    126f <convert_to_sg_path+0x11f>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<convert_to_sg_path>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%rbx
call   1070 <is_sg_device@plt>
mov    $0xffffffff,%ebp
test   %eax,%eax
js     1184 <convert_to_sg_path+0x34>
je     1191 <convert_to_sg_path+0x41>
mov    %r15,%rdi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1050 <pathcpy@plt>
cmp    $0x1,%rax
sbb    %ebp,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %rbx,%rdi
call   1060 <is_sd_device@plt>
test   %eax,%eax
js     1184 <convert_to_sg_path+0x34>
je     11cd <convert_to_sg_path+0x7d>
mov    %rbx,%rdi
call   1030 <get_id_lun@plt>
cmp    $0xffffffff,%eax
je     1184 <convert_to_sg_path+0x34>
xor    %ebp,%ebp
mov    %eax,%edi
mov    %r15,%rsi
mov    %r14,%rdx
xor    %ecx,%ecx
call   1040 <get_sg_path@plt>
test   %rax,%rax
jns    1184 <convert_to_sg_path+0x34>
mov    0x2e17(%rip),%rax
negl   (%rax)
jmp    11df <convert_to_sg_path+0x8f>
mov    0x2e04(%rip),%rax
mov    (%rax),%eax
mov    0x2e03(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%ebp
jmp    1184 <convert_to_sg_path+0x34>

LLVM IR (-O0):
@ENODEV = global i32 0
@errno = global i32 0
define i32 @convert_to_sg_path(i8* %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i64 %2, i64* %7
%10 = load i8*, i8** %5
%11 = call i32 @is_sg_device(i8* %10)
store i32 %11, i32* %9
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %14
13: ; preds = %3
store i32 -1, i32* %4
br label %50
14: ; preds = %3
%15 = load i32, i32* %9
%16 = icmp ne i32 %15, 0
br i1 %16, label %17, label %25
17: ; preds = %14
%18 = load i8*, i8** %6
%19 = load i8*, i8** %5
%20 = load i64, i64* %7
%21 = call i32* @pathcpy(i8* %18, i8* %19, i64 %20)
%22 = icmp eq i32* %21, null
br i1 %22, label %23, label %24
23: ; preds = %17
store i32 -1, i32* %4
br label %50
24: ; preds = %17
store i32 0, i32* %4
br label %50
25: ; preds = %14
%26 = load i8*, i8** %5
%27 = call i32 @is_sd_device(i8* %26)
store i32 %27, i32* %9
%28 = icmp slt i32 %27, 0
br i1 %28, label %29, label %30
29: ; preds = %25
store i32 -1, i32* %4
br label %50
30: ; preds = %25
%31 = load i32, i32* %9
%32 = icmp ne i32 %31, 0
br i1 %32, label %35, label %33
33: ; preds = %30
%34 = load i32, i32* @ENODEV
store i32 %34, i32* @errno
store i32 -1, i32* %4
br label %50
35: ; preds = %30
%36 = load i8*, i8** %5
%37 = call i32 @get_id_lun(i8* %36)
store i32 %37, i32* %8
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %40
39: ; preds = %35
store i32 -1, i32* %4
br label %50
40: ; preds = %35
%41 = load i32, i32* %8
%42 = load i8*, i8** %6
%43 = load i64, i64* %7
%44 = call i64 @get_sg_path(i32 %41, i8* %42, i64 %43, i32 0)
%45 = icmp slt i64 %44, 0
br i1 %45, label %46, label %49
46: ; preds = %40
%47 = load i32, i32* @errno
%48 = sub nsw i32 0, %47
store i32 %48, i32* @errno
store i32 -1, i32* %4
br label %50
49: ; preds = %40
store i32 0, i32* %4
br label %50
50: ; preds = %49, %46, %39, %33, %29, %24, %23, %13
%51 = load i32, i32* %4
ret i32 %51
}

LLVM IR (-O2):
@ENODEV = global i32 0
@errno = global i32 0
define i32 @convert_to_sg_path(i8* %0, i8* %1, i64 %2) {
%4 = tail call i32 @is_sg_device(i8* %0)
%5 = icmp slt i32 %4, 0
br i1 %5, label %28, label %6
6: ; preds = %3
%7 = icmp eq i32 %4, 0
br i1 %7, label %12, label %8
8: ; preds = %6
%9 = tail call i32* @pathcpy(i8* %1, i8* %0, i64 %2)
%10 = icmp eq i32* %9, null
%11 = sext i1 %10 to i32
br label %28
12: ; preds = %6
%13 = tail call i32 @is_sd_device(i8* %0)
%14 = icmp slt i32 %13, 0
br i1 %14, label %28, label %15
15: ; preds = %12
%16 = icmp eq i32 %13, 0
br i1 %16, label %17, label %19
17: ; preds = %15
%18 = load i32, i32* @ENODEV
store i32 %18, i32* @errno
br label %28
19: ; preds = %15
%20 = tail call i32 @get_id_lun(i8* %0)
%21 = icmp eq i32 %20, -1
br i1 %21, label %28, label %22
22: ; preds = %19
%23 = tail call i64 @get_sg_path(i32 %20, i8* %1, i64 %2, i32 0)
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %28
25: ; preds = %22
%26 = load i32, i32* @errno
%27 = sub nsw i32 0, %26
store i32 %27, i32* @errno
br label %28
28: ; preds = %22, %19, %12, %8, %3, %25, %17
%29 = phi i32 [ -1, %25 ], [ -1, %17 ], [ -1, %3 ], [ %11, %8 ], [ -1, %12 ], [ -1, %19 ], [ 0, %22 ]
ret i32 %29
}