#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 */
scalar_t__ EACCES;
scalar_t__ ENOENT;
int PATH_MAX;
scalar_t__ errno;
int get_id_lun(char *);
int /*<<< orphan*/ *pathcpy(char *, char *, size_t);
int /*<<< orphan*/ sprintf(char *, char *, int);
int get_sg_path(int idlun, char *path, size_t len, int host_id_only) {
  char tmp[PATH_MAX];
  int i;
  int eacces;
  int idlun2;
  eacces = 0;
  if (host_id_only) {
    idlun = (idlun & 0xff) << 24;
  }
  for (i = 0; i < 256; i++) {
    sprintf(tmp, "/dev/sg%d", i);
    if ((idlun2 = get_id_lun(tmp)) == -1) {
      if (errno == EACCES) {
        eacces = 1;
      }
    } else if (idlun == idlun2) {
      if (pathcpy(path, tmp, len) == NULL) {
        return -1;
      }
      return 0;
    }
  }
  errno = eacces ? EACCES : ENOENT;
  return -1;
}


x86 assembly (clang, -O0):
<get_sg_path>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    0x2ea3(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x28(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x48(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x30(%rbp)
movl   $0x0,-0x38(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     1191 <get_sg_path+0x61>
mov    -0x8(%rbp),%eax
and    $0xff,%eax
shl    $0x18,%eax
mov    %eax,-0x8(%rbp)
movl   $0x0,-0x34(%rbp)
cmpl   $0x100,-0x34(%rbp)
jge    1258 <get_sg_path+0x128>
mov    -0x48(%rbp),%rdi
mov    -0x34(%rbp),%edx
lea    0xe4d(%rip),%rsi
call   1050 <sprintf@plt>
mov    -0x48(%rbp),%rdi
call   1030 <get_id_lun@plt>
mov    %eax,-0x3c(%rbp)
cmp    $0xffffffff,%eax
jne    11f3 <get_sg_path+0xc3>
mov    0x2dfc(%rip),%rax
mov    (%rax),%rax
mov    0x2e02(%rip),%rcx
cmp    (%rcx),%rax
jne    11ee <get_sg_path+0xbe>
movl   $0x1,-0x38(%rbp)
jmp    1245 <get_sg_path+0x115>
mov    -0x8(%rbp),%eax
cmp    -0x3c(%rbp),%eax
jne    1240 <get_sg_path+0x110>
mov    -0x48(%rbp),%rsi
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rdx
call   1040 <pathcpy@plt>
cmp    $0x0,%rax
jne    122d <get_sg_path+0xfd>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x40(%rbp)
jmp    129f <get_sg_path+0x16f>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x40(%rbp)
jmp    129f <get_sg_path+0x16f>
jmp    1245 <get_sg_path+0x115>
jmp    124a <get_sg_path+0x11a>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    1198 <get_sg_path+0x68>
cmpl   $0x0,-0x38(%rbp)
je     1275 <get_sg_path+0x145>
mov    0x2d77(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x50(%rbp)
jmp    1283 <get_sg_path+0x153>
mov    0x2d44(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x50(%rbp)
mov    -0x50(%rbp),%rcx
mov    0x2d42(%rip),%rax
mov    %rcx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x40(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_sg_path>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,-0x40(%rbp)
mov    %rsi,-0x38(%rbp)
mov    0x2ea0(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%r13
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r13
mov    %r13,%rsp
mov    %edi,%r15d
shl    $0x18,%r15d
test   %ecx,%ecx
cmove  %edi,%r15d
xor    %r14d,%r14d
lea    0xe86(%rip),%r12
mov    0x2e5f(%rip),%rax
mov    %rax,-0x30(%rbp)
xor    %ebx,%ebx
jmp    11b8 <get_sg_path+0x88>
nopl   0x0(%rax)
mov    0x2e39(%rip),%rax
mov    (%rax),%rax
mov    0x2e3f(%rip),%rcx
cmp    (%rcx),%rax
mov    $0x1,%eax
cmove  %eax,%r14d
add    $0x1,%ebx
cmp    $0x100,%ebx
je     11ef <get_sg_path+0xbf>
mov    %r13,%rdi
mov    %r12,%rsi
mov    %ebx,%edx
call   1050 <sprintf@plt>
mov    %r13,%rdi
call   1030 <get_id_lun@plt>
cmp    $0xffffffff,%eax
je     1190 <get_sg_path+0x60>
cmp    %eax,%r15d
jne    11ad <get_sg_path+0x7d>
mov    -0x38(%rbp),%rdi
mov    %r13,%rsi
mov    -0x40(%rbp),%rdx
call   1040 <pathcpy@plt>
cmp    $0x1,%rax
sbb    %eax,%eax
jmp    1215 <get_sg_path+0xe5>
test   %r14d,%r14d
jne    11ff <get_sg_path+0xcf>
mov    0x2dc5(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    (%rax),%rax
mov    0x2dc3(%rip),%rcx
mov    %rax,(%rcx)
mov    $0xffffffff,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@PATH_MAX = global i32 0
@.str = [10 x i8] c"/dev/sg%d\00"
@errno = global i64 0
@EACCES = global i64 0
@ENOENT = global i64 0
define i32 @get_sg_path(i32 %0, i8* %1, i64 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i64
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
store i32 %0, i32* %6
store i8* %1, i8** %7
store i64 %2, i64* %8
store i32 %3, i32* %9
%16 = load i32, i32* @PATH_MAX
%17 = zext i32 %16 to i64
%18 = call i8* @llvm.stacksave()
store i8* %18, i8** %10
%19 = alloca i8, i64 %17
store i64 %17, i64* %11
store i32 0, i32* %13
%20 = load i32, i32* %9
%21 = icmp ne i32 %20, 0
br i1 %21, label %22, label %26
22: ; preds = %4
%23 = load i32, i32* %6
%24 = and i32 %23, 255
%25 = shl i32 %24, 24
store i32 %25, i32* %6
br label %26
26: ; preds = %22, %4
store i32 0, i32* %12
br label %27
27: ; preds = %54, %26
%28 = load i32, i32* %12
%29 = icmp slt i32 %28, 256
br i1 %29, label %30, label %57
30: ; preds = %27
%31 = load i32, i32* %12
%32 = call i32 @sprintf(i8* %19, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %31)
%33 = call i32 @get_id_lun(i8* %19)
store i32 %33, i32* %14
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %41
35: ; preds = %30
%36 = load i64, i64* @errno
%37 = load i64, i64* @EACCES
%38 = icmp eq i64 %36, %37
br i1 %38, label %39, label %40
39: ; preds = %35
store i32 1, i32* %13
br label %40
40: ; preds = %39, %35
br label %53
41: ; preds = %30
%42 = load i32, i32* %6
%43 = load i32, i32* %14
%44 = icmp eq i32 %42, %43
br i1 %44, label %45, label %52
45: ; preds = %41
%46 = load i8*, i8** %7
%47 = load i64, i64* %8
%48 = call i32* @pathcpy(i8* %46, i8* %19, i64 %47)
%49 = icmp eq i32* %48, null
br i1 %49, label %50, label %51
50: ; preds = %45
store i32 -1, i32* %5
store i32 1, i32* %15
br label %66
51: ; preds = %45
store i32 0, i32* %5
store i32 1, i32* %15
br label %66
52: ; preds = %41
br label %53
53: ; preds = %52, %40
br label %54
54: ; preds = %53
%55 = load i32, i32* %12
%56 = add nsw i32 %55, 1
store i32 %56, i32* %12
br label %27
57: ; preds = %27
%58 = load i32, i32* %13
%59 = icmp ne i32 %58, 0
br i1 %59, label %60, label %62
60: ; preds = %57
%61 = load i64, i64* @EACCES
br label %64
62: ; preds = %57
%63 = load i64, i64* @ENOENT
br label %64
64: ; preds = %62, %60
%65 = phi i64 [ %61, %60 ], [ %63, %62 ]
store i64 %65, i64* @errno
store i32 -1, i32* %5
store i32 1, i32* %15
br label %66
66: ; preds = %64, %51, %50
%67 = load i8*, i8** %10
%68 = load i32, i32* %5
ret i32 %68
}

LLVM IR (-O2):
@PATH_MAX = global i32 0
@.str = [10 x i8] c"/dev/sg%d\00"
@errno = global i64 0
@EACCES = global i64 0
@ENOENT = global i64 0
define i32 @get_sg_path(i32 %0, i8* %1, i64 %2, i32 %3) {
%5 = load i32, i32* @PATH_MAX
%6 = zext i32 %5 to i64
%7 = alloca i8, i64 %6
%8 = icmp eq i32 %3, 0
%9 = shl i32 %0, 24
%10 = select i1 %8, i32 %0, i32 %9
br label %11
11: ; preds = %4, %28
%12 = phi i32 [ 0, %4 ], [ %29, %28 ]
%13 = phi i32 [ 0, %4 ], [ %30, %28 ]
%14 = call i32 @sprintf(i8* %7, i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %13)
%15 = call i32 @get_id_lun(i8* %7)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %22
17: ; preds = %11
%18 = load i64, i64* @errno
%19 = load i64, i64* @EACCES
%20 = icmp eq i64 %18, %19
%21 = select i1 %20, i32 1, i32 %12
br label %28
22: ; preds = %11
%23 = icmp eq i32 %10, %15
br i1 %23, label %24, label %28
24: ; preds = %22
%25 = call i32* @pathcpy(i8* %1, i8* %7, i64 %2)
%26 = icmp eq i32* %25, null
%27 = sext i1 %26 to i32
br label %37
28: ; preds = %17, %22
%29 = phi i32 [ %12, %22 ], [ %21, %17 ]
%30 = add nuw nsw i32 %13, 1
%31 = icmp eq i32 %30, 256
br i1 %31, label %32, label %11
32: ; preds = %28
%33 = icmp eq i32 %29, 0
%34 = load i64, i64* @EACCES
%35 = load i64, i64* @ENOENT
%36 = select i1 %33, i64 %35, i64 %34
store i64 %36, i64* @errno
br label %37
37: ; preds = %24, %32
%38 = phi i32 [ -1, %32 ], [ %27, %24 ]
ret i32 %38
}