#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 */
struct TYPE_3__ {
  int m;
  int /*<<< orphan*/ flags;
  scalar_t__ e;
};
struct TYPE_4__ {
  TYPE_1__ freq;
};
struct iwreq {
  int /*<<< orphan*/ ifr_name;
  TYPE_2__ u;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ IW_FREQ_FIXED;
int /*<<< orphan*/ SIOCSIWFREQ;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ free(struct iwreq *);
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*/);
int setchannel(char *intname, int channel) {
  int sock;
  struct iwreq *iwr;
  iwr = malloc(sizeof(struct iwreq));
  if (iwr == NULL) {
    perror("can't allocate memory");
    exit(1);
  }
  memset(iwr, 0x00, sizeof(struct iwreq));
  iwr->u.freq.m = channel;
  iwr->u.freq.e = 0;
  iwr->u.freq.flags = IW_FREQ_FIXED;
  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, SIOCSIWFREQ, iwr) < 0) {
    close(sock);
    free(iwr);
    perror("can't set frequency");
    exit(1);
  }
  free(iwr);
  close(sock);
  return (0);
}


x86 assembly (clang, -O0):
<setchannel>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    $0x18,%edi
call   1090 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11de <setchannel+0x3e>
lea    0xe31(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1060 <memset@plt>
mov    -0xc(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,0x8(%rax)
mov    -0x18(%rbp),%rax
movq   $0x0,0x10(%rax)
mov    0x2dd5(%rip),%rax
mov    (%rax),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,0xc(%rax)
mov    -0x18(%rbp),%rax
mov    (%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    0x2da5(%rip),%rax
mov    (%rax),%edi
mov    0x2d8c(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,-0x10(%rbp)
cmp    $0x0,%eax
jge    1277 <setchannel+0xd7>
lea    0xdae(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x10(%rbp),%edi
mov    0x2d47(%rip),%rax
mov    (%rax),%esi
mov    -0x18(%rbp),%rdx
call   1070 <ioctl@plt>
cmp    $0x0,%rax
jge    12bd <setchannel+0x11d>
mov    -0x10(%rbp),%edi
call   1080 <close@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
lea    0xd7a(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x10(%rbp),%edi
call   1080 <close@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<setchannel>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%ebp
mov    %rdi,%r14
mov    $0x18,%edi
call   1090 <malloc@plt>
test   %rax,%rax
je     1248 <setchannel+0xa8>
mov    %rax,%rbx
mov    %rax,%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1060 <memset@plt>
mov    %ebp,0x8(%rbx)
movq   $0x0,0x10(%rbx)
mov    0x2e00(%rip),%rax
mov    (%rax),%eax
mov    %eax,0xc(%rbx)
mov    (%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    0x2dcd(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
test   %eax,%eax
js     1251 <setchannel+0xb1>
mov    %eax,%ebp
mov    0x2da7(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
mov    %rbx,%rdx
call   1070 <ioctl@plt>
test   %rax,%rax
js     125a <setchannel+0xba>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %ebp,%edi
call   1080 <close@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xdb1(%rip),%rdi
jmp    1270 <setchannel+0xd0>
lea    0xdbe(%rip),%rdi
jmp    1270 <setchannel+0xd0>
mov    %ebp,%edi
call   1080 <close@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
lea    0xdb8(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>

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

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