#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
struct ip_tunnel_parm {
  int dummy;
};
struct TYPE_2__ {
  void *ifru_data;
};
struct ifreq {
  TYPE_1__ ifr_ifru;
  int /*<<< orphan*/ ifr_name;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
scalar_t__ SIOCDEVPRIVATE;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ close(int);
int ioctl(int, scalar_t__, struct ifreq *);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(int /*<<< orphan*/, char *);
int do_get_ioctl(char *basedev, struct ip_tunnel_parm *p) {
  struct ifreq ifr;
  int fd;
  int err;
  strcpy(ifr.ifr_name, basedev);
  ifr.ifr_ifru.ifru_data = (void *)p;
  fd = socket(AF_INET, SOCK_DGRAM, 0);
  err = ioctl(fd, (SIOCDEVPRIVATE + 0), &ifr);
  if (err)
    perror("ioctl");
  close(fd);
  return err;
}


x86 assembly (clang, -O0):
<do_get_ioctl>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x18(%rbp),%edi
mov    -0x8(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    0x2e6d(%rip),%rax
mov    (%rax),%edi
mov    0x2e5c(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%edi
mov    0x2e2e(%rip),%rax
mov    (%rax),%rsi
add    $0x0,%rsi
lea    -0x20(%rbp),%rdx
call   1040 <ioctl@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
je     11c3 <do_get_ioctl+0x73>
lea    0xe42(%rip),%rdi
call   1060 <perror@plt>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
mov    -0x28(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_get_ioctl>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%rbx
mov    %rdi,%rsi
call   1030 <strcpy@plt>
mov    %rbx,0x8(%rsp)
mov    0x2e7b(%rip),%rax
mov    (%rax),%edi
mov    0x2e6a(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,%ebx
mov    0x2e40(%rip),%rax
mov    (%rax),%rsi
lea    0x8(%rsp),%rdx
mov    %ebx,%edi
call   1040 <ioctl@plt>
mov    %eax,%ebp
test   %eax,%eax
jne    11ad <do_get_ioctl+0x5d>
mov    %ebx,%edi
call   1050 <close@plt>
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe4c(%rip),%rdi
call   1060 <perror@plt>
jmp    119d <do_get_ioctl+0x4d>

LLVM IR (-O0):
%struct.ip_tunnel_parm = type { i32 }
%struct.ifreq = type { %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i8* }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@SIOCDEVPRIVATE = global i64 0
@.str = [6 x i8] c"ioctl\00"
define i32 @do_get_ioctl(i8* %0, %struct.ip_tunnel_parm* %1) {
%3 = alloca i8*
%4 = alloca %struct.ip_tunnel_parm*
%5 = alloca %struct.ifreq
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %3
store %struct.ip_tunnel_parm* %1, %struct.ip_tunnel_parm** %4
%8 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %5, i32 0, i32 1
%9 = load i32, i32* %8
%10 = load i8*, i8** %3
%11 = call i32 @strcpy(i32 %9, i8* %10)
%12 = load %struct.ip_tunnel_parm*, %struct.ip_tunnel_parm** %4
%13 = bitcast %struct.ip_tunnel_parm* %12 to i8*
%14 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %5, i32 0, i32 0
%15 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %14, i32 0, i32 0
store i8* %13, i8** %15
%16 = load i32, i32* @AF_INET
%17 = load i32, i32* @SOCK_DGRAM
%18 = call i32 @socket(i32 %16, i32 %17, i32 0)
store i32 %18, i32* %6
%19 = load i32, i32* %6
%20 = load i64, i64* @SIOCDEVPRIVATE
%21 = add nsw i64 %20, 0
%22 = call i32 @ioctl(i32 %19, i64 %21, %struct.ifreq* %5)
store i32 %22, i32* %7
%23 = load i32, i32* %7
%24 = icmp ne i32 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %2
%26 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
br label %27
27: ; preds = %25, %2
%28 = load i32, i32* %6
%29 = call i32 @close(i32 %28)
%30 = load i32, i32* %7
ret i32 %30
}

LLVM IR (-O2):
%struct.ip_tunnel_parm = type { i32 }
%struct.ifreq = type { %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i8* }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@SIOCDEVPRIVATE = global i64 0
@.str = [6 x i8] c"ioctl\00"
define i32 @do_get_ioctl(i8* %0, %struct.ip_tunnel_parm* %1) {
%3 = alloca %struct.ifreq
%4 = bitcast %struct.ifreq* %3 to i8*
%5 = tail call i32 @strcpy(i32 undef, i8* %0)
%6 = bitcast %struct.ifreq* %3 to %struct.ip_tunnel_parm**
store %struct.ip_tunnel_parm* %1, %struct.ip_tunnel_parm** %6
%7 = load i32, i32* @AF_INET
%8 = load i32, i32* @SOCK_DGRAM
%9 = tail call i32 @socket(i32 %7, i32 %8, i32 0)
%10 = load i64, i64* @SIOCDEVPRIVATE
%11 = call i32 @ioctl(i32 %9, i64 %10, %struct.ifreq* %3)
%12 = icmp eq i32 %11, 0
br i1 %12, label %15, label %13
13: ; preds = %2
%14 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
br label %15
15: ; preds = %13, %2
%16 = call i32 @close(i32 %9)
ret i32 %11
}