#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 */

/* Variables and functions */
int /*<<< orphan*/ O_RDWR;
int atoi(char *);
int /*<<< orphan*/ gets(char *);
int ioctl(int, int, int *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int main(int argc, char **argv) {
  int err = -1;
  int fd = -1;
  int freq = 0;
  int tmp;
  int cmd = 0;
  char buf[32];
  fd = open("/dev/pwm", O_RDWR);
  if (fd == -1) {
    printf("Failed to open %s.\n", "/dev/pwm");
    return -1;
  }
  printf("Open device: %s success.\n", "/dev/pwm");
  while (1) {
    printf("Please input freq:");
    gets(buf);
    tmp = atoi(buf);
    switch (tmp) {
    case 1:
      freq = 1000;
      cmd = 1;
      err = ioctl(fd, cmd, &freq);
      break;
    case 0:
      cmd = 0;
      err = ioctl(fd, cmd, &freq);
      break;
    case 2:
      cmd = 0;
      err = ioctl(fd, cmd, &freq);
      return -1;
    default:
      freq = tmp;
      err = ioctl(fd, cmd, &freq);
      break;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0xffffffff,-0x14(%rbp)
movl   $0xffffffff,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x24(%rbp)
mov    0x2e5f(%rip),%rax
mov    (%rax),%esi
lea    0xe6e(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    11c5 <main+0x75>
lea    0xe5e(%rip),%rdi
lea    0xe4e(%rip),%rsi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12bb <main+0x16b>
lea    0xe51(%rip),%rdi
lea    0xe2d(%rip),%rsi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xe56(%rip),%rdi
xor    %eax,%eax
call   1030 <printf@plt>
lea    -0x50(%rbp),%rdi
mov    %rdi,-0x60(%rbp)
call   1050 <gets@plt>
mov    -0x60(%rbp),%rdi
call   1070 <atoi@plt>
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
mov    %eax,-0x54(%rbp)
test   %eax,%eax
je     125b <main+0x10b>
jmp    1214 <main+0xc4>
mov    -0x54(%rbp),%eax
sub    $0x1,%eax
je     1236 <main+0xe6>
jmp    1225 <main+0xd5>
mov    -0x54(%rbp),%eax
sub    $0x2,%eax
je     1279 <main+0x129>
jmp    129e <main+0x14e>
movl   $0x3e8,-0x1c(%rbp)
movl   $0x1,-0x24(%rbp)
mov    -0x18(%rbp),%edi
mov    -0x24(%rbp),%esi
lea    -0x1c(%rbp),%rdx
call   1040 <ioctl@plt>
mov    %eax,-0x14(%rbp)
jmp    12b6 <main+0x166>
movl   $0x0,-0x24(%rbp)
mov    -0x18(%rbp),%edi
mov    -0x24(%rbp),%esi
lea    -0x1c(%rbp),%rdx
call   1040 <ioctl@plt>
mov    %eax,-0x14(%rbp)
jmp    12b6 <main+0x166>
movl   $0x0,-0x24(%rbp)
mov    -0x18(%rbp),%edi
mov    -0x24(%rbp),%esi
lea    -0x1c(%rbp),%rdx
call   1040 <ioctl@plt>
mov    %eax,-0x14(%rbp)
movl   $0xffffffff,-0x4(%rbp)
jmp    12bb <main+0x16b>
mov    -0x20(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%edi
mov    -0x24(%rbp),%esi
lea    -0x1c(%rbp),%rdx
call   1040 <ioctl@plt>
mov    %eax,-0x14(%rbp)
jmp    11da <main+0x8a>
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x30,%rsp
movl   $0x0,0xc(%rsp)
mov    0x2e7d(%rip),%rax
mov    (%rax),%esi
lea    0xe8c(%rip),%rdi
call   1060 <open@plt>
cmp    $0xffffffff,%eax
je     1215 <main+0xc5>
mov    %eax,%r12d
lea    0xe91(%rip),%rdi
lea    0xe6d(%rip),%rsi
xor    %eax,%eax
call   1030 <printf@plt>
lea    0xe96(%rip),%r14
lea    0x10(%rsp),%rbx
lea    0xc(%rsp),%r15
xor    %ebp,%ebp
jmp    11ca <main+0x7a>
nop
movl   $0x3e8,0xc(%rsp)
mov    $0x1,%ebp
mov    %r12d,%edi
mov    %ebp,%esi
mov    %r15,%rdx
call   1040 <ioctl@plt>
mov    %r14,%rdi
xor    %eax,%eax
call   1030 <printf@plt>
mov    %rbx,%rdi
call   1050 <gets@plt>
mov    %rbx,%rdi
call   1070 <atoi@plt>
test   %eax,%eax
je     1200 <main+0xb0>
cmp    $0x1,%eax
je     11b0 <main+0x60>
cmp    $0x2,%eax
je     1204 <main+0xb4>
mov    %eax,0xc(%rsp)
jmp    11bd <main+0x6d>
nopl   0x0(%rax,%rax,1)
mov    %eax,%ebp
jmp    11bd <main+0x6d>
lea    0xc(%rsp),%rdx
mov    %r12d,%edi
xor    %esi,%esi
call   1040 <ioctl@plt>
jmp    122a <main+0xda>
lea    0xded(%rip),%rdi
lea    0xddd(%rip),%rsi
xor    %eax,%eax
call   1030 <printf@plt>
mov    $0xffffffff,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [9 x i8] c"/dev/pwm\00"
@O_RDWR = global i32 0
@.str.1 = [20 x i8] c"Failed to open %s.\0A\00"
@.str.2 = [26 x i8] c"Open device: %s success.\0A\00"
@.str.3 = [19 x i8] c"Please input freq:\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca [32 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 -1, i32* %6
store i32 -1, i32* %7
store i32 0, i32* %8
store i32 0, i32* %10
%12 = load i32, i32* @O_RDWR
%13 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %12)
store i32 %13, i32* %7
%14 = load i32, i32* %7
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %18
16: ; preds = %2
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %45
18: ; preds = %2
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
br label %20
20: ; preds = %18, %44
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0))
%22 = getelementptr inbounds [32 x i8], [32 x i8]* %11, i64 0, i64 0
%23 = call i32 @gets(i8* %22)
%24 = getelementptr inbounds [32 x i8], [32 x i8]* %11, i64 0, i64 0
%25 = call i32 @atoi(i8* %24)
store i32 %25, i32* %9
%26 = load i32, i32* %9
switch i32 %26, label %39 [
i32 1, label %27
i32 0, label %31
i32 2, label %35
]
27: ; preds = %20
store i32 1000, i32* %8
store i32 1, i32* %10
%28 = load i32, i32* %7
%29 = load i32, i32* %10
%30 = call i32 @ioctl(i32 %28, i32 %29, i32* %8)
store i32 %30, i32* %6
br label %44
31: ; preds = %20
store i32 0, i32* %10
%32 = load i32, i32* %7
%33 = load i32, i32* %10
%34 = call i32 @ioctl(i32 %32, i32 %33, i32* %8)
store i32 %34, i32* %6
br label %44
35: ; preds = %20
store i32 0, i32* %10
%36 = load i32, i32* %7
%37 = load i32, i32* %10
%38 = call i32 @ioctl(i32 %36, i32 %37, i32* %8)
store i32 %38, i32* %6
store i32 -1, i32* %3
br label %45
39: ; preds = %20
%40 = load i32, i32* %9
store i32 %40, i32* %8
%41 = load i32, i32* %7
%42 = load i32, i32* %10
%43 = call i32 @ioctl(i32 %41, i32 %42, i32* %8)
store i32 %43, i32* %6
br label %44
44: ; preds = %39, %31, %27
br label %20
45: ; preds = %35, %16
%46 = load i32, i32* %3
ret i32 %46
}

LLVM IR (-O2):
@.str = [9 x i8] c"/dev/pwm\00"
@O_RDWR = global i32 0
@.str.1 = [20 x i8] c"Failed to open %s.\0A\00"
@.str.2 = [26 x i8] c"Open device: %s success.\0A\00"
@.str.3 = [19 x i8] c"Please input freq:\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i32
%4 = alloca [32 x i8]
%5 = bitcast i32* %3 to i8*
store i32 0, i32* %3
%6 = getelementptr inbounds [32 x i8], [32 x i8]* %4, i64 0, i64 0
%7 = load i32, i32* @O_RDWR
%8 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %7)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %12
10: ; preds = %2
%11 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
br label %26
12: ; preds = %2
%13 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
br label %14
14: ; preds = %23, %12
%15 = phi i32 [ 0, %12 ], [ %24, %23 ]
%16 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @.str.3, i64 0, i64 0))
%17 = call i32 @gets(i8* %6)
%18 = call i32 @atoi(i8* %6)
switch i32 %18, label %22 [
i32 1, label %19
i32 0, label %23
i32 2, label %20
]
19: ; preds = %14
store i32 1000, i32* %3
br label %23
20: ; preds = %14
%21 = call i32 @ioctl(i32 %8, i32 0, i32* %3)
br label %26
22: ; preds = %14
store i32 %18, i32* %3
br label %23
23: ; preds = %14, %22, %19
%24 = phi i32 [ %15, %22 ], [ 1, %19 ], [ %18, %14 ]
%25 = call i32 @ioctl(i32 %8, i32 %24, i32* %3)
br label %14
26: ; preds = %20, %10
ret i32 -1
}