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

/* Variables and functions */
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ close(int);
int errno;
int ntohl(int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
char *strerror(int);
int write(int, char *, int);
int main() {
  char reset[] = {0, 193, 0, 0, 0, 10, 0, 0, 0, 90};
  int fd;
  int err;
  int rc = 1;
  fd = open("/dev/tpm0", O_RDWR);
  if (fd < 0) {
    printf("Unable to open the device.\n");
    goto out;
  }
  err = write(fd, reset, sizeof(reset));
  if (err != sizeof(reset)) {
    printf("Error occured while writing the reset command: %d\n", errno);
    goto out;
  }
  err = read(fd, reset, sizeof(reset));
  if (err != 10) {
    printf("Error occured while reading the reset result: %d %d %s\n", err,
           errno, strerror(errno));
    goto out;
  }
  err = ntohl(*((uint32_t *)(reset + 6)));
  if (err == 38) {
    printf("TPM not started.\n");
    goto out;
  }
  if (err != 0) {
    printf("TPM Error occured: %d\n", err);
    goto out;
  }
  rc = 0;
  printf("Reset successful\n");
out:
  close(fd);
  return rc;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    0xe7a(%rip),%rax
mov    %rax,-0xe(%rbp)
mov    0xe77(%rip),%ax
mov    %ax,-0x6(%rbp)
movl   $0x1,-0x1c(%rbp)
mov    0x2e45(%rip),%rax
mov    (%rax),%esi
lea    0xe5e(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11d1 <main+0x61>
lea    0xe4f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12c6 <main+0x156>
mov    -0x14(%rbp),%edi
lea    -0xe(%rbp),%rsi
mov    $0xa,%edx
call   1030 <write@plt>
mov    %eax,-0x18(%rbp)
movslq -0x18(%rbp),%rax
cmp    $0xa,%rax
je     120f <main+0x9f>
mov    0x2dde(%rip),%rax
mov    (%rax),%esi
lea    0xe2d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12c6 <main+0x156>
mov    -0x14(%rbp),%edi
lea    -0xe(%rbp),%rsi
mov    $0xa,%edx
call   1060 <read@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xa,-0x18(%rbp)
je     1269 <main+0xf9>
mov    -0x18(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    0x2d9e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2d92(%rip),%rax
mov    (%rax),%edi
call   1090 <strerror@plt>
mov    -0x24(%rbp),%esi
mov    -0x20(%rbp),%edx
mov    %rax,%rcx
lea    0xe06(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12c6 <main+0x156>
mov    -0x8(%rbp),%edi
call   1080 <ntohl@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x26,-0x18(%rbp)
jne    1291 <main+0x121>
lea    0xe16(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12c6 <main+0x156>
cmpl   $0x0,-0x18(%rbp)
je     12b1 <main+0x141>
mov    -0x18(%rbp),%esi
lea    0xe08(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    12c6 <main+0x156>
movl   $0x0,-0x1c(%rbp)
lea    0xe05(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
mov    -0x1c(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
movabs $0xa000000c100,%rax
mov    %rax,(%rsp)
movw   $0x5a00,0x8(%rsp)
mov    0x2e44(%rip),%rax
mov    (%rax),%esi
lea    0xe5d(%rip),%rdi
call   1080 <open@plt>
mov    %eax,%ebx
test   %eax,%eax
js     11ff <main+0x7f>
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0xa,%edx
call   1040 <write@plt>
cmp    $0xa,%eax
jne    1212 <main+0x92>
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0xa,%edx
call   1070 <read@plt>
cmp    $0xa,%eax
jne    1230 <main+0xb0>
mov    0x6(%rsp),%edi
call   1090 <ntohl@plt>
test   %eax,%eax
je     1261 <main+0xe1>
cmp    $0x26,%eax
jne    1281 <main+0x101>
lea    0xeaa(%rip),%rdi
jmp    1206 <main+0x86>
lea    0xeb2(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%ebp
jmp    126f <main+0xef>
mov    0x2dbf(%rip),%rax
mov    (%rax),%esi
lea    0xdf2(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%ebp
jmp    126f <main+0xef>
mov    %eax,%ebp
mov    0x2d9f(%rip),%rax
mov    (%rax),%r14d
mov    %r14d,%edi
call   10a0 <strerror@plt>
lea    0xdfc(%rip),%rdi
mov    %ebp,%esi
mov    %r14d,%edx
mov    %rax,%rcx
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x1,%ebp
jmp    126f <main+0xef>
lea    0xe2e(%rip),%rdi
call   1030 <puts@plt>
xor    %ebp,%ebp
mov    %ebx,%edi
call   1060 <close@plt>
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xdf7(%rip),%rdi
mov    %eax,%esi
jmp    1222 <main+0xa2>

LLVM IR (-O0):
@__const.main.reset = [10 x i8] c"\00\C1\00\00\00\0A\00\00\00Z"
@.str = [10 x i8] c"/dev/tpm0\00"
@O_RDWR = global i32 0
@.str.1 = [28 x i8] c"Unable to open the device.\0A\00"
@.str.2 = [51 x i8] c"Error occured while writing the reset command: %d\0A\00"
@errno = global i32 0
@.str.3 = [56 x i8] c"Error occured while reading the reset result: %d %d %s\0A\00"
@.str.4 = [18 x i8] c"TPM not started.\0A\00"
@.str.5 = [23 x i8] c"TPM Error occured: %d\0A\00"
@.str.6 = [18 x i8] c"Reset successful\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [10 x i8]
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i32 0, i32* %1
%6 = bitcast [10 x i8]* %2 to i8*
store i32 1, i32* %5
%7 = load i32, i32* @O_RDWR
%8 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %7)
store i32 %8, i32* %3
%9 = load i32, i32* %3
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %0
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
br label %53
13: ; preds = %0
%14 = load i32, i32* %3
%15 = getelementptr inbounds [10 x i8], [10 x i8]* %2, i64 0, i64 0
%16 = call i32 @write(i32 %14, i8* %15, i32 10)
store i32 %16, i32* %4
%17 = load i32, i32* %4
%18 = sext i32 %17 to i64
%19 = icmp ne i64 %18, 10
br i1 %19, label %20, label %23
20: ; preds = %13
%21 = load i32, i32* @errno
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.2, i64 0, i64 0), i32 %21)
br label %53
23: ; preds = %13
%24 = load i32, i32* %3
%25 = getelementptr inbounds [10 x i8], [10 x i8]* %2, i64 0, i64 0
%26 = call i32 @read(i32 %24, i8* %25, i32 10)
store i32 %26, i32* %4
%27 = load i32, i32* %4
%28 = icmp ne i32 %27, 10
br i1 %28, label %29, label %35
29: ; preds = %23
%30 = load i32, i32* %4
%31 = load i32, i32* @errno
%32 = load i32, i32* @errno
%33 = call i8* @strerror(i32 %32)
%34 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str.3, i64 0, i64 0), i32 %30, i32 %31, i8* %33)
br label %53
35: ; preds = %23
%36 = getelementptr inbounds [10 x i8], [10 x i8]* %2, i64 0, i64 0
%37 = getelementptr inbounds i8, i8* %36, i64 6
%38 = bitcast i8* %37 to i32*
%39 = load i32, i32* %38
%40 = call i32 @ntohl(i32 %39)
store i32 %40, i32* %4
%41 = load i32, i32* %4
%42 = icmp eq i32 %41, 38
br i1 %42, label %43, label %45
43: ; preds = %35
%44 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0))
br label %53
45: ; preds = %35
%46 = load i32, i32* %4
%47 = icmp ne i32 %46, 0
br i1 %47, label %48, label %51
48: ; preds = %45
%49 = load i32, i32* %4
%50 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.5, i64 0, i64 0), i32 %49)
br label %53
51: ; preds = %45
store i32 0, i32* %5
%52 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.6, i64 0, i64 0))
br label %53
53: ; preds = %51, %48, %43, %29, %20, %11
%54 = load i32, i32* %3
%55 = call i32 @close(i32 %54)
%56 = load i32, i32* %5
ret i32 %56
}

