#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 */
typedef int uint32_t;
typedef int pin;

/* Variables and functions */
int volatile *MAP_FAILED;
int /*<<< orphan*/ MAP_SHARED;
int O_RDWR;
int O_SYNC;
int PROT_READ;
int PROT_WRITE;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
scalar_t__ mmap(int /*<<< orphan*/ *, int, int, int /*<<< orphan*/, int, int);
int /*<<< orphan*/ munmap(char *, int);
int open(char *, int);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
int /*<<< orphan*/ usleep(int);
void set_pullupdown(int pin, int direction) {
  volatile uint32_t *gpio;
  int fd = open("/dev/mem", O_RDWR | O_SYNC);
  if (fd < 0) {
    fprintf(stderr, "could not open /dev/mem: %s\n", strerror(errno));
    exit(-1);
  }
  gpio = (volatile uint32_t *)mmap(NULL, 4096, PROT_READ | PROT_WRITE,
                                   MAP_SHARED, fd, 0x20200000);
  if (gpio == MAP_FAILED) {
    fprintf(stderr, "could not map GPIO region: %s\n", strerror(errno));
    close(fd);
    exit(-1);
  }
  gpio[37] = direction;
  usleep(100);
  gpio[38 + (((pin)&32) >> 5)] = (1 << ((pin)&31));
  usleep(100);
  gpio[37] = 0;
  gpio[38 + (((pin)&32) >> 5)] = 0;
  munmap((char *)gpio, 4096);
  close(fd);
}


