#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_4__ TYPE_2__;
typedef struct TYPE_3__ TYPE_1__;

/* Type definitions */
typedef int /*<<< orphan*/ uint8_t;
struct TYPE_3__ {
  int /*<<< orphan*/ m;
};
struct TYPE_4__ {
  TYPE_1__ freq;
};
struct iwreq {
  TYPE_2__ u;
  int /*<<< orphan*/ ifr_name;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SIOCGIWFREQ;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ free(struct iwreq *);
int /*<<< orphan*/ frequencytochannel(int /*<<< orphan*/);
scalar_t__ ioctl(int, int /*<<< orphan*/, struct iwreq *);
struct iwreq *malloc(int);
int /*<<< orphan*/ memset(struct iwreq *, int, int);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ strncpy(int /*<<< orphan*/, char *, int /*<<< orphan*/);
uint8_t getchannel(char *intname) {
  int sock;
  uint8_t channel;
  struct iwreq *iwr;
  iwr = malloc(sizeof(struct iwreq));
  if (iwr == NULL) {
    perror("can't allocate memory");
    exit(1);
  }
  memset(iwr, 0x00, sizeof(struct iwreq));
  strncpy(iwr->ifr_name, intname, strlen(intname));
  if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
    perror("can't open socket");
    exit(1);
  }
  if (ioctl(sock, SIOCGIWFREQ, iwr) < 0) {
    close(sock);
    free(iwr);
    perror("can't get frequency");
    exit(1);
  }
  channel = frequencytochannel(iwr->u.freq.m);
  free(iwr);
  close(sock);
  return (channel);
}


