#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*/ subsys;
struct udev_device {
  int dummy;
};
struct udev {
  int dummy;
};
struct ifreq {
  int ifr_ifindex;
  char *ifr_name;
};

/* Variables and functions */
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SIOCGIFNAME;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ ioctl(int, int /*<<< orphan*/, struct ifreq *);
int /*<<< orphan*/ makedev(int, int);
int /*<<< orphan*/ memset(struct ifreq *, int, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int sscanf(char const *, char *, char *, int *, int *);
char *strchr(char *, char);
int /*<<< orphan*/ strscpy(char *, int, char const *);
int strtoul(char const *, int /*<<< orphan*/ *, int);
int udev_device_get_ifindex(struct udev_device *);
struct udev_device *udev_device_new_from_devnum(struct udev *, char,
                                                int /*<<< orphan*/);
struct udev_device *udev_device_new_from_subsystem_sysname(struct udev *,
                                                           char *, char *);
int /*<<< orphan*/ udev_device_unref(struct udev_device *);
__attribute__((visibility("default"))) struct udev_device *
udev_device_new_from_device_id(struct udev *udev, const char *id) {
  char type;
  int maj, min;
  char subsys[1024];
  char *sysname;
  switch (id[0]) {
  case 'b':
  case 'c':
    if (sscanf(id, "%c%i:%i", &type, &maj, &min) != 3)
      return NULL;
    return udev_device_new_from_devnum(udev, type, makedev(maj, min));
  case 'n': {
    int sk;
    struct ifreq ifr;
    struct udev_device *dev;
    int ifindex;
    ifindex = strtoul(&id[1], NULL, 10);
    if (ifindex <= 0)
      return NULL;
    sk = socket(PF_INET, SOCK_DGRAM, 0);
    if (sk < 0)
      return NULL;
    memset(&ifr, 0x00, sizeof(struct ifreq));
    ifr.ifr_ifindex = ifindex;
    if (ioctl(sk, SIOCGIFNAME, &ifr) != 0) {
      close(sk);
      return NULL;
    }
    close(sk);
    dev = udev_device_new_from_subsystem_sysname(udev, "net", ifr.ifr_name);
    if (dev == NULL)
      return NULL;
    if (udev_device_get_ifindex(dev) == ifindex)
      return dev;
    udev_device_unref(dev);
    return NULL;
  }
  case '+':
    strscpy(subsys, sizeof(subsys), &id[1]);
    sysname = strchr(subsys, ':');
    if (sysname == NULL)
      return NULL;
    sysname[0] = '\0';
    sysname = &sysname[1];
    return udev_device_new_from_subsystem_sysname(udev, subsys, sysname);
  default:
    return NULL;
  }
}


x86 assembly (clang, -O0):
<udev_device_new_from_device_id>:
push   %rbp
mov    %rsp,%rbp
sub    $0x470,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
mov    %eax,-0x460(%rbp)
sub    $0x2b,%eax
je     13e9 <udev_device_new_from_device_id+0x219>
jmp    11fe <udev_device_new_from_device_id+0x2e>
mov    -0x460(%rbp),%eax
add    $0xffffff9e,%eax
sub    $0x2,%eax
jb     1229 <udev_device_new_from_device_id+0x59>
jmp    1215 <udev_device_new_from_device_id+0x45>
mov    -0x460(%rbp),%eax
sub    $0x6e,%eax
je     129a <udev_device_new_from_device_id+0xca>
jmp    1471 <udev_device_new_from_device_id+0x2a1>
mov    -0x18(%rbp),%rdi
lea    0xdcc(%rip),%rsi
lea    -0x19(%rbp),%rdx
lea    -0x20(%rbp),%rcx
lea    -0x24(%rbp),%r8
call   10a0 <sscanf@plt>
cmp    $0x3,%eax
je     125b <udev_device_new_from_device_id+0x8b>
movq   $0x0,-0x8(%rbp)
jmp    1479 <udev_device_new_from_device_id+0x2a9>
mov    -0x10(%rbp),%rax
mov    %rax,-0x470(%rbp)
mov    -0x19(%rbp),%al
mov    %al,-0x461(%rbp)
mov    -0x20(%rbp),%edi
mov    -0x24(%rbp),%esi
call   10b0 <makedev@plt>
mov    -0x470(%rbp),%rdi
mov    %eax,%edx
mov    -0x461(%rbp),%al
movsbl %al,%esi
call   10d0 <udev_device_new_from_devnum@plt>
mov    %rax,-0x8(%rbp)
jmp    1479 <udev_device_new_from_device_id+0x2a9>
mov    -0x18(%rbp),%rdi
add    $0x1,%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0xa,%edx
call   10c0 <strtoul@plt>
mov    %eax,-0x45c(%rbp)
cmpl   $0x0,-0x45c(%rbp)
jg     12d0 <udev_device_new_from_device_id+0x100>
movq   $0x0,-0x8(%rbp)
jmp    1479 <udev_device_new_from_device_id+0x2a9>
mov    0x2cf9(%rip),%rax
mov    (%rax),%edi
mov    0x2d08(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10f0 <socket@plt>
mov    %eax,-0x43c(%rbp)
cmpl   $0x0,-0x43c(%rbp)
jge    1309 <udev_device_new_from_device_id+0x139>
movq   $0x0,-0x8(%rbp)
jmp    1479 <udev_device_new_from_device_id+0x2a9>
lea    -0x450(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1060 <memset@plt>
mov    -0x45c(%rbp),%eax
mov    %eax,-0x450(%rbp)
mov    -0x43c(%rbp),%edi
mov    0x2ca3(%rip),%rax
mov    (%rax),%esi
lea    -0x450(%rbp),%rdx
call   1070 <ioctl@plt>
cmp    $0x0,%eax
je     1364 <udev_device_new_from_device_id+0x194>
mov    -0x43c(%rbp),%edi
call   1080 <close@plt>
movq   $0x0,-0x8(%rbp)
jmp    1479 <udev_device_new_from_device_id+0x2a9>
mov    -0x43c(%rbp),%edi
call   1080 <close@plt>
mov    -0x10(%rbp),%rdi
mov    -0x448(%rbp),%rdx
lea    0xc87(%rip),%rsi
call   1030 <udev_device_new_from_subsystem_sysname@plt>
mov    %rax,-0x458(%rbp)
cmpq   $0x0,-0x458(%rbp)
jne    13a8 <udev_device_new_from_device_id+0x1d8>
movq   $0x0,-0x8(%rbp)
jmp    1479 <udev_device_new_from_device_id+0x2a9>
mov    -0x458(%rbp),%rdi
call   1040 <udev_device_get_ifindex@plt>
cmp    -0x45c(%rbp),%eax
jne    13d0 <udev_device_new_from_device_id+0x200>
mov    -0x458(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1479 <udev_device_new_from_device_id+0x2a9>
mov    -0x458(%rbp),%rdi
call   1090 <udev_device_unref@plt>
movq   $0x0,-0x8(%rbp)
jmp    1479 <udev_device_new_from_device_id+0x2a9>
lea    -0x430(%rbp),%rdi
mov    -0x18(%rbp),%rdx
add    $0x1,%rdx
mov    $0x400,%esi
call   10e0 <strscpy@plt>
lea    -0x430(%rbp),%rdi
mov    $0x3a,%esi
call   1050 <strchr@plt>
mov    %rax,-0x438(%rbp)
cmpq   $0x0,-0x438(%rbp)
jne    1435 <udev_device_new_from_device_id+0x265>
movq   $0x0,-0x8(%rbp)
jmp    1479 <udev_device_new_from_device_id+0x2a9>
mov    -0x438(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x438(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x438(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x430(%rbp),%rsi
mov    -0x438(%rbp),%rdx
call   1030 <udev_device_new_from_subsystem_sysname@plt>
mov    %rax,-0x8(%rbp)
jmp    1479 <udev_device_new_from_device_id+0x2a9>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x470,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<udev_device_new_from_device_id>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x410,%rsp
mov    %rsi,%rax
mov    %rdi,%r14
movsbl (%rsi),%ecx
lea    -0x62(%rcx),%edx
cmp    $0x2,%edx
jb     12ca <udev_device_new_from_device_id+0xfa>
cmp    $0x2b,%ecx
je     130d <udev_device_new_from_device_id+0x13d>
cmp    $0x6e,%ecx
jne    1354 <udev_device_new_from_device_id+0x184>
add    $0x1,%rax
xor    %ebx,%ebx
mov    %rax,%rdi
xor    %esi,%esi
mov    $0xa,%edx
call   10c0 <strtoul@plt>
test   %eax,%eax
jle    1356 <udev_device_new_from_device_id+0x186>
mov    %eax,%r15d
mov    0x2da3(%rip),%rax
mov    (%rax),%edi
mov    0x2db2(%rip),%rax
mov    (%rax),%esi
xor    %ebx,%ebx
xor    %edx,%edx
call   10f0 <socket@plt>
test   %eax,%eax
js     1356 <udev_device_new_from_device_id+0x186>
mov    %eax,%ebp
lea    0x10(%rsp),%r12
xor    %ebx,%ebx
mov    %r12,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1060 <memset@plt>
mov    %r15d,0x10(%rsp)
mov    0x2d6b(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
mov    %r12,%rdx
call   1070 <ioctl@plt>
mov    %eax,%r12d
mov    %ebp,%edi
call   1080 <close@plt>
test   %r12d,%r12d
jne    1356 <udev_device_new_from_device_id+0x186>
mov    0x18(%rsp),%rdx
lea    0xd70(%rip),%rsi
mov    %r14,%rdi
call   1030 <udev_device_new_from_subsystem_sysname@plt>
test   %rax,%rax
je     1354 <udev_device_new_from_device_id+0x184>
mov    %rax,%rbx
mov    %rax,%rdi
call   1040 <udev_device_get_ifindex@plt>
cmp    %r15d,%eax
je     1356 <udev_device_new_from_device_id+0x186>
mov    %rbx,%rdi
call   1090 <udev_device_unref@plt>
jmp    1354 <udev_device_new_from_device_id+0x184>
lea    0xd2f(%rip),%rsi
lea    0xb(%rsp),%rdx
lea    0x10(%rsp),%rcx
lea    0xc(%rsp),%r8
mov    %rax,%rdi
call   10a0 <sscanf@plt>
cmp    $0x3,%eax
jne    1354 <udev_device_new_from_device_id+0x184>
movsbl 0xb(%rsp),%ebx
mov    0x10(%rsp),%edi
mov    0xc(%rsp),%esi
call   10b0 <makedev@plt>
mov    %r14,%rdi
mov    %ebx,%esi
mov    %eax,%edx
call   10d0 <udev_device_new_from_devnum@plt>
jmp    134f <udev_device_new_from_device_id+0x17f>
add    $0x1,%rax
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x400,%esi
mov    %rax,%rdx
call   10e0 <strscpy@plt>
mov    %rbx,%rdi
mov    $0x3a,%esi
call   1050 <strchr@plt>
test   %rax,%rax
je     1354 <udev_device_new_from_device_id+0x184>
movb   $0x0,(%rax)
add    $0x1,%rax
lea    0x10(%rsp),%rsi
mov    %r14,%rdi
mov    %rax,%rdx
call   1030 <udev_device_new_from_subsystem_sysname@plt>
mov    %rax,%rbx
jmp    1356 <udev_device_new_from_device_id+0x186>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x410,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.udev_device = type { i32 }
%struct.udev = type { i32 }
%struct.ifreq = type { i32, i8* }
@.str = [8 x i8] c"%c%i:%i\00"
@PF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@SIOCGIFNAME = global i32 0
@.str.1 = [4 x i8] c"net\00"
define %struct.udev_device* @udev_device_new_from_device_id(%struct.udev* %0, i8* %1) {
%3 = alloca %struct.udev_device*
%4 = alloca %struct.udev*
%5 = alloca i8*
%6 = alloca i8
%7 = alloca i32
%8 = alloca i32
%9 = alloca [1024 x i8]
%10 = alloca i8*
%11 = alloca i32
%12 = alloca %struct.ifreq
%13 = alloca %struct.udev_device*
%14 = alloca i32
store %struct.udev* %0, %struct.udev** %4
store i8* %1, i8** %5
%15 = load i8*, i8** %5
%16 = getelementptr inbounds i8, i8* %15, i64 0
%17 = load i8, i8* %16
%18 = sext i8 %17 to i32
switch i32 %18, label %95 [
i32 98, label %19
i32 99, label %19
i32 110, label %31
i32 43, label %76
]
19: ; preds = %2, %2
%20 = load i8*, i8** %5
%21 = call i32 @sscanf(i8* %20, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* %6, i32* %7, i32* %8)
%22 = icmp ne i32 %21, 3
br i1 %22, label %23, label %24
23: ; preds = %19
store %struct.udev_device* null, %struct.udev_device** %3
br label %96
24: ; preds = %19
%25 = load %struct.udev*, %struct.udev** %4
%26 = load i8, i8* %6
%27 = load i32, i32* %7
%28 = load i32, i32* %8
%29 = call i32 @makedev(i32 %27, i32 %28)
%30 = call %struct.udev_device* @udev_device_new_from_devnum(%struct.udev* %25, i8 signext %26, i32 %29)
store %struct.udev_device* %30, %struct.udev_device** %3
br label %96
31: ; preds = %2
%32 = load i8*, i8** %5
%33 = getelementptr inbounds i8, i8* %32, i64 1
%34 = call i32 @strtoul(i8* %33, i32* null, i32 10)
store i32 %34, i32* %14
%35 = load i32, i32* %14
%36 = icmp sle i32 %35, 0
br i1 %36, label %37, label %38
37: ; preds = %31
store %struct.udev_device* null, %struct.udev_device** %3
br label %96
38: ; preds = %31
%39 = load i32, i32* @PF_INET
%40 = load i32, i32* @SOCK_DGRAM
%41 = call i32 @socket(i32 %39, i32 %40, i32 0)
store i32 %41, i32* %11
%42 = load i32, i32* %11
%43 = icmp slt i32 %42, 0
br i1 %43, label %44, label %45
44: ; preds = %38
store %struct.udev_device* null, %struct.udev_device** %3
br label %96
45: ; preds = %38
%46 = call i32 @memset(%struct.ifreq* %12, i32 0, i32 16)
%47 = load i32, i32* %14
%48 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %12, i32 0, i32 0
store i32 %47, i32* %48
%49 = load i32, i32* %11
%50 = load i32, i32* @SIOCGIFNAME
%51 = call i32 @ioctl(i32 %49, i32 %50, %struct.ifreq* %12)
%52 = icmp ne i32 %51, 0
br i1 %52, label %53, label %56
53: ; preds = %45
%54 = load i32, i32* %11
%55 = call i32 @close(i32 %54)
store %struct.udev_device* null, %struct.udev_device** %3
br label %96
56: ; preds = %45
%57 = load i32, i32* %11
%58 = call i32 @close(i32 %57)
%59 = load %struct.udev*, %struct.udev** %4
%60 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %12, i32 0, i32 1
%61 = load i8*, i8** %60
%62 = call %struct.udev_device* @udev_device_new_from_subsystem_sysname(%struct.udev* %59, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %61)
store %struct.udev_device* %62, %struct.udev_device** %13
%63 = load %struct.udev_device*, %struct.udev_device** %13
%64 = icmp eq %struct.udev_device* %63, null
br i1 %64, label %65, label %66
65: ; preds = %56
store %struct.udev_device* null, %struct.udev_device** %3
br label %96
66: ; preds = %56
%67 = load %struct.udev_device*, %struct.udev_device** %13
%68 = call i32 @udev_device_get_ifindex(%struct.udev_device* %67)
%69 = load i32, i32* %14
%70 = icmp eq i32 %68, %69
br i1 %70, label %71, label %73
71: ; preds = %66
%72 = load %struct.udev_device*, %struct.udev_device** %13
store %struct.udev_device* %72, %struct.udev_device** %3
br label %96
73: ; preds = %66
%74 = load %struct.udev_device*, %struct.udev_device** %13
%75 = call i32 @udev_device_unref(%struct.udev_device* %74)
store %struct.udev_device* null, %struct.udev_device** %3
br label %96
76: ; preds = %2
%77 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%78 = load i8*, i8** %5
%79 = getelementptr inbounds i8, i8* %78, i64 1
%80 = call i32 @strscpy(i8* %77, i32 1024, i8* %79)
%81 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%82 = call i8* @strchr(i8* %81, i8 signext 58)
store i8* %82, i8** %10
%83 = load i8*, i8** %10
%84 = icmp eq i8* %83, null
br i1 %84, label %85, label %86
85: ; preds = %76
store %struct.udev_device* null, %struct.udev_device** %3
br label %96
86: ; preds = %76
%87 = load i8*, i8** %10
%88 = getelementptr inbounds i8, i8* %87, i64 0
store i8 0, i8* %88
%89 = load i8*, i8** %10
%90 = getelementptr inbounds i8, i8* %89, i64 1
store i8* %90, i8** %10
%91 = load %struct.udev*, %struct.udev** %4
%92 = getelementptr inbounds [1024 x i8], [1024 x i8]* %9, i64 0, i64 0
%93 = load i8*, i8** %10
%94 = call %struct.udev_device* @udev_device_new_from_subsystem_sysname(%struct.udev* %91, i8* %92, i8* %93)
store %struct.udev_device* %94, %struct.udev_device** %3
br label %96
95: ; preds = %2
store %struct.udev_device* null, %struct.udev_device** %3
br label %96
96: ; preds = %95, %86, %85, %73, %71, %65, %53, %44, %37, %24, %23
%97 = load %struct.udev_device*, %struct.udev_device** %3
ret %struct.udev_device* %97
}

LLVM IR (-O2):
%struct.udev_device = type { i32 }
%struct.udev = type { i32 }
%struct.ifreq = type { i32, i8* }
@.str = [8 x i8] c"%c%i:%i\00"
@PF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@SIOCGIFNAME = global i32 0
@.str.1 = [4 x i8] c"net\00"
define %struct.udev_device* @udev_device_new_from_device_id(%struct.udev* %0, i8* %1) {
%3 = alloca i8
%4 = alloca i32
%5 = alloca i32
%6 = alloca [1024 x i8]
%7 = alloca %struct.ifreq
%8 = bitcast i32* %4 to i8*
%9 = bitcast i32* %5 to i8*
%10 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%11 = load i8, i8* %1
%12 = sext i8 %11 to i32
switch i32 %12, label %59 [
i32 98, label %13
i32 99, label %13
i32 110, label %22
i32 43, label %51
]
13: ; preds = %2, %2
%14 = call i32 @sscanf(i8* %1, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* %3, i32* %4, i32* %5)
%15 = icmp eq i32 %14, 3
br i1 %15, label %16, label %59
16: ; preds = %13
%17 = load i8, i8* %3
%18 = load i32, i32* %4
%19 = load i32, i32* %5
%20 = call i32 @makedev(i32 %18, i32 %19)
%21 = call %struct.udev_device* @udev_device_new_from_devnum(%struct.udev* %0, i8 signext %17, i32 %20)
br label %59
22: ; preds = %2
%23 = bitcast %struct.ifreq* %7 to i8*
%24 = getelementptr inbounds i8, i8* %1, i64 1
%25 = tail call i32 @strtoul(i8* nocapture %24, i32* null, i32 10)
%26 = icmp slt i32 %25, 1
br i1 %26, label %49, label %27
27: ; preds = %22
%28 = load i32, i32* @PF_INET
%29 = load i32, i32* @SOCK_DGRAM
%30 = tail call i32 @socket(i32 %28, i32 %29, i32 0)
%31 = icmp slt i32 %30, 0
br i1 %31, label %49, label %32
32: ; preds = %27
%33 = call i32 @memset(%struct.ifreq* %7, i32 0, i32 16)
%34 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %7, i64 0, i32 0
store i32 %25, i32* %34
%35 = load i32, i32* @SIOCGIFNAME
%36 = call i32 @ioctl(i32 %30, i32 %35, %struct.ifreq* %7)
%37 = icmp eq i32 %36, 0
%38 = call i32 @close(i32 %30)
br i1 %37, label %39, label %49
39: ; preds = %32
%40 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %7, i64 0, i32 1
%41 = load i8*, i8** %40
%42 = call %struct.udev_device* @udev_device_new_from_subsystem_sysname(%struct.udev* %0, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %41)
%43 = icmp eq %struct.udev_device* %42, null
br i1 %43, label %49, label %44
44: ; preds = %39
%45 = call i32 @udev_device_get_ifindex(%struct.udev_device* %42)
%46 = icmp eq i32 %45, %25
br i1 %46, label %49, label %47
47: ; preds = %44
%48 = call i32 @udev_device_unref(%struct.udev_device* %42)
br label %49
49: ; preds = %32, %44, %39, %27, %22, %47
%50 = phi %struct.udev_device* [ null, %47 ], [ null, %22 ], [ null, %27 ], [ null, %39 ], [ %42, %44 ], [ null, %32 ]
br label %59
51: ; preds = %2
%52 = getelementptr inbounds i8, i8* %1, i64 1
%53 = call i32 @strscpy(i8* %10, i32 1024, i8* %52)
%54 = call i8* @strchr(i8* %10, i8 signext 58)
%55 = icmp eq i8* %54, null
br i1 %55, label %59, label %56
56: ; preds = %51
store i8 0, i8* %54
%57 = getelementptr inbounds i8, i8* %54, i64 1
%58 = call %struct.udev_device* @udev_device_new_from_subsystem_sysname(%struct.udev* %0, i8* %10, i8* %57)
br label %59
59: ; preds = %2, %51, %13, %56, %49, %16
%60 = phi %struct.udev_device* [ %58, %56 ], [ %50, %49 ], [ %21, %16 ], [ null, %13 ], [ null, %51 ], [ null, %2 ]
ret %struct.udev_device* %60
}