#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 sockaddr_in {
  int /*<<< orphan*/ rt_flags;
  void *sin_family;
  int /*<<< orphan*/ rt_genmask;
  int /*<<< orphan*/ rt_dst;
  int /*<<< orphan*/ rt_gateway;
  scalar_t__ sin_port;
  int /*<<< orphan*/ sin_addr;
};
struct rtentry {
  int /*<<< orphan*/ rt_flags;
  void *sin_family;
  int /*<<< orphan*/ rt_genmask;
  int /*<<< orphan*/ rt_dst;
  int /*<<< orphan*/ rt_gateway;
  scalar_t__ sin_port;
  int /*<<< orphan*/ sin_addr;
};
typedef int /*<<< orphan*/ sin;
typedef int /*<<< orphan*/ rt;

/* Variables and functions */
void *AF_INET;
int /*<<< orphan*/ RTF_GATEWAY;
int /*<<< orphan*/ SIOCADDRT;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ close(int);
scalar_t__ inet_aton(char *, int /*<<< orphan*/ *);
scalar_t__ ioctl(int, int /*<<< orphan*/, struct sockaddr_in *);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/ *, struct sockaddr_in *, int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int socket(void *, int /*<<< orphan*/, int /*<<< orphan*/);
int set_gateway_ip(char *gateway) {
  if (gateway == NULL) {
    return -1;
  }
  int fd = 0;
  int ret = 0;
  struct sockaddr_in sin;
  struct rtentry rt;
  fd = socket(AF_INET, SOCK_DGRAM, 0);
  if (fd < 0) {
    return -1;
  }
  memset(&sin, 0, sizeof(sin));
  memset(&rt, 0, sizeof(rt));
  if ((inet_aton(gateway, &sin.sin_addr)) < 0) {
    perror("inet_aton:");
    return -1;
  }
  sin.sin_family = AF_INET;
  sin.sin_port = 0;
  memcpy(&rt.rt_gateway, &sin, sizeof(sin));
  ((struct sockaddr_in *)&rt.rt_dst)->sin_family = AF_INET;
  ((struct sockaddr_in *)&rt.rt_genmask)->sin_family = AF_INET;
  rt.rt_flags = RTF_GATEWAY;
  if (ioctl(fd, SIOCADDRT, &rt) < 0) {
    close(fd);
    return -1;
  }
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<set_gateway_ip>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1196 <set_gateway_ip+0x26>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c0 <set_gateway_ip+0x150>
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    0x2e3d(%rip),%rax
mov    (%rax),%rdi
mov    0x2e2b(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11d7 <set_gateway_ip+0x67>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c0 <set_gateway_ip+0x150>
lea    -0x48(%rbp),%rdi
xor    %esi,%esi
mov    $0x30,%edx
call   1030 <memset@plt>
lea    -0x78(%rbp),%rdi
xor    %esi,%esi
mov    $0x30,%edx
call   1030 <memset@plt>
mov    -0x10(%rbp),%rdi
lea    -0x48(%rbp),%rsi
add    $0x28,%rsi
call   1070 <inet_aton@plt>
cmp    $0x0,%rax
jge    122a <set_gateway_ip+0xba>
lea    0xde7(%rip),%rdi
call   1080 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c0 <set_gateway_ip+0x150>
mov    0x2db7(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x40(%rbp)
movq   $0x0,-0x28(%rbp)
lea    -0x78(%rbp),%rdi
add    $0x18,%rdi
lea    -0x48(%rbp),%rsi
mov    $0x30,%edx
call   1060 <memcpy@plt>
mov    0x2d8b(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x5c(%rbp)
mov    0x2d7d(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x60(%rbp)
mov    0x2d57(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x78(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2d40(%rip),%rax
mov    (%rax),%esi
lea    -0x78(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0x0,%rax
jge    12b1 <set_gateway_ip+0x141>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12c0 <set_gateway_ip+0x150>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_gateway_ip>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x68,%rsp
test   %rdi,%rdi
je     124a <set_gateway_ip+0xda>
mov    %rdi,%rbp
mov    0x2e5b(%rip),%r15
mov    (%r15),%rdi
mov    0x2e49(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    $0xffffffff,%ebx
test   %eax,%eax
js     124f <set_gateway_ip+0xdf>
mov    %eax,%r14d
lea    0x38(%rsp),%rdi
xor    %esi,%esi
mov    $0x30,%edx
call   1030 <memset@plt>
lea    0x8(%rsp),%rdi
xor    %esi,%esi
mov    $0x30,%edx
call   1030 <memset@plt>
lea    0x60(%rsp),%rsi
mov    %rbp,%rdi
call   1070 <inet_aton@plt>
test   %rax,%rax
js     125c <set_gateway_ip+0xec>
mov    (%r15),%rax
mov    %rax,0x40(%rsp)
movq   $0x0,0x58(%rsp)
lea    0x20(%rsp),%rdi
lea    0x38(%rsp),%rsi
mov    $0x30,%edx
call   1060 <memcpy@plt>
mov    (%r15),%rax
mov    %rax,0x24(%rsp)
mov    %rax,0x20(%rsp)
mov    0x2db3(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    0x2d9e(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %r14d,%edi
call   1040 <ioctl@plt>
mov    %rax,%rbx
mov    %r14d,%edi
call   1050 <close@plt>
sar    $0x3f,%rbx
jmp    124f <set_gateway_ip+0xdf>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x68,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd9d(%rip),%rdi
call   1080 <perror@plt>
jmp    124f <set_gateway_ip+0xdf>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i8*, i32, i32, i32, i64, i32 }
%struct.rtentry = type { i32, i8*, i32, i32, i32, i64, i32 }
@AF_INET = global i8* null
@SOCK_DGRAM = global i32 0
@.str = [11 x i8] c"inet_aton:\00"
@RTF_GATEWAY = global i32 0
@SIOCADDRT = global i32 0
define i32 @set_gateway_ip(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.sockaddr_in
%7 = alloca %struct.rtentry
store i8* %0, i8** %3
%8 = load i8*, i8** %3
%9 = icmp eq i8* %8, null
br i1 %9, label %10, label %11
10: ; preds = %1
store i32 -1, i32* %2
br label %55
11: ; preds = %1
store i32 0, i32* %4
store i32 0, i32* %5
%12 = load i8*, i8** @AF_INET
%13 = load i32, i32* @SOCK_DGRAM
%14 = call i32 @socket(i8* %12, i32 %13, i32 0)
store i32 %14, i32* %4
%15 = load i32, i32* %4
%16 = icmp slt i32 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %11
store i32 -1, i32* %2
br label %55
18: ; preds = %11
%19 = call i32 @memset(%struct.sockaddr_in* %6, i32 0, i32 48)
%20 = bitcast %struct.rtentry* %7 to %struct.sockaddr_in*
%21 = call i32 @memset(%struct.sockaddr_in* %20, i32 0, i32 48)
%22 = load i8*, i8** %3
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 6
%24 = call i64 @inet_aton(i8* %22, i32* %23)
%25 = icmp slt i64 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %18
%27 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %2
br label %55
28: ; preds = %18
%29 = load i8*, i8** @AF_INET
%30 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
store i8* %29, i8** %30
%31 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 5
store i64 0, i64* %31
%32 = getelementptr inbounds %struct.rtentry, %struct.rtentry* %7, i32 0, i32 4
%33 = call i32 @memcpy(i32* %32, %struct.sockaddr_in* %6, i32 48)
%34 = load i8*, i8** @AF_INET
%35 = getelementptr inbounds %struct.rtentry, %struct.rtentry* %7, i32 0, i32 3
%36 = bitcast i32* %35 to %struct.sockaddr_in*
%37 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %36, i32 0, i32 1
store i8* %34, i8** %37
%38 = load i8*, i8** @AF_INET
%39 = getelementptr inbounds %struct.rtentry, %struct.rtentry* %7, i32 0, i32 2
%40 = bitcast i32* %39 to %struct.sockaddr_in*
%41 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %40, i32 0, i32 1
store i8* %38, i8** %41
%42 = load i32, i32* @RTF_GATEWAY
%43 = getelementptr inbounds %struct.rtentry, %struct.rtentry* %7, i32 0, i32 0
store i32 %42, i32* %43
%44 = load i32, i32* %4
%45 = load i32, i32* @SIOCADDRT
%46 = bitcast %struct.rtentry* %7 to %struct.sockaddr_in*
%47 = call i64 @ioctl(i32 %44, i32 %45, %struct.sockaddr_in* %46)
%48 = icmp slt i64 %47, 0
br i1 %48, label %49, label %52
49: ; preds = %28
%50 = load i32, i32* %4
%51 = call i32 @close(i32 %50)
store i32 -1, i32* %2
br label %55
52: ; preds = %28
%53 = load i32, i32* %4
%54 = call i32 @close(i32 %53)
store i32 0, i32* %2
br label %55
55: ; preds = %52, %49, %26, %17, %10
%56 = load i32, i32* %2
ret i32 %56
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i8*, i32, i32, i32, i64, i32 }
%struct.rtentry = type { i32, i8*, i32, i32, i32, i64, i32 }
@AF_INET = global i8* null
@SOCK_DGRAM = global i32 0
@.str = [11 x i8] c"inet_aton:\00"
@RTF_GATEWAY = global i32 0
@SIOCADDRT = global i32 0
define i32 @set_gateway_ip(i8* %0) {
%2 = alloca %struct.sockaddr_in
%3 = alloca %struct.rtentry
%4 = icmp eq i8* %0, null
br i1 %4, label %43, label %5
5: ; preds = %1
%6 = bitcast %struct.sockaddr_in* %2 to i8*
%7 = bitcast %struct.rtentry* %3 to i8*
%8 = load i8*, i8** @AF_INET
%9 = load i32, i32* @SOCK_DGRAM
%10 = tail call i32 @socket(i8* %8, i32 %9, i32 0)
%11 = icmp slt i32 %10, 0
br i1 %11, label %41, label %12
12: ; preds = %5
%13 = call i32 @memset(%struct.sockaddr_in* %2, i32 0, i32 48)
%14 = bitcast %struct.rtentry* %3 to %struct.sockaddr_in*
%15 = call i32 @memset(%struct.sockaddr_in* %14, i32 0, i32 48)
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 6
%17 = call i64 @inet_aton(i8* %0, i32* %16)
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %12
%20 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
br label %41
21: ; preds = %12
%22 = load i8*, i8** @AF_INET
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1
store i8* %22, i8** %23
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 5
store i64 0, i64* %24
%25 = getelementptr inbounds %struct.rtentry, %struct.rtentry* %3, i64 0, i32 4
%26 = call i32 @memcpy(i32* %25, %struct.sockaddr_in* %2, i32 48)
%27 = load i8*, i8** @AF_INET
%28 = getelementptr inbounds %struct.rtentry, %struct.rtentry* %3, i64 0, i32 3
%29 = getelementptr inbounds i32, i32* %28, i64 2
%30 = bitcast i32* %29 to i8**
store i8* %27, i8** %30
%31 = getelementptr inbounds %struct.rtentry, %struct.rtentry* %3, i64 0, i32 2
%32 = getelementptr inbounds i32, i32* %31, i64 2
%33 = bitcast i32* %32 to i8**
store i8* %27, i8** %33
%34 = load i32, i32* @RTF_GATEWAY
%35 = getelementptr inbounds %struct.rtentry, %struct.rtentry* %3, i64 0, i32 0
store i32 %34, i32* %35
%36 = load i32, i32* @SIOCADDRT
%37 = call i64 @ioctl(i32 %10, i32 %36, %struct.sockaddr_in* %14)
%38 = call i32 @close(i32 %10)
%39 = ashr i64 %37, 63
%40 = trunc i64 %39 to i32
br label %41
41: ; preds = %21, %5, %19
%42 = phi i32 [ -1, %19 ], [ -1, %5 ], [ %40, %21 ]
br label %43
43: ; preds = %1, %41
%44 = phi i32 [ %42, %41 ], [ -1, %1 ]
ret i32 %44
}