x86 assembly (clang, -O0):
<set_pullupdown>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    0x2e43(%rip),%rax
mov    (%rax),%esi
mov    0x2e32(%rip),%rax
or     (%rax),%esi
lea    0xe59(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11ef <set_pullupdown+0x6f>
mov    0x2e00(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    0x2dec(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x18(%rbp),%edi
mov    %rax,%rdx
lea    0xe29(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
mov    0x2e02(%rip),%rax
mov    (%rax),%edx
mov    0x2db1(%rip),%rax
or     (%rax),%edx
mov    0x2de0(%rip),%rax
mov    (%rax),%ecx
mov    -0x14(%rbp),%r8d
xor    %eax,%eax
mov    %eax,%edi
mov    $0x1000,%esi
mov    $0x20200000,%r9d
call   1030 <mmap@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    0x2d77(%rip),%rcx
cmp    (%rcx),%rax
jne    1278 <set_pullupdown+0xf8>
mov    0x2d7f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
mov    0x2d6b(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x1c(%rbp),%edi
mov    %rax,%rdx
lea    0xdc5(%rip),%rsi
call   1050 <fprintf@plt>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
mov    -0x8(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,0x94(%rax)
mov    $0x64,%edi
call   10a0 <usleep@plt>
mov    -0x4(%rbp),%ecx
and    $0x1f,%ecx
mov    $0x1,%edx
shl    %cl,%edx
mov    -0x10(%rbp),%rax
mov    -0x4(%rbp),%ecx
and    $0x20,%ecx
sar    $0x5,%ecx
add    $0x26,%ecx
movslq %ecx,%rcx
mov    %edx,(%rax,%rcx,4)
mov    $0x64,%edi
call   10a0 <usleep@plt>
mov    -0x10(%rbp),%rax
movl   $0x0,0x94(%rax)
mov    -0x10(%rbp),%rax
mov    -0x4(%rbp),%ecx
and    $0x20,%ecx
sar    $0x5,%ecx
add    $0x26,%ecx
movslq %ecx,%rcx
movl   $0x0,(%rax,%rcx,4)
mov    -0x10(%rbp),%rdi
mov    $0x1000,%esi
call   1060 <munmap@plt>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_pullupdown>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%r14d
mov    %edi,%ebx
mov    0x2e45(%rip),%rax
mov    0x2e36(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe5b(%rip),%rdi
call   1070 <open@plt>
test   %eax,%eax
js     125a <set_pullupdown+0xda>
mov    %eax,%r15d
mov    0x2e3c(%rip),%rax
mov    0x2ded(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2e1a(%rip),%rax
mov    (%rax),%ecx
xor    %edi,%edi
mov    $0x1000,%esi
mov    %r15d,%r8d
mov    $0x20200000,%r9d
call   1030 <mmap@plt>
mov    %rax,%rbp
mov    0x2db9(%rip),%rax
cmp    %rbp,(%rax)
je     128c <set_pullupdown+0x10c>
mov    %r14d,0x94(%rbp)
mov    $0x64,%edi
call   10a0 <usleep@plt>
mov    $0x1,%eax
mov    %ebx,%ecx
shl    %cl,%eax
shr    $0x5,%ebx
and    $0x1,%ebx
or     $0x26,%ebx
mov    %eax,0x0(%rbp,%rbx,4)
mov    $0x64,%edi
call   10a0 <usleep@plt>
movl   $0x0,0x94(%rbp)
movl   $0x0,0x0(%rbp,%rbx,4)
mov    %rbp,%rdi
mov    $0x1000,%esi
call   1060 <munmap@plt>
mov    %r15d,%edi
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <close@plt>
mov    0x2d5f(%rip),%rax
mov    (%rax),%ebx
mov    0x2d4e(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xd91(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1050 <fprintf@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>
mov    0x2d2d(%rip),%rax
mov    (%rax),%ebx
mov    0x2d1c(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
lea    0xd7c(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1050 <fprintf@plt>
mov    %r15d,%edi
call   1040 <close@plt>
mov    $0xffffffff,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [9 x i8] c"/dev/mem\00"
@O_RDWR = global i32 0
@O_SYNC = global i32 0
@stderr = global i32 0
@.str.1 = [29 x i8] c"could not open /dev/mem: %s\0A\00"
@errno = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i32* null
@.str.2 = [31 x i8] c"could not map GPIO region: %s\0A\00"
define void @set_pullupdown(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32*
%6 = alloca i32
store i32 %0, i32* %3
store i32 %1, i32* %4
%7 = load i32, i32* @O_RDWR
%8 = load i32, i32* @O_SYNC
%9 = or i32 %7, %8
%10 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %9)
store i32 %10, i32* %6
%11 = load i32, i32* %6
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %19
13: ; preds = %2
%14 = load i32, i32* @stderr
%15 = load i32, i32* @errno
%16 = call i8* @strerror(i32 %15)
%17 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i8* %16)
%18 = call i32 @exit(i32 -1)
unreachable
19: ; preds = %2
%20 = load i32, i32* @PROT_READ
%21 = load i32, i32* @PROT_WRITE
%22 = or i32 %20, %21
%23 = load i32, i32* @MAP_SHARED
%24 = load i32, i32* %6
%25 = call i64 @mmap(i32* null, i32 4096, i32 %22, i32 %23, i32 %24, i32 538968064)
%26 = inttoptr i64 %25 to i32*
store i32* %26, i32** %5
%27 = load i32*, i32** %5
%28 = load i32*, i32** @MAP_FAILED
%29 = icmp eq i32* %27, %28
br i1 %29, label %30, label %38
30: ; preds = %19
%31 = load i32, i32* @stderr
%32 = load i32, i32* @errno
%33 = call i8* @strerror(i32 %32)
%34 = call i32 @fprintf(i32 %31, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i8* %33)
%35 = load i32, i32* %6
%36 = call i32 @close(i32 %35)
%37 = call i32 @exit(i32 -1)
unreachable
38: ; preds = %19
%39 = load i32, i32* %4
%40 = load i32*, i32** %5
%41 = getelementptr inbounds i32, i32* %40, i64 37
store volatile i32 %39, i32* %41
%42 = call i32 @usleep(i32 100)
%43 = load i32, i32* %3
%44 = and i32 %43, 31
%45 = shl i32 1, %44
%46 = load i32*, i32** %5
%47 = load i32, i32* %3
%48 = and i32 %47, 32
%49 = ashr i32 %48, 5
%50 = add nsw i32 38, %49
%51 = sext i32 %50 to i64
%52 = getelementptr inbounds i32, i32* %46, i64 %51
store volatile i32 %45, i32* %52
%53 = call i32 @usleep(i32 100)
%54 = load i32*, i32** %5
%55 = getelementptr inbounds i32, i32* %54, i64 37
store volatile i32 0, i32* %55
%56 = load i32*, i32** %5
%57 = load i32, i32* %3
%58 = and i32 %57, 32
%59 = ashr i32 %58, 5
%60 = add nsw i32 38, %59
%61 = sext i32 %60 to i64
%62 = getelementptr inbounds i32, i32* %56, i64 %61
store volatile i32 0, i32* %62
%63 = load i32*, i32** %5
%64 = bitcast i32* %63 to i8*
%65 = call i32 @munmap(i8* %64, i32 4096)
%66 = load i32, i32* %6
%67 = call i32 @close(i32 %66)
ret void
}

LLVM IR (-O2):
@.str = [9 x i8] c"/dev/mem\00"
@O_RDWR = global i32 0
@O_SYNC = global i32 0
@stderr = global i32 0
@.str.1 = [29 x i8] c"could not open /dev/mem: %s\0A\00"
@errno = global i32 0
@PROT_READ = global i32 0
@PROT_WRITE = global i32 0
@MAP_SHARED = global i32 0
@MAP_FAILED = global i32* null
@.str.2 = [31 x i8] c"could not map GPIO region: %s\0A\00"
define void @set_pullupdown(i32 %0, i32 %1) {
%3 = load i32, i32* @O_RDWR
%4 = load i32, i32* @O_SYNC
%5 = or i32 %4, %3
%6 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %14
8: ; preds = %2
%9 = load i32, i32* @stderr
%10 = load i32, i32* @errno
%11 = tail call i8* @strerror(i32 %10)
%12 = tail call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.1, i64 0, i64 0), i8* %11)
%13 = tail call i32 @exit(i32 -1)
unreachable
14: ; preds = %2
%15 = load i32, i32* @PROT_READ
%16 = load i32, i32* @PROT_WRITE
%17 = or i32 %16, %15
%18 = load i32, i32* @MAP_SHARED
%19 = tail call i64 @mmap(i32* null, i32 4096, i32 %17, i32 %18, i32 %6, i32 538968064)
%20 = inttoptr i64 %19 to i32*
%21 = load i32*, i32** @MAP_FAILED
%22 = icmp eq i32* %21, %20
br i1 %22, label %23, label %30
23: ; preds = %14
%24 = load i32, i32* @stderr
%25 = load i32, i32* @errno
%26 = tail call i8* @strerror(i32 %25)
%27 = tail call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i8* %26)
%28 = tail call i32 @close(i32 %6)
%29 = tail call i32 @exit(i32 -1)
unreachable
30: ; preds = %14
%31 = getelementptr inbounds i32, i32* %20, i64 37
store volatile i32 %1, i32* %31
%32 = tail call i32 @usleep(i32 100)
%33 = and i32 %0, 31
%34 = shl nuw i32 1, %33
%35 = lshr i32 %0, 5
%36 = and i32 %35, 1
%37 = or i32 %36, 38
%38 = zext i32 %37 to i64
%39 = getelementptr inbounds i32, i32* %20, i64 %38
store volatile i32 %34, i32* %39
%40 = tail call i32 @usleep(i32 100)
store volatile i32 0, i32* %31
store volatile i32 0, i32* %39
%41 = inttoptr i64 %19 to i8*
%42 = tail call i32 @munmap(i8* %41, i32 4096)
%43 = tail call i32 @close(i32 %6)
ret void
}