x86 assembly (clang, -O0):
<getchannel>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    $0x8,%edi
call   10a0 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11eb <getchannel+0x3b>
lea    0xe24(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1060 <memset@plt>
mov    -0x18(%rbp),%rax
mov    0x4(%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x24(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    %eax,%edx
call   1040 <strncpy@plt>
mov    0x2db5(%rip),%rax
mov    (%rax),%edi
mov    0x2da4(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jge    125f <getchannel+0xaf>
lea    0xdc6(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0xc(%rbp),%edi
mov    0x2d7f(%rip),%rax
mov    (%rax),%esi
mov    -0x18(%rbp),%rdx
call   1070 <ioctl@plt>
cmp    $0x0,%rax
jge    12a5 <getchannel+0xf5>
mov    -0xc(%rbp),%edi
call   1090 <close@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
lea    0xd92(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x18(%rbp),%rax
mov    (%rax),%edi
call   1080 <frequencytochannel@plt>
mov    %eax,-0x10(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0xc(%rbp),%edi
call   1090 <close@plt>
mov    -0x10(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getchannel>:
push   %rbp
push   %r14
push   %rbx
mov    %rdi,%r14
mov    $0x8,%edi
call   10a0 <malloc@plt>
test   %rax,%rax
je     124b <getchannel+0x9b>
mov    %rax,%rbx
mov    %rax,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1060 <memset@plt>
mov    0x4(%rbx),%ebp
mov    %r14,%rdi
call   1050 <strlen@plt>
mov    %ebp,%edi
mov    %r14,%rsi
mov    %eax,%edx
call   1040 <strncpy@plt>
mov    0x2de6(%rip),%rax
mov    (%rax),%edi
mov    0x2dd5(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
test   %eax,%eax
js     1254 <getchannel+0xa4>
mov    %eax,%ebp
mov    0x2dcf(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
mov    %rbx,%rdx
call   1070 <ioctl@plt>
test   %rax,%rax
js     125d <getchannel+0xad>
mov    (%rbx),%edi
call   1080 <frequencytochannel@plt>
mov    %eax,%r14d
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %ebp,%edi
call   1090 <close@plt>
mov    %r14d,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xdae(%rip),%rdi
jmp    1273 <getchannel+0xc3>
lea    0xdbb(%rip),%rdi
jmp    1273 <getchannel+0xc3>
mov    %ebp,%edi
call   1090 <close@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
lea    0xdb5(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
%struct.iwreq = type { %struct.TYPE_4__, i32 }
%struct.TYPE_4__ = type { %struct.TYPE_3__ }
%struct.TYPE_3__ = type { i32 }
@.str = [22 x i8] c"can't allocate memory\00"
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str.1 = [18 x i8] c"can't open socket\00"
@SIOCGIWFREQ = global i32 0
@.str.2 = [20 x i8] c"can't get frequency\00"
define i32 @getchannel(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.iwreq*
store i8* %0, i8** %2
%6 = call %struct.iwreq* @malloc(i32 8)
store %struct.iwreq* %6, %struct.iwreq** %5
%7 = load %struct.iwreq*, %struct.iwreq** %5
%8 = icmp eq %struct.iwreq* %7, null
br i1 %8, label %9, label %12
9: ; preds = %1
%10 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
%11 = call i32 @exit(i32 1)
unreachable
12: ; preds = %1
%13 = load %struct.iwreq*, %struct.iwreq** %5
%14 = call i32 @memset(%struct.iwreq* %13, i32 0, i32 8)
%15 = load %struct.iwreq*, %struct.iwreq** %5
%16 = getelementptr inbounds %struct.iwreq, %struct.iwreq* %15, i32 0, i32 1
%17 = load i32, i32* %16
%18 = load i8*, i8** %2
%19 = load i8*, i8** %2
%20 = call i32 @strlen(i8* %19)
%21 = call i32 @strncpy(i32 %17, i8* %18, i32 %20)
%22 = load i32, i32* @AF_INET
%23 = load i32, i32* @SOCK_DGRAM
%24 = call i32 @socket(i32 %22, i32 %23, i32 0)
store i32 %24, i32* %3
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %29
26: ; preds = %12
%27 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
%28 = call i32 @exit(i32 1)
unreachable
29: ; preds = %12
%30 = load i32, i32* %3
%31 = load i32, i32* @SIOCGIWFREQ
%32 = load %struct.iwreq*, %struct.iwreq** %5
%33 = call i64 @ioctl(i32 %30, i32 %31, %struct.iwreq* %32)
%34 = icmp slt i64 %33, 0
br i1 %34, label %35, label %42
35: ; preds = %29
%36 = load i32, i32* %3
%37 = call i32 @close(i32 %36)
%38 = load %struct.iwreq*, %struct.iwreq** %5
%39 = call i32 @free(%struct.iwreq* %38)
%40 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
%41 = call i32 @exit(i32 1)
unreachable
42: ; preds = %29
%43 = load %struct.iwreq*, %struct.iwreq** %5
%44 = getelementptr inbounds %struct.iwreq, %struct.iwreq* %43, i32 0, i32 0
%45 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %44, i32 0, i32 0
%46 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %45, i32 0, i32 0
%47 = load i32, i32* %46
%48 = call i32 @frequencytochannel(i32 %47)
store i32 %48, i32* %4
%49 = load %struct.iwreq*, %struct.iwreq** %5
%50 = call i32 @free(%struct.iwreq* %49)
%51 = load i32, i32* %3
%52 = call i32 @close(i32 %51)
%53 = load i32, i32* %4
ret i32 %53
}

LLVM IR (-O2):
%struct.iwreq = type { %struct.TYPE_4__, i32 }
%struct.TYPE_4__ = type { %struct.TYPE_3__ }
%struct.TYPE_3__ = type { i32 }
@.str = [22 x i8] c"can't allocate memory\00"
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str.1 = [18 x i8] c"can't open socket\00"
@SIOCGIWFREQ = global i32 0
@.str.2 = [20 x i8] c"can't get frequency\00"
define i32 @getchannel(i8* %0) {
%2 = tail call %struct.iwreq* @malloc(i32 8)
%3 = icmp eq %struct.iwreq* %2, null
br i1 %3, label %4, label %7
4: ; preds = %1
%5 = tail call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
%6 = tail call i32 @exit(i32 1)
unreachable
7: ; preds = %1
%8 = tail call i32 @memset(%struct.iwreq* %2, i32 0, i32 8)
%9 = getelementptr inbounds %struct.iwreq, %struct.iwreq* %2, i64 0, i32 1
%10 = load i32, i32* %9
%11 = tail call i32 @strlen(i8* %0)
%12 = tail call i32 @strncpy(i32 %10, i8* %0, i32 %11)
%13 = load i32, i32* @AF_INET
%14 = load i32, i32* @SOCK_DGRAM
%15 = tail call i32 @socket(i32 %13, i32 %14, i32 0)
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %7
%18 = tail call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0))
%19 = tail call i32 @exit(i32 1)
unreachable
20: ; preds = %7
%21 = load i32, i32* @SIOCGIWFREQ
%22 = tail call i64 @ioctl(i32 %15, i32 %21, %struct.iwreq* %2)
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %29
24: ; preds = %20
%25 = tail call i32 @close(i32 %15)
%26 = tail call i32 @free(%struct.iwreq* %2)
%27 = tail call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
%28 = tail call i32 @exit(i32 1)
unreachable
29: ; preds = %20
%30 = getelementptr inbounds %struct.iwreq, %struct.iwreq* %2, i64 0, i32 0, i32 0, i32 0
%31 = load i32, i32* %30
%32 = tail call i32 @frequencytochannel(i32 %31)
%33 = tail call i32 @free(%struct.iwreq* %2)
%34 = tail call i32 @close(i32 %15)
ret i32 %32
}