#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 */
struct ifreq {
  int /*<<< orphan*/ ifr_name;
  void *ifr_data;
};

/* Variables and functions */
int /*<<< orphan*/ IFNAMSIZ;
scalar_t__ SIOCDEVPRIVATE;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ _IO(char, int);
int /*<<< orphan*/ clean_exit(int);
int /*<<< orphan*/ close(int);
scalar_t__ ioctl(int, scalar_t__, ...);
int /*<<< orphan*/ perror(char *);
int socket(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char *, int /*<<< orphan*/);
void attach_dongle(int ttyfd, char *dev, int dongle) {
  if (ttyfd != -1) {
    ioctl(ttyfd, _IO('e', 1), dongle);
  } else {
    int sockfd;
    struct ifreq ifr;
    sockfd = socket(23, SOCK_STREAM, 0);
    if (sockfd < 0) {
      perror("socket");
      clean_exit(-1);
    }
    ifr.ifr_data = (void *)dongle;
    strncpy(ifr.ifr_name, dev, IFNAMSIZ);
    if (ioctl(sockfd, (SIOCDEVPRIVATE + 0), &ifr) < 0) {
      perror("ioctl");
      close(sockfd);
      clean_exit(-1);
    }
    close(sockfd);
  }
}


x86 assembly (clang, -O0):
<attach_dongle>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
cmpl   $0xffffffff,-0x4(%rbp)
je     11b6 <attach_dongle+0x46>
mov    -0x4(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    $0x65,%edi
mov    $0x1,%esi
call   1040 <_IO@plt>
mov    -0x2c(%rbp),%edi
movslq %eax,%rsi
mov    -0x14(%rbp),%edx
mov    $0x0,%al
call   1060 <ioctl@plt>
jmp    1257 <attach_dongle+0xe7>
mov    0x2e1b(%rip),%rax
mov    (%rax),%esi
mov    $0x17,%edi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    11ee <attach_dongle+0x7e>
lea    0xe21(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%edi
call   1050 <clean_exit@plt>
movslq -0x14(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x28(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    0x2ddc(%rip),%rax
mov    (%rax),%edx
call   1030 <strncpy@plt>
mov    -0x18(%rbp),%edi
mov    0x2db3(%rip),%rax
mov    (%rax),%rsi
add    $0x0,%rsi
lea    -0x28(%rbp),%rdx
mov    $0x0,%al
call   1060 <ioctl@plt>
cmp    $0x0,%rax
jge    124f <attach_dongle+0xdf>
lea    0xdcf(%rip),%rdi
call   1080 <perror@plt>
mov    -0x18(%rbp),%edi
call   1070 <close@plt>
mov    $0xffffffff,%edi
call   1050 <clean_exit@plt>
mov    -0x18(%rbp),%edi
call   1070 <close@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<attach_dongle>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %edx,%ebp
cmp    $0xffffffff,%edi
je     11aa <attach_dongle+0x3a>
mov    %edi,%ebx
mov    $0x65,%edi
mov    $0x1,%esi
call   1040 <_IO@plt>
movslq %eax,%rsi
mov    %ebx,%edi
mov    %ebp,%edx
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1060 <ioctl@plt>
mov    %rsi,%r15
mov    0x2e24(%rip),%rax
mov    (%rax),%esi
mov    $0x17,%edi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,%r14d
test   %eax,%eax
js     1213 <attach_dongle+0xa3>
movslq %ebp,%rax
mov    %rax,0x10(%rsp)
mov    0x2e08(%rip),%rax
mov    (%rax),%edx
mov    %r15,%rsi
call   1030 <strncpy@plt>
mov    0x2ddf(%rip),%rax
mov    (%rax),%rsi
lea    0x8(%rsp),%rdx
mov    %r14d,%edi
xor    %eax,%eax
call   1060 <ioctl@plt>
test   %rax,%rax
js     122b <attach_dongle+0xbb>
mov    %r14d,%edi
call   1070 <close@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xde6(%rip),%rdi
call   1080 <perror@plt>
mov    $0xffffffff,%edi
call   1050 <clean_exit@plt>
jmp    11c9 <attach_dongle+0x59>
lea    0xdd5(%rip),%rdi
call   1080 <perror@plt>
mov    %r14d,%edi
call   1070 <close@plt>
mov    $0xffffffff,%edi
call   1050 <clean_exit@plt>
jmp    1200 <attach_dongle+0x90>

LLVM IR (-O0):
%struct.ifreq = type { i32, i8* }
@SOCK_STREAM = global i32 0
@.str = [7 x i8] c"socket\00"
@IFNAMSIZ = global i32 0
@SIOCDEVPRIVATE = global i64 0
@.str.1 = [6 x i8] c"ioctl\00"
define void @attach_dongle(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.ifreq
store i32 %0, i32* %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%9 = load i32, i32* %4
%10 = icmp ne i32 %9, -1
br i1 %10, label %11, label %17
11: ; preds = %3
%12 = load i32, i32* %4
%13 = call i32 @_IO(i8 signext 101, i32 1)
%14 = sext i32 %13 to i64
%15 = load i32, i32* %6
%16 = call i64 (i32, i64, ...) @ioctl(i32 %12, i64 %14, i32 %15)
br label %48
17: ; preds = %3
%18 = load i32, i32* @SOCK_STREAM
%19 = call i32 @socket(i32 23, i32 %18, i32 0)
store i32 %19, i32* %7
%20 = load i32, i32* %7
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %17
%23 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%24 = call i32 @clean_exit(i32 -1)
br label %25
25: ; preds = %22, %17
%26 = load i32, i32* %6
%27 = sext i32 %26 to i64
%28 = inttoptr i64 %27 to i8*
%29 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %8, i32 0, i32 1
store i8* %28, i8** %29
%30 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %8, i32 0, i32 0
%31 = load i32, i32* %30
%32 = load i8*, i8** %5
%33 = load i32, i32* @IFNAMSIZ
%34 = call i32 @strncpy(i32 %31, i8* %32, i32 %33)
%35 = load i32, i32* %7
%36 = load i64, i64* @SIOCDEVPRIVATE
%37 = add nsw i64 %36, 0
%38 = call i64 (i32, i64, ...) @ioctl(i32 %35, i64 %37, %struct.ifreq* %8)
%39 = icmp slt i64 %38, 0
br i1 %39, label %40, label %45
40: ; preds = %25
%41 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%42 = load i32, i32* %7
%43 = call i32 @close(i32 %42)
%44 = call i32 @clean_exit(i32 -1)
br label %45
45: ; preds = %40, %25
%46 = load i32, i32* %7
%47 = call i32 @close(i32 %46)
br label %48
48: ; preds = %45, %11
ret void
}

LLVM IR (-O2):
%struct.ifreq = type { i32, i8* }
@SOCK_STREAM = global i32 0
@.str = [7 x i8] c"socket\00"
@IFNAMSIZ = global i32 0
@SIOCDEVPRIVATE = global i64 0
@.str.1 = [6 x i8] c"ioctl\00"
define void @attach_dongle(i32 %0, i8* %1, i32 %2) {
%4 = alloca %struct.ifreq
%5 = icmp eq i32 %0, -1
br i1 %5, label %10, label %6
6: ; preds = %3
%7 = tail call i32 @_IO(i8 signext 101, i32 1)
%8 = sext i32 %7 to i64
%9 = tail call i64 (i32, i64, ...) @ioctl(i32 %0, i64 %8, i32 %2)
br label %33
10: ; preds = %3
%11 = bitcast %struct.ifreq* %4 to i8*
%12 = load i32, i32* @SOCK_STREAM
%13 = tail call i32 @socket(i32 23, i32 %12, i32 0)
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %10
%16 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%17 = tail call i32 @clean_exit(i32 -1)
br label %18
18: ; preds = %15, %10
%19 = sext i32 %2 to i64
%20 = inttoptr i64 %19 to i8*
%21 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %4, i64 0, i32 1
store i8* %20, i8** %21
%22 = load i32, i32* @IFNAMSIZ
%23 = tail call i32 @strncpy(i32 undef, i8* %1, i32 %22)
%24 = load i64, i64* @SIOCDEVPRIVATE
%25 = call i64 (i32, i64, ...) @ioctl(i32 %13, i64 %24, %struct.ifreq* %4)
%26 = icmp slt i64 %25, 0
br i1 %26, label %27, label %31
27: ; preds = %18
%28 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%29 = call i32 @close(i32 %13)
%30 = call i32 @clean_exit(i32 -1)
br label %31
31: ; preds = %27, %18
%32 = call i32 @close(i32 %13)
br label %33
33: ; preds = %31, %6
ret void
}