LLVM IR (-O2):
@__const.main.reset = [10 x i8] c"\00\C1\00\00\00\0A\00\00\00Z"
@.str = [10 x i8] c"/dev/tpm0\00"
@O_RDWR = global i32 0
@.str.2 = [51 x i8] c"Error occured while writing the reset command: %d\0A\00"
@errno = global i32 0
@.str.3 = [56 x i8] c"Error occured while reading the reset result: %d %d %s\0A\00"
@.str.5 = [23 x i8] c"TPM Error occured: %d\0A\00"
@str = [17 x i8] c"Reset successful\00"
@str.7 = [17 x i8] c"TPM not started.\00"
@str.8 = [27 x i8] c"Unable to open the device.\00"
define i32 @main() {
%1 = alloca [10 x i8]
%2 = getelementptr inbounds [10 x i8], [10 x i8]* %1, i64 0, i64 0
%3 = load i32, i32* @O_RDWR
%4 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = icmp slt i32 %4, 0
br i1 %5, label %6, label %8
6: ; preds = %0
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str.8, i64 0, i64 0))
br label %32
8: ; preds = %0
%9 = call i32 @write(i32 %4, i8* %2, i32 10)
%10 = icmp eq i32 %9, 10
br i1 %10, label %14, label %11
11: ; preds = %8
%12 = load i32, i32* @errno
%13 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([51 x i8], [51 x i8]* @.str.2, i64 0, i64 0), i32 %12)
br label %32
14: ; preds = %8
%15 = call i32 @read(i32 %4, i8* %2, i32 10)
%16 = icmp eq i32 %15, 10
br i1 %16, label %21, label %17
17: ; preds = %14
%18 = load i32, i32* @errno
%19 = tail call i8* @strerror(i32 %18)
%20 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([56 x i8], [56 x i8]* @.str.3, i64 0, i64 0), i32 %15, i32 %18, i8* %19)
br label %32
21: ; preds = %14
%22 = getelementptr inbounds [10 x i8], [10 x i8]* %1, i64 0, i64 6
%23 = bitcast i8* %22 to i32*
%24 = load i32, i32* %23
%25 = tail call i32 @ntohl(i32 %24)
switch i32 %25, label %28 [
i32 38, label %26
i32 0, label %30
]
26: ; preds = %21
%27 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str.7, i64 0, i64 0))
br label %32
28: ; preds = %21
%29 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.5, i64 0, i64 0), i32 %25)
br label %32
30: ; preds = %21
%31 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @str, i64 0, i64 0))
br label %32
32: ; preds = %30, %28, %26, %17, %11, %6
%33 = phi i32 [ 1, %6 ], [ 1, %11 ], [ 1, %17 ], [ 1, %26 ], [ 1, %28 ], [ 0, %30 ]
%34 = tail call i32 @close(i32 %4)
ret i32 %33
}