#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 ifr_flags;
  int /*<<< orphan*/ ifr_name;
};
typedef int /*<<< orphan*/ req;

/* Variables and functions */
int /*<<< orphan*/ IFNAMSIZ;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ TUNSETIFF;
int /*<<< orphan*/ close(int);
int ioctl(int, int /*<<< orphan*/, void *);
int /*<<< orphan*/ memset(struct ifreq *, int /*<<< orphan*/, int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ strcpy(char *, int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char *, int /*<<< orphan*/);
int getTunTap(char *name, int flags) {
  struct ifreq req;
  int fd, err;
  char *device = "/dev/net/tun";
  if ((fd = open(device, O_RDWR)) < 0) {
    perror("open /dev/net/tun");
    return fd;
  }
  memset(&req, 0, sizeof(req));
  if (*name) {
    strncpy(req.ifr_name, name, IFNAMSIZ);
  }
  req.ifr_flags = flags;
  if ((err = ioctl(fd, TUNSETIFF, (void *)&req)) < 0) {
    perror("ioctl on tun device");
    close(fd);
    return err;
  }
  strcpy(name, req.ifr_name);
  return fd;
}


x86 assembly (clang, -O0):
<getTunTap>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
lea    0xe7f(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    0x2e53(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x24(%rbp)
cmp    $0x0,%eax
jge    11bf <getTunTap+0x4f>
lea    0xe51(%rip),%rdi
call   1090 <perror@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1249 <getTunTap+0xd9>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
mov    -0x10(%rbp),%rax
cmpb   $0x0,(%rax)
je     11f1 <getTunTap+0x81>
mov    -0x1c(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    0x2dee(%rip),%rax
mov    (%rax),%edx
call   1030 <strncpy@plt>
mov    -0x14(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x24(%rbp),%edi
mov    0x2dcf(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1060 <ioctl@plt>
mov    %eax,-0x28(%rbp)
cmp    $0x0,%eax
jge    1237 <getTunTap+0xc7>
lea    0xdf3(%rip),%rdi
call   1090 <perror@plt>
mov    -0x24(%rbp),%edi
call   1070 <close@plt>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1249 <getTunTap+0xd9>
mov    -0x10(%rbp),%rdi
mov    -0x1c(%rbp),%esi
call   1040 <strcpy@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getTunTap>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%r14d
mov    %rdi,%rbx
mov    0x2e64(%rip),%rax
mov    (%rax),%esi
lea    0xe78(%rip),%rdi
call   1080 <open@plt>
mov    %eax,%r15d
test   %eax,%eax
js     11f8 <getTunTap+0x88>
mov    %rsp,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
cmpb   $0x0,(%rbx)
je     11c2 <getTunTap+0x52>
mov    0x4(%rsp),%edi
mov    0x2e20(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rsi
call   1030 <strncpy@plt>
mov    %r14d,(%rsp)
mov    0x2e03(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %r15d,%edi
call   1060 <ioctl@plt>
test   %eax,%eax
js     1206 <getTunTap+0x96>
mov    0x4(%rsp),%esi
mov    %rbx,%rdi
call   1040 <strcpy@plt>
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe01(%rip),%rdi
call   1090 <perror@plt>
jmp    11ea <getTunTap+0x7a>
mov    %eax,%ebp
lea    0xe03(%rip),%rdi
call   1090 <perror@plt>
mov    %r15d,%edi
call   1070 <close@plt>
mov    %ebp,%r15d
jmp    11ea <getTunTap+0x7a>

LLVM IR (-O0):
%struct.ifreq = type { i32, i32 }
@.str = [13 x i8] c"/dev/net/tun\00"
@O_RDWR = global i32 0
@.str.1 = [18 x i8] c"open /dev/net/tun\00"
@IFNAMSIZ = global i32 0
@TUNSETIFF = global i32 0
@.str.2 = [20 x i8] c"ioctl on tun device\00"
define i32 @getTunTap(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca %struct.ifreq
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
store i8* %0, i8** %4
store i32 %1, i32* %5
store i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i8** %9
%10 = load i8*, i8** %9
%11 = load i32, i32* @O_RDWR
%12 = call i32 @open(i8* %10, i32 %11)
store i32 %12, i32* %7
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %2
%15 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
%16 = load i32, i32* %7
store i32 %16, i32* %3
br label %47
17: ; preds = %2
%18 = call i32 @memset(%struct.ifreq* %6, i32 0, i32 8)
%19 = load i8*, i8** %4
%20 = load i8, i8* %19
%21 = icmp ne i8 %20, 0
br i1 %21, label %22, label %28
22: ; preds = %17
%23 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 1
%24 = load i32, i32* %23
%25 = load i8*, i8** %4
%26 = load i32, i32* @IFNAMSIZ
%27 = call i32 @strncpy(i32 %24, i8* %25, i32 %26)
br label %28
28: ; preds = %22, %17
%29 = load i32, i32* %5
%30 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 0
store i32 %29, i32* %30
%31 = load i32, i32* %7
%32 = load i32, i32* @TUNSETIFF
%33 = bitcast %struct.ifreq* %6 to i8*
%34 = call i32 @ioctl(i32 %31, i32 %32, i8* %33)
store i32 %34, i32* %8
%35 = icmp slt i32 %34, 0
br i1 %35, label %36, label %41
36: ; preds = %28
%37 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
%38 = load i32, i32* %7
%39 = call i32 @close(i32 %38)
%40 = load i32, i32* %8
store i32 %40, i32* %3
br label %47
41: ; preds = %28
%42 = load i8*, i8** %4
%43 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %6, i32 0, i32 1
%44 = load i32, i32* %43
%45 = call i32 @strcpy(i8* %42, i32 %44)
%46 = load i32, i32* %7
store i32 %46, i32* %3
br label %47
47: ; preds = %41, %36, %14
%48 = load i32, i32* %3
ret i32 %48
}

LLVM IR (-O2):
%struct.ifreq = type { i32, i32 }
@.str = [13 x i8] c"/dev/net/tun\00"
@O_RDWR = global i32 0
@.str.1 = [18 x i8] c"open /dev/net/tun\00"
@IFNAMSIZ = global i32 0
@TUNSETIFF = global i32 0
@.str.2 = [20 x i8] c"ioctl on tun device\00"
define i32 @getTunTap(i8* %0, i32 %1) {
%3 = alloca %struct.ifreq
%4 = bitcast %struct.ifreq* %3 to i8*
%5 = load i32, i32* @O_RDWR
%6 = tail call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %2
%9 = tail call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
br label %31
10: ; preds = %2
%11 = call i32 @memset(%struct.ifreq* %3, i32 0, i32 8)
%12 = load i8, i8* %0
%13 = icmp eq i8 %12, 0
br i1 %13, label %19, label %14
14: ; preds = %10
%15 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i64 0, i32 1
%16 = load i32, i32* %15
%17 = load i32, i32* @IFNAMSIZ
%18 = call i32 @strncpy(i32 %16, i8* %0, i32 %17)
br label %19
19: ; preds = %14, %10
%20 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i64 0, i32 0
store i32 %1, i32* %20
%21 = load i32, i32* @TUNSETIFF
%22 = call i32 @ioctl(i32 %6, i32 %21, i8* %4)
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %27
24: ; preds = %19
%25 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
%26 = call i32 @close(i32 %6)
br label %31
27: ; preds = %19
%28 = getelementptr inbounds %struct.ifreq, %struct.ifreq* %3, i64 0, i32 1
%29 = load i32, i32* %28
%30 = call i32 @strcpy(i8* %0, i32 %29)
br label %31
31: ; preds = %27, %24, %8
%32 = phi i32 [ %6, %8 ], [ %22, %24 ], [ %6, %27 ]
ret i32 %